' {$STAMP BS2}
' {$PBASIC 2.5}
' **************************************************************
' *              The "Thereclock" SYNC / MIDI unit             *
' *  (c) 2005/2006 Vern Graner SSE, Texas Information Services *
' **************************************************************
' * Code for BSII to SEND SYNC pulses to the Thereping and     *
' * send MIDI KICK/SNARE beats to drum machine                 *
' **************************************************************
' * Created:  V1.0 12-21-2005                                  *
' **************************************************************

' ************************
' * Pin Assignments      *
' ************************
   SYNC    PIN 15   ' Sync Pulse
   MidiOut PIN 14   ' MIDI Output
   BTN0    PIN 1    'N.O held HIGH 0= "pushed"
   BTN1    PIN 0    'N.O held HIGH 0= "pushed"
   BTN2    PIN 3    'N.O held HIGH 0= "pushed"
   BTN3    PIN 2    'N.O held HIGH 0= "pushed"

' ************************
' * Variables            *
' ************************
 cntI       VAR Byte  ' used in FOR loops

' ************************
' * Constants            *
' ************************
Clock           CON     240
MidiBaud        CON     $8000 + 12              ' 31.25 kBaud -- open
Channel         CON     8                       ' MIDI Channel for Drum Machine: 10
NN              CON     $90 | Channel           ' note on
NX              CON     $80 | Channel           ' note off
N01             CON     1600                    ' whole note
ND2             CON     N01 / 4 * 3             ' dotted half
N02             CON     N01 / 2                 ' half note
N04             CON     N01 / 4                 ' quarter note
N08             CON     N01 / 8                 ' eighth note
N12             CON     N04 / 3                 ' quarter note triplet
N16             CON     N01 / 16
N32             CON     N01 / 32



' ************************
' * Main Program Start   *
' ************************

WaitForStart:

IF BTN2 = 0 THEN

  FOR cntI = 1 TO 8
    SEROUT MidiOut, MidiBaud, [NN,$47,$7F] 'NoteOn  whistle note# 38 at full velocity
    'HIGH SYNC
    PAUSE CLOCK
    SEROUT MidiOut, MidiBaud, [NX,$47,$7F] 'NoteOff whistle note# 38 at full velocity
    'LOW SYNC
    PAUSE CLOCK
  NEXT
  GOTO AROUND
ENDIF

IF btn0 = 1 THEN WaitForStart


' ************************
' * Main Drum Beat       *
' ************************
AROUND:
SEROUT MidiOut, MidiBaud, [NN,$24,$7f] 'NoteOn  kick note# 36 at full velocity
HIGH SYNC
PAUSE CLOCK
SEROUT MidiOut, MidiBaud, [NX,$24,$7f] 'NoteOff kick note# 36 at full velocity
LOW SYNC
PAUSE CLOCK

SEROUT MidiOut, MidiBaud, [NN,$26,$7f] 'NoteOn  snare note# 38 at full velocity
HIGH SYNC
PAUSE CLOCK
SEROUT MidiOut, MidiBaud, [NX,$26,$7f] 'NoteOff snare note# 38 at full velocity
LOW SYNC
PAUSE CLOCK

HIGH SYNC
SEROUT MidiOut, MidiBaud, [NN,$24,$7f] 'NoteOn  kick note# 36 at full velocity
PAUSE CLOCK/2
SEROUT MidiOut, MidiBaud, [NX,$24,$7f] 'NoteOff kick note# 36 at full velocity
PAUSE CLOCK/2

LOW SYNC
SEROUT MidiOut, MidiBaud, [NN,$24,$7f] 'NoteOn  kick note# 36 at full velocity
PAUSE CLOCK/2
SEROUT MidiOut, MidiBaud, [NX,$24,$7f] 'NoteOff kick note# 36 at full velocity
PAUSE CLOCK/2


SEROUT MidiOut, MidiBaud, [NN,$26,$7f] 'NoteOn  snare note# 38 at full velocity
HIGH SYNC
PAUSE CLOCK
SEROUT MidiOut, MidiBaud, [NX,$26,$7f] 'NoteOff snare note# 38 at full velocity
LOW SYNC
PAUSE CLOCK

IF btn1 = 0 THEN WaitForStart

IF BTN3 = 0 THEN

  FOR cntI = 1 TO 8
    SEROUT MidiOut, MidiBaud, [NN,$47,$7F] 'NoteOn  Whistle note# 71 at full velocity
    SEROUT MidiOut, MidiBaud, [NN,$26,$7f] 'NoteOn  snare note# 38 at full velocity
    HIGH SYNC
    PAUSE CLOCK
    SEROUT MidiOut, MidiBaud, [NX,$47,$7F] 'NoteOff Whistle note# 71 at full velocity
    SEROUT MidiOut, MidiBaud, [NN,$24,$7f] 'NoteOn  kick note# 36 at full velocity
    LOW SYNC
    PAUSE CLOCK
  NEXT

    SEROUT MidiOut, MidiBaud, [NN,$47,$7F] 'NoteOn  Whistle note# 71 at full velocity
    SEROUT MidiOut, MidiBaud, [NN,$39,$7f] 'NoteOn  Crash note# 39 at full velocity
    SEROUT MidiOut, MidiBaud, [NN,$26,$7f] 'NoteOn  snare note# 38 at full velocity
    SEROUT MidiOut, MidiBaud, [NN,$24,$7f] 'NoteOn  kick note# 36 at full velocity
    HIGH SYNC
    PAUSE CLOCK
    SEROUT MidiOut, MidiBaud, [NX,$47,$7F] 'NoteOff Whistle note# 71 at full velocity


  GOTO WaitForStart
ENDIF




GOTO AROUND


'references
'********************************************************************
'from   http://www.borg.com/~jglatt/tutr/gm.htm#Keys
'
'GM Drum Sounds
'
'This chart shows what drum sounds are assigned to each MIDI note for a GM module (ie, that has a drum part).
'
'
'MIDI    Drum Sound          MIDI    Drum Sound
'Note #                      Note #
' 35   Acoustic Bass Drum     59   Ride Cymbal 2
' 36   Bass Drum 1            60   Hi Bongo
' 37   Side Stick             61   Low Bongo
' 38   Acoustic Snare         62   Mute Hi Conga
' 39   Hand Clap              63   Open Hi Conga
' 40   Electric Snare         64   Low Conga
' 41   Low Floor Tom          65   High Timbale
' 42   Closed Hi-Hat          66   Low Timbale
' 43   High Floor Tom         67   High Agogo
' 44   Pedal Hi-Hat           68   Low Agogo
' 45   Low Tom                69   Cabasa
' 46   Open Hi-Hat            70   Maracas
' 47   Low-Mid Tom            71   Short Whistle
' 48   Hi-Mid Tom             72   Long Whistle
' 49   Crash Cymbal 1         73   Short Guiro
' 50   High Tom               74   Long Guiro
' 51   Ride Cymbal 1          75   Claves
' 52   Chinese Cymbal         76   Hi Wood Block
' 53   Ride Bell              77   Low Wood Block
' 54   Tambourine             78   Mute Cuica
' 55   Splash Cymbal          79   Open Cuica
' 56   Cowbell                80   Mute Triangle
' 57   Crash Cymbal 2         81   Open Triangle
' 58   Vibraslap