linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Speaker Sound
@ 2006-03-10 19:33 Alan Bryan
  2006-03-11  1:32 ` Progga
  0 siblings, 1 reply; 2+ messages in thread
From: Alan Bryan @ 2006-03-10 19:33 UTC (permalink / raw)
  To: linux-c-programming

Can someone tell me how, in linux, I could get a tone
from the PC Speaker. In DOS or Windows, I would use
something like the following code:

void Sound(int, int);

main ()
{
    Sound(1000, 100);
}

void Sound(int hertz, int duration)
{
     unsigned divisor = 1193180L / hertz;
     int portval;
     
     portval = inb( 0x61 );
     portval |= 0x03;
     outb( 0x61, portval );
     outb( 0x43, 0xB6 );
     outb( 0x42, divisor & 0xFF ) ;
     outb( 0x42, divisor >> 8 ) ; 
     sleep(duration);
     portval = inb( 0x61 );
     portval &= 0x03;
     outb( 0x61, portval );
     return;
}

The problem is, I'm not sure if I can just read and
output bytes from a port. Any alternate way would be
appreciated!

Thanks


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Speaker Sound
  2006-03-10 19:33 Speaker Sound Alan Bryan
@ 2006-03-11  1:32 ` Progga
  0 siblings, 0 replies; 2+ messages in thread
From: Progga @ 2006-03-11  1:32 UTC (permalink / raw)
  To: linux-c-programming; +Cc: icemanind


[-- Attachment #1.1: Type: text/plain, Size: 289 bytes --]

> }
> 
> The problem is, I'm not sure if I can just read and
> output bytes from a port. Any alternate way would be
> appreciated!

Sample code attached.
Mainly based on the Linux Programmer's Guide.
Runs as suid root.
Depends on ncurses.
Press q-i buttons for various tones.


[-- Attachment #1.2: music.c++ --]
[-- Type: text/plain, Size: 1329 bytes --]


   // Simple harmonium - needs root's privilege to run .


#include<sys/ioctl.h>
#include<sys/kd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<cerrno>
#include<cstdlib>
#include<curses.h>
#include<iostream>




void
main ( )
{

  char ch ;
  int freq, fd = open ("/dev/console", O_WRONLY);


    initscr() ;




    while( 1 ){


          ch = getch() ;

          switch( ch ){

             case 'u' :

             freq = 46   ;

             break ;


             case 'y' :

             freq = 48   ;

             break ;



             case 't' :

             freq = 53   ;

             break ;


             case 'r' :

             freq = 59   ;

             break ;



             case 'e' :

             freq = 65  ;

             break ;



             case 'w' :

             freq =  70  ;

             break ;



             case 'q' :

             freq =  80  ;

             break ;



             case 'i' :

             freq = 90 ;

             break ;


             default:

               freq = -1 ;

                   }


          if( freq == -1 )break ;

          freq *=10 ;


          ioctl (fd, KIOCSOUND, freq );

          usleep ( 200000 );

          ioctl( fd, KIOCSOUND, 0 ) ;

                                 }



  close (fd);

  endwin() ;

}

[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-03-11  1:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-10 19:33 Speaker Sound Alan Bryan
2006-03-11  1:32 ` Progga

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).