Making Music
with Computers
Creative Programming in Python
Bill Manaris
Andrew R. Brown
Making Music
with Computers
Creative Programming in Python
CHAPMAN & HALL/CRC
TEXTBOOKS IN COMPUTING
Series Editors
John Impagliazzo
Professor Emeritus, Hofstra University
Andrew McGettrick
Department of Computer
and Information Sciences
University of Strathclyde
Aims and Scope
This series covers traditional areas of computing, as well as related technical areas, such as
software engineering, artificial intelligence, computer engineering, information systems, and
information technology. The series will accommodate textbooks for undergraduate and gradu-
ate students, generally adhering to worldwide curriculum standards from professional societ-
ies. The editors wish to encourage new and imaginative ideas and proposals, and are keen to
help and encourage new authors. The editors welcome proposals that: provide groundbreaking
and imaginative perspectives on aspects of computing; present topics in a new and exciting
context; open up opportunities for emerging areas, such as multi-media, security, and mobile
systems; capture new developments and applications in emerging fields of computing; and
address topics that provide support for computing, such as mathematics, statistics, life and
physical sciences, and business.
Published Titles
Paul Anderson, Web 2.0 and Beyond: Principles and Technologies
Henrik Bærbak Christensen, Flexible, Reliable Software: Using Patterns and Agile
Development
John S. Conery, Explorations in Computing: An Introduction to Computer Science
Ted Herman, A Functional Start to Computing with Python
Pascal Hitzler, Markus Krötzsch, and Sebastian Rudolph, Foundations of Semantic Web
Technologies
Mark J. Johnson, A Concise Introduction to Data Structures using Java Uvais Qidwai and
C.H. Chen, Digital Image Processing: An Algorithmic Approach with MATLAB®
Mark J. Johnson, A Concise Introduction to Programming in Python
Lisa C. Kaczmarczyk, Computers and Society: Computing for Good
Mark C. Lewis, Introduction to the Art of Programming Using Scala
Bill Manaris and Andrew R. Brown, Making Music with Computers: Creative Programming
in Python
Henry M. Walker, The Tao of Computing, Second Edition
Chapman & Hall/CRC
TEXTBOOKS IN COMPUTING
Making Music
with Computers
Creative Programming in Python
Bill Manaris
College of Charleston
South Carolina, USA
Andrew R. Brown
Queensland University of Technology
Keperra, Australia
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2014 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20140402
International Standard Book Number-13: 978-1-4822-2221-0 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts
have been made to publish reliable data and information, but the author and publisher cannot assume
responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to
copyright holders if permission to publish in this form has not been obtained. If any copyright material has
not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmit-
ted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented,
including photocopying, microfilming, and recording, or in any information storage or retrieval system,
without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.
com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and
registration for a variety of users. For organizations that have been granted a photocopy license by the CCC,
a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used
only for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Contents
Foreword, xix
Preface, xxi
The Authors, xxvii
Acknowledgments, xxix
Chapter 1 ◾ Introduction and History
1.1 OVERVIEW
1.2 CONNECTING MUSIC, NATURE, AND NUMBER
1.2.1 Pythagoras—Harmonic Series
1.2.2 The Antikythera Mechanism—The First Known
Computer
Johannes Kepler—Harmony of the World
1.2.3
1.2.4 Cymatics
1.2.5 Fractals
1.3 COMPUTER MUSIC HISTORY
1.3.1 Automated Music
1.3.2 Early Computer Music
1.3.3 Electronic Music
1.3.3.1 Reflection Questions
1.4 ALGORITHMS AND PROGRAMMING
1.5 THE COMPUTER AS A MUSICAL INSTRUMENT
1.6 SOFTWARE USED IN THIS BOOK
1.6.1 Case Study: Running a Python Program
1.7 SUMMARY
1
1
1
2
4
4
6
7
9
10
11
12
17
17
19
21
22
23
v
vi ◾ Contents
Chapter 2 ◾ Elements of Music and Code
2.1 OVERVIEW
2.2 MUSIC IS SOUND AND …
2.3 NOTES
2.3.1 Musical Notation
2.3.2 Pitch
2.3.2.1 Pitches Are Integers
2.3.3 Duration
2.3.3.1 Durations Are Real Numbers
2.3.4 Dynamic
2.3.5 Panning
2.3.6 Creating Notes
2.4 RESTS
2.4.1 Creating Rests
2.4.2 Case Study: Playing a Note
2.4.2.1 Comments
2.4.3 Exercise
2.5 VARIABLES AND ASSIGNMENT
2.5.1 Examples
2.5.2 Reserved Words
2.6 NUMBERS
Integers
2.6.1
2.6.2 Floats
2.6.3 Arithmetic Expressions
INPUT AND OUTPUT
2.7.1
2.7.2 Output to the Screen
Input from the Keyboard
2.7
2.8 DATA TYPES
2.8.1 The type() Function
2.8.2 Case Study: Finding the Octave of a Pitch
2.8.3 Testing Programs
2.8.4 Exercise
2.9 SUMMARY
25
25
25
26
27
28
28
29
31
31
31
32
33
34
34
35
36
36
37
38
39
39
40
40
42
42
43
44
44
45
46
46
47