Chapter 22. From Performance to MIDI. Motivation. Abstractly, an MDL program denotes a Performance . But a Performance is just a Haskell data structure – we can analyze it, but we cannot hear it! To remedy this, we will convert a Performance into a MIDI File.
From Performance to MIDI
[ see text for details ]
performToMEvs :: (MidiChannel,ProgNum,Performance) -> [MEvent]
performToMEvs (ch,pn,perf)= let setupInst = MidiEvent 0 (ProgChange ch pn) setTempo = MetaEvent 0 (SetTempo tempo) loop  =  loop (e:es) = let (mev1,mev2) = mkMEvents ch e in mev1 : insertMEvent mev2 (loop es) in setupInst : setTempo : loop perf
tempo = 500000 :: Int -- number of microsecs in one beat
insertMEvent :: MEvent -> [MEvent] -> [MEvent]insertMEvent ev1  = [ev1]insertMEvent ev1@(MidiEvent t1 _) evs@(ev2@(MidiEvent t2 _) : evs') = if t1 <= t2 then ev1 : evs else ev2 : insertMEvent ev1 evs'