navigationGo.pngQuick Navigation
allprojects32.pngAll projects
hardware32.pngHardware
links32.pngLinks

favoriteStar32.pngTop projects
Alan numitron clock
Clapclap 2313/1386
SNES Pi Webserver
USB Volume/USB toys
Smokey amp
Laser cutter
WordClock
ardReveil v3
SNES Arcade cabinet
Game boy projects
cameleon
Home Presence Detector

github32.pngGitHub
AlanFromJapan

navigationMail.pngContact me

alanfjmail.png
3flags.pngWho's Alan?


Akizukidenshi
Elec-lab
Rand Nerd Tut
EEVblog
SpritesMods
AvrFreaks
Gameboy Dev
FLOZz' blog
Switch-science
Sparkfun
Suzusho
Datasheet Lib
Reddit Elec
Ermicro
Carnet du maker (fr)

MIDI, CSharp and Yamaha chips

Last update: Thu Jun 5 22:25:41 2025

How to transform a MIDI file to Yamaha's chip command ?

That was my question since in addition to be lame with electronics, I am lame at music. So, how could I revamp a not-too-complex tune from a MIDI file to something Yamaha's chip can understand.

After little search I found on CodeProject (heaven for C# developpers) a very good tool for MIDI : CSharpMidiToolkit from Mr. Leslie Sanford. This man has passion for MIDI, and his code is quite nice. And also it can replay a midi tune and interprete it ... and show it on a visual piano by getting the MIDI key code... WAIT A SEC : that's exactly what I need!

So after a little peep at the code one can easily manage to retrieve the keys (on the piano) that are pressed (just listen to the good event and you're done, code is very clear). Problem comes with MIDI structure: it has many channel that are playing in parallel.
Indeed, our Yamaha YMZ294 (or whatever of its siblings) has also many channel... but no way to program it parallely. We have to do time-multiplexing of the notes to be able to program the chip. No worries, it's IT engineering school classics, and anyway a chronogram and a little logic can lead you through.

MIDI_Chronogramme1.png
We have 3 primitives only available : play(channel, note) that starts playing a note, stop(channel) that mutes a channel and sleep (duration) that makes the cpu sleep.
Good thing is that the implementation of MIDI intepretation in Mr Sanford's code is based on events. Play note, stop note are the 2 only events we're interrested in.

So in both our chronograms, we start playing a note on channel A (play(A, note1)) and save time as T.
Then comes an even :
  • Stop A : cpu should have slept now - T, and then stop channel A
    • sleep(Now - T)
    • stop(A)
    • T = Now
  • Play B : cpu should have slept now - T, and then play channel B
    • sleep(Now - T)
    • play(B, Note2)
    • T = Now

So logic is simple in fact : at each event, sleep for duration since latest even, do the event, save the time... that's all...

MidiSequencerToYMZ.PNG
Tadaaaa ♪ You can see at the bottom the "arduino + YMZ294" version. It's not perfect but with the few parameters and depending on the tune it might give a quite nice output (especially this Final Fantasy IV tune that sounds like the real stuff!). Enjoy.

Codes

CSharpMidiToolkitV5_demo.zip This is the Leslie Sanford excellent application that I scavenged. Please have a look to latest version on Codeproject. Thanks again to him!.
Form1.SequencerDemo.zip the 3 files I've changed to allow generating YMZ294 compatible tune from midi (in the SequencerDemo project, the main form in fact).
SequencerDemoPlusVamp.zip last but not least, the precompiled application (C# 2.0)
All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated)
electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2025])