QNX RTOS v4 Knowledge Base
QNX RTOS v4 Knowledge Base
Title |
Making tones and beeps come out of the sound card |
Ref. No. |
QNX.000009373 |
Category(ies) |
Development, Audio |
Issue |
Instead of using the PC speaker to make tinny little beeps, we want to make sound come out of the sound card using the QNX4 RTOS. We need beeps or something that can be amplified and will sound different from each other. They will be used as alarms. It goes without saying that the easier the method is, the better.
Do you have an example of how to do this? |
Solution |
Here is a sample program that will play a tone on the sound card. It assumes that Audio is up and running and that /dev/dsp exists.
Changing the size of 'buffer[]' determines how long the note plays for, and 'blah' changes the frequency of the note played. With a bit of massaging you can make it the actual frequency. The program looks for the quasi-frequency (blah) of the note to be passed to it. Note: buffer size has to equal x which is set to 8196.
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
char buffer [8196];
int main( int argc, char *argv[]) { char *system_string; int my_fd; int x, blah, cycles, changes, note;
if (argc == 1) blah = 75; else { blah = atoi( argv[1] ); }
// the next 3 lines of code generate an oscillating // set of values that gives the tone
x = 8196;
cycles = x / blah; changes = cycles / 2; note = 255; while (x > 0) { memset ( buffer, note, x); if (note >0) note =0; else note =255; x-= changes ; } printf ( "opening the /dev/dsp to write ton"); my_fd = open ( "/dev/dsp", O_WRONLY | O_TRUNC); if ( my_fd == -1) { printf (" error connecting to /dev/dsp n"); return -1; } write ( my_fd, buffer,sizeof(buffer)); printf (" it should be done now. n"); close (my_fd); return 0; }
|
|