686
|
1 // This small util discovers your audio driver's behaviour
|
|
2
|
752
|
3 //#define OUTBURST 512
|
686
|
4 //#define OUTBURST 4096
|
752
|
5 #define MAX_OUTBURST 32768
|
686
|
6
|
|
7
|
|
8 #include <stdio.h>
|
|
9 #include <stdlib.h>
|
|
10 #include <fcntl.h>
|
|
11 #include <sys/soundcard.h>
|
|
12
|
|
13 #include <sys/time.h>
|
|
14 #include <sys/types.h>
|
|
15 #include <unistd.h>
|
|
16
|
|
17
|
|
18 // Returns current time in microseconds
|
|
19 unsigned int GetTimer(){
|
|
20 struct timeval tv;
|
|
21 struct timezone tz;
|
|
22 // float s;
|
|
23 gettimeofday(&tv,&tz);
|
|
24 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
|
|
25 return (tv.tv_sec*1000000+tv.tv_usec);
|
|
26 }
|
|
27
|
752
|
28 static unsigned char a_buffer[MAX_OUTBURST];
|
686
|
29
|
730
|
30 void inline print_info(int audio_fd){
|
|
31 #if 1
|
|
32 audio_buf_info zz;
|
|
33 ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &zz);
|
|
34 printf("Status: %3d/%d (%d byte/frag) free: %6d\n",
|
|
35 zz.fragments, zz.fragstotal, zz.fragsize, zz.bytes);
|
|
36 #endif
|
|
37 }
|
|
38
|
686
|
39 int main(){
|
|
40 int audio_buffer_size=0;
|
|
41 int r;
|
|
42 int xxx=1024*2;
|
|
43 int audio_fd;
|
|
44 char *dsp="/dev/dsp";
|
|
45 unsigned int t0,t1,t2;
|
752
|
46 int outburst;
|
686
|
47
|
|
48 audio_fd=open(dsp, O_WRONLY);
|
|
49 if(audio_fd<0){
|
|
50 printf("Can't open audio device %s\n",dsp);
|
|
51 return 1;
|
|
52 }
|
730
|
53
|
|
54 // ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
|
|
55
|
746
|
56 // ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
|
686
|
57
|
|
58 r=AFMT_S16_LE;ioctl (audio_fd, SNDCTL_DSP_SETFMT, &r);
|
|
59 r=1; ioctl (audio_fd, SNDCTL_DSP_STEREO, &r);
|
|
60 r=44100; if(ioctl (audio_fd, SNDCTL_DSP_SPEED, &r)==-1)
|
|
61 printf("audio_setup: your card doesn't support %d Hz samplerate\n",r);
|
|
62
|
746
|
63 r=0; ioctl (audio_fd, SNDCTL_DSP_GETBLKSIZE, &r);
|
|
64 printf("fragment size = %d\n",r);
|
752
|
65 outburst=r; if(outburst>4096) outburst=4096;
|
746
|
66
|
|
67 print_info(audio_fd);
|
730
|
68
|
686
|
69 t0=t1=GetTimer();
|
|
70
|
|
71 while(xxx-->0){
|
|
72 char c='B';
|
|
73 fd_set rfds;
|
|
74 struct timeval tv;
|
|
75 FD_ZERO(&rfds); FD_SET(audio_fd,&rfds);
|
|
76 tv.tv_sec=0; tv.tv_usec = 0;
|
752
|
77 // if(select(audio_fd+1, NULL, &rfds, NULL, &tv)) c=' ';
|
|
78
|
|
79 print_info(audio_fd);
|
686
|
80
|
752
|
81 r=0; ioctl (audio_fd, SNDCTL_DSP_GETODELAY, &r); printf("delay = %d\n",r);
|
730
|
82
|
752
|
83 r=write(audio_fd,a_buffer,outburst);
|
686
|
84 t2=GetTimer();
|
|
85 if(r<0) printf("Error writting to device\n"); else
|
|
86 if(r==0) printf("EOF writting to device???\n"); else {
|
|
87 printf("%c %6.3f %6.3f [%6d] writting %3d of %3d bytes in %7d us\n",c,
|
|
88 (float)audio_buffer_size/(44100.0f*4.0f),(float)(t1-t0)*0.000001f,
|
752
|
89 audio_buffer_size,r,outburst,t2-t1);
|
686
|
90 audio_buffer_size+=r;
|
|
91 }
|
|
92 t1=t2;
|
|
93 }
|
|
94
|
|
95 close(audio_fd);
|
|
96
|
|
97 return 0;
|
|
98 }
|
|
99
|