Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!brl-adm!rutgers!ames!ucbcad!ucbvax!ohio-state.ARPA!terrell
From: terrell@ohio-state.ARPA (Eric Terrell)
Newsgroups: comp.sys.atari.st
Subject: (none)
Message-ID: <8701011803.AA01667@ohio-state.ARPA>
Date: Thu, 1-Jan-87 13:03:00 EST
Article-I.D.: ohio-sta.8701011803.AA01667
Posted: Thu Jan  1 13:03:00 1987
Date-Received: Thu, 1-Jan-87 22:24:01 EST
Sender: daemon@ucbvax.BERKELEY.EDU
Organization: The ARPA Internet
Lines: 109

Below is an OSS Personal Pascal program that will enable you to drive
the ST's sound chip from Pascal.

The note numbers entered from the keyboard when the program is run
determine the frequency of the tone played - but if someone can
tell me how to get the note number corresponding to various notes on
the scale, I would appreciate it.


{ sound_demo - A simple Personal Pascal sound demo program.

    You must turn off key-clicks using the control panel before running this
    program, as the key-click routine in the OS will mess up your sound!  We
    hope soon to add to this demo an XBIOS call to turn off the keyclicks. }

PROGRAM sound_demo ;

  CONST
    cmd_write = 128 ;
    cmd_read  = 0 ;
    chana_lo = 0 ;
    chana_hi = 1 ;
    chana_vol = 8 ;
    chan_enable = 7 ;
    enable_sound = 7 ;

  TYPE
    channel = 0..2 ;

  VAR
    volume, note : integer ;



{ Two XBIOS functions (actually one call with two definitions!) needed to
  access the General Instruments sound chip. }

  FUNCTION gia_read( data, register : integer ) : integer ;
    XBIOS( 28 ) ;

  PROCEDURE gia_write( data, register : integer ) ;
    XBIOS( 28 ) ;



{ Call this routine to enable sound to be generated. }

  PROCEDURE Sound_Init ;

    VAR
      port_state : integer ;

    BEGIN
      port_state := gia_read( 0, chan_enable+cmd_read ) ;
      gia_write( port_state&(~enable_sound), chan_enable+cmd_write ) ;
    END ;



{ This routine turns on a particular note on one of the three channels. }

  PROCEDURE Sound( ch : channel ; pitch : integer ; vol : integer ) ;

    BEGIN
      gia_write( vol, chana_vol+ch+cmd_write ) ;
      gia_write( pitch&$FF, chana_lo+ch*2+cmd_write ) ;
      gia_write( shr(pitch,8), chana_hi+ch*2+cmd_write ) ;
    END ;



{ Call this routine to turn off sound after you're finished. }

  PROCEDURE Sound_Off ;

    VAR
      port_state : integer ;

    BEGIN
      Sound( 0, 0, 0 ) ;        { First, make sure all volumes are zero. }
      Sound( 1, 0, 0 ) ;
      Sound( 2, 0, 0 ) ;
      { Now disable sound production on all three channels. }
      port_state := gia_read( 0, chan_enable+cmd_read ) ;
      gia_write( port_state|enable_sound, chan_enable+cmd_write ) ;
    END ;


  BEGIN
    { Main program loop-- ask user for a volume to use... should be 0-15. }
    LOOP
      write( 'volume: ' ) ;
      readln( volume ) ;

      EXIT IF volume = 0 ;

      sound_init ;      { Enable sound. }

      { Sub-loop-- keep generating notes until user enters 0 as a pitch. }
      LOOP
          write( 'note: ' ) ;
          readln( note ) ;
          EXIT IF note = 0 ;
          sound( 0, note, volume ) ;
      END ;

      sound_off ;       { Disable the sound. }
    END ;
  END.