MIDI input from a keyboard?

Blitz3D Forums/Blitz3D Programming/MIDI input from a keyboard?

Caff(Posted 2005) [#1]
Does anyone know if it's it possible to capture keyboard midi data into Blitz3d from a soundcard MIDI input port?

I suspect this is a job for a userlib DLL? Is there one out there?


Damien Sturdy(Posted 2005) [#2]
*grumbles about HAL and a real Peek/Poke command*

Poke &H330,<forgot>
e=Peek &h331

*grumbles more about real Peek/Poke command*

Microsoft killed off an fm synth/sequencer of mine by shoving the HAL into 2000/xp... the buggers.... now i cant access crap :/


John Blackledge(Posted 2005) [#3]
There's a midi system in the Tools section.


Caff(Posted 2005) [#4]
Crikey! Never noticed that. Thanks very much John :)


Caff(Posted 2005) [#5]
And it seems to work very nicely! Thanks Ziltch :)


fall_x(Posted 2005) [#6]
Before I get over excited and start planning something that is not possible : could I make a midi controller with this? I mean, a program that sends out specific midi messages when certain buttons are pressed, or the mouse wheel is moved, or the joystick?
It seems to be the case, but I'm not very familiar with midi (I use it, but I don't know how the actual midi messages work).


John Blackledge(Posted 2005) [#7]
Ooh, be very careful before you jump in.

Years ago I wrote a midi-sequencer and it took me some months to get my head around the structure of each type of midi message, before I could even begin programming.

To complicate matters further, Microsoft then arrived and began to throw their weight around (before, it had just been up to the International Midi Consortium to create and maintain a cross-platform standard), and now I gather that things have changed dramatically with e.g. a channel potential that takes you way past the original 16 standard channels.

Search the web, download docs, and become very familiar with what you're up against before you start.

The trouble is, in effect you are in the same position as someone who can code, but wants to hand-code a browser or ftp program. You need all the docs, background and specs before you begin.

As a final note can I just add that the hub of any midi program is the concept of 'internal clocks' (one for each track) driven by a master sync-clock.
Understanding Midi only gets you so far; you will still have to create a helluva realtime framework for it all to hang off.

The best of luck to you.


Caff(Posted 2005) [#8]
I'm trying to play a 22khz sample at the correct rate for the key pressed... any ideas how to go about this?

It seems to be a logarithmic effect(or exponential? my mathematics is shocking - apologies).

The higher/lower the pitch the further 'out' the sound is. ie. it's not just a case of:

ChannelPitch(chan1, 22000 + midi_pitch)


John Blackledge(Posted 2005) [#9]
Caff, sorry if this is news to you:

1) M.I.D.I. (musical instrument digital interface)
and
2) 'sample-playing'
are two completely different systems.

If you want to play samples then follow through the Channel commands.

Trust me, to a programmer midi is more akin to printer control codes than it is to sample pitch or frequency, so don't get sidetracked.


John Blackledge(Posted 2005) [#10]
Let me just expand on that:

With the statement "ChannelPitch(chan1, 22000 + midi_pitch)"
-you have mixed up two completely (incompatible) systems.

1) M.I.D.I. (Musical Instrument Digital Interface)
and
2) 'Sample-Playing'
are two completely different systems.

To a programmer M.I.D.I. transmissions are closer to printer control codes than to samples.
For example Middle C in midi is the value 60, C# is 61, D is 62 etc.
Whereas Middle C in sample terms is 384hz (-possibly not, my memory's sketchy, but you get the idea).
At this point think of midi as a similar system to teletext, or morse code - merely a way to get one hardware device to send messages to another; not actually a direct way of creating pitched sound.

The value 22000 that you show is not the 'pitch' of a sample (since a sample can contain many pitches) merely the value of accuracy with which the sample was recorded; similar to the concept of '16 bit color','32 bit color'.

If you want to play samples (albeit at different pitches) then follow the Channel commands and search the forums.


Caff(Posted 2005) [#11]
Um, yep I am a grade 8 pianist! So I know what the values of midi 'pitch' represent (ie. 60 = middle C).

I am attempting to use midi input to play samples at the correct pitch. It's a really rough attempt, and I'm only really doing it for fun. If I increment the sample rate by the difference in midi 'pitch' (ie. 62 = D = 2 above middle C) it's accurate, but the further away I get from middle C, the further out the pitch goes.

I was wondering if anyone had experimented with sample pitch according to midi pitch before. There must be an accurate way of reproducing a sample at the correct pitch based on input - ok, so it might sound awful due to the pitch-shifting of the sample used, but if I use several samples spread across each region of the keyboard (ala soundfonts) I might be able to get a decent sound.

I'm a wee bit drunk to play with it again tonight, but maybe tomorrow I'll have another go. I'm sure it's a matter of exponentially increasing the sample pitch.


John Blackledge(Posted 2005) [#12]
I'm going to guess that you have a sample of middle C (midi value 60) recorded at 22000hz.

C one octave up should be double the pitch (22000 x 2).

Therefore C# (one note above middle C) should be 22000 + (22000/12).
Therefore:
D = 22000 + 2x(22000/12)
E = 22000 + 3x(22000/12)
F = 22000 + 4x(22000/12)
etc

Any use? Worth trying anyway.


fall_x(Posted 2005) [#13]
@ John Blackledge : Thanks, and I know the complexity of MIDI. However, I'm not going to write a sequencer. Basically what I want to do is create a simple scripting system where you can set up conditions : if I move my joystick right, I want to send midi message x to channel y with the value of the joystick (multiplied by 127 and rounded, so it's in the 0-127 range that midi uses instead of the 0-1 range that the blitz joystick commands return, for instance). Maybe later I'd add a midi learn function or something.
So basically deciding what messages to send is up to the user.
It will not need to be synchronized to midi either (no time code stuff), it's just a controller, that allows you to send midi messages based on key presses, the mouse wheel and buttons, and the joystick.

What I'd use it for is controlling a program like ableton live, trough Midi Yoke (great midi interface drivers). I don't have the money to buy a midi controller, but I have a ps2-style controller that would do the job perfectly. There are programs that already do this, but none of the free ones seem to have the flexibility I want.


John Blackledge(Posted 2005) [#14]
Sorry, I must have been tired.
I didn't realise that we were having two separate conversations here.

@Caff. The question above still stands. You should be able to to get the keyscan code, then tweak your base sample to produce the appropriate pitch.

@fall_x. Sounds like what you are doing is very much for fun, coz otherwise I think you can buy a small midi-keyboard from as little as £40.
As you are aware the midi note range is from 0 to 127, with middle C at 60.

So a typical 'note-on' message for middle C is:
144 + channel (0 to 15), 60, 127 (velocity value).
And a typical 'note-off' message for middle C is:
128 + channel (0 to 15), 60, 127 (velocity value).
(Sorry if I'm 'teaching my grandmother to suck eggs' here.)

So your only real problem is converting your input device range to a range of 0 to 127. OK?


Caff(Posted 2005) [#15]
@John - ok thanks I'll give it a go! Thinking back, I'll need to check my sample again...


Damien Sturdy(Posted 2005) [#16]
I wasnt aware that library allows me to directly access midi input.. I'll have to take a peek.. alot of fun to be had here!!!!

John, lot of useful info there. We may already know it but i dont doubt other users will find it great to use.

By the way, i'm used to using hex. If you check the internet, the majority of MIDI file tech details seem to be in hex also.

I'm yabbering on, so i'll assume you know what and bugger off :)


fall_x(Posted 2005) [#17]
"@fall_x. Sounds like what you are doing is very much for fun, coz otherwise I think you can buy a small midi-keyboard from as little as £40."
Yes it is, and it's not specifically for controlling notes (but it could be used for that as well), but more for midi control messages, that could be assigned to cut off of a synth for instance. That way I could do filter sweeps with my analog sticks, change the volume (or whatever) with my mouse wheel, etc.
My (very very very basic) scripting language which I'll use for this is almost finished meanwhile, so I'll have a basic version of this up and running in a few days :)

BTW, sorry for hijacking this topic a bit, but since this topic was midi related I posted here.


John Blackledge(Posted 2005) [#18]
@Cygnus: With apologies, I hate hex with a vengeance.
It might have been necessary once to get big numbers into a few bytes, but would you rather see FF, or 255?

"John, lot of useful info there. We may already know it but i dont doubt other users will find it great to use."

Go for it. Midi is great fun. I still remember the first time I sent a 'note-on' followed by a 'note-off' and my synth (on the other end of the wire) chimed a note!
Ah, the heady days of 8-bit.

Similarly, I guess we've all had a kick from seeing our first revolving cube in 3D.
Are we sad, or what? (...Nah.)


Caff(Posted 2005) [#19]
MIDI rocks... :) I used to hook up my Atari ST 1024 to my dad's Yamaha Clavinova!

Only slight problem with this DLL (not the fault of the DLL though) is that it won't take advantage of ASIO drivers, so the latency's a bit high (I'm guessing about 50ms with my E-MU 0404).


fredborg(Posted 2005) [#20]
I did a beat 'em up with motion detectors, that were hooked up to the computer with midi, for a university project some years ago. It could register punch speed and location with reasonable accuracy. Quite fun :)


John Blackledge(Posted 2005) [#21]
MIDI is to music what word-processing is to literature - the essential composing tool.

I remember the first time I heard a 'sample-player':
My friend said 'I've composed this.'
I said 'No, you've dragged some coloured blocks around the screen; and they weren't even yours'.
I couldn't help but laugh in his face.

Modern 'music', eh?

We better end this thread now before it just becomes a set of remeniscences.


fall_x(Posted 2005) [#22]
Anyone tried the midi out functions?

It seems to me that whenever I press a button in my program (which immediately sends a midi note on message, program is running at full speed, just a loop), my midi sequencer plays the note too late, about 1/4 of a second or something.

Could someone verify please? Could be a problem with the sequencer or my midi drivers (yoke nt).

Example :

Include "MidiLib.bb"

MidiOutDevice = SelectMIDIoutDevice()
MidiOutHandle = OpenMidiOut(MidiOutDevice)


While Not KeyHit(1)

	If KeyHit(30) Then
		SendMidiOut(MidiOutHandle,1,MidiNoteOn,60,127)
	End If

Wend



John Blackledge(Posted 2005) [#23]
Midi-Yoke always did have a slight delay.
Could be that, but more likely these new fangled modern drivers that stack/buffer midi messages.

Unfortunately this might be the time for you to start frequenting midi forums, rather than asking someone who hasn't programmed midi for 10 years.


Damien Sturdy(Posted 2005) [#24]
I used the delay as an advantage... allowed the work to sound more live by introducing natural delays :) 1/4 second is pretty high though.. Yoke wasnt that bad...


John Blackledge (Posted 2005-05-19 09:38:51)

@Cygnus: With apologies, I hate hex with a vengeance.
It might have been necessary once to get big numbers into a few bytes, but would you rather see FF, or 255?



yeah, i dont actually like hex, but whenever i do midi, i use it. I'm not even sure why... i suppose, $80+channel (i could decipher $8f to command<channel> pretty easy..)

Similarly, I guess we've all had a kick from seeing our first revolving cube in 3D.
Are we sad, or what? (...Nah.)



Heh, i was worse. i actually had DREAMS about my little 3d dude running aronud a castle driving a car i put in it and the dragon i put in....


Caff(Posted 2005) [#25]
I just found this page...

http://www.armchairarcade.com/aamain/forum_viewtopic.php?10.13163

hehe

I'll give it a shot tonight!


John Blackledge(Posted 2005) [#26]
Strange, but that's how I made _my_ TI994A play music!
A frequency table, and lots of other lookup tables.
Those were the days.
(Oh god, there I go again.)