#include #include #include #include #include #include #include #include #include unsigned char errmsg[256]; int r; int dsp; int bits=16; int channels; int blocksize; float left=10000; float right=10000; double left_angle=0; double right_angle=0; unsigned char buffer[65536]; void hum(void) { int a; int val; int remains; unsigned char *ptr; new_buffer: ptr=buffer; for (a=0;a>8; val=32767*sin(right_angle); *ptr++=val&255; *ptr++=val>>8; left_angle+=2*M_PI/44100*left; if (left_angle>=2*M_PI) left_angle-=2*M_PI; right_angle+=2*M_PI/44100*right; if (right_angle>=2*M_PI) right_angle-=2*M_PI; } remains=sizeof(buffer); ptr=buffer; a2: if (!remains) goto new_buffer; val=write(dsp,ptr,remains); if (val>=0){ remains-=val; ptr+=val; goto a2; } if (errno==EAGAIN||errno==EINTR) goto a2; perror("beat"); exit(1); } /*-------------------------------MAIN---------------------------------------*/ int main(int argc, char **argv) { int a; again: dsp=open("/dev/dsp",O_WRONLY); if (dsp<0){ if (errno==EINTR||errno==EAGAIN) goto again; } if (r){ fprintf(stderr,"Can't open sound device.\n"); exit(1); } r=ioctl(dsp,SNDCTL_DSP_RESET,NULL); if (r){ fprintf(stderr,"Can't reset sound device.\n"); exit(1); } a=44100; r=ioctl(dsp,SNDCTL_DSP_SPEED,&a); if (r){ fprintf(stderr,"Can't set 44100.\n"); exit(1); } a=1; r=ioctl(dsp,SNDCTL_DSP_STEREO,&a); if (r){ fprintf(stderr,"Can't set stereo.\n"); exit(1); } a=16; r=ioctl(dsp,SOUND_PCM_WRITE_BITS,&a); if (r){ fprintf(stderr,"Can't set 16bit.\n"); exit(1); } r=ioctl(dsp,SNDCTL_DSP_GETBLKSIZE,&blocksize); if (r){ fprintf(stderr,"Can't get blocksize.\n"); exit(1); } r=ioctl(dsp,SNDCTL_DSP_SYNC,NULL); if (r){ fprintf(stderr,"Can't sync device.\n"); exit(1); } hum(); return 0; printf("Error setting up driver\n"); return 1; }