rameo
asked on
Program PC speaker.
anyone can give me a simple C program that can make the PC speaker beep at a certain frequency and certain length in Linux. Thank you very much.
Regards.
Regards.
ASKER
I'm actually looking for a simple ioctl system call that can let me do it. not down to assembler level.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
' NOTE FREQUENCIES (Hz)
' +-------------------------
' | OCTAVE |
'+-----+------------------
'|NOTE | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
'+-----+------------------
'| A |13.7|27.5| 55.0|110.0|220.0|440.0| 880.0|1760.0|3520.0| 7040.0|14080.0|
'|A#/Bb|14.6|29.1| 58.3|116.5|233.1|466.2| 932.4|1864.7|3729.4| 7458.9|14917.8|
'| B |15.4|30.9| 61.7|123.5|247.0|493.9| 987.8|1975.7|3951.3| 7902.7|15805.3|
'| C |16.4|32.7| 65.4|130.8|261.6|523.3|104
'|C#/Db|17.3|34.6| 69.3|138.6|277.2|554.4|110
'| D |18.4|36.7| 73.4|146.8|293.7|587.4|117
'|D#/Eb|19.4|38.9| 77.8|155.6|311.2|622.4|124
'| E |20.6|41.2| 82.4|164.9|329.7|659.4|131
'| F |21.8|43.7| 87.3|174.7|349.3|698.7|139
'|F#/Gb|23.1|46.2| 92.5|185.1|370.1|740.2|148
'| G |24.4|49.0| 98.0|196.1|392.1|784.3|156
'|G#/Ab|26.0|51.9|103.9|20
'+-----+------------------
const C4 = 261.6;
const C5 = 523.3;
static void tone_on(void)
{
__asm
{
mov al,0b6h ; timer control code
out 43h,al ; write it to c2
mov al,bl ; bx = count value
out 42h,al ; write low value
mov al,bh ; high count value
out 42h,al ; write high value
in al,61h ; get control word
or al,03h ; set low two bits, speaker on!
out 61h,al ; write back control word
}
}
void sound(long freq/*in Hz*/)
{
int count=1193180/*frequency of counter*/
/freq;
__asm mov bx, count
tone_on();
}
void nosound(void)
{
__asm
{
in al,61h ; get control word
and al,0FCh ; clear low two bits
out 61h,al ; write back control word
}
}
int main(void)
{
#define SECOND *1000
#define SECONDS SECOND
sound(C4);
sleep(1 SECOND); 'Play for 1 second
nosound();
sound(C5);
sleep(2 SECONDS); 'Play for 2 seconds
nosound();
}