00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <sys/soundcard.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <fcntl.h>
00018 #include <sys/ioctl.h>
00019 #include <math.h>
00020
00021 int main(int argc, char *argv[]){
00022 int dsp;
00023 int duplexenable = 1;
00024 audio_buf_info playinfo, recinfo;
00025 int bufsize = 0x7FFF0000;
00026 int fragsize;
00027
00028 int format = AFMT_S16_LE;
00029 int channels = 2;
00030 int samplerate = 44100;
00031
00032 if(argc < 2)
00033 {
00034 bufsize += 15;
00035 }
00036 else
00037 {
00038 fragsize = atol(argv[1]);
00039 if(fragsize <= 0) fragsize = 32768;
00040 bufsize += (long)(log(fragsize) / log(2));
00041 }
00042
00043 #ifdef USE_FULLDUPLEX
00044
00045 printf("*** Full duplex\n");
00046
00047 if((dsp = open("/dev/dsp", O_RDWR)) < 0){
00048 printf("Can't open audio device.\n");
00049 close(dsp);
00050 return 1;
00051 }
00052
00053 if (ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &bufsize))
00054 printf("Couldn't set buffer parameters.\n");
00055
00056 if(duplexenable && ioctl(dsp, SNDCTL_DSP_SETDUPLEX, 0) == -1){
00057 printf("Couldn't enable full duplex audio.\n");
00058 duplexenable = 0;
00059 }
00060
00061 if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
00062 printf("playback file format failed\n");
00063 if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
00064 printf("playback channel allocation failed\n");
00065 if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
00066 printf("playback sample rate set failed\n");
00067
00068 ioctl(dsp, SNDCTL_DSP_GETOSPACE, &playinfo);
00069
00070 printf(" fragments fragstotal fragsize bytes TOTAL BYTES AVAILABLE\n");
00071 printf("Playback: %9d %10d %8d %7d %12d\n", playinfo.fragments,
00072 playinfo.fragstotal, playinfo.fragsize, playinfo.bytes, playinfo.bytes);
00073
00074 if(duplexenable)
00075 {
00076 if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
00077 printf("record file format failed\n");
00078 if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
00079 printf("record channel allocation failed\n");
00080 if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
00081 printf("record sample rate set failed\n");
00082
00083 ioctl(dsp, SNDCTL_DSP_GETISPACE, &recinfo);
00084
00085 printf("Record: %9d %10d %8d %7d %12d\n", recinfo.fragments,
00086 recinfo.fragstotal, recinfo.fragsize, recinfo.bytes, recinfo.fragstotal * recinfo.fragsize);
00087 }
00088 close(dsp);
00089
00090
00091 #endif
00092
00093
00094 printf("\n*** Half duplex\n");
00095
00096 if((dsp = open("/dev/dsp", O_WRONLY)) < 0){
00097 printf("Can't open audio device.\n");
00098 close(dsp);
00099 return 1;
00100 }
00101
00102 if (ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &bufsize))
00103 printf("Couldn't set buffer parameters.\n");
00104
00105 if(duplexenable && ioctl(dsp, SNDCTL_DSP_SETDUPLEX, 0) == -1){
00106 printf("Couldn't enable full duplex audio.\n");
00107 duplexenable = 0;
00108 }
00109
00110 if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
00111 printf("playback file format failed\n");
00112 if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
00113 printf("playback channel allocation failed\n");
00114 if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
00115 printf("playback sample rate set failed\n");
00116
00117 ioctl(dsp, SNDCTL_DSP_GETOSPACE, &playinfo);
00118
00119 printf(" fragments fragstotal fragsize bytes TOTAL BYTES AVAILABLE\n");
00120 printf("Playback: %9d %10d %8d %7d %12d\n", playinfo.fragments,
00121 playinfo.fragstotal, playinfo.fragsize, playinfo.bytes, playinfo.bytes);
00122 close(dsp);
00123
00124
00125
00126 if((dsp = open("/dev/dsp", O_RDONLY)) < 0){
00127 printf("Can't open audio device.\n");
00128 close(dsp);
00129 return 1;
00130 }
00131
00132 if (ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &bufsize))
00133 printf("Couldn't set buffer parameters.\n");
00134
00135 if(duplexenable && ioctl(dsp, SNDCTL_DSP_SETDUPLEX, 0) == -1){
00136 printf("Couldn't enable full duplex audio.\n");
00137 duplexenable = 0;
00138 }
00139
00140 if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
00141 printf("playback file format failed\n");
00142 if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
00143 printf("playback channel allocation failed\n");
00144 if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
00145 printf("playback sample rate set failed\n");
00146
00147 if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
00148 printf("record file format failed\n");
00149 if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
00150 printf("record channel allocation failed\n");
00151 if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
00152 printf("record sample rate set failed\n");
00153
00154
00155
00156
00157 ioctl(dsp, SNDCTL_DSP_GETISPACE, &recinfo);
00158
00159 printf("Record: %9d %10d %8d %7d %12d\n", recinfo.fragments,
00160 recinfo.fragstotal, recinfo.fragsize, recinfo.bytes, recinfo.fragstotal * recinfo.fragsize);
00161
00162 ioctl(dsp, SNDCTL_DSP_RESET);
00163 close(dsp);
00164 }
00165
00166
00167
00168