Restored from previous forum. Originally posted by Stan.
 
Hi,
I am a complete newbie at PB, and would appreciate any help dealing
with sending MIDI commands from PB ( I am trying to make a semi-random
composer for my MIDI racks, kind of poor man's Max or (Korg) Karma thing ).
TIA.
Hi,
Here is what you need :
=====================================================================
;  Simple MIDI messages access
;
;  Copyright 02/2002 by Stan a.k.a [url]mailto:
Daniel-Marc.Ducros@lcpc.fr">
Daniel-Marc.Ducros@lcpc.fr
;
;  Credits : 
;
;  - ALL DLL INTERFACE PROVIDED BY Mr. SKUNKS 
;  
Procedure.s Get_Dev_Caps( Device_Num.l, *Cap_Pointer.MIDIOUTCAPS, Cap_Size.l ) 
  CallDLL3( 4, Device_Num, *Cap_Pointer, Cap_Size )
  n.w = DLLNum( )
  name_string.s = "                                                                      "
  If n = 0
    j = 0
    While *Cap_Pointer\szPname[j]
      PokeB( @name_string+j, *Cap_Pointer\szPname[j] )
      j = j + 1
    Wend
    PokeB( @name_string+j, *Cap_Pointer\szPname[j] )
  EndIf 
  ProcedureReturn name_string  
EndProcedure
; Opens the MIDI device  and return a pointer to it in 
Procedure.w Midi_Out_Open( *Device_Pointer.l, Dev_Num.l ) 
  Null.l = 0
  CallDLL5( 1, *Device_Pointer, Dev_Num, Null, Null, Null ) ; No CallBack ...
  ProcedureReturn DLLNum( )  
EndProcedure
; Closes the MIDI device 
Procedure.w Midi_Out_Close( Midi_Device.l ) 
  CallDLL1( 2, Midi_Device )
  ProcedureReturn DLLNum( )  
EndProcedure
; Changes sound ( GM set only )
;
Procedure.w Change_Sound( Sound.b, Channel.b, *Midi_Device.l ) 
  Zp.l = 0
  PokeB( ( @Zp ) + 1, Sound )         ; New sound number ( see a GM synth doc )
  PokeB( ( @Zp ) + 0, $Bf + Channel ) ; MIDI Change program message ( n = MIDI Channel - 1 )
  CallDLL2( 3, *Midi_Device, Zp )     ; Send the whole shebang !
  ProcedureReturn DLLNum( )    
EndProcedure
; Begins to play Note , with a velocity , on MIDI Channel 
; on MIDI device pointed by 
Procedure.w Note_On( Note.b, Vel.b, Channel.b, Midi_Device.l ) 
  Zp.l = 0
  PokeB( ( @Zp ) + 2, Vel )           ; Velocity 0 = NoteOff ( on most Synths )  64 = mp, 127 = ff 
  PokeB( ( @Zp ) + 1, Note )          ; Note 0 = C-2  127 = G8 ( Depending on synth ... )
  PokeB( ( @Zp ) + 0, $8f + Channel ) ; MIDI Note On Msg 0x9n ( n = MIDI Channel - 1 ) 
  CallDLL2( 3, Midi_Device, Zp )      ; Send the whole shebang !
  ProcedureReturn DLLNum( )    
EndProcedure
; Stop Note , with a velocity , on MIDI Channel 
; on MIDI device pointed by 
Procedure.w Note_Off( Note.b, Vel.b, Channel.b, Midi_Device.l ) 
  Zp.l = 0
  PokeB( ( @Zp ) + 2, Vel )           ; Velocity ( Depends on Synth ... Most don't use it ... )
  PokeB( ( @Zp ) + 1, Note )          ; Note 0 = C-2  127 = G8 ( Depending on synth ... )
  PokeB( ( @Zp ) + 0, $7f + Channel ) ; MIDI Note Off Msg 0x8n ( n = MIDI Channel - 1 )
  CallDLL2( 3, Midi_Device, Zp )      ; Send the whole shebang !
  ProcedureReturn DLLNum( )    
EndProcedure
N.w = Get_Num_Devs( )
For Midi_Dev.l = -1 To N - 1  ; -1 = "Magic" value !!! ( Microsoft MIDI Mapper )
  PrintN( Get_Dev_Caps( Midi_Dev, @midicap, Cap_Size.l ) ) ; Get device properties and print name
  Midi_Out_Open( @Device_Pointer, Midi_Dev )       ; Open device
  Note.b = 48
  Vel.b = 40
  Chan.b = 1
  Sound.b = 0
  For ii = 0 To 4                                  ; Make some noise
    Change_Sound( Sound, Chan, Device_Pointer )     ; Select sound      
    Note_On( Note, Vel, Chan, Device_Pointer )      ; Play 1 note
    Delay( 500 )                                    ; Wait a bit
    Note_Off( Note, Vel, Chan, Device_Pointer )     ; Stop note
    Note = Note + 12                                ; 1 octave up
    Vel = Vel + 20                                  ; Strike harder next time
    Sound = Sound + 10
  Next ii
  Midi_Out_Close( Device_Pointer ) 
Next Midi_Dev
=====================================================================
This works under 98SE, could people with other OS ( W2000, XP ... ),
let me know if it still work for them ?
TIA
Learning and Love are what life is all about ... [ PB. registered user ]