You are previewing Making Music with Computers.
O'Reilly logo
Making Music with Computers

Book Description

Teach Your Students How to Use Computing to Explore Powerful and Creative Ideas

In the twenty-first century, computers have become indispensable in music making, distribution, performance, and consumption. Making Music with Computers: Creative Programming in Python introduces important concepts and skills necessary to generate music with computers. It interweaves computing pedagogy with musical concepts and creative activities, showing students how to integrate the creativity and design of the arts with the mathematical rigor and formality of computer science.

The book provides an introduction to creative software development in the Python programming language. It uses innovative music-creation activities to illustrate introductory computer programming concepts, including data types, algorithms, operators, iteration, lists, functions, and classes. The authors also cover GUIs, event-driven programming, big data, sonification, MIDI programming, client–server programming, recursion, fractals, and complex system dynamics.

Requiring minimal musical or programming experience, the text is designed for courses in introductory computer science and computing in the arts. It helps students learn computer programming in a creative context and understand how to build computer music applications. Also suitable for self-study, the book shows musicians and digital music enthusiasts how to write music software and create algorithmic music compositions.

Web Resource
A supplementary website (http://jythonMusic.org) provides a music library and other software resources used in the text. The music library is an extension of the jMusic library and incorporates other cross-platform programming tools. The website also offers example course and associated media resources.

Table of Contents

  1. Foreword
  2. Preface
    1. Creative Programming
    2. Target Audience
    3. Navigating the Book
    4. Pedagogy
    5. Software Libraries
  3. The Authors
  4. Acknowledgments
  5. Chapter 1 - Introduction and History
    1. 1.1 Overview
    2. 1.2 Connecting Music, Nature, and Number
      1. 1.2.1 Pythagoras—Harmonic Series
      2. 1.2.2 The Antikythera Mechanism—The First Known Computer
      3. 1.2.3 Johannes Kepler—Harmony of the World
      4. 1.2.4 Cymatics
      5. 1.2.5 Fractals
    3. 1.3 Computer Music History
      1. 1.3.1 Automated Music
      2. 1.3.2 Early Computer Music
      3. 1.3.3 Electronic Music
        1. 1.3.3.1 Reflection Questions
    4. 1.4 Algorithms and Programming
    5. 1.5 The Computer as a Musical Instrument
    6. 1.6 Software Used in This Book
      1. 1.6.1 Case Study: Running a Python Program
    7. 1.7 Summary
  6. Chapter 2 - Elements of Music and Code
    1. 2.1 Overview
    2. 2.2 Music Is Sound and …
    3. 2.3 Notes
      1. 2.3.1 Musical Notation
      2. 2.3.2 Pitch
        1. 2.3.2.1 Pitches Are Integers
      3. 2.3.3 Duration
        1. 2.3.3.1 Durations Are Real Numbers
      4. 2.3.4 Dynamic
      5. 2.3.5 Panning
      6. 2.3.6 Creating Notes
    4. 2.4 Rests
      1. 2.4.1 Creating Rests
      2. 2.4.2 Case Study: Playing a Note
        1. 2.4.2.1 Comments
      3. 2.4.3 Exercise
    5. 2.5 Variables and Assignment
      1. 2.5.1 Examples
      2. 2.5.2 Reserved Words
    6. 2.6 Numbers
      1. 2.6.1 Integers
      2. 2.6.2 Floats
      3. 2.6.3 Arithmetic Expressions
    7. 2.7 Input and Output
      1. 2.7.1 Input from the Keyboard
      2. 2.7.2 Output to the Screen
    8. 2.8 Data Types
      1. 2.8.1 The type() Function
      2. 2.8.2 Case Study: Finding the Octave of a Pitch
      3. 2.8.3 Testing Programs
      4. 2.8.4 Exercise
    9. 2.9 Summary
  7. Chapter 3 - Organization and Data
    1. 3.1 Overview
    2. 3.2 Musical Organization
      1. 3.2.1 Music Data Structure
    3. 3.3 Phrases
      1. 3.3.1 Creating Phrases
      2. 3.3.2 Adding Notes
    4. 3.4 Python Lists
      1. 3.4.1 List Concatenation
      2. 3.4.2 List Repetition
    5. 3.5 Adding Notes with Lists
    6. 3.6 Case Study: Ludwig van Beethoven—“Für Elise”
      1. 3.6.1 Exercise
    7. 3.7 Musical Scales
      1. 3.7.1 The Major Scale
      2. 3.7.2 The Minor Scale
      3. 3.7.3 Other Scales
      4. 3.7.4 Exercise
    8. 3.8 Musical Instruments
      1. 3.8.1 MIDI Instruments
    9. 3.9 Setting the Instrument
      1. 3.9.1 Exercise
        1. 3.9.1.1 Setting the Tempo
    10. 3.10 Case Study: Harold Faltermeyer—“Axel F”
      1. 3.10.1 Exercises
    11. 3.11 Chords
      1. 3.11.1 Adding Chords
      2. 3.11.2 Case Study: Bruce Hornsby—“The Way It Is”
      3. 3.11.3 Adding Chords with Lists
      4. 3.11.4 Case Study: 2Pac—“Changes”
    12. 3.12 Parts
      1. 3.12.1 Creating Parts
      2. 3.12.2 MIDI Channels
      3. 3.12.3 Adding Phrases
      4. 3.12.4 Creating Ensembles
    13. 3.13 Scores
      1. 3.13.1 Creating Scores
      2. 3.13.2 Putting It All Together
    14. 3.14 A Complete Example
      1. 3.14.1 Case Study: Joseph Kosma—“Autumn Leaves” (Jazz Trio)
      2. 3.14.2 Exercise
    15. 3.15 MIDI Drums and Percussive Sounds
      1. 3.15.1 Exercises
      2. 3.15.2 Case Study: Drum Machines
        1. 3.15.2.1 Drum Machine Pattern #1
        2. 3.15.2.2 Exercise
      3. 3.15.3 Case Study: Deep Purple—“Smoke on the Water”
    16. 3.16 Top-Down Design
    17. 3.17 Input and Output
      1. 3.17.1 Reading MIDI Files
      2. 3.17.2 Writing MIDI Files
      3. 3.17.3 Exercises
    18. 3.18 Summary
  8. Chapter 4 - Transformation and Process
    1. 4.1 Overview
    2. 4.2 Gestures, Emotion, and Musical Structure
      1. 4.2.1 Musical Patterns
    3. 4.3 Minimalism
      1. 4.3.1 Repetition and Phasing
      2. 4.3.2 Case Study: Steve Reich, “Piano Phase” (1967)
    4. 4.4 Modifying Musical Material (Mod Functions)
      1. 4.4.1 Modifying Volume
      2. 4.4.2 Modifying Duration
      3. 4.4.3 Modifying Pitch
      4. 4.4.4 Modifying with Randomness
    5. 4.5 Musical Canon
      1. 4.5.1 Case Study: Traditional “Row Your Boat”
        1. 4.5.1.1 Exercise
      2. 4.5.2 Analyzing the Musical Process
        1. 4.5.2.1 Creating Musical Material
        2. 4.5.2.2 Making Copies of Musical Material
        3. 4.5.2.3 Shifting Musical Material in Time
        4. 4.5.2.4 Transposing Musical Material
        5. 4.5.2.5 Combining Music Material
        6. 4.5.2.6 Saving and Playing Musical Material
      3. 4.5.3 Case Study: J.S. Bach—Goldberg Ground, Canon 1 (BWV 1087)
      4. 4.5.4 Case Study: Trias Harmonica canon (BWV 1072)
      5. 4.5.5 Exercises
      6. 4.5.6 Case Study: Arvo Pärt—“Cantus in Memoriam” (1977)
      7. 4.5.7 Exercises
    6. 4.6 Viewing Music
      1. 4.6.1 Notation Display
      2. 4.6.2 Piano Roll Display
      3. 4.6.3 Internal Values Display
      4. 4.6.4 Sketch Display
      5. 4.6.5 Exercises
    7. 4.7 The Software Development Process
      1. 4.7.1 Design
      2. 4.7.2 Implementation
      3. 4.7.3 Testing
      4. 4.7.4 Documentation—Good Style and Comments
    8. 4.8 Case Study: Computer-Aided Music Composition
      1. 4.8.1 Exercise
    9. 4.9 Summary
  9. Chapter 5 - Iteration and Lists
    1. 5.1 Overview
    2. 5.2 Iteration
      1. 5.2.1 The Python for Loop
      2. 5.2.2 Exercises
    3. 5.3 Case Study: Arpeggiators
      1. 5.3.1 Arpeggiator #1—Using Absolute Pitches
      2. 5.3.2 Constants
        1. 5.3.2.1 Exercise
      3. 5.3.3 Interactive Processes
      4. 5.3.4 Arpeggiator #2—Using Relative Pitches
        1. 5.3.4.1 Exercises
    4. 5.4 Python List Operations
      1. 5.4.1 Accessing List Items
      2. 5.4.2 Modifying List Items
      3. 5.4.3 List Functions
      4. 5.4.4 Case Study: Scale Tutor
      5. 5.4.5 Case Study: Interactive PianoRoll Generator
        1. 5.4.5.1 Exercises
      6. 5.4.6 The range() Function
        1. 5.4.6.1 Exercises
      7. 5.4.7 The frange() Function
      8. 5.4.8 Iterating with Lists
    5. 5.5 Iterative Musical Processes
      1. 5.5.1 Case Study: Mod Retrograde
      2. 5.5.2 Exercises
      3. 5.5.3 Case Study: Guitar Effect, FX-35 Octoplus
      4. 5.5.4 Exercises
    6. 5.6 DNA Music
      1. 5.6.1 Case Study: Protein Music—Human Thymidylate Synthase A
        1. 5.6.1.1 Exercises
    7. 5.7 Summary
  10. Chapter 6 - Randomness and Choices
    1. 6.1 Overview
    2. 6.2 Randomness and Creativity
      1. 6.2.1 Case Study: Mozart—“Musikalisches Würfelspiel”
        1. 6.2.1.1 Exercise
    3. 6.3 Indeterminism and Serialism
      1. 6.3.1 Case Study: Pierre Cage—“Structures pour deux Chances”
        1. 6.3.1.1 Exercises
    4. 6.4 Python Random Functions
      1. 6.4.1 Exercise
      2. 6.4.2 randint()
      3. 6.4.3 choice()
    5. 6.5 Stochastic Music
      1. 6.5.1 Case Study: Iannis Xenakis—“Concret PH”
    6. 6.6 Harnessing (or Sieving) Randomness
      1. 6.6.1 Case Study: Wind Chimes
        1. 6.6.1.1 Exercises
      2. 6.6.2 Case Study: Pentatonic Melody Generator
      3. 6.6.3 Weighted Probabilities
    7. 6.7 Selection
      1. 6.7.1 Python if Statement
        1. 6.7.1.1 Many Cases
      2. 6.7.2 Case Study: Flipping a Coin
      3. 6.7.3 Case Study: Russian Roulette
        1. 6.7.3.1 Exercise
      4. 6.7.4 Case Study: Throwing Dice
        1. 6.7.4.1 Nesting “If” Statements
        2. 6.7.4.2 Exercise
      5. 6.7.5 Case Study: Let the Drums Come Alive
        1. 6.7.5.1 Exercises
    8. 6.8 Python Relational Operators
    9. 6.9 Python Boolean Values
    10. 6.10 Python Logical Operators
      1. 6.10.1 Case Study: Music from Weighted Probabilities
        1. 6.10.1.1 Exercises
    11. 6.11 Summary
  11. Chapter 7 - Sonification and Big Data
    1. 7.1 Overview
    2. 7.2 Data Sonification
      1. 7.2.1 The mapValue() Function
      2. 7.2.2 The mapScale() Function
    3. 7.3 Case Study: Kepler—“Harmonies of the World” (1619)
      1. 7.3.1 Exercise
    4. 7.4 Python Strings
      1. 7.4.1 Case Study: Music from Text
        1. 7.4.1.1 Exercise
      2. 7.4.2 String Library Functions
      3. 7.4.3 Case Study: Guido d’Arezzo—“Word Music” (ca. 1000)
      4. 7.4.4 Python Nested Loops
      5. 7.4.5 Exercise
    5. 7.5 File Input and Output
      1. 7.5.1 Reading Files
      2. 7.5.2 Writing Files
      3. 7.5.3 Exercises
    6. 7.6 Python while Loop
      1. 7.6.1 Exercise
    7. 7.7 Big Data
      1. 7.7.1 Case Study: Biosignal Sonification
        1. 7.7.1.1 Sonification Design
        2. 7.7.1.2 Python Parallel Assignment
      2. 7.7.2 Exercises
    8. 7.8 Python Functions
      1. 7.8.1 Defining Functions
      2. 7.8.2 Exercise
      3. 7.8.3 Returning Values
      4. 7.8.4 Exercises
      5. 7.8.5 Scope of Variables
    9. 7.9 Image Sonification
      1. 7.9.1 Python Images
      2. 7.9.2 Image Library Functions
      3. 7.9.3 Case Study: Visual Soundscape
        1. 7.9.3.1 Sonification Design
        2. 7.9.3.2 Defining a Function
      4. 7.9.4 Python Nested Loops (again)
      5. 7.9.5 Exercises
    10. 7.10 Summary
  12. Chapter 8 - Interactive Musical Instruments
    1. 8.1 Overview
    2. 8.2 Building Musical Instruments
    3. 8.3 Graphical User Interfaces
      1. 8.3.1 Creating Displays
      2. 8.3.2 Graphics Objects
        1. 8.3.2.1 Exercise
      3. 8.3.3 Showing Display Coordinates
    4. 8.4 Case Study: Random Circles
      1. 8.4.1 Exercises
    5. 8.5 GUI Widgets
      1. 8.5.1 Event-Driven Programming
      2. 8.5.2 Callback Functions
    6. 8.6 Case Study: A Simple Musical Instrument
      1. 8.6.1 Python Global Statement
      2. 8.6.2 Exercise
    7. 8.7 Play Class
    8. 8.8 Case Study: An Audio Instrument for Continuous Pitch control
    9. 8.9 AudioSample Class
      1. 8.9.1 Creating Audio Samples
      2. 8.9.2 Exercise
    10. 8.10 MidiSequence Class
      1. 8.10.1 Creating MIDI Sequences
      2. 8.10.2 Exercises
    11. 8.11 Paper Prototyping
    12. 8.12 A Simple Methodology for Developing GUIs
      1. 8.12.1 Listen, Listen, Listen
    13. 8.13 Event Handling
      1. 8.13.1 Keyboard Events
      2. 8.13.2 Mouse Events
        1. 8.13.2.1 Example
        2. 8.13.2.2 Exercises
      3. 8.13.3 Case Study: Drawing Musical Circles
        1. 8.13.3.1 Defining Callback Functions
        2. 8.13.3.2 Exercises
    14. 8.14 Case Study: A Virtual Piano
      1. 8.14.1 Exercise
      2. 8.14.2 A Variation, Using Parallel Lists
        1. 8.14.2.1 Exercises
    15. 8.15 Scheduling Future Events
      1. 8.15.1 Case Study: Random Circles with Timer
      2. 8.15.2 The Timer Class
        1. 8.15.2.1 Creating Timers
    16. 8.16 Summary
  13. Chapter 9 - Making Connections
    1. 9.1 Overview
    2. 9.2 MIDI Devices—Connecting to Pianos, Guitars, etc.
      1. 9.2.1 Case Study: Make Music with a MIDI Instrument
        1. 9.2.1.1 Exercise
      2. 9.2.2 The MIDI Library
        1. 9.2.2.1 The MidiIn Class
        2. 9.2.2.2 The MidiOut Class
    3. 9.3 OSC Devices—Connecting to Smartphones, Tablets, etc.
      1. 9.3.1 OSC Messages
      2. 9.3.2 Case Study: Hello (OSC) World!
        1. 9.3.2.1 Program for OSC Server Device
        2. 9.3.2.2 Program for OSC Client Device
        3. 9.3.2.3 Exercises
      3. 9.3.3 The OSC Library
        1. 9.3.3.1 The OscIn Class
        2. 9.3.3.2 The OscOut Class
      4. 9.3.4 Case Study: Make Music with your Smartphone
        1. 9.3.4.1 Performance Instructions
        2. 9.3.4.2 Setting up Your Smartphone (OSC Client)
        3. 9.3.4.3 Setting up Your Computer (OSC Server)
        4. 9.3.4.4 Exercises
      5. 9.3.5 Hybrid Musical Instrument Projects
    4. 9.4 Summary
  14. Chapter 10 - Music, Number, and Nature
    1. 10.1 Overview
    2. 10.2 Origins and Representations
      1. 10.2.1 Pythagorean Theorem
      2. 10.2.2 Python as a Representation
    3. 10.3 Case Study: Music from Math Curves
      1. 10.3.1 Hearing the Music
      2. 10.3.2 Exercises
    4. 10.4 Math Library
    5. 10.5 Case Study: The Harmonograph
      1. 10.5.1 Lateral Harmonograph
      2. 10.5.2 Rotary Harmonograph
      3. 10.5.3 Exercises
      4. 10.5.4 Noninteger Ratios
    6. 10.6 Case Study: Kepler’s Harmony of the World, No. 2
      1. 10.6.1 Exercises
    7. 10.7 Summary
  15. Chapter 11 - Exploring Powerful Ideas
    1. 11.1 Overview
    2. 11.2 Fractals and Recursion
    3. 11.3 Fibonacci Numbers and the Golden Ratio
      1. 11.3.1 Case Study: The Golden Tree
        1. 11.3.1.1 Exercises
    4. 11.4 Zipf’s Law
      1. 11.4.1 Zipf’s Law and Music
      2. 11.4.2 What Does It Mean?
      3. 11.4.3 Measuring Zipf Proportions
        1. 11.4.3.1 Top-Down Design (Revisited)
      4. 11.4.4 Python Dictionaries
      5. 11.4.5 Exercises
    5. 11.5 Python Classes
    6. 11.6 Case Study: The Note Class
      1. 11.6.1 Creating Note Objects
      2. 11.6.2 Defining the Class
        1. 11.6.2.1 Checking for Data Integrity
      3. 11.6.3 Python Exceptions
      4. 11.6.4 Exercises
    7. 11.7 Case Study: A Slider Control
      1. 11.7.1 Creating SliderControl Objects
      2. 11.7.2 Defining the Class
      3. 11.7.3 Exercises
    8. 11.8 Animation
      1. 11.8.1 Frame Rate
      2. 11.8.2 Case Study: A Revolving Musical Sphere
        1. 11.8.2.1 Color Gradients
      3. 11.8.3 Defining the Class
        1. 11.8.3.1 Spherical Coordinate System
      4. 11.8.4 Exercises
    9. 11.9 Cymatics
      1. 11.9.1 Vectors and Python Complex Numbers
      2. 11.9.2 Defining the Boid Universe
      3. 11.9.3 Defining the Boids
        1. 11.9.3.1 Boid Sensing
        2. 11.9.3.2 Boid Acting
      4. 11.9.4 Creating the Simulation
    10. 11.10 Exercises
    11. 11.11 Summary
  16. References
  17. Appendix A: MIDI Constants
    1. A.1 Overview
    2. A.2 Pitch Constants
    3. A.3 Rhythm Value Constants
    4. A.4 Dynamic Constants
    5. A.5 Panning Constants
    6. A.6 General MIDI Instrument Constants
      1. A.6.1 Piano Family
      2. A.6.2 Pitched Percussion Family
      3. A.6.3 Organ Family
      4. A.6.4 Guitar Family
      5. A.6.5 Bass Family
      6. A.6.6 Strings and Timpani Family
      7. A.6.7 Ensemble Family
      8. A.6.8 Brass Family
      9. A.6.9 Reed Family
      10. A.6.10 Pipe Family
      11. A.6.11 Synth Lead Family
      12. A.6.12 Synth Pad Family
      13. A.6.13 Synth Effects Family
      14. A.6.14 Ethnic Family
      15. A.6.15 Percussive Family
      16. A.6.16 Sound Effects Family
    7. A.7 General MIDI Drum and Percussion Constants
    8. A.8 Scale and Mode Constants
  18. Appendix B: Music Library Functions
    1. B.1 Overview
    2. B.2 Note Functions
    3. B.3 Phrase Functions
    4. B.4 Part Functions
    5. B.5 Score Functions
    6. B.6 View Functions
    7. B.7 Mod Functions
  19. Appendix C: GUI Library Functions
    1. C.1 Overview
    2. C.2 GUI Display
      1. C.2.1 Drawing on Display
    3. C.3 GUI Widgets
      1. C.3.1 Label
      2. C.3.2 Button
      3. C.3.3 Checkbox
      4. C.3.4 Slider
      5. C.3.5 DropDownList
      6. C.3.6 TextField
      7. C.3.7 TextArea
      8. C.3.8 Icon
      9. C.3.9 Menu
        1. C.3.9.1 Drop-down Menus
        2. C.3.9.2 Pop-Up Menus
    4. C.4 Graphics Objects
      1. C.4.1 Line
      2. C.4.2 Circle
      3. C.4.3 Point
      4. C.4.4 Oval
      5. C.4.5 Rectangle
    5. C.5 Additional Color Functions of Graphics Objects
    6. C.6 Event Functions
      1. C.6.1 Keyboard Events
        1. C.6.1.1 Key Typed Events
        2. C.6.1.2 Key Down/Up Events
      2. C.6.2 Mouse Events
      3. C.6.3 Display Events
    7. C.7 Scheduling Tasks – The Timer Class
      1. C.7.1 Creating Timers
  20. Appendix D: Other Functions
    1. D.1 Map Functions
    2. D.2 Image Functions
    3. D.3 Play Functions
    4. D.4 AudioSample Functions
    5. D.5 MidiSequence Functions
    6. D.6 Timer Functions
    7. D.7 MIDI Input Output, and Functions
      1. D.7.1 The MidiIn Class
      2. D.7.2 The MidiOut Class
    8. D.8 Open Sound Control (OSC) Functions
      1. D.8.1 The OscIn Class
      2. D.8.2 The OscOut Class
    9. D.9 Zipf Library
    10. D.10 ColorGradient Function