Variable-length quantity in Midi files

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Variable-length quantity in Midi files

Post by capellan » Sun Feb 26, 2017 3:47 am

Hi All,

Recently, while looking at the binary data of MIDI files, I found about Variable Length Quantity, a clever method for storing very large quantities using just a few hexadecimal numbers.

Using a single byte (1 ascii character = 1 hexadecimal number)
we could represent quantities from 0 to 127 (In hexadecimal 00 to 7F)
With two bytes, from 0 to 16383 (hexadecimal 0000 to FF7F)
Three bytes, from 0 to 2,097,151 (hexadecimal from 000000 to FFFF7F)
Four bytes, from 0 to 268,435,455 (hexadecimal from 00000000 to FFFFFF7F)

This stack is a simple interface for code posted by Paul McCleman in rossetacode:
https://rosettacode.org/wiki/Variable-l ... y#LiveCode

Al
Variable_Lenght_Quantity_v01.zip

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Variable-length quantity in Midi files

Post by PaulDaMacMan » Wed Mar 29, 2017 4:24 pm

:D
Glad someone found it useful.
My interest in VLQ has everything to do with the MIDI file format.
As far as I know it's not really used for much else.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Variable-length quantity in Midi files

Post by PaulDaMacMan » Wed Mar 29, 2017 4:30 pm

Also there are upper limits to that code, If I remember correctly it's only accurate up to 28 bit numbers. That's fine for MIDI use because in MIDI it's used for META Times (the amount of time that's passed since the last event on a given channel) so it's enough for many hours of time passing.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Variable-length quantity in Midi files

Post by capellan » Wed Mar 29, 2017 9:56 pm

Hi Paul,

I was looking for creating a LiveCode version of Mozart Dice Musical Game :D
but have to stop until I finish some pending work...
http://sunsite.univie.ac.at/Mozart/dice/


Al

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Variable-length quantity in Midi files

Post by PaulDaMacMan » Thu Mar 30, 2017 5:22 pm

Not sure what platform you are targeting. There hasn't been an easy way to play MIDI files from LiveCode on Mac OS X since Apple abandoned QT Musical Instruments after Quicktime 7.
It is possible to do it in LC using the Browser Widget (NO AudioContext Support) or BrowserCEF extension (AudioContext supported) & Web Browser Plug-In called Jazz-MIDI Widget, then using JavaScript to send MIDI to Apples DLS Synth, or with BrowserCEF extension you can use various Javascript Libraries for WebAudio based output (this does not work with the Browser Widget)
http://jazz-soft.net/
Another option is installing & using FluidSynth to play MIDI, which I'm hoping could will eventually be wrapped with LCB w/FFI for a LC Library Extension.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Variable-length quantity in Midi files

Post by PaulDaMacMan » Thu Mar 30, 2017 5:25 pm

There is also a Commercial LiveCode extension called SunnyMIDI (sorry if that's not accurate name), but that has several drawbacks IMO (it's Mac OS only for one)
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Variable-length quantity in Midi files

Post by capellan » Mon Feb 08, 2021 6:48 am

capellan wrote:
Wed Mar 29, 2017 9:56 pm
Hi Paul,

I was looking for creating a LiveCode version of Mozart Dice Musical Game :D
but have to stop until I finish some pending work...
http://sunsite.univie.ac.at/Mozart/dice/

Al
After all, I made this Mozart Dice Musical Game.
Download from this forum thread:
http://forums.livecode.com/viewtopic.ph ... 79#p201479

al

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Variable-length quantity in Midi files

Post by PaulDaMacMan » Wed Feb 10, 2021 2:55 am

Cool. Since this thread is now 4 years old, I just want to point out for any future visitors that since then I've created a bunch of LiveCode Builder libraries, including several for doing things with MIDI files like playback and real-time MIDI data on macOS, and most recently I worked on an LCB lib that wraps the cross-platform (Mac,win,linux.android) FluidSynth library which can do MIDI playback, realtime, and rendering to static audio .wav files. They're all available in my GitHub repos:
https://github.com/PaulMcClernan?tab=repositories

These variable length quantity LCS functions are still useful for generating MIDI files from scratch though. At some point I may rewrite them in LCB using the bit-shifting commands, which may improve speed.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Variable-length quantity in Midi files

Post by capellan » Wed Feb 10, 2021 4:31 am

Hi Paul,

Many Thanks for creating all Midi utilities!
Do you have a step by step tutorial for the
install order and configuring on Mac?

Al

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Variable-length quantity in Midi files

Post by PaulDaMacMan » Wed Feb 10, 2021 6:25 pm

capellan wrote:
Wed Feb 10, 2021 4:31 am
Hi Paul,

