MIDI test

Archives Forums/Win32 Discussion/MIDI test

Ziltch(Posted 2004) [#1]
Here is some code to play some MIDI music via the Windows API.

It can use the Windows or your Sound card Midi softsynth device, so no external synth is needed to hear noises.




Next MIDI input....


Kanati(Posted 2004) [#2]
Nice. Good goin.


LAB[au](Posted 2005) [#3]
Midi input would be nice indeed, it would be opening blitzmax proggies to a lot of custom interfaces, faders, etc...

Good work :)


Ziltch(Posted 2005) [#4]
I am have troubles get into in/out of windows api data structures.

I wish to figure it out without resorting to peeks and pokes (like my blitz3D version).


Russell(Posted 2005) [#5]
Does the program do anything? For me, it just outputted (?sp) a lot of stuff and then quit (no sound).

Russell


Ziltch(Posted 2005) [#6]
look in code for the line.

OutDevID=0 ' <  Change this if No sound. (Devices start at -1 )

If you comp can play midi then try -1,1,2 , etc


McFox(Posted 2005) [#7]
this is just awesome !
keep going !


Damien Sturdy(Posted 2005) [#8]
Get the INPUT done. Ive got several projects waiting for something like this :D

Excelent work.


Ziltch(Posted 2005) [#9]
I have got basic input working using a callback in C.
I am now working on a blitmax callback version. This I think would be far more useful.

I will post code soon.


semar(Posted 2005) [#10]
It works, nice job ADAmor !

A question (not MIDI related):
1)
Extern "Os" ---> what does this statement mean ?

2)
Function midiOutGetNumDevs:Int()
Function midiOutClose(hMidiOut:Int)
etc...
I seem that these function are located in the WINMM.DLL.
So, shouldn't be Extern "Winmm" instead of Extern "Os" ???

Would be very nice to have the possibility to monitor the MIDI messages of a midi input driver, without take the driver it in exclusive mode.. well you may already know this whish ;-)

Please keep on this *good* project !

Sergio.


semar(Posted 2005) [#11]
@Ziltch,
check this page, it may contain useful info about MidiIn and MidiOut:
http://www.borg.com/~jglatt/tech/lowmidi.htm

Also, I guess you can call MidiInOpen providing a function callback pointer, so to have a function called at each midi activity.. this would work exaclty as I need, like a Midi Monitor - if I understand correctly.

That is, no need of MidiInStart in this case. Or am I missing something ?

How to set up a function callback in BMAX ? Using VarPtr(MyFunction) perhaps ?

Sergio.


Ziltch(Posted 2005) [#12]
The Extern param does work with 'Winmm'. I can not recall why i put 'Os'!

That site you mentioned is a great source for MIDI info. Infact I based my code from the code examples.

The MidiInOpen is what sets up the callback.
Then;
Prepare the MIDI Header with MidiInPrepareHeader,
Setup buffer with midiInAddBuffer,
finally the midiInStart starts the capturing (using the callback function).
So it is needed!!

In regards to Midi Monitors, it looks like only one callback at a time can capture MIDI info. If another MIDI prog is running like cubase, then it will want to be capturing the data itself using a callback. So MIDI Monitors are not so simple.

I have been checking out how to use the midiConnect function. It may help in some kind of work around.

This is what the PSDK help says about it;
"The midiConnect function connects a MIDI input device to a MIDI thru or output device, or connects a MIDI thru device to a MIDI output device."


Here is the purebasic code for my MidiIn.Dll




semar(Posted 2005) [#13]
ADAmor,
With the code below I can't get the callback function to work.. well I get id called once, but suddenly I get unhandled exception error... can you say what is wrong here ?

Graphics 300,200,0
Extern "Os"
'midiInOpen(LPHMIDIIN lphMidiIn,UINT_PTR  uDeviceID,DWORD_PTR dwCallback,DWORD_PTR dwCallbackInstance,DWORD dwFlags)
'Function midiInOpen(lphMidiIn:Byte Ptr, uDeviceID:Byte Ptr, dwCallback:Byte Ptr, dwInstance:Byte Ptr, dwFlags:Int)
Function midiInOpen(lphMidiIn:Byte Ptr, uDeviceID:Int, dwCallback:Byte Ptr, dwInstance:Int, dwFlags:Int)

Function midiInClose(hMidiIn:Int)
Function midiInMessage(hMidiIn:Int, msg:Int, dw1:Int, dw2:Int)
Function midiInGetNumDevs()
Function midiInStart(hMidiIn:Int)
Function midiInStop(hMidiIn:Int)

End Extern

Const CF = $30000

'Local p()
'p = test
'p

'p = test2
'p

Local MidiIn:Int
Local MidiInHandle:Byte Ptr 

MidiInHandle = Varptr MidiIn

'Local callback(handle,  uMsg,  dwInstance,  dwParam1,  dwParam2)
'callback = mycallback


'MidiInStart(1)
Print "Input Devices: " + midiInGetNumDevs()

Try
	ok = MidiInOpen(Byte Ptr(MidiInHandle),2,Byte Ptr(MyCallBack),369,CF)
	'ok = MidiInOpen(Byte Ptr(MidiInHandle),2,Byte Ptr(0),0,0)

'Rem	
Catch ex:Object
	'Cls
	DrawText (ex.Tostring() ,0,30)
	Flip
	'WaitKey()
	Print "catch: " + ex.Tostring()
EndTry
'End Rem

Print "ok = " + ok + " Midi handle " + MidiInHandleI

'midiInStart(MIDI)

'MIDI = MidiInOpen(0,0,Byte Ptr(0),0,0)
'Print "Midi in " + MIDI + " Opened"

While Not KeyDown(KEY_ESCAPE)
Wend

'midiInStop(MIDI)
MidiInClose(MIDI)
Print "Midi in " + MIDI + " Closed"

End

Function test()
Print "test !"
End Function

Function test2()
Print "test 2 !"
End Function


'midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);
Function MyCallBack ( handle:Byte Ptr,  uMsg,  dwInstance,  dwParam1,  dwParam2)
'midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);
'Function MyCallBack()

	DrawText "midi callback",0,0
	Print "midi callback! " + dwInstance
	
	Flip


End Function


Thanks for the code m8 !

Sergio.


Ziltch(Posted 2005) [#14]
I will see if I can see what is wrong.


Ziltch(Posted 2005) [#15]
Got it!

It still has some problems to iron out, but I thought you may want to have a look.



This makes me very Happy !!!!!


Ziltch(Posted 2005) [#16]
I am getting this error sometimes

-------------------------------------------------
Assertion failed: scopeStackTop>0, file f:/blitzmax/mod/brl.mod/appstub.mod/debu
gger.stdio.c, line 487

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Unhandled Exception: Received Signal
BlitzMax Console Debugger H=Help
-------------------------------------------------

I am not sure what is causing it!


semar(Posted 2005) [#17]
I'll check it out too - thanks for the code sharing dude !

Sergio.


Curtastic(Posted 2005) [#18]
hey this seems cool.
I don't understand why this hasn't been asked or stated already but can you use this to play a midi file?


Ziltch(Posted 2005) [#19]
Midi output is alot easier than input.
If you want to play a midi file, windows MCI commands can do that.
Multi platform code would be harder!


Damien Sturdy(Posted 2005) [#20]

I don't understand why this hasn't been asked or stated already but can you use this to play a midi file?



Well, if you know much about the Midi file format, then yes- since this does output :)

Man this reminds me of the old days of QBASIC. Hardcore programming :)