home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / SOUNDIO.CMM < prev    next >
Text File  |  1995-10-12  |  2KB  |  61 lines

  1. // SOUNDIO.CMM - Demonstration inport() and outport() functions with the
  2. // ver.2         PORTIO.LIB routines.  This generates sound by directly
  3. //               accessing the hardware port addresses.
  4.  
  5. #include <PortIO.lib>
  6.  
  7. Instructions()
  8. {
  9.    printf("\a\n")
  10.    printf("SoundIO - Sound a specified tone on the internal speaker for specified time\n")
  11.    printf("          using hardword port I/O calls and PORTIO.LIB\n")
  12.    printf("\n")
  13.    printf("SYNTAX:  SOUND Freqency Duration\n")
  14.    printf("\n")
  15.    printf("Where:  Frequency     Tone in hertz\n")
  16.    printf("        Duration      In milliseconds, accurate to %d milliseconds\n",1000/CLOCKS_PER_SEC)
  17.    printf("\n")
  18.    printf("The following example would play middle A for 2 seconds:\n");
  19.    printf("    CENVI2 SOUNDIO 440 2000\n")
  20.    printf("\n")
  21. }
  22.  
  23. main(argc,argv)
  24. {
  25.    if ( argc != 3 || 0 == (frequency=atol(argv[1])) || 0 == (duration=atol(argv[2])) )
  26.       Instructions();
  27.    else
  28.       sound(frequency,duration)
  29. }
  30.  
  31. sound(pFrequency,pDuration)
  32. {
  33.    StartTone(pFrequency);
  34.    suspend(pDuration);
  35.    StopTone();
  36. }
  37.  
  38. StartTone(pFrequency) // start 8253 programmable timer playing this frequency
  39. {
  40.    // determine counter to send to the 8253 programmable timer
  41.    #define  CHIP_RATE   1193180
  42.    counter = CHIP_RATE / pFrequency;
  43.    // program 8253
  44.    #define  COUNTER_REGISTER  0x42
  45.    #define  COMMAND_REGISTER  0x43
  46.    #define  SPEAKER_REGISTER  0x61
  47.    speaker = inport(SPEAKER_REGISTER);
  48.    if ( !(speaker & 0x3) ) {
  49.       speaker |= 0x3;
  50.       outport(SPEAKER_REGISTER,speaker);
  51.       outport(COMMAND_REGISTER,0xB6);
  52.    }
  53.    outport(COUNTER_REGISTER,counter & 0xFF);
  54.    outport(COUNTER_REGISTER,(counter >> 8) & 0xFF);
  55. }
  56.  
  57. StopTone()
  58. {
  59.    outport(SPEAKER_REGISTER,inport(SPEAKER_REGISTER) & 0xfc);
  60. }
  61.