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!

11.1 Polymorphic data structures with multiple types

11.1.1 Existential quantification

11.1.2 Using existentially quantified types

11.2 Interpreting structures

11.2.1 Mixing signals

11.2.2 Groups of polyphony

11.3 Implementing a domain-specific language

11.3.1 Simplifying syntax

11.3.2 Custom operators for list-like data structures

11.3.3 Fixity declarations

Summary