[Haskell-beginners] strict evaluation and MIDI playback
apfelmus at quantentunnel.de
Fri Nov 6 22:22:46 UTC 2015
note that the `evaluate` function only evaluates to weak head normal
form (WHNF). A list may be in WHNF even when the head and tail are
unevaluated expressions. What you want is to evaluate the list of notes
to normal form (NF).
The `Control.DeepSeq` module collects functions related to obtaining
normal forms. In your case, changing the line to
midiEvents <- evaluate $ force $ computeMidiEvents music
will probably do the trick, but you may need to implement an instance
NFData for your `MidiEvent` type.
For more details on NF, WHNF and lazy evaluation in general, see also .
Dennis Raddle wrote:
> I wrote a program in Haskell that does real-time MIDI playback. This
> involves computing what are called MIDI "events" (things like notes and
> volume controls) and then using the PortMidi library to play back the
> events. PortMidi also provides a time reading function that is accurate to
> 1 millisecond which is useful for timing the playback accurately.
> I used 'evaluate' in the IO Monad to make sure that all events were fully
> computed before playback started. However I was still getting delays
> (timing errors). I added some code to the program that wrote the data to
> disk before playing it back, and the timing errors went away. Apparently
> 'evaluate' was not forcing a complete evaluation, while writing a disk file
> was. Or at least I think that's what happened.
> But writing to disk is a hack. What can I do to get 'evaluate' to work?
> A simplified version of the code is here: (the actual program is pretty
> long and complex so I posted something which is stripped down but I hope
> retains the essential features)
More information about the Beginners