It is ridiculously easy to generate any audio signal using Python

Updated: May 15,2019

Now it comes as a surprise to many people when I tell them that generating an audio waveform is extremely simple.

One needs to have basic understanding on how audio signals work and basic python programming to generate any audio wave form. This post will show you exactly how.

Python packages needed: Numpy, Scipy


How to play the audio the generated audio file on computer ?

1. Command line using SoX

play -t raw -r 44.1k -e signed -b 8 -c 1 test.wav

where -r = sampling rate -b = sampling precision (bits) -c = number of channels

2. Use Audacity (check video)


Link to code : GitHub

You can find a list of other waveforms that can be generated using SciPy here

Known Issues:

[1] This does add any headers to the audio file and therefore you cannot play it on any media player as is . Check this reddit post if you really want to have one.

[2] Adding headers to the above code seems to be making it slower. And this is a problem if you want to make larger audio files.

[3] The code generates only 8-bit audio signal. Feel free to play around with the code to change it to other formats.

[4] A lot of technical details were conveniently not included in code in order to appeal to the theme of this post. And therefore this code is not “efficient”.

10 thoughts on “It is ridiculously easy to generate any audio signal using Python

  1. The code shown here does not generate wav files.
    The wav file format is not just a collection of samples. It is a collection of chunks of data. One of these chunks contains samples.


  2. Pingback: Import Python: import python weekly Issue No 130 – fast sudoku solver, colorama, ShutIt, audio analysis and more | Adrian Tudor Web Designer and Programmer

  3. Pingback: Simple Wave Generation in Python (and SciPy) | Hackaday

  4. Thanks, very useful article!
    By the way, I guess number of samples should be equal Fs * t, where t is the duration of the audio track? On the screenshot you have “sample = 44100 / 440”.


  5. Hy there,
    I’m new to python and I have to do a project on a raspberry pi 2 – I hope you can help…
    I need to record a 10 second audio file throug a electrek microphone at an mcp3008 which is cabled to the SPI interface on the raspberry pi.
    Now I have values form 0 to 1023 but I don’t now how to transfer this into a wave file that I can play on the raspi…
    Do you have any advices?

    Thanks πŸ™‚


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s