11 Programming musical compositions
This chapter covers
- Constructing a polymorphic data structure that contains data from different types
- Interpreting complex data structures to compute more information
- Implementing a domain-specific language within Haskell
In the last chapter, we built the basis for a software synthesizer. We have introduced types to abstractly represent audio signals and pitch information. By using the HCodec library, we were able to write our audio data to the filesystem.
With the technical details taken care of, we now can talk about composing within our program. We obviously do not want to write down notes on a piece of paper; we want to do it directly within our program. Essentially, our compiled Haskell binary becomes the composition, packed with a synthesizer that also plays the composition! We can think of it as our own digital music box.
In this chapter, we will build this music box by designing a type for representing compositions. While doing so, we will learn how to construct heterogenous data structures, mixing values of different types in the same structure. After defining musical structures and how to interpret them, we will design our own domain-specific language for music compositions within Haskell!