Working with Sound

Published

April 3, 2025

Playing with sound can be a lot of fun. Python comes with a module called wave, which makes it relatively easy to work with “.wav” files and sound data, but there are some message edges that make it hard for us to use for this class. So, we created a module called middsound which allows us to do some basic things while hiding the messy details.

Installing midd-media

Before you can use middsound, you will need to install the midd-media package if you have not done so already.

In Thonny, select the “Manage Packages” item from the “Tools” menu. You will see the Package Manager

Thonny Package Manager

In the search bar, type midd-media. It should appear in the search results. Click on it.

Search results showing midd-media

You should now see the installer page. Click install.

If you have previously installed it, this page will allow you to Upgrade it or Uninstall it.

Install page

You can now close the package manager window.

Using middsound

To use middsound, you will need to import it into you Python file. We will use a syntax that we haven’t seen as much in class.

from midd_media import middsound

middsound functions and properties

There are a couple of values and functions available directly from the middsound module. If there are () on the end, the property is a function and needs to be called, if there aren’t, it is a value and can be used like a variable (like math.pi).

middsound.MAXVALUE
This is the maximum value that a sample can have (this is a constant and should not be set)
middsound.MINVALUE
This is the minimum value that a sample can have (this is a constant and should not be set)
middsound.new()
This returns a new empty sound object. This takes an optional framerate argument which can be used to create a sound with a framerate other than the default 44.1kHz. (e.g., middsound.new(framerate=22000)).
middsound.open(filename)
This returns a sound object loaded with the contents of the specified file.filename should be a string that specifies the path relative to the directory where your Python script is.
middsound.get_frequency(note)
This is a convenience function that will return the frequencies of some notes (c4 to b5). The notes should be specified as a two character string of the form ‘c4’ (this would be middle C – the letter gives us the note and the number tells us which octave).

Sound methods

When you usemiddsound.new() or middsound.open(filename), the return value is a Sound, which you can consider to be a type like string or list. The Sound type behaves like it was a list of samples, so we can use square bracket notation to access and set sample values, just like we would a list (e.g., snd[1] = snd[0] * 2 would set the second sample to be twice the value of the first sample). We can also use len() to get the length of the sound (i.e., the number of samples). There is also a collection of functions and properties we can access using dot notation (there is nothing special about the name snd, it is just a place holder for whatever you called the variable holding your sound):

snd.framerate
This returns the number of samples this sound uses every second. The default is 44.1kHz, so 44,100 samples a second. Note that this is a variable, not a function, so do not include the ()
snd.append(sample)
Append sample to the end of the sound. The sample should be an integer between middSound.MAXVALUE and middSound.MINVALUE.
snd.play()
Play the sound.
snd.save(filename)
Save the sound out to a .wav file in the same directory as the Python file you are working on. The filename should be a string.

Wav files

The Waveform Audio File Format is a relatively old format for storing audio data. We are going to use it because it is a simple format and we have some tools for working with it.

Saving wav files

As you can see in the description above, our Sound objects have a save function. When you use it, you should pass in a string containing the filename. The filename should end in ‘.wav’, so your computer will know how to play it. You should be able to play the resulting file on any computer (on Macs, you can even preview it with Quicklook).

Opening wav files

The middsound library also contains an open function that will allow you to read in a wav file. Make sure the file is in the same directory as your code.

Wav files are a favorite for folks sharing soundbites from movies and TV shows, so you won’t have any problem finding files to play with (just type ‘wav files’ into your favorite search engine).

However, most of these wav files won’t work directly. Wav files can be compressed or uncompressed. We need uncompressed wav files, but most of the ones you find online are compressed. The quickest fix is to grab a copy of Audacity. It should open the file just fine. Then just export it as a wav again (we also recommend making sure the framerate is set to 44100 Hz in the lower left hand corner as well). In truth, once you get Audacity involved, you should be able to make your own wav files out of just about any sound.

If you don’t want to make your own files, here are some random samples.