# Trogramming

54726f6772616d6d696e672e636f6d

# Creating and Exporting Audio in Python

The following will demonstrate the basics of creating simple sounds in Python and using the wave and library to export it. It serves as a simple Python sine wave audio export tutorial (my SEO focus keyphrase)

Making the sound is as simple as creating an array of oscillations, exporting the sound is a touch more complex but still trivially easy.

The first example will be in vanilla python without the use of numpy so make it slightly more clear. Where speed is prioritized I would advise using numpy, however this initial example will be much easier to port into other imperative languages.

The first step is to import the required libraries

For the demo we will be creating a pure sine wave at 440Hz which is known as “Middle A” in the most common standards.
Additionally we must assign a framerate, the number of channels, and the sample width.

Frame Rate: this number represents the number of samples which occur each second. There are a number of commonly used values but 44,100 Hz (alternately represented as 44.1 kHz) remains one of the most common as it was the standard for CD players since the late 70s.
Channels: This represents how many different audio streams will be playing concurrently, this can be as high as 65535 channels! However the most common is 1 channel for mono, 2 channels for stereo, 6 channels for 5.1 surround sound, or 8 for 7.1 surround sound.

The next step is to simply generate the sine wave. This could easily be done with a list comprehension mapping a range to a sine function like

However a major downside to doing this is that you can’t change the frequency and it rules out basically all frequency modulation, so while it would be fine for this demo it’s a bad habit to get into.
A much better way is to keep track of the current position of the oscillation and within a loop increment it proportionally to the current frequency (which in this example is constant)

Now that we have our sound all that’s left is to simply export it.
I will cover the creation of .wav files in more depth in future, however python has it covered for us with the wave library we imported in the beginning.

More about the wave library can be found here: https://docs.python.org/3/library/wave.html
And the struct library here: https://docs.python.org/3/library/struct.html

The final step is simply to take out sound and use it as the first argument of SaveSound with our desired arguments as defined earlier.

## Complete Code

And that’s everything for now.
If you need to visualise it, just add

To the imports at the beginning and then add

at the end before or after SaveSound depending on if you want to preview it before or after exporting the file.

In the next demo I will show how to envelope sounds and add overtones to create a simulated guitar or piano pluck.
I already have the code for it on github here:
https://github.com/Troy-Osborne/PluckLab/blob/main/Pluck.py
however it’s not yet adequately commented.

In larger projects it’s best to export the sound as you’re creating it, this ensures you don’t have to store the entire sound in memory, which usually isn’t a good idea but often doesn’t matter with smaller files. You’ll definitely notice the difference if trying to export an entire song. I will give examples of this soon.

If you are planning on storing the entire sound in memory before exporting it I recommend doing it as a numpy array and vectorizing the process, you’ll notice incredible performance increases, however I wanted to ensure this could easily be ported to C or whatever other language you want to work with.

I hope this has helped, thank you very much for your time. I wish you luck on all future audio programming endeavors.

Back to top