Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Program PC speaker.

Posted on 1999-07-27
3
Medium Priority
?
305 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:rameo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:Iexpert
ID: 1263972
#include <stdlib.h>

'                             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|1046.6|2093.2|4186.5| 8372.9|16745.8|
'|C#/Db|17.3|34.6| 69.3|138.6|277.2|554.4|1108.8|2217.7|4435.5| 8871.1|17742.1|
'|  D  |18.4|36.7| 73.4|146.8|293.7|587.4|1174.8|2349.7|4699.5| 9398.9|18797.8|
'|D#/Eb|19.4|38.9| 77.8|155.6|311.2|622.4|1244.8|2489.5|4979.1| 9958.1|19161.3|
'|  E  |20.6|41.2| 82.4|164.9|329.7|659.4|1318.8|2637.7|5275.3|10550.6|21101.3|
'|  F  |21.8|43.7| 87.3|174.7|349.3|698.7|1397.3|2794.6|5589.2|11178.4|22356.8|
'|F#/Gb|23.1|46.2| 92.5|185.1|370.1|740.2|1480.4|2960.8|5921.8|11843.5|23687.1|
'|  G  |24.4|49.0| 98.0|196.1|392.1|784.3|1568.2|3137.1|6274.1|12548.2|25096.4|
'|G#/Ab|26.0|51.9|103.9|207.7|415.5|830.9|1661.9|3323.7|6647.4|13294.8|29589.7|
'+-----+----------------------------------------------------------------------+

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();
}

0
 

Author Comment

by:rameo
ID: 1263973
I'm actually looking for a simple ioctl system call that can let me do it.  not down to assembler level.


0
 
LVL 2

Accepted Solution

by:
serena77 earned 100 total points
ID: 1263974
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
main() {
int pitch = 1000;
int duration = 50;
int fd;
fd = open("/dev/console", "w");
ioctl(fd, KDMKTONE, ((1193190 / pitch) & 0xffff)
      | ((unsigned long)duration*2 << 16));
}

Dont forget to run this program as root ;)

Regards

Serena
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question