Creating Audio Files with Python

Image for post
Image for post

This month I am teaching an intensive boot camp in Python for grad students and postdocs in mathematics and physics. One topic is sound: how to generate it using code, and how to convert what we generate — just an array of numbers — into a file that can be played on a computer.

There are quite a few resources for this sort of thing on the web, but it took me a while to find a solution that I was really happy with — just six lines of code after the imports. The code, listed below, is an example from the library. To use it, you will have to run first.

# Source: https://pypi.org/project/wavio/
# File: gen_sound.py
# Run
#
# $ python gen_sound.py
#
# to create the file `sine440.py
#
# Then run (for example on Mac OS)
#
# $ afplay sine.wav
#
# to listen to the sound
import numpy as np
import wavio
# Parameters
rate = 44100 # samples per second
T = 3 # sample duration (seconds)
f = 440.0 # sound frequency (Hz)
# Compute waveform samples
t = np.linspace(0, T, T*rate, endpoint=False)
x = np.sin(2*np.pi * f * t)
# Write the samples to a file
wavio.write("sine.wav", x, rate, sampwidth=3)

On my laptop (Macbook Pro, 3.3 GHz Intel Core i7), generating the 3-second sample array took about 2.2 milliseconds, while writing the file took about 10 milliseconds.

Notes

I tried several other snippets of code that were of similar nature. All used more or less the same method to generate the wave sample (the array ), but either (a) writing the file was too slow (using the library), or (b) wrting files was fast, but the data file lacked the header that made it playable.

Written by

jxxcarlson on elm slack, http://jxxcarlson.github.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store