Coding style..

This commit is contained in:
Jaroslav Kysela 1998-11-27 15:07:56 +00:00
parent d9087a8773
commit fb1938d29f
6 changed files with 569 additions and 553 deletions

View file

@ -2,7 +2,7 @@
#include <string.h> #include <string.h>
#include "../include/asoundlib.h" #include "../include/asoundlib.h"
void main( void ) void main(void)
{ {
int idx, idx1, cards, err; int idx, idx1, cards, err;
void *handle; void *handle;
@ -12,56 +12,56 @@ void main( void )
char str[128]; char str[128];
cards = snd_cards(); cards = snd_cards();
printf( "Detected %i soundcard%s...\n", cards, cards > 1 ? "s" : "" ); printf("Detected %i soundcard%s...\n", cards, cards > 1 ? "s" : "");
if ( cards <= 0 ) { if (cards <= 0) {
printf( "Giving up...\n" ); printf("Giving up...\n");
return; return;
} }
for ( idx = 0; idx < cards; idx++ ) { for (idx = 0; idx < cards; idx++) {
if ( (err = snd_ctl_open( &handle, idx )) < 0 ) { if ((err = snd_ctl_open(&handle, idx)) < 0) {
printf( "Open error: %s\n", snd_strerror( err ) ); printf("Open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (err = snd_ctl_hw_info( handle, &info )) < 0 ) { if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
printf( "HW info error: %s\n", snd_strerror( err ) ); printf("HW info error: %s\n", snd_strerror(err));
continue; continue;
} }
printf( "Soundcard #%i:\n", idx + 1 ); printf("Soundcard #%i:\n", idx + 1);
printf( " type - %i\n", info.type ); printf(" type - %i\n", info.type);
printf( " gcaps - 0x%x\n", info.gcaps ); printf(" gcaps - 0x%x\n", info.gcaps);
printf( " lcaps - 0x%x\n", info.lcaps ); printf(" lcaps - 0x%x\n", info.lcaps);
printf( " pcm devs - 0x%x\n", info.pcmdevs ); printf(" pcm devs - 0x%x\n", info.pcmdevs);
printf( " mixer devs - 0x%x\n", info.mixerdevs ); printf(" mixer devs - 0x%x\n", info.mixerdevs);
printf( " midi devs - 0x%x\n", info.mididevs ); printf(" midi devs - 0x%x\n", info.mididevs);
memset( str, 0, sizeof( str ) ); memset(str, 0, sizeof(str));
strncpy( str, info.id, sizeof( info.id ) ); strncpy(str, info.id, sizeof(info.id));
printf( " id - '%s'\n", str ); printf(" id - '%s'\n", str);
printf( " abbreviation - '%s'\n", info.abbreviation ); printf(" abbreviation - '%s'\n", info.abbreviation);
printf( " name - '%s'\n", info.name ); printf(" name - '%s'\n", info.name);
printf( " longname - '%s'\n", info.longname ); printf(" longname - '%s'\n", info.longname);
for ( idx1 = 0; idx1 < info.pcmdevs; idx1++ ) { for (idx1 = 0; idx1 < info.pcmdevs; idx1++) {
printf( "PCM info, device #%i:\n", idx1 ); printf("PCM info, device #%i:\n", idx1);
if ( (err = snd_ctl_pcm_info( handle, idx1, &pcminfo )) < 0 ) { if ((err = snd_ctl_pcm_info(handle, idx1, &pcminfo)) < 0) {
printf( " PCM info error: %s\n", snd_strerror( err ) ); printf(" PCM info error: %s\n", snd_strerror(err));
continue; continue;
} }
printf( " type - %i\n", pcminfo.type ); printf(" type - %i\n", pcminfo.type);
printf( " flags - 0x%x\n", pcminfo.flags ); printf(" flags - 0x%x\n", pcminfo.flags);
printf( " id - '%s'\n", pcminfo.id ); printf(" id - '%s'\n", pcminfo.id);
printf( " name - '%s'\n", pcminfo.name ); printf(" name - '%s'\n", pcminfo.name);
} }
for ( idx1 = 0; idx1 < info.mixerdevs; idx1++ ) { for (idx1 = 0; idx1 < info.mixerdevs; idx1++) {
printf( "MIXER info, device #%i:\n", idx1 ); printf("MIXER info, device #%i:\n", idx1);
if ( (err = snd_ctl_mixer_info( handle, idx1, &mixerinfo )) < 0 ) { if ((err = snd_ctl_mixer_info(handle, idx1, &mixerinfo)) < 0) {
printf( " MIXER info error: %s\n", snd_strerror( err ) ); printf(" MIXER info error: %s\n", snd_strerror(err));
continue; continue;
} }
printf( " type - %i\n", mixerinfo.type ); printf(" type - %i\n", mixerinfo.type);
printf( " channels - %i\n", mixerinfo.channels ); printf(" channels - %i\n", mixerinfo.channels);
printf( " caps - 0x%x\n", mixerinfo.caps ); printf(" caps - 0x%x\n", mixerinfo.caps);
printf( " id - '%s'\n", mixerinfo.id ); printf(" id - '%s'\n", mixerinfo.id);
printf( " name - '%s'\n", mixerinfo.name ); printf(" name - '%s'\n", mixerinfo.name);
} }
snd_ctl_close( handle ); snd_ctl_close(handle);
} }
} }

View file

@ -74,7 +74,8 @@ int setparams(void *phandle, void *rhandle, int *fragmentsize)
step = rinfo.min_fragment_size; step = rinfo.min_fragment_size;
while (step < 4096 && while (step < 4096 &&
(step & pinfo.fragment_align) != 0 && (step & pinfo.fragment_align) != 0 &&
(step & rinfo.fragment_align) != 0) step++; (step & rinfo.fragment_align) != 0)
step++;
if (*fragmentsize) { if (*fragmentsize) {
*fragmentsize += step; *fragmentsize += step;
} else { } else {
@ -147,7 +148,7 @@ void setscheduler(void)
{ {
struct sched_param sched_param; struct sched_param sched_param;
if (sched_getparam(0, &sched_param)<0) { if (sched_getparam(0, &sched_param) < 0) {
printf("Scheduler getparam failed...\n"); printf("Scheduler getparam failed...\n");
return; return;
} }
@ -165,7 +166,7 @@ long timediff(struct timeval t1, struct timeval t2)
signed long l; signed long l;
t1.tv_sec -= t2.tv_sec; t1.tv_sec -= t2.tv_sec;
l = (signed long)t1.tv_usec - (signed long)t2.tv_usec; l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
if (l < 0) { if (l < 0) {
t1.tv_sec--; t1.tv_sec--;
l = -l; l = -l;
@ -191,12 +192,12 @@ void main(void)
return; return;
} }
if ((err = snd_pcm_open(&rhandle, rcard, rdevice, SND_PCM_OPEN_RECORD)) < 0) { if ((err = snd_pcm_open(&rhandle, rcard, rdevice, SND_PCM_OPEN_RECORD)) < 0) {
printf( "Record open error: %s\n", snd_strerror(err)); printf("Record open error: %s\n", snd_strerror(err));
return; return;
} }
setformat(phandle, rhandle); setformat(phandle, rhandle);
while (1) { while (1) {
if (setparams(phandle, rhandle, &fragmentsize)<0) if (setparams(phandle, rhandle, &fragmentsize) < 0)
break; break;
ok = 1; ok = 1;
ridx = pidx = size = 0; ridx = pidx = size = 0;
@ -217,7 +218,7 @@ void main(void)
playfragment(phandle, buffer, pidx++, fragmentsize); playfragment(phandle, buffer, pidx++, fragmentsize);
size += fragmentsize; size += fragmentsize;
pidx %= 2; pidx %= 2;
if (playbackunderrun(phandle)>0) if (playbackunderrun(phandle) > 0)
ok = 0; ok = 0;
} }
if ((err = snd_pcm_playback_status(phandle, &pstatus)) < 0) { if ((err = snd_pcm_playback_status(phandle, &pstatus)) < 0) {
@ -226,7 +227,7 @@ void main(void)
} }
snd_pcm_flush_record(rhandle); snd_pcm_flush_record(rhandle);
snd_pcm_flush_playback(phandle); snd_pcm_flush_playback(phandle);
if (ok && !playbackunderrun(phandle)>0) { if (ok && !playbackunderrun(phandle) > 0) {
printf("Playback OK!!!\n"); printf("Playback OK!!!\n");
printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n", printf("Playback time = %li.%i, Record time = %li.%i, diff = %li\n",
pstatus.stime.tv_sec, pstatus.stime.tv_sec,

View file

@ -2,59 +2,59 @@
#include <string.h> #include <string.h>
#include "../include/asoundlib.h" #include "../include/asoundlib.h"
static void mixer_test( int card, int device ) static void mixer_test(int card, int device)
{ {
int err, channels; int err, channels;
void *handle; void *handle;
snd_mixer_info_t info; snd_mixer_info_t info;
if ( (err = snd_mixer_open( &handle, card, device)) < 0 ) { if ((err = snd_mixer_open(&handle, card, device)) < 0) {
printf( "Mixer open error: %s\n", snd_strerror( err ) ); printf("Mixer open error: %s\n", snd_strerror(err));
return; return;
} }
printf( "Mixer %i/%i open ok...\n", card, device ); printf("Mixer %i/%i open ok...\n", card, device);
channels = snd_mixer_channels( handle ); channels = snd_mixer_channels(handle);
if ( channels < 0 ) { if (channels < 0) {
printf( "Mixer channels error: %s\n", snd_strerror( channels ) ); printf("Mixer channels error: %s\n", snd_strerror(channels));
return; return;
} }
printf( " Mixer have %i channel%s.\n", channels, channels > 1 ? "s" : "" ); printf(" Mixer have %i channel%s.\n", channels, channels > 1 ? "s" : "");
if ( (err = snd_mixer_info( handle, &info )) < 0 ) { if ((err = snd_mixer_info(handle, &info)) < 0) {
printf( "Mixer info error: %s\n", snd_strerror( err ) ); printf("Mixer info error: %s\n", snd_strerror(err));
return; return;
} }
printf( " Info:\n" ); printf(" Info:\n");
printf( " type - %i\n", info.type ); printf(" type - %i\n", info.type);
printf( " channels - %i\n", info.channels ); printf(" channels - %i\n", info.channels);
printf( " caps - 0x%x\n", info.caps ); printf(" caps - 0x%x\n", info.caps);
printf( " id - '%s'\n", info.id ); printf(" id - '%s'\n", info.id);
printf( " name - '%s'\n", info.name ); printf(" name - '%s'\n", info.name);
snd_mixer_close( handle ); snd_mixer_close(handle);
} }
void main( void ) void main(void)
{ {
int idx, idx1, cards, err; int idx, idx1, cards, err;
void *handle; void *handle;
struct snd_ctl_hw_info info; struct snd_ctl_hw_info info;
cards = snd_cards(); cards = snd_cards();
printf( "Detected %i soundcard%s...\n", cards, cards > 1 ? "s" : "" ); printf("Detected %i soundcard%s...\n", cards, cards > 1 ? "s" : "");
if ( cards <= 0 ) { if (cards <= 0) {
printf( "Giving up...\n" ); printf("Giving up...\n");
return; return;
} }
for ( idx = 0; idx < cards; idx++ ) { for (idx = 0; idx < cards; idx++) {
if ( (err = snd_ctl_open( &handle, idx )) < 0 ) { if ((err = snd_ctl_open(&handle, idx)) < 0) {
printf( "Open error: %s\n", snd_strerror( err ) ); printf("Open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (err = snd_ctl_hw_info( handle, &info )) < 0 ) { if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
printf( "HW info error: %s\n", snd_strerror( err ) ); printf("HW info error: %s\n", snd_strerror(err));
continue; continue;
} }
for ( idx1 = 0; idx1 < info.mixerdevs; idx1++ ) for (idx1 = 0; idx1 < info.mixerdevs; idx1++)
mixer_test( idx, idx1 ); mixer_test(idx, idx1);
snd_ctl_close( handle ); snd_ctl_close(handle);
} }
} }

View file

@ -4,27 +4,27 @@
#define AU_FILE "/home/ftp/pub/audio/AlwaysOnTheRun.au" #define AU_FILE "/home/ftp/pub/audio/AlwaysOnTheRun.au"
static void show_playback_status( void *handle ) static void show_playback_status(void *handle)
{ {
snd_pcm_playback_status_t pstatus; snd_pcm_playback_status_t pstatus;
if ( snd_pcm_playback_status( handle, &pstatus ) < 0 ) { if (snd_pcm_playback_status(handle, &pstatus) < 0) {
perror( "playback status" ); perror("playback status");
return; return;
} }
printf( "PCM playback status:\n" ); printf("PCM playback status:\n");
printf( " rate = %i\n", pstatus.rate ); printf(" rate = %i\n", pstatus.rate);
printf( " fragments = %i\n", pstatus.fragments ); printf(" fragments = %i\n", pstatus.fragments);
printf( " fragment_size = %i\n", pstatus.fragment_size ); printf(" fragment_size = %i\n", pstatus.fragment_size);
printf( " count = %i\n", pstatus.count ); printf(" count = %i\n", pstatus.count);
printf( " queue = %i\n", pstatus.queue ); printf(" queue = %i\n", pstatus.queue);
printf( " underrun = %i\n", pstatus.underrun ); printf(" underrun = %i\n", pstatus.underrun);
printf( " time = %i.%i\n", (int)pstatus.time.tv_sec, (int)pstatus.time.tv_usec ); printf(" time = %i.%i\n", (int) pstatus.time.tv_sec, (int) pstatus.time.tv_usec);
printf( " stime = %i.%i\n", (int)pstatus.stime.tv_sec, (int)pstatus.stime.tv_usec ); printf(" stime = %i.%i\n", (int) pstatus.stime.tv_sec, (int) pstatus.stime.tv_usec);
printf( " scount = %i\n", pstatus.scount ); printf(" scount = %i\n", pstatus.scount);
} }
void main( void ) void main(void)
{ {
int card = 0, device = 0, err, fd, count, count1, size, idx; int card = 0, device = 0, err, fd, count, count1, size, idx;
void *handle; void *handle;
@ -32,67 +32,70 @@ void main( void )
snd_pcm_playback_status_t status; snd_pcm_playback_status_t status;
char *buffer, *buffer1; char *buffer, *buffer1;
buffer = (char *)malloc( 512 * 1024 ); buffer = (char *) malloc(512 * 1024);
if ( !buffer ) return; if (!buffer)
if ( (err = snd_pcm_open( &handle, card, device, SND_PCM_OPEN_PLAYBACK )) < 0 ) { return;
fprintf( stderr, "open failed: %s\n", snd_strerror( err ) ); if ((err = snd_pcm_open(&handle, card, device, SND_PCM_OPEN_PLAYBACK)) < 0) {
fprintf(stderr, "open failed: %s\n", snd_strerror(err));
return; return;
} }
format.format = SND_PCM_SFMT_MU_LAW; format.format = SND_PCM_SFMT_MU_LAW;
format.rate = 8000; format.rate = 8000;
format.channels = 1; format.channels = 1;
if ( (err = snd_pcm_playback_format( handle, &format )) < 0 ) { if ((err = snd_pcm_playback_format(handle, &format)) < 0) {
fprintf( stderr, "format setup failed: %s\n", snd_strerror( err ) ); fprintf(stderr, "format setup failed: %s\n", snd_strerror(err));
snd_pcm_close( handle ); snd_pcm_close(handle);
return; return;
} }
if ( (err = snd_pcm_playback_status( handle, &status )) < 0 ) { if ((err = snd_pcm_playback_status(handle, &status)) < 0) {
fprintf( stderr, "status failed: %s\n", snd_strerror( err ) ); fprintf(stderr, "status failed: %s\n", snd_strerror(err));
snd_pcm_close( handle ); snd_pcm_close(handle);
return; return;
} }
fd = open( AU_FILE, O_RDONLY ); fd = open(AU_FILE, O_RDONLY);
if ( fd < 0 ) { if (fd < 0) {
perror( "open file" ); perror("open file");
snd_pcm_close( handle ); snd_pcm_close(handle);
return; return;
} }
idx = 0; idx = 0;
count = read( fd, buffer, 512 * 1024 ); count = read(fd, buffer, 512 * 1024);
if ( count <= 0 ) { if (count <= 0) {
perror( "read from file" ); perror("read from file");
snd_pcm_close( handle ); snd_pcm_close(handle);
return; return;
} }
close( fd ); close(fd);
if ( !memcmp( buffer, ".snd", 4 ) ) { if (!memcmp(buffer, ".snd", 4)) {
idx = (buffer[4]<<24)|(buffer[5]<<16)|(buffer[6]<<8)|(buffer[7]); idx = (buffer[4] << 24) | (buffer[5] << 16) | (buffer[6] << 8) | (buffer[7]);
if ( idx > 128 ) idx = 128; if (idx > 128)
if ( idx > count ) idx = count; idx = 128;
if (idx > count)
idx = count;
} }
buffer1 = buffer + idx; buffer1 = buffer + idx;
count -= idx; count -= idx;
if ( count < 256 * 1024 ) { if (count < 256 * 1024) {
perror( "small count < 256k" ); perror("small count < 256k");
snd_pcm_close( handle ); snd_pcm_close(handle);
return; return;
} }
count1 = status.fragment_size * 12; count1 = status.fragment_size * 12;
show_playback_status( handle ); show_playback_status(handle);
size = snd_pcm_write( handle, buffer1, count1 ); size = snd_pcm_write(handle, buffer1, count1);
sleep( 2 ); sleep(2);
show_playback_status( handle ); show_playback_status(handle);
printf( "Pause.. Bytes written %i from %i...\n", size, count1 ); printf("Pause.. Bytes written %i from %i...\n", size, count1);
count -= count1; count -= count1;
buffer1 += count1; buffer1 += count1;
snd_pcm_playback_pause( handle, 1 ); snd_pcm_playback_pause(handle, 1);
show_playback_status( handle ); show_playback_status(handle);
sleep( 5 ); sleep(5);
printf( "Pause end..\n" ); printf("Pause end..\n");
snd_pcm_playback_pause( handle, 0 ); snd_pcm_playback_pause(handle, 0);
show_playback_status( handle ); show_playback_status(handle);
size = snd_pcm_write( handle, buffer1, count ); size = snd_pcm_write(handle, buffer1, count);
printf( "Pause end.. Bytes written %i from %i...\n", size, count ); printf("Pause end.. Bytes written %i from %i...\n", size, count);
snd_pcm_close( handle ); snd_pcm_close(handle);
free( buffer ); free(buffer);
} }

View file

@ -4,15 +4,15 @@
#define BUFFER_SIZE 800000 #define BUFFER_SIZE 800000
static char *xitoa( int aaa ) static char *xitoa(int aaa)
{ {
static char str[12]; static char str[12];
sprintf( str, "%i", aaa ); sprintf(str, "%i", aaa);
return str; return str;
} }
void setformat( void *phandle, void *rhandle ) void setformat(void *phandle, void *rhandle)
{ {
int err; int err;
snd_pcm_format_t format; snd_pcm_format_t format;
@ -21,134 +21,140 @@ void setformat( void *phandle, void *rhandle )
format.format = SND_PCM_SFMT_S16_LE; format.format = SND_PCM_SFMT_S16_LE;
format.channels = 2; format.channels = 2;
format.rate = 22050; format.rate = 22050;
if ( (err = snd_pcm_playback_format( phandle, &format )) < 0 ) { if ((err = snd_pcm_playback_format(phandle, &format)) < 0) {
printf( "Playback format error: %s\n", snd_strerror( err ) ); printf("Playback format error: %s\n", snd_strerror(err));
} }
if ( (err = snd_pcm_record_format( rhandle, &format )) < 0 ) { if ((err = snd_pcm_record_format(rhandle, &format)) < 0) {
printf( "Record format error: %s\n", snd_strerror( err ) ); printf("Record format error: %s\n", snd_strerror(err));
} }
} }
void method1( void ) void method1(void)
{ {
void *phandle, *rhandle; void *phandle, *rhandle;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
int err; int err;
if ( (err = snd_pcm_open( &phandle, 0, 0, SND_PCM_OPEN_PLAYBACK )) < 0 ) { if ((err = snd_pcm_open(&phandle, 0, 0, SND_PCM_OPEN_PLAYBACK)) < 0) {
printf( "Playback open error: %s\n", snd_strerror( err ) ); printf("Playback open error: %s\n", snd_strerror(err));
return; return;
} }
if ( (err = snd_pcm_open( &rhandle, 0, 0, SND_PCM_OPEN_RECORD )) < 0 ) { if ((err = snd_pcm_open(&rhandle, 0, 0, SND_PCM_OPEN_RECORD)) < 0) {
printf( "Record open error: %s\n", snd_strerror( err ) ); printf("Record open error: %s\n", snd_strerror(err));
return; return;
} }
setformat( phandle, rhandle ); setformat(phandle, rhandle);
printf( "Recording... " ); fflush( stdout ); printf("Recording... ");
if ( (err = snd_pcm_read( rhandle, buffer, sizeof( buffer ) )) != sizeof( buffer ) ) { fflush(stdout);
printf( "Read error: %s\n", err < 0 ? snd_strerror( err ) : xitoa( err ) ); if ((err = snd_pcm_read(rhandle, buffer, sizeof(buffer))) != sizeof(buffer)) {
printf("Read error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
return; return;
} }
printf( "done...\n" ); printf("done...\n");
printf( "Playback... " ); fflush( stdout ); printf("Playback... ");
if ( (err = snd_pcm_write( phandle, buffer, sizeof( buffer ) )) != sizeof( buffer ) ) { fflush(stdout);
printf( "Write error: %s\n", err < 0 ? snd_strerror( err ) : xitoa( err ) ); if ((err = snd_pcm_write(phandle, buffer, sizeof(buffer))) != sizeof(buffer)) {
printf("Write error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
return; return;
} }
printf( "done...\n" ); printf("done...\n");
snd_pcm_close( phandle ); snd_pcm_close(phandle);
printf( "Playback close...\n" ); printf("Playback close...\n");
snd_pcm_close( rhandle ); snd_pcm_close(rhandle);
printf( "Record close...\n" ); printf("Record close...\n");
} }
void method2( void ) void method2(void)
{ {
void *phandle, *rhandle; void *phandle, *rhandle;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
int err; int err;
if ( (err = snd_pcm_open( &phandle, 0, 0, SND_PCM_OPEN_PLAYBACK )) < 0 ) { if ((err = snd_pcm_open(&phandle, 0, 0, SND_PCM_OPEN_PLAYBACK)) < 0) {
printf( "Playback open error: %s\n", snd_strerror( err ) ); printf("Playback open error: %s\n", snd_strerror(err));
return; return;
} }
if ( (err = snd_pcm_open( &rhandle, 0, 0, SND_PCM_OPEN_RECORD )) < 0 ) { if ((err = snd_pcm_open(&rhandle, 0, 0, SND_PCM_OPEN_RECORD)) < 0) {
printf( "Record open error: %s\n", snd_strerror( err ) ); printf("Record open error: %s\n", snd_strerror(err));
return; return;
} }
setformat( phandle, rhandle ); setformat(phandle, rhandle);
printf( "Recording... " ); fflush( stdout ); printf("Recording... ");
if ( (err = snd_pcm_read( rhandle, buffer, sizeof( buffer ) )) != sizeof( buffer ) ) { fflush(stdout);
printf( "Read error: %s\n", err < 0 ? snd_strerror( err ) : xitoa( err ) ); if ((err = snd_pcm_read(rhandle, buffer, sizeof(buffer))) != sizeof(buffer)) {
printf("Read error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
return; return;
} }
printf( "done...\n" ); printf("done...\n");
if ( (err = snd_pcm_flush_record( rhandle )) < 0 ) { if ((err = snd_pcm_flush_record(rhandle)) < 0) {
printf( "Record flush error: %s\n", snd_strerror( err ) ); printf("Record flush error: %s\n", snd_strerror(err));
return; return;
} }
printf( "Record flush done...\n" ); printf("Record flush done...\n");
printf( "Playback... " ); fflush( stdout ); printf("Playback... ");
if ( (err = snd_pcm_write( phandle, buffer, sizeof( buffer ) )) != sizeof( buffer ) ) { fflush(stdout);
printf( "Write error: %s\n", err < 0 ? snd_strerror( err ) : xitoa( err ) ); if ((err = snd_pcm_write(phandle, buffer, sizeof(buffer))) != sizeof(buffer)) {
printf("Write error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
return; return;
} }
printf( "done...\n" ); printf("done...\n");
if ( (err = snd_pcm_flush_playback( phandle )) < 0 ) { if ((err = snd_pcm_flush_playback(phandle)) < 0) {
printf( "Playback flush error: %s\n", snd_strerror( err ) ); printf("Playback flush error: %s\n", snd_strerror(err));
return; return;
} }
printf( "Playback flush done...\n" ); printf("Playback flush done...\n");
snd_pcm_close( phandle ); snd_pcm_close(phandle);
printf( "Playback close...\n" ); printf("Playback close...\n");
snd_pcm_close( rhandle ); snd_pcm_close(rhandle);
printf( "Record close...\n" ); printf("Record close...\n");
} }
void method3( void ) void method3(void)
{ {
void *handle; void *handle;
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
int err; int err;
if ( (err = snd_pcm_open( &handle, 0, 0, SND_PCM_OPEN_DUPLEX )) < 0 ) { if ((err = snd_pcm_open(&handle, 0, 0, SND_PCM_OPEN_DUPLEX)) < 0) {
printf( "Duplex open error: %s\n", snd_strerror( err ) ); printf("Duplex open error: %s\n", snd_strerror(err));
return; return;
} }
setformat( handle, handle ); setformat(handle, handle);
printf( "Recording... " ); fflush( stdout ); printf("Recording... ");
if ( (err = snd_pcm_read( handle, buffer, sizeof( buffer ) )) != sizeof( buffer ) ) { fflush(stdout);
printf( "Read error: %s\n", err < 0 ? snd_strerror( err ) : xitoa( err ) ); if ((err = snd_pcm_read(handle, buffer, sizeof(buffer))) != sizeof(buffer)) {
printf("Read error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
return; return;
} }
printf( "done...\n" ); printf("done...\n");
if ( (err = snd_pcm_flush_record( handle )) < 0 ) { if ((err = snd_pcm_flush_record(handle)) < 0) {
printf( "Record flush error: %s\n", snd_strerror( err ) ); printf("Record flush error: %s\n", snd_strerror(err));
return; return;
} }
printf( "Record flush done...\n" ); printf("Record flush done...\n");
printf( "Playback... " ); fflush( stdout ); printf("Playback... ");
if ( (err = snd_pcm_write( handle, buffer, sizeof( buffer ) )) != sizeof( buffer ) ) { fflush(stdout);
printf( "Write error: %s\n", err < 0 ? snd_strerror( err ) : xitoa( err ) ); if ((err = snd_pcm_write(handle, buffer, sizeof(buffer))) != sizeof(buffer)) {
printf("Write error: %s\n", err < 0 ? snd_strerror(err) : xitoa(err));
return; return;
} }
printf( "done...\n" ); printf("done...\n");
if ( (err = snd_pcm_flush_playback( handle )) < 0 ) { if ((err = snd_pcm_flush_playback(handle)) < 0) {
printf( "Playback flush error: %s\n", snd_strerror( err ) ); printf("Playback flush error: %s\n", snd_strerror(err));
return; return;
} }
printf( "Playback flush done...\n" ); printf("Playback flush done...\n");
snd_pcm_close( handle ); snd_pcm_close(handle);
printf( "Close...\n" ); printf("Close...\n");
} }
void main( void ) void main(void)
{ {
printf( ">>>>> METHOD 1\n" ); printf(">>>>> METHOD 1\n");
method1(); method1();
printf( ">>>>> METHOD 2\n" ); printf(">>>>> METHOD 2\n");
method2(); method2();
printf( ">>>>> METHOD 3\n" ); printf(">>>>> METHOD 3\n");
method3(); method3();
} }

View file

@ -2,20 +2,25 @@
#include <string.h> #include <string.h>
#include "../include/asoundlib.h" #include "../include/asoundlib.h"
const char *get_type( unsigned int type ) const char *get_type(unsigned int type)
{ {
switch ( type ) { switch (type) {
case 0: return "Boolean"; case 0:
case 1: return "Byte"; return "Boolean";
case 2: return "Word"; case 1:
case 3: return "DWord"; return "Byte";
case 4: return "User"; case 2:
return "Word";
case 3:
return "DWord";
case 4:
return "User";
default: default:
return "Unknown"; return "Unknown";
} }
} }
void main( void ) void main(void)
{ {
int cards, card, device, direction, idx, count, err; int cards, card, device, direction, idx, count, err;
void *handle, *chandle; void *handle, *chandle;
@ -28,106 +33,106 @@ void main( void )
snd_rawmidi_info_t rmidi_info; snd_rawmidi_info_t rmidi_info;
cards = snd_cards(); cards = snd_cards();
printf( "Detected %i soundcard%s...\n", cards, cards > 1 ? "s" : "" ); printf("Detected %i soundcard%s...\n", cards, cards > 1 ? "s" : "");
if ( cards <= 0 ) { if (cards <= 0) {
printf( "Giving up...\n" ); printf("Giving up...\n");
return; return;
} }
/* control interface */ /* control interface */
for ( card = 0; card < cards; card++ ) { for (card = 0; card < cards; card++) {
if ( (err = snd_ctl_open( &handle, card )) < 0 ) { if ((err = snd_ctl_open(&handle, card)) < 0) {
printf( "CTL open error: %s\n", snd_strerror( err ) ); printf("CTL open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (err = snd_ctl_hw_info( handle, &info )) < 0 ) { if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
printf( "CTL hw info error: %s\n", snd_strerror( err ) ); printf("CTL hw info error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (count = snd_ctl_switches( handle )) < 0 ) { if ((count = snd_ctl_switches(handle)) < 0) {
printf( "CTL switches error: %s\n", snd_strerror( count ) ); printf("CTL switches error: %s\n", snd_strerror(count));
continue; continue;
} }
for ( idx = 0; idx < count; idx++ ) { for (idx = 0; idx < count; idx++) {
if ( (err = snd_ctl_switch_read( handle, idx, &ctl_switch )) < 0 ) { if ((err = snd_ctl_switch_read(handle, idx, &ctl_switch)) < 0) {
printf( "CTL switch read error: %s\n", snd_strerror( count ) ); printf("CTL switch read error: %s\n", snd_strerror(count));
continue; continue;
} }
printf( "CTL switch: '%s' %s (%i-%i)\n", ctl_switch.name, get_type( ctl_switch.type ), ctl_switch.low, ctl_switch.high ); printf("CTL switch: '%s' %s (%i-%i)\n", ctl_switch.name, get_type(ctl_switch.type), ctl_switch.low, ctl_switch.high);
if ( (err = snd_ctl_switch_write( handle, idx, &ctl_switch )) < 0 ) { if ((err = snd_ctl_switch_write(handle, idx, &ctl_switch)) < 0) {
printf( "CTL switch write error: %s\n", snd_strerror( count ) ); printf("CTL switch write error: %s\n", snd_strerror(count));
continue; continue;
} }
} }
if ( count <= 0 ) if (count <= 0)
printf( "No CTL switches detected for soundcard #%i '%s'...\n", idx, info.name ); printf("No CTL switches detected for soundcard #%i '%s'...\n", idx, info.name);
snd_ctl_close( handle ); snd_ctl_close(handle);
} }
/* mixer interface */ /* mixer interface */
for ( card = 0; card < cards; card++ ) { for (card = 0; card < cards; card++) {
if ( (err = snd_ctl_open( &handle, card )) < 0 ) { if ((err = snd_ctl_open(&handle, card)) < 0) {
printf( "CTL open error: %s\n", snd_strerror( err ) ); printf("CTL open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (err = snd_ctl_hw_info( handle, &info )) < 0 ) { if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
printf( "CTL hw info error: %s\n", snd_strerror( err ) ); printf("CTL hw info error: %s\n", snd_strerror(err));
continue; continue;
} }
snd_ctl_close( handle ); snd_ctl_close(handle);
for ( device = 0; device < info.mixerdevs; device++ ) { for (device = 0; device < info.mixerdevs; device++) {
if ( (err = snd_mixer_open( &handle, card, device )) < 0 ) { if ((err = snd_mixer_open(&handle, card, device)) < 0) {
printf( "Mixer open error: %s\n", snd_strerror( err ) ); printf("Mixer open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (count = snd_mixer_switches( handle )) < 0 ) { if ((count = snd_mixer_switches(handle)) < 0) {
printf( "Mixer switches error: %s\n", snd_strerror( count ) ); printf("Mixer switches error: %s\n", snd_strerror(count));
continue; continue;
} }
for ( idx = 0; idx < count; idx++ ) { for (idx = 0; idx < count; idx++) {
if ( (err = snd_mixer_switch_read( handle, idx, &mixer_switch )) < 0 ) { if ((err = snd_mixer_switch_read(handle, idx, &mixer_switch)) < 0) {
printf( "Mixer switch read error: %s\n", snd_strerror( count ) ); printf("Mixer switch read error: %s\n", snd_strerror(count));
continue; continue;
} }
printf( "Mixer switch: '%s' %s (%i-%i)\n", mixer_switch.name, get_type( mixer_switch.type ), mixer_switch.low, mixer_switch.high ); printf("Mixer switch: '%s' %s (%i-%i)\n", mixer_switch.name, get_type(mixer_switch.type), mixer_switch.low, mixer_switch.high);
if ( (err = snd_mixer_switch_write( handle, idx, &mixer_switch )) < 0 ) { if ((err = snd_mixer_switch_write(handle, idx, &mixer_switch)) < 0) {
printf( "Mixer switch write error: %s\n", snd_strerror( count ) ); printf("Mixer switch write error: %s\n", snd_strerror(count));
continue; continue;
} }
} }
if ( count <= 0 ) if (count <= 0)
printf( "No mixer switches detected for soundcard #%i '%s'...\n", idx, info.name ); printf("No mixer switches detected for soundcard #%i '%s'...\n", idx, info.name);
snd_mixer_close( handle ); snd_mixer_close(handle);
} }
} }
/* pcm switches */ /* pcm switches */
for ( card = 0; card < cards; card++ ) { for (card = 0; card < cards; card++) {
if ( (err = snd_ctl_open( &chandle, card )) < 0 ) { if ((err = snd_ctl_open(&chandle, card)) < 0) {
printf( "CTL open error: %s\n", snd_strerror( err ) ); printf("CTL open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (err = snd_ctl_hw_info( chandle, &info )) < 0 ) { if ((err = snd_ctl_hw_info(chandle, &info)) < 0) {
printf( "CTL hw info error: %s\n", snd_strerror( err ) ); printf("CTL hw info error: %s\n", snd_strerror(err));
continue; continue;
} }
for ( device = 0; device < info.pcmdevs; device++ ) { for (device = 0; device < info.pcmdevs; device++) {
if ( (err = snd_ctl_pcm_info( chandle, device, &pcm_info )) < 0 ) { if ((err = snd_ctl_pcm_info(chandle, device, &pcm_info)) < 0) {
printf( "CTL PCM info error: %s\n", snd_strerror( err ) ); printf("CTL PCM info error: %s\n", snd_strerror(err));
continue; continue;
} }
for ( direction = 0; direction < 2; direction++ ) { for (direction = 0; direction < 2; direction++) {
int (*switches)( void *handle ); int (*switches) (void *handle);
int (*switch_read)( void *handle, int switchn, snd_pcm_switch_t *data ); int (*switch_read) (void *handle, int switchn, snd_pcm_switch_t * data);
int (*switch_write)( void *handle, int switchn, snd_pcm_switch_t *data ); int (*switch_write) (void *handle, int switchn, snd_pcm_switch_t * data);
char *str; char *str;
if ( !(pcm_info.flags & (!direction ? SND_PCM_INFO_PLAYBACK : SND_PCM_INFO_RECORD)) ) continue; if (!(pcm_info.flags & (!direction ? SND_PCM_INFO_PLAYBACK : SND_PCM_INFO_RECORD)))
if ( (err = snd_pcm_open( &handle, card, device, !direction ? SND_PCM_OPEN_PLAYBACK : SND_PCM_OPEN_RECORD )) < 0 ) { continue;
printf( "PCM open error: %s\n", snd_strerror( err ) ); if ((err = snd_pcm_open(&handle, card, device, !direction ? SND_PCM_OPEN_PLAYBACK : SND_PCM_OPEN_RECORD)) < 0) {
printf("PCM open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( !direction ) { if (!direction) {
switches = snd_pcm_playback_switches; switches = snd_pcm_playback_switches;
switch_read = snd_pcm_playback_switch_read; switch_read = snd_pcm_playback_switch_read;
switch_write = snd_pcm_playback_switch_write; switch_write = snd_pcm_playback_switch_write;
@ -138,56 +143,57 @@ void main( void )
switch_write = snd_pcm_record_switch_write; switch_write = snd_pcm_record_switch_write;
str = "record"; str = "record";
} }
if ( (count = switches( handle )) < 0 ) { if ((count = switches(handle)) < 0) {
printf( "PCM %s switches error: %s\n", str, snd_strerror( count ) ); printf("PCM %s switches error: %s\n", str, snd_strerror(count));
continue; continue;
} }
for ( idx = 0; idx < count; idx++ ) { for (idx = 0; idx < count; idx++) {
if ( (err = switch_read( handle, idx, &pcm_switch )) < 0 ) { if ((err = switch_read(handle, idx, &pcm_switch)) < 0) {
printf( "PCM %s switch read error: %s\n", str, snd_strerror( count ) ); printf("PCM %s switch read error: %s\n", str, snd_strerror(count));
continue; continue;
} }
printf( "PCM switch: '%s' %s (%i-%i)\n", pcm_switch.name, get_type( pcm_switch.type ), pcm_switch.low, pcm_switch.high ); printf("PCM switch: '%s' %s (%i-%i)\n", pcm_switch.name, get_type(pcm_switch.type), pcm_switch.low, pcm_switch.high);
if ( (err = switch_write( handle, idx, &pcm_switch )) < 0 ) { if ((err = switch_write(handle, idx, &pcm_switch)) < 0) {
printf( "PCM %s switch write error: %s\n", str, snd_strerror( count ) ); printf("PCM %s switch write error: %s\n", str, snd_strerror(count));
continue; continue;
} }
} }
if ( count <= 0 ) if (count <= 0)
printf( "No PCM %s switches detected for soundcard #%i/#%i '%s'/'%s'...\n", str, idx, device, info.name, pcm_info.name ); printf("No PCM %s switches detected for soundcard #%i/#%i '%s'/'%s'...\n", str, idx, device, info.name, pcm_info.name);
snd_pcm_close( handle ); snd_pcm_close(handle);
} }
} }
snd_ctl_close( chandle ); snd_ctl_close(chandle);
} }
/* rawmidi switches */ /* rawmidi switches */
for ( card = 0; card < cards; card++ ) { for (card = 0; card < cards; card++) {
if ( (err = snd_ctl_open( &chandle, card )) < 0 ) { if ((err = snd_ctl_open(&chandle, card)) < 0) {
printf( "CTL open error: %s\n", snd_strerror( err ) ); printf("CTL open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( (err = snd_ctl_hw_info( chandle, &info )) < 0 ) { if ((err = snd_ctl_hw_info(chandle, &info)) < 0) {
printf( "CTL hw info error: %s\n", snd_strerror( err ) ); printf("CTL hw info error: %s\n", snd_strerror(err));
continue; continue;
} }
for ( device = 0; device < info.mididevs; device++ ) { for (device = 0; device < info.mididevs; device++) {
if ( (err = snd_ctl_rawmidi_info( chandle, device, &rmidi_info )) < 0 ) { if ((err = snd_ctl_rawmidi_info(chandle, device, &rmidi_info)) < 0) {
printf( "CTL RawMIDI info error: %s\n", snd_strerror( err ) ); printf("CTL RawMIDI info error: %s\n", snd_strerror(err));
continue; continue;
} }
for ( direction = 0; direction < 2; direction++ ) { for (direction = 0; direction < 2; direction++) {
int (*switches)( void *handle ); int (*switches) (void *handle);
int (*switch_read)( void *handle, int switchn, snd_rawmidi_switch_t *data ); int (*switch_read) (void *handle, int switchn, snd_rawmidi_switch_t * data);
int (*switch_write)( void *handle, int switchn, snd_rawmidi_switch_t *data ); int (*switch_write) (void *handle, int switchn, snd_rawmidi_switch_t * data);
char *str; char *str;
if ( !(pcm_info.flags & (!direction ? SND_RAWMIDI_INFO_OUTPUT : SND_RAWMIDI_INFO_INPUT)) ) continue; if (!(pcm_info.flags & (!direction ? SND_RAWMIDI_INFO_OUTPUT : SND_RAWMIDI_INFO_INPUT)))
if ( (err = snd_rawmidi_open( &handle, card, device, !direction ? SND_RAWMIDI_OPEN_OUTPUT : SND_RAWMIDI_OPEN_INPUT )) < 0 ) { continue;
printf( "RawMIDI CTL open error: %s\n", snd_strerror( err ) ); if ((err = snd_rawmidi_open(&handle, card, device, !direction ? SND_RAWMIDI_OPEN_OUTPUT : SND_RAWMIDI_OPEN_INPUT)) < 0) {
printf("RawMIDI CTL open error: %s\n", snd_strerror(err));
continue; continue;
} }
if ( !direction ) { if (!direction) {
switches = snd_rawmidi_output_switches; switches = snd_rawmidi_output_switches;
switch_read = snd_rawmidi_output_switch_read; switch_read = snd_rawmidi_output_switch_read;
switch_write = snd_rawmidi_output_switch_write; switch_write = snd_rawmidi_output_switch_write;
@ -198,27 +204,27 @@ void main( void )
switch_write = snd_rawmidi_input_switch_write; switch_write = snd_rawmidi_input_switch_write;
str = "input"; str = "input";
} }
if ( (count = switches( handle )) < 0 ) { if ((count = switches(handle)) < 0) {
printf( "RawMIDI %s switches error: %s\n", str, snd_strerror( count ) ); printf("RawMIDI %s switches error: %s\n", str, snd_strerror(count));
continue; continue;
} }
for ( idx = 0; idx < count; idx++ ) { for (idx = 0; idx < count; idx++) {
if ( (err = switch_read( handle, idx, &rmidi_switch )) < 0 ) { if ((err = switch_read(handle, idx, &rmidi_switch)) < 0) {
printf( "RawMIDI %s switch read error: %s\n", str, snd_strerror( count ) ); printf("RawMIDI %s switch read error: %s\n", str, snd_strerror(count));
continue; continue;
} }
printf( "RawMIDI switch: '%s' %s (%i-%i)\n", rmidi_switch.name, get_type( rmidi_switch.type ), rmidi_switch.low, rmidi_switch.high ); printf("RawMIDI switch: '%s' %s (%i-%i)\n", rmidi_switch.name, get_type(rmidi_switch.type), rmidi_switch.low, rmidi_switch.high);
if ( (err = switch_write( handle, idx, &rmidi_switch )) < 0 ) { if ((err = switch_write(handle, idx, &rmidi_switch)) < 0) {
printf( "RawMIDI %s switch write error: %s\n", str, snd_strerror( count ) ); printf("RawMIDI %s switch write error: %s\n", str, snd_strerror(count));
continue; continue;
} }
} }
if ( count <= 0 ) if (count <= 0)
printf( "No RawMIDI %s switches detected for soundcard #%i/#%i '%s'/'%s'...\n", str, idx, device, info.name, rmidi_info.name ); printf("No RawMIDI %s switches detected for soundcard #%i/#%i '%s'/'%s'...\n", str, idx, device, info.name, rmidi_info.name);
snd_rawmidi_close( handle ); snd_rawmidi_close(handle);
} }
} }
snd_ctl_close( chandle ); snd_ctl_close(chandle);
} }
} }