Many Thanks for creating all Midi utilities!
Do you have a step by step tutorial for the
install order and configuring on Mac?

Al
My pleasure! Installing LCB extensions is done with the extension builder in the tools menu of the LC IDE, same as any other LiveCode Builder project. There are tutorials from the mothership so I won't try to reinvent the wheel here. Once Installed in the IDE, most of my LCB is documented and should show up in the Dictionary.

There are several different libraries, depending on what you want to do, note that I have done basically zero testing on iOS, but I have tested these on macOS v10.10.x,10.12.x, and 10.14.x:

By far, the easiest to use on macOS is AVMIDIPlayer https://github.com/PaulMcClernan/LCB_AppleAVMidiPlayer which is very simple API that basically takes a path to a MIDI file and a path to a soundFont or DLS sound bank (there's one included with macOS, buried deep inside the CoreAudio.framework bundle, which I use as the default sound bank), and then plays the music. This should also work on iOS but iOS does not provide any DLS SoundBank so you would have to include one with your iOS standalone build (there's lots of free SoundFonts to use on the web, I've found some some full GM sound sets as small as 2MB).

If you want to do realtime triggering of notes using SoundFonts or DLS on macOS/iOS you would use the AVAudioSampler:https://github.com/PaulMcClernan/LCB_Ap ... dioSampler. With this you load a SoundFont (passing a Path) into memory and then you can send it commands such as noteOn/noteOff along with parameters for note pitch number (1-127), velocity (translates to loudness 1-127), you can also send it other MIDI commands, pitchbend, modulation, etc. Apple's sampler can also load arbitrary sound files (which if you name them with a note name like "C4.aif" or "60.wav" would be assigned to note# 60/middle-C), and Apple Sampler .aupreset files (which is basically XML). This is a single channel virtual instrument (mono-trimble) so you can only use one sample-sound-set at a time.

If you want to get adventurous about realtime software instruments on Mac/iOS there's AppleAudioUnitMIDIInstrument, very much a crash-prone work in progress: https://github.com/PaulMcClernan/LCB_Ap ... Instrument This one can (at least try to) load ANY AudioUnit Software Instrument, including Apple's AUSampler, AppleDLSSynth (similar to the Sampler, but multichannel), Native Instruments like Kontakt (pretty much instant crash), and lots of synthesizer plugins like Yamaha DX7 emulations and such found on the internet. This is a bit more complicated to use, and I wrote an associated LCB wrapper library that goes along with it called AVAudioUnitComponentManager https://github.com/PaulMcClernan/LCB_AV ... entManager that is helpful for getting the information needed to load various AU Effects Plug-ins / Instruments you have installed in your system (again, there are some that come with Apple's OSes).

If you want to send (and receive, although that is a bit crash-prone) MIDI Data to / from other Applications (such as GarageBand or Logic Pro) or External MIDI hardware (Keyboard, Drum Machine, etc) you would use my CoreMIDI library https://github.com/PaulMcClernan/LCB_CoreMIDI. This produces no sound on it's own. It's purely for using MIDI data on macOS / iOS. CoreMIDI is the oldest MIDI API on macOS/iOS (OSX 10.0/iOS4).

If you want something cross-platform music capabilities, I've recently been working on a wrapper for FluidSynth:https://github.com/PaulMcClernan/lcb_fluidSynth. I've tested this on macOS and Windows 10, but includes the library binaries for Linux and Android as well (making the whole thing rather large). This can do playback of MIDI files via SoundFonts/DLS soundbanks, as well as RealTime triggering of notes/MIDI parameters. It includes two built-in effects (reverb & chorus) and can render MIDI files to flat sound file format (such as .wav). It can also receive incoming MIDI data, although like the CoreMIDI-In, this is also crash prone (which I'm pretty sure is due to threading issues with the engine), it's not quite as crash-prone as the lcb_CoreMIDI-lib's MIDI-in, which is strange because I'm sure it taps into CoreMIDI when running on macOS.

Lastly there's a library called GMIDI https://github.com/PaulMcClernan/LCB_GMIDILib which is just a collection of functions useful for working with General MIDI (GM) / General Standard (Roland GS), such as getting a list of Standard GM Sound Names, or translating Note Numbers to Names (examp. 61 = "C#4/Db4") or Drum Note Names (36 = "Bass Drum 1"), that sort of thing. Also useful is the GUI element Piano Widget: https://github.com/PaulMcClernan/LCB_PianoWidget which sends noteOn/noteOff messages to the engine which you can then pass to the associated commands in some of these other libraries above and it can do some other tricks too.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Variable-length quantity in Midi files

Post by capellan » Wed Feb 10, 2021 8:45 pm

Hi Paul,

Thanks a lot for your detailed answer!

Al

Post Reply

Return to “Multimedia”