Page 1 of 2

Receiving MIDI System Exclusive Messages

Posted: Wed Aug 31, 2011 6:34 am
by Khorus
Hi everyone,

Been writing some software with PureBasic that deals with realtime MIDI commands. Everything is working good using the Callback method and all, read many examples over here and I got my thing running smoothly.

Now, I realized that I have to deal with system exclusive (sysex) as well and I can't figure out how to implement this in PureBasic. I've tried to find examples and blurbs of source code but to no avail.

Here's what I have as a test program, it almost works but I really don't know how to read the Sysex data once the buffer is filed up.

Code: Select all

Global MidiHeader.MIDIHDR

Procedure MIDICallBack0(hMi.l, wMsg.l, dummy.l , Data1.l, Data2.l)
  Select wMsg
    Case #MIM_DATA
      Debug "Normal MIDI Msg"      
    Case #MIM_LONGDATA
      Debug MidiHeader\dwBytesRecorded
  EndSelect
EndProcedure

*SysexBuffer = AllocateMemory(16384)
hMiP0.l = 0

MIDIHeader\dwFlags = 0
MidiHeader\dwBufferLength = 16384
MidiHeader\lpData = *SysexBuffer

midiInOpen_(@hMiP0, 1, @MIDICallBack0(), 0, #CALLBACK_FUNCTION)
midiInPrepareHeader_(hMiP0, @MidiHeader, SizeOf(MidiHeader)) 
midiInAddBuffer_(hMiP0, @MidiHeader, SizeOf(MidiHeader))
midiInStart_(hMiP0)

OpenWindow(0, 10, 10, 200, 200, "MIDI Test")

Repeat
  Event = WaitWindowEvent(10)
Until Event = #PB_Event_CloseWindow


;midiInStop_(hMiP0)
midiInUnprepareHeader_(hMiP0, @MidiHeader, SizeOf(MidiHeader)) 
midiInReset_(hMiP0)
midiInClose_(hMiP0)
Now, if someone already dealt with this type of "problem", I'd be glad to hear from you! :)

Cheers,

-Khorus

Re: Receiving MIDI System Exclusive Messages

Posted: Wed Aug 31, 2011 7:39 am
by infratec
Hi,

I'm not familar with MIDI, but...

A short look in the wiki tells me, that you simply have to check the first byte of the message.
If it is $F0, than it is a sysex message, else a normal MIDI message.

After $F0 comes the manufacturer code (1 or 3 bytes):
http://www.midi.org/techspecs/manid.php

Than comes the customer data.
That mean you have to know the sequence of bytes.

The last byte of a sysex message is always $F7.

For sending sysex messages to a device look at this:
http://www.midi.org/techspecs/ca18.pdf


Hope this helps a bit.

Bernd

Re: Receiving MIDI System Exclusive Messages

Posted: Wed Aug 31, 2011 4:59 pm
by Khorus
Infratec, I'm very familiar with MIDI messages, been doing studio work for many years. My problem lies with the interaction of PureBasic and the Windows MM API. I'd like to see an example of a working MIDI monitor (or anything else) that works with incoming system exclusive messages.

-K

Re: Receiving MIDI System Exclusive Messages

Posted: Wed Aug 31, 2011 7:34 pm
by infratec
Hi,

have you seen this:

http://www.purebasic.fr/english/viewtop ... hilit=MIDI

I think on page 2 or 3 is interesting code for you.

Re: Receiving MIDI System Exclusive Messages

Posted: Fri Sep 09, 2011 1:31 pm
by Khorus
Hello everyone,

So, in the end, I found the way to do this... I'm posting my source code here for those who would like to check it out.

Code: Select all

; Incoming MIDI Sysex Handler

Global MidiHeader.MIDIHDR
Global hMiP0.l = 0
Global *SysexBuffer = AllocateMemory(4096)

Procedure MIDICallBack0(hMi.l, wMsg.l, dummy.l , Data1.l, Data2.l)
  
  Select wMsg

    Case #MIM_LONGDATA
      
      Debug "---------- NEW INCOMING SYSEX -----------"
      
      For i = 0 To MidiHeader\dwBytesRecorded - 1                 ; Read Buffer Sent from LPData
        Debug Hex(PeekB(*SysexBuffer + i) & $FF)
      Next
      
      midiInAddBuffer_(hMiP0, @MidiHeader, SizeOf(MidiHeader))    ; Recycle Sysex Buffer
 
  EndSelect
EndProcedure

MidiHeader\lpData = *SysexBuffer
MidiHeader\dwBufferLength = 4096

midiInOpen_(@hMiP0, 1, @MIDICallBack0(), 0, #CALLBACK_FUNCTION)
midiInPrepareHeader_(hMiP0, @MidiHeader, SizeOf(MidiHeader)) 
midiInAddBuffer_(hMiP0, @MidiHeader, SizeOf(MidiHeader))
midiInStart_(hMiP0)

OpenWindow(0, 10, 10, 300, 200, "MIDI Test")

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

midiInStop_(hMiP0)
midiInUnprepareHeader_(hMiP0, @MidiHeader, SizeOf(MidiHeader)) 
midiInClose_(hMiP0)

Re: Receiving MIDI System Exclusive Messages

Posted: Sat Feb 07, 2015 1:28 pm
by Joris

Re: Receiving MIDI System Exclusive Messages

Posted: Thu Mar 29, 2018 6:43 am
by MarcosPC
Perfect!
Many thanks Khorus!
I'll test here with the files I already have.

I've been looking for this for a long time!
Gracias!

Re: Receiving MIDI System Exclusive Messages

Posted: Sat Jan 18, 2020 4:54 pm
by MarcosPC
So!
I wonder if with this code I can have midi sound in real time along with sysex.
I saw that there is no command that plays sound.
I realized that there is instruction to receive messages from a keyboard, if I'm not mistaken.
I don't have a keyboard!
I have a vst installed, which accepts sysex message.
In windows media player, it performs perfectly.
Is it possible to produce sound in purebasic with sysex in real time?

Translated from Brazilian Portuguese to English, using the Google translator. Sorry for any mistake!

Re: Receiving MIDI System Exclusive Messages

Posted: Sat Jan 18, 2020 5:41 pm
by NicTheQuick
A long time ago I wrote this code for communicating with MIDI devices on Windows. Maybe this can help you too but it's old Purebasic code you may have to change a little to get it to work: https://www.purebasic.fr/german/viewtop ... hilit=Midi
Then there is also this little keyboard interface to play MIDI instruments: https://www.purebasic.fr/german/viewtop ... hilit=Midi

Re: Receiving MIDI System Exclusive Messages

Posted: Sun Jan 19, 2020 1:41 pm
by Joris
MarcosPC wrote:... Is it possible to produce sound in purebasic with sysex in real time?
Sysex itself does never produce sound.
Sysex is a block of data that lets an instrument or vst set some or all controls, that it has, to get a specific setup. The instrument will then act according the setup just set. Meaning : when played, sound like an organ or piano or whatever.

Re: Receiving MIDI System Exclusive Messages

Posted: Sun Jan 19, 2020 5:28 pm
by SiggeSvahn
Deleted by author.

Re: Receiving MIDI System Exclusive Messages

Posted: Fri Jan 24, 2020 4:35 pm
by MarcosPC
Joris wrote:
MarcosPC wrote:... Is it possible to produce sound in purebasic with sysex in real time?
Sysex itself does never produce sound.
Sysex is a block of data that lets an instrument or vst set some or all controls, that it has, to get a specific setup. The instrument will then act according the setup just set. Meaning : when played, sound like an organ or piano or whatever.
Okay. I may have missed something in the translation.
I will try to explain again.
I know what sysex is.
The "windows media player" plays songs that have sysex on the controller.
Of course, it only does this if the vst or device is selected as the default to run.

Purebasic has code that plays midi sounds (not music!) In real time.
I have sysex parameters to be played by vst and device, in a song.
For example:

"f0 41 10 42 12 00 00 7f 00 01 f7"

I know what each part does.

I need to know if there is a possibility to make the sound emitted by the purebasic code sound, obeying the parameter that I enter, and if there is such a possibility, how to do it.

I don't know how to ask that question any other way. But if it is not clear, I try again, in another way.

Re: Receiving MIDI System Exclusive Messages

Posted: Fri Jan 24, 2020 5:10 pm
by MarcosPC
SiggeSvahn wrote:@MarcosPC: I wrote a sequenser i PureBasic (still in Beta stage) for training your groove timing in samba swing. Really fun to do and to use if you have rhythmic talent. I will post it here when I come home tonight.
/ Sigge
I created something like this!
I'm just involved with sysex, as I said, and how to save the project in midi format.

I cannot use common programs like sonar or cubase, because I am blind, and my screen reader cannot read some tools from these editors.

So I am breaking my head to create something similar for me, even with minimal resources.
If you want, I can send you what I already have.
Even if you can't help me, but want to see the progress, and give me some code suggestions, I'll send it to you.

Re: Receiving MIDI System Exclusive Messages

Posted: Fri Jan 24, 2020 8:35 pm
by Wolfram
Hello MarcosPC,

sysex data are device specific data and can contain data for sound parameter or settings for a individual device.
If you want to address specific sounds you should use a GM Player (General MIDI Player).
There are some software programs like Virtual Canvas that can do this job.
Send your MIDI date over a virtual MIDI connection from your program to the Software.

SysEX never contains a sound just parameter for a device which describe the sound.
Except a SDS. This is a sysex message which contains raw sample data (audio) for a device.

Your Sysex example "f0 41 10 42 12 00 00 7f 00 01 f7" is for an Roland GM Player like SC-88 and includes settings of CC of the SC-88.
Do you have an SC-88?

Re: Receiving MIDI System Exclusive Messages

Posted: Fri Jan 24, 2020 9:08 pm
by MarcosPC
Wolfram wrote:Hello MarcosPC,

sysex data are device specific data and can contain data for sound parameter or settings for a individual device.
If you want to address specific sounds you should use a GM Player (General MIDI Player).
There are some software programs like Virtual Canvas that can do this job.
Send your MIDI date over a virtual MIDI connection from your program to the Software.

SysEX never contains a sound just parameter for a device which describe the sound.
Except a SDS. This is a sysex message which contains raw sample data (audio) for a device.

Your Sysex example "f0 41 10 42 12 00 00 7f 00 01 f7" is for an Roland GM Player like SC-88 and includes settings of CC of the SC-88.
Do you have an SC-88?


Yes!
I have Roland's soundcanvas installed and working perfectly.
I also have the datalist.txt file, which contains more than 1000 sounds from this device, and I also know how to access them.
The parameter I sent is just an example contained in a demo song. But I have other parameters for direct modification on this device, instead of accessing the common "general midi" controls.
I would like to avoid using other programs, not least because they are generally unreadable by my screen reader. So I would like to resolve it directly via code.