Receiving MIDI System Exclusive Messages

Just starting out? Need help? Post your questions and find answers here.
Khorus
User
User
Posts: 25
Joined: Sat Nov 13, 2010 11:22 pm

Receiving MIDI System Exclusive Messages

Post 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
infratec
Always Here
Always Here
Posts: 6869
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Receiving MIDI System Exclusive Messages

Post 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
Khorus
User
User
Posts: 25
Joined: Sat Nov 13, 2010 11:22 pm

Re: Receiving MIDI System Exclusive Messages

Post 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
infratec
Always Here
Always Here
Posts: 6869
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Receiving MIDI System Exclusive Messages

Post 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.
Khorus
User
User
Posts: 25
Joined: Sat Nov 13, 2010 11:22 pm

Re: Receiving MIDI System Exclusive Messages

Post 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)
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Receiving MIDI System Exclusive Messages

Post by Joris »

Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: Receiving MIDI System Exclusive Messages

Post 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!
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: Receiving MIDI System Exclusive Messages

Post 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!
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Receiving MIDI System Exclusive Messages

Post 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
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Joris
Addict
Addict
Posts: 885
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: Receiving MIDI System Exclusive Messages

Post 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.
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
SiggeSvahn
User
User
Posts: 40
Joined: Wed Oct 06, 2010 9:37 pm

Re: Receiving MIDI System Exclusive Messages

Post by SiggeSvahn »

Deleted by author.
Last edited by SiggeSvahn on Sat Mar 21, 2020 8:48 pm, edited 1 time in total.
Newbie
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: Receiving MIDI System Exclusive Messages

Post 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.
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: Receiving MIDI System Exclusive Messages

Post 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.
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: Receiving MIDI System Exclusive Messages

Post 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?
macOS Catalina 10.15.7
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: Receiving MIDI System Exclusive Messages

Post 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.
Post Reply