#include /* next 3 lines for open */ #include #include #include /* malloc */ #include /* read()/write() */ #include /* For IOCTL calls */ #include /* For Audio Defs */ #include #include /* for cos() */ #define QUANTIZE 16 #define CHANNELS 1 int main(void) { int n; unsigned long int i; int sound_fd, status; unsigned long int bufsize, arg; unsigned short int *buf; unsigned int samplerate = 32000; unsigned long int samples = samplerate * .330; sound_fd = open("/dev/dsp", O_RDWR); if (sound_fd < 0) { perror("/dev/dsp"); exit(1); } bufsize = samples * sizeof(unsigned short int); buf = (unsigned short int *) malloc(bufsize); arg = QUANTIZE; status = ioctl(sound_fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_BITS ioctl failed"); exit(1); } if (arg != QUANTIZE) { perror("unable to set quantize rate"); exit(1); } arg = samplerate; status = ioctl(sound_fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_RATE ioctl failed"); exit(1); } arg = CHANNELS; status = ioctl(sound_fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1) { perror("SOUND_PCM_WRITE_CHANNELS ioctl failed"); exit(1); } printf("Bufsize: %i\n", bufsize); for (i = 0; i < samples; i++) { buf[i] = 0; } for (n = 0; n < 5; n++) { for (i = (n * .066 * samplerate); i < (samplerate * (.033 + .066*n)); i++) { buf[i] = (unsigned short int) ((cos(2*M_PI*1700*i/samplerate) + cos(2*M_PI*2200*i/samplerate)) * 10000); } } printf("Generating tones...\n"); status = write(sound_fd, buf, bufsize); if (status != bufsize) { perror("wrote wrong number of bytes"); } status = ioctl(sound_fd, SOUND_PCM_SYNC, 0); if (status == -1) { perror("SOUND_PCM_SYNC ioctl failed"); } return 0; }