10 Digital music box

 

This chapter covers

  • Correctly dealing with numeric values and their type classes
  • Working with infinite lists
  • Modeling a domain-specific problem using Haskell’s data types
  • Building abstractions and high-level structures around low-level implementations

So far, we have been dealing with very earnest topics. Ancient encryption schemes, shortest paths, and CSV file mangling are all serious business. In this chapter, we want to be a bit more playful and let our creative spirit run free by making music! Don’t worry if you have no instrument ready at hand or lack any general idea of how to compose. We will use the power of Haskell not just to serve as our own musical synthesizer but also to function as a composition tool allowing us to easily create the next musical masterpiece remembered by audiences for centuries.

Modeling real-world data inside programs is a normal activity for any programmer. This chapter will specifically teach you how to think about abstracting details and modeling a part of a system in reality (the system of western music theory) as types and values. This kind of modeling comes up every time you want to write software that incorporates some kind of business logic or concepts not directly related to software specifications or architecture. Haskell allows for highly abstract modeling, which fully omits any details that are not relevant to the use case. This is what we are trying to achieve in this chapter.

10.1 Modeling sound with numbers

10.1.1 The zoo of numeric type classes

10.1.2 Creating periodic functions

10.2 Using infinite lists

10.2.1 Attack, decay, sustain, and release

10.2.2 Building and working with infinite lists

10.3 Controlling synthesis

10.3.1 Partial field selectors

10.3.2 A function as a type

10.4 Note models

10.4.1 A type class for pitches

10.4.2 The Ratio type

10.4.3 Different kinds of exponentiation

Summary