Thursday, April 3, 2014

2. Creating Audio with PySynth

In this tutorial we will go over one open source project for creating audio in Python. The music generated is very simple however it illustrates some principles. It creates files for a few simple musical scores.




This is the PySyth homepage. It describes the project and you may download it on this page, if you wish, in a compressed format. However there is another method.




If we have to go this web address: github.com/mdoege/PySynth, we can see a list of files. Click on pysynth.py.




It will show the Python code. In the upper right you can see the word Raw. Then you may right click on Raw.




After right clicking on Raw, we get the menu and we choose Save link as... and then we can save the file to our computer.




This is the folder I saved to (IPythonCode) with the default name (pysynth.py).




In the Anaconda Command Prompt, I type cd c:\IPy and press tab, it will autocomplete (as I only have one folder that begins with those three letters).




Then after typing in the Anaconda Command Prompt the command to start ipython (which is ipython qtconsole), I create a bookmark. A bookmark is basically an alias to the current directory, which happens to be C:\IPythonCode. The bookmarks name, for the example, is pyCode.




I quit the ipython shell and start a new one. It starts at the directory where Anaconda is located. Then I change directory, that is cd to pyCode. I verify with pwd command.




In future sessions, you will only have to type cd and then press either the up arrow or down arrow to see history of commands that start with those two letters. Since most probably you will have few such commands, it will find the correct one quickly.




Since I am in the directory with the pySynth.py file, I can execute it using the %run command. I show the first few lines of output.




This shows the music files which were created.




I will go into the Python code of this program later. This program is 285 lines. The actual code is less because there is a lot of empty lines, comments, and print lines. The print lines are only useful in showing what is happening.




I use the audio program Audacity. Here I will select a portion of the audio with happens to C5 note.




The reason that I know that this is C5 is because the notes in this song are the scale: C4, D4, E4, F4, G4, A4, B4 and C5. All notes are quarter notes except for the last (which is a half-note). You can see from the time signal it is twice the size of a previous note, and there is a whole-rest after C5.




For the selected audio, this is the frequency spectrum. This shows that it is a sum of three sine waves. The waves happen to be at these frequencies: C5 (253 Hz), C6 (2*253 Hz) and C7 (4*253 Hz).




This program used additive synthesis as one note is composed of a few waveforms. Real audio signals will be composed of all frequencies.




Since real audio is composed of many frequencies, sometimes it is better to start with a signal that already has many harmonics such as the square wave. Then we subtract some frequencies using filtering.




Also we can use sampled data. Actually this happens to be the most common way as it leads to the most realistic sounds.




Some math is necessary to understand how waves are composed from simple sine waves.




First I will go over the Wave file format and how to write it using Python. After describing the python program, pysynth.py, I will start some math for audio processing.





This is the video of Tutorial 2:


1 comment:

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.