The Python Audio Cookbook offers an introduction to Python for sound and multimedia applications, with chapters that cover writing your first Python programs, controlling Pyo with physical computing, and writing your own GUI, among many other topics.
Guiding the reader through a variety of audio synthesis techniques, the book empowers readers to combine their projects with popular platforms, from the Arduino to Twitter, and state-of-the-art practices such as AI. The Python Audio Cookbook balances accessible explanations for theoretical concepts, including Python syntax, audio processing and machine learning, with practical applications.
This book is an essential introductory guide to Python for sound and multimedia practitioners, as well as programmers interested in audio applications.
Author(s): Alexandros Drymonitis
Publisher: Focal Press
Year: 2023
Language: English
Pages: 318
Cover
Half Title
Title
Copyright
Dedication
Contents
Acknowledgements
Glossary
1 Getting Started
1.1 Installing Python
1.2 Installing Python’s Package Manager
1.2.1 Searching Modules with Pip
1.2.2 Uninstalling Modules with Pip
1.3 Installing an Integrated Development Environment
1.4 Installing Pyo
1.4.1 Installing Pyo with Pip
1.4.2 Compiling Pyo from Sources
1.5 Coding Conventions
1.6 Conclusion
Bibliography
2 Writing Your First Python Programs
2.1 Hello World!
2.2 Comments in Python
2.3 Data Types in Python
2.3.1 Numeric Data Types
2.3.2 Boolean Data Type
2.3.3 Sequence Data Types
2.3.4 The Dictionary Data Type
2.3.5 The Set Data Type
2.3.6 Reserved Data Type Keywords
2.3.7 NoneType
2.3.8 Data Types Are Classes
2.4 Conditional Tests
2.5 Functions in Python
2.5.1 Positional and Keyword Arguments
2.5.2 Recursive Functions
2.6 Loops in Python
2.7 Scope
2.8 Traceback
2.8.1 Exceptions
2.9 Importing Modules and Scripts Inside Other Scripts
2.9.1 Importing All Methods and Classes of a Module
2.9.2 if __name__ == “__main__”
2.10 Hello Sound!
2.10.1 Choosing an Audio Backend
2.11 Hello GUI!
2.12 Combining Interpreter Input with Predefined Scripts
2.13 Conclusion
2.14 Exercises
2.14.1 Exercise 1
2.14.2 Exercise 2
2.14.3 Exercise 3
3 Diving into Audio Synthesis Techniques
3.1 Oscillators and Waveforms
3.2 Audio Synthesis
3.2.1 Ring Modulation
3.2.2 Amplitude Modulation
3.2.3 Frequency Modulation
3.2.4 Additive Synthesis
3.2.5 Granular Synthesis
3.3 Effects
3.3.1 Subtractive Synthesis and Filtering
3.3.2 Delay
3.3.3 Reverb
3.4 Control
3.4.1 Applying Envelopes
3.4.2 Sequencing
3.4.3 MIDI
3.4.4 Routing and Panning
3.5 Sampling Rate, Bit Depth, Buffer Size, Latency, and Audio Range
3.5.1 Sampling Rate and Bit Depth
3.5.2 Buffer Size and Latency
3.5.3 Audio Range
3.6 FFT
3.7 Conclusion
3.8 Exercises
3.8.1 Exercise 1
3.8.2 Exercise 2
3.8.3 Exercise 3
3.8.4 Exercise 4
3.8.5 Exercise 5
3.8.6 Exercise 6
3.8.7 Exercise 7
3.8.8 Exercise 8
Bibliography
4 Phase Vocoder Techniques
4.1 What is the Phase Vocoder?
4.2 Cross Synthesis
4.3 Spectral Morphing
4.4 PVOC Additive Synthesis
4.5 Spectral Filtering with PVOC
4.6 Spectral Reverberation
4.7 Conclusion
4.8 Exercises
4.8.1 Exercise 1
4.8.2 Exercise 2
4.8.3 Exercise 3
Bibliography
5 Controlling Pyo with Physical Computing
5.1 What is Physical Computing
5.1.1 What is the Arduino
5.2 How Can We Connect the Arduino to Python
5.2.1 Testing Our Arduino Board
5.2.2 Connecting the Arduino to Python
5.3 Using Proximity Sensors to Control Amplitude and Frequency
5.3.1 Using One Proximity Sensor to Control the Amplitude of an Oscillator
5.3.2 Using Two Proximity Sensors to Control the Amplitude and the Frequency of an Oscillator
5.4 Controlling LEDs with Oscillators or by Detecting Attacks
5.4.1 Blinking LEDs by Detecting Attacks
5.4.2 Fading an LED In and Out with an Oscillator
5.5 Creating a Granular Synthesizer with Arduino and Python
5.5.1 Using pySerial
5.5.2 Using pyFirmata
5.6 Pros and Cons of pySerial and pyFirmata
5.7 Conclusion
5.8 Exercises
5.8.1 Exercise 1
5.8.2 Exercise 2
5.8.3 Exercise 3
5.8.4 Exercise 4
Bibliography
6 Communicating with Other Software with OSC
6.1 What is OSC?
6.2 Available Python Modules for Sending and Receiving OSC Messages
6.3 Receiving and Sending OSC Messages between Your Smartphone and Pyo
6.3.1 Receiving OSC Messages from Your Smartphone in Pyo
6.3.2 Sending OSC Messages from Pyo to Your Smartphone
6.4 Sending and Receiving Messages between Pyo and an openFrameworks Program
6.5 Conclusion
6.6 Exercises
6.6.1 Exercise 1
6.6.2 Exercise 2
6.6.3 Exercise 3
6.6.4 Exercise 4
Bibliography
7 Machine Learning in Music
7.1 What are Machine Learning and Neural Networks
7.1.1 Applications of ML
7.1.2 How NNs work
7.2 Available Python Modules for ML
7.3 A Regression Neural Network
7.4 A Classification Neural Network
7.5 A Few Notes on Neural Networks and Training Datasets
7.6 Conclusion
7.7 Exercises
7.7.1 Exercise 1
7.7.2 Exercise 2
7.7.3 Exercise 3
Bibliography
8 Writing Your Own Classes in Python
8.1 What are Python Classes and How to Write One
8.1.1 Inheritance in Classes
8.2 Writing a Square Wave Oscillator Class with Duty Cycle Control
8.3 Writing a Triangle Wave Oscillator Class with Breakpoint Control
8.4 Conclusion
8.5 Exercises
8.5.1 Exercise 1
8.5.2 Exercise 2
9 Switched on music21
9.1 What is music21?
9.2 Applying Schoenberg’s Twelve-Tone System to Electronic Music
9.2.1 Displaying the Score
9.3 Playing Bach with Oscillators
9.4 Conclusion
9.5 Exercises
9.5.1 Exercise 1
9.5.2 Exercise 2
9.5.3 Exercise 3
Bibliography
10 The Events Framework
10.1 The Events() and EventSeq() Classes
10.1.1 Setting the Number of Repetitions
10.1.2 Setting an EventSeq() Object for any Events() Kwarg
10.1.3 Controlling the Envelope of an Events() Object
10.1.4 Other Sequencing Classes
10.2 Using Our Own Audio Classes
10.3 Calling Functions
10.4 Nesting Events Generators
10.5 Sharing Values between Generators
10.6 Using Other PyoObjects as Input to Generators
10.7 Accessing the Audio Output of Events() in Other PyoObjects
10.8 Revisiting Bach
10.9 Conclusion
10.10 Exercises
10.10.1 Exercise 1
10.10.2 Exercise 2
10.10.3 Exercise 3
10.10.4 Exercise 4
10.10.5 Exercise 5
11 The MML and Prefix Expression
11.1 What is the MML
11.2 The MML Language
11.2.1 Notes and Durations
11.2.2 Rests, Loops, Comments, and Variables
11.2.3 Tuplets and Randomness
11.3 What is the Prefix Expression
11.4 The Prefix Expression Language
11.4.1 Arithmetic Operators
11.4.2 Moving Phase Operators
11.4.3 Conditional Operators
11.4.4 Trigonometric Functions
11.4.5 Power and Logarithmic Functions
11.4.6 Clipping Functions
11.4.7 Random Functions
11.4.8 Complex Numbers
11.4.9 Filter Functions
11.4.10 Multiple Outputs
11.4.11 Constants
11.4.12 Accessing Input and Output Samples
11.4.13 Defining Functions Within the Prefix Language
11.4.14 State Variables
11.4.15 User Variables
11.4.16 Loading External Files
11.5 A few more Examples with Prefix Expressions
11.6 Conclusion
11.7 Exercises
11.7.1 Exercise 1
11.7.2 Exercise 2
11.7.3 Exercise 3
Bibliography
12 Writing Your Own GUI
12.1 Available Python Modules for GUI Widgets
12.2 Using Python’s Native Tkinter
12.3 Using PyQt
12.4 Conclusion
13 Using Various Python APIs in Music Scripts
13.1 What Are Python APIs?
13.2 Using Google Maps API to Retrieve Geolocation Information as Musical Input
13.2.1 Contextualising Our Project
13.2.2 Writing the Code
13.3 Mining Tweets from Twitter and Applying Sentiment Analysis to Control Sound
13.3.1 Contextualising Our Project
13.3.2 Writing the Code
13.4 Conclusion
13.5 Exercises
13.5.1 Exercise 1
13.5.2 Exercise 2
13.5.3 Exercise 3
13.5.4 Exercise 4
13.5.5 Exercise 5
Conclusion
Index