Sunday, April 20, 2014

5. Writing a Wav File - 2

Here numpy and scipy modules will be used to create an audio wave file.




First numpy is imported as np, and scipy.io.wavfile as wavfile. Then an integer variable N is created and initialized to 168. Finally the numpy function, np.arange, is used to create an input array called x, which is an array from 0 to 167. This function is very similar to the standard Python function range.




Next the output array y is determined. It is the sum of three sine functions at 3 different frequencies. These functions are array functions as they use x. If an input is an array, numpy knows the output will be array of the same size. These are called element-wise operations.




Like before, the y is repeated 1313 times to make a 5-second sound. This is done with a np.tile function. There is a np.repeat function but that merely stretches the array. Next we have to normalize the array, because in the write function that we will use, there is clipping if the signal goes above +1 or below -1.




Finally the output, which is stored in the array y, is written out.




This shows the entire program without comments. This is not recommended. However sometimes it is easier to understand program structure without looking at comments, especially with Python.


# WriteWavWithNumpy.py

import numpy as np
import scipy.io.wavfile as wavfile
N = 168
x = np.arange(N)
y = 4/np.pi*np.sin(2*np.pi*x/N)
y += 4/(3*np.pi)*np.sin(6*np.pi*x/N)
y += 4/(5*np.pi)*np.sin(10*np.pi*x/N)
y = np.tile(y,1313)
y = y/max(y)
wavfile.write("SquareWave.wav",44100,y)



This is the wave file opened in audio program Audacity.




We will not use Audacity for creating music. Even though it has some basic generators in the Generate menu and allows writing of plug-ins in a language known as Nyquist, there is a more established program csound, with many more ways of generating sounds as well as creating effects. It is well documented and is efficient. It includes support for Python with several modules. Furthermore it is possible to include Python code inside csound files.




csound is written in the C language. However Python is often used as a glue to piece different components. One of those pieces involve writing text ASCII files, in terms of the instruments, tables and scores. We will later see what those terms mean for csound. Once the file is written, csound is usually run from the command prompt and has a lot of flags. Python has many ways of automating processing in the command prompt.




You may go to pythonaudio.blogspot.com to see the slides. To see a larger image of the slide, you can click on them at that page, which provides easy navigation controls. The text for the audio of the slides as well as any relevant source code is also on that page.




This is the video of Tutorial 5:



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.