The Haskell School of Music
Total Page:16
File Type:pdf, Size:1020Kb
The Haskell School of Music — From Signals to Symphonies — Paul Hudak Yale University Department of Computer Science Version 2.4 (February 22, 2012) i The Haskell School of Music — From Signals to Symphonies — Paul Hudak Yale University Department of Computer Science New Haven, CT, USA Version 2.4 (February 22, 2012) Copyright c Paul Hudak January 2011 All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a data base or retrieval system, without the prior written permission of the author. Cover image: Euterpe, the Greek Muse of Music (attribution unknown) Contents Preface xiv 1 Overview of Computer Music, Euterpea, and Haskell 1 1.1 The Note vs. Signal Dichotomy ................. 2 1.2 Basic Principles of Programming ................ 3 1.3 Computation by Calculation ................... 4 1.4 Expressions and Values ..................... 8 1.5 Types ............................... 10 1.6 Function Types and Type Signatures .............. 11 1.7 Abstraction, Abstraction, Abstraction ............. 13 1.7.1 Naming .......................... 13 1.7.2 Functional Abstraction .................. 16 1.7.3 Data Abstraction ..................... 19 1.8 Haskell Equality vs. Euterpean Equality ............ 22 1.9 Code Reuse and Modularity ................... 23 1.10 [Advanced] Programming with Numbers ............ 24 2 Simple Music 28 2.1 Preliminaries ........................... 28 2.2 Notes, Music, and Polymorphism ................ 30 2.3 Convenient Auxiliary Functions ................. 33 2.3.1 A Simple Example .................... 35 2.4 Absolute Pitches ......................... 39 ii CONTENTS iii 3 Polymorphic & Higher-Order Functions 43 3.1 Polymorphic Types ........................ 44 3.2 Abstraction Over Recursive Definitions ............. 45 3.2.1 Map is Polymorphic ................... 47 3.2.2 Using map ........................ 48 3.3 Append .............................. 49 3.3.1 [Advanced] The Efficiency and Fixity of Append ... 50 3.4 Fold ................................ 51 3.4.1 Haskell’s Folds ...................... 53 3.4.2 [Advanced] Why Two Folds? .............. 54 3.4.3 Fold for Non-empty Lists ................ 55 3.5 [Advanced] A Final Example: Reverse ............. 56 3.6 Currying .............................. 57 3.6.1 Currying Simplification ................. 59 3.6.2 [Advanced] Simplification of reverse .......... 60 3.7 Errors ............................... 61 4 A Musical Interlude 64 4.1 Modules .............................. 64 4.2 Transcribing an Existing Score ................. 65 4.2.1 Auxiliary Functions ................... 67 4.2.2 Bass Line ......................... 68 4.2.3 Main Voice ........................ 69 4.2.4 Putting It All Together ................. 69 4.3 Simple Algorithmic Composition ................ 71 5 Syntactic Magic 72 5.1 Sections .............................. 72 5.2 Anonymous Functions ...................... 74 5.3 List Comprehensions ....................... 75 5.3.1 Arithmetic Sequences .................. 77 5.4 Function Composition ...................... 78 CONTENTS iv 5.5 Higher-Order Thinking ...................... 79 5.6 Infix Function Application .................... 80 6 More Music 82 6.1 Delay and Repeat ......................... 82 6.2 Inversion and Retrograde .................... 83 6.3 Polyrhythms ........................... 84 6.4 Symbolic Meter Changes ..................... 86 6.5 Computing Duration ....................... 86 6.6 Super-retrograde ......................... 87 6.7 Truncating Parallel Composition ................ 88 6.8 Trills ................................ 89 6.9 Grace Notes ............................ 90 6.10 Percussion ............................. 91 6.11 A Map for Music ......................... 93 6.12 A Fold for Music ......................... 94 6.13 Crazy Recursion ......................... 95 7 Qualified Types and Type Classes 98 7.1 Motivation ............................ 98 7.2 Equality ..............................100 7.3 Defining Your Own Type Classes ................102 7.4 Inheritance ............................106 7.5 Haskell’s Standard Type Classes ................107 7.5.1 The Num Class ......................108 7.5.2 The Show Class .....................111 7.6 Derived Instances .........................112 7.7 Reasoning With Type Classes ..................115 8 Interpretation and Performance 118 8.1 Abstract Performance ......................118 8.2 Players ...............................124 CONTENTS v 8.2.1 Example of Player Construction ............126 8.2.2 Deriving New Players From Old Ones .........128 8.2.3 A Fancy Player ......................129 8.3 Putting it all Together ......................129 9 Self-Similar Music 133 9.1 Self-Similar Melody ........................133 9.1.1 Sample Compositions ..................136 9.2 Self-Similar Harmony .......................137 9.3 Other Self-Similar Structures ..................138 10 Proof by Induction 141 10.1 Induction and Recursion .....................141 10.2 Examples of List Induction ...................142 10.3 Proving Function Equivalences .................144 10.3.1 [Advanced] Reverse ....................145 10.4 Useful Properties on Lists ....................147 10.4.1 [Advanced] Function Strictness .............150 10.5 Induction on the Music Data Type ...............151 10.5.1 The Need for Musical Equivalence ...........156 10.6 [Advanced] Induction on Other Data Types ..........156 10.6.1 A More Efficient Exponentiation Function .......158 11 An Algebra of Music 163 11.1 Musical Equivalance .......................163 11.2 Some Simple Axioms .......................165 11.3 The Axiom Set ..........................168 11.4 Soundness and Completeness ..................169 12 Musical L-Systems 170 12.1 Generative Grammars ......................170 12.2 A Simple Implementation ....................171 12.3 Grammars in Haskell .......................173 CONTENTS vi 12.4 An L-System Grammar for Music ................175 12.5 Examples .............................176 13 Random Numbers ... and Markov Chains 179 13.1 Random Numbers ........................179 13.2 Probability Distributions .....................182 13.2.1 Random Melodies and Random Walks .........186 13.3 Markov Chains ..........................188 13.3.1 Training Data .......................189 14 From Performance to Midi 192 14.1 An Introduction to Midi .....................192 14.1.1 General Midi .......................193 14.1.2 Channels and Patch Maps ................194 14.1.3 Standard Midi Files ...................196 14.2 Converting a Performance into Midi ..............198 14.3 Putting It All Together .....................201 15 Basic Input/Output 202 15.1 IO in Haskell ...........................202 15.2 do Syntax .............................204 15.3 Actions are Just Values .....................205 15.4 Reading and Writing Midi Files .................207 16 Musical User Interface 208 16.1 Signals ...............................209 16.1.1 Numeric Signals .....................210 16.1.2 Time ............................211 16.1.3 Musical Signals ......................212 16.1.4 Useful Signal Operators .................213 16.1.5 Stateful Signals ......................213 16.2 Events and Reactivity ......................214 16.2.1 Manipulating Event Streams ..............214 CONTENTS vii 16.2.2 Turning Signals into Events ...............215 16.2.3 Signal Samplers ......................216 16.2.4 Switches and Reactivity .................216 16.3 The UI Level ...........................217 16.3.1 Input Widgets ......................217 16.3.2 UI Transformers .....................220 16.3.3 MIDI Input and Output .................221 16.3.4 Midi Device IDs .....................222 16.3.5 Timer Widgets ......................224 16.4 Putting It All Together .....................225 16.5 Musical Examples ........................225 16.5.1 Chord Builder ......................225 16.5.2 Bifurcate Me, Baby! ...................227 16.5.3 MIDI Echo Effect .....................229 17 Sound and Signals 231 17.1 The Nature of Sound .......................231 17.1.1 Frequency and Period ..................234 17.1.2 Amplitude and Loudness ................235 17.1.3 Frequency Spectrum ...................239 17.2 Digital Audio ...........................241 17.2.1 From Continuous to Discrete ..............243 17.2.2 Fixed-Waveform Table-Lookup Synthesis .......245 17.2.3 Aliasing ..........................246 17.2.4 Quantization Error ....................249 17.2.5 Dynamic Range ......................251 18 Euterpea’s Signal Functions 253 18.1 Signals and Signal Functions ..................254 18.1.1 The Type of a Signal Function .............256 18.1.2 Four Useful Functions ..................258 18.1.3 Some Simple Examples .................259 CONTENTS viii 18.2 Generating Sound ........................264 18.3 Instruments ............................266 18.3.1 Turning a Signal Function into an Instruement ....266 18.3.2 Envelopes .........................269 19 Spectrum Analysis 273 19.1 Fourier’s Theorem ........................273 19.1.1 The Fourier Transform ..................275 19.1.2 Examples .........................276 19.2 The Discrete Fourier Transform .................277 19.2.1 Interpreting the Frequency Spectrum ..........280 19.2.2 Amplitude and Power of Spectrum ...........282 19.2.3 A Haskell Implementation of the DFT .........284 19.3 The Fast Fourier Transform ...................290 19.4 Further Pragmatics ........................291 19.5