I didn't intend on sharing this MIDI2CODE$ function, made it for my purposes to just quickly convert MIDI data to callable QB64 code snippet to stick small sounds in programs, but thought might as well share it here anyway. Below is a sample of what the MIDI2CODE$ function outputs. It basically just hexify's the data and makes the QB64 code all set up to use. I know we have EMBED stuff now, but I still like to put pure code stuff in now and then. MIDI's are small, just a few lines of code for a neat sound effect is OK with me.
- Dav
Explosion sound effect below....
(.MID file was made with the MidiNotes program)
And here's the MIDI2CODE$ Function. I usually just send the code to clipboard for copy/paste.
- Dav
Explosion sound effect below....
(.MID file was made with the MidiNotes program)
Code: (Select All)
$Unstable:Midi
$MidiSoundFont: Default
'Make Explosion& sound handle
a$ = "4D546864000000060001000101E04D54726B0000001700FF51030F424000C07F0090157F87408015"
a$ = a$ + "4000FF2F00": midi$ = ""
For i = 1 To Len(a$) Step 2
midi$ = midi$ + Chr$(Val("&H" + Mid$(a$, i, 2)))
Next
Explosion& = _SndOpen(midi$, "memory")
_SndPlay Explosion&: _Delay 2
_SndStop Explostion&
And here's the MIDI2CODE$ Function. I usually just send the code to clipboard for copy/paste.
Code: (Select All)
'MIDI2CODE$.BAS
'==============
'Converts MIDI FILE/DATA to callable QB64-PE CODE.
'By Dav, SEP/2024
'This is a tool I made for personal use, sharing it tho.
'You can grab a .MID file...
'Open "4tracks.mid" For Binary As 1
'm$ = Input$(LOF(1), 1): Close 1
'Send the code to the clipboard to look at.
'_Clipboard$ = MIDI2CODE$(m$, 1)
'Print "Sent to clipboard"
Function MIDI2CODE$ (in$, compress)
'in$ = the midi data you want to use.
'compress = use 1 to use compression, 0 for none.
'It's most always better to use compression, unless you
'have a one or two note midi for a sound effect. If
'that's the case you end up a little less code bloat
'witout using compression.
If compress = 1 Then in$ = _Deflate$(in$)
out$ = ""
For i = 1 To Len(in$)
out$ = out$ + Right$("0" + Hex$(Asc(Mid$(in$, i, 1))), 2)
Next
'out$ is now hexified in$
'wrap that and build qb64 code
max = 80 'max length of line
startpos = 1: q$ = Chr$(34): first = 1
Do While startpos <= Len(out$)
endpos = startpos + max - 1
If endpos > Len(out$) Then endpos = Len(out$)
'complete lineof maxlength
If first = 1 Then
out2$ = "midi$ = " + q$ + Mid$(out$, startpos, endpos - startpos + 1) + q$ + Chr$(13)
first = 0
max = max - 8 'shift len now for midi$ name, looks better
Else
out2$ = out2$ + "midi$ = midi$ + " + q$ + Mid$(out$, startpos, endpos - startpos + 1) + q$ + Chr$(13)
End If
'move start position to the next group
startpos = endpos + 1
Loop
'now out2$ is hexifided data in qb64 code
'so make out3$, final product with decoder
out3$ = "$Unstable:Midi" + Chr$(13)
out3$ = out3$ + "$MidiSoundFont: Default" + Chr$(13)
out3$ = out3$ + "'Name your midi file here...." + Chr$(13)
out3$ = out3$ + out2$
out3$ = out3$ + "mididata$ = " + q$ + q$ + Chr$(13)
out3$ = out3$ + "For i = 1 To Len(midi$) Step 2" + Chr$(13)
out3$ = out3$ + "mididata$ = mididata$ + Chr$(Val(" + q$ + "&H" + q$ + " + Mid$(midi$, i, 2)))" + Chr$(13)
out3$ = out3$ + "Next" + Chr$(13)
If compress = 1 Then
out3$ = out3$ + "mididata$ = _inflate$(mididata$)" + Chr$(13)
End If
out3$ = out3$ + "'make your midi sound to use" + Chr$(13)
out3$ = out3$ + "midi& = _SndOpen(mididata$, " + q$ + "memory" + q$ + ")" + Chr$(13)
out3$ = out3$ + "_SndPlay midi&" + Chr$(13)
MIDI2CODE$ = out3$
End Function