There are many good Python tutorials online, so it is not my intention to repeat those. If you know nothing about Python, if you have never compiled a Python program, then you may need to start with the official Python Tutorials and go from there. Learn Python 3. Python 2.7 is legacy.
If you know a bit about Python or have used lots of other programming languages, then here are some notes to get you started with these tutorials. In particular we will need to know how modules and classes work with Python because those are not covered in the basic Tutorials.
Firstly you need to install Python 3.x. When it asks if you want to put Python in the path, say yes. Or add it to the path after installing.
Then install dependencies. From the command line run the following:
pip install numpy pip install matplotlib pip install pyaudio
If pip is not in the path then find your Python implementation and find pip in one of the sub directories. We will need more modules later, but that will get us started.
Do development in IDLE unless you already have an IDE you like. IDEL should be available from the Start menu on Windows after you have installed Python. This helps with the strict indentation that Python requires.
If you have not worked through Tutorial 1, then do that in parallel with this. This tutorial tells you how to organize, compile and run the Python code in Tutorial 1.
Unlike Java, Python stores its code in modules. Each file is called a module. As a result we end up with less physical files. In addition to grouping the code, the module creates a name space so that variables do not overlap.
Python also supports objects. We define them as classes, but unlike Java, we can put many in the same module. It is also worth discussing coding style, because most Python programs are not written in an object oriented way. This tutorial will be no exception, but we will need to use some classes. For example, when we create an oscillator in Tutorial 1, it works best if it can store the current phase. If we create two oscillators at different frequencies they each need to store their internal phase. This works best when defined as classes and then instantiated as objects. Purists might suggest we call a function called something like get_osc(), which returns an next_osc() function. We then call next_osc() for each value we need. This works quite well as you can see from the link, but it starts to get hard if we want to change the frequency after setup. Now we have to pass the frequency into the next_osc() function. Any additional complexity and this is hard to implement. So I will use classes in that type of situation.
If you work your way through Tutorial 1 and put the python code into two modules, as I did, then you should end up with:
tutorial1/dsp.py
tutorial1/test_osc.py
If we look at the start of the test_osc.py file we see the following:
import matplotlib.pyplot as plt
import dsp
import numpy as np
import math
def main():
...
This imports the python modules that we need. By convention we try to put all code in a function. So the main program goes in main(). We have one line of code that is not in a function, which just runs main. We do this because when a module is imported, any code that is not in a function is run. If we want that behavior, then good. But mostly we don't want that to happen.
As long as a module was installed with pip, if will be in the python path and will be found at run time.
There is no compile step for Python, you just run it. You can run it from the command line or from the Run menu in IDLE. If you use the run menu in IDLE it runs that module. This can be helpful to check for syntax errors in a module which does not have a main. To run the whole program you run the module with main() in it. This imports and calls the other modules as needed
Or from the command line you type something like this. Which is useful if you want to redirect the output to a file.
python osc_test.py
If you are on Unix of course you can make osc_test.py executable and put #!/usr/bin/python on the first line, or whatever the path is to python.
Copyright 2001-2021 Chris Thompson
Send me an email