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”.

Lightning detector with a simple headphone jack

Okay, this probably one of those posts that is ecstatic to draft because of the shear wonder that it encapsulates. We captured lightning using nothing more than just a mere headphone jack and its so simple that anyone around the world with an access to an old headphone jack can do it too!



Take an old headphone jack and strip off the mic part and you are good to go OR if you have a dedicated USB sound card you can use that!



Sparks of any kind produce electromagnetic disturbances and it is these disturbances made by lightning that we will be detecting. That is the reason we are stripping off the microphone from the headphone jack and keeping only the wire.  The wire will act as an antenna to pick up the signal.

Just to be clear, this is NOT related to the sound of the thunder by any means.

Test with Gas Lighter

Before we test the headphone jack with lightning, we needed to know how the headphone jack reacted to sparks and how a spark looks like when recorded with a headphone jack.

Since gas lighters are capable of producing sparks, we thought that it would be a great place to start. Here’s what we observed:

** Although we use a USB sound card in our videos, this works with an ordinary TRS/TRRS headphone jack as well.

Capturing lightning

Now that we have a good idea of what is going on with a gas lighter, we can predict a similar pattern with a lightning as well. Why ? Well, because lightning is the same phenomenon but, at a much bigger scale.

How do you do it ? First things first, to state the obvious one needs lightning. But besides that the setup is similar to the previous one except that unlike the gas lighter, we have to use the human body as an antenna by holding the wire coming from the mic pin of the headphone jack in our hand

Processing the recording

The audio file that we acquired has been uploaded to Google Drive (links below) but we strongly suggest that you try it out yourself to get a feel for this powerful technology that many people disregard as quotidian.

This is how the raw audio file might look (recorded with Audacity):

Now we amplify this signal on Audacity. (Effect –> Amplify)



Now you see these three prominent peaks, now these are for sure caused by the lightning. How are we so sure? Well, we were able to visually validate the data. What are those other peaks ? They are lightning too (!!!), but ones that are happening at a distance (probably many kilometers away).

Now in order to be completely sure, we repeated the same experiment another evening.. And here’s what we got:

Screenshot from 2017-05-18 10:05:53

This was a much more dramatic day with lots of lightning! ( as is evident from the numerous peaks )

How cool is that you can something as simple as a headphone jack to procure information about lightning that is probably happens kilometers away. This was absolutely ecstatic!

What does one peak look like ?

Here is a close up of one peak that was recorded:


Sampling rate: 44100 Hz

Screenshot from 2017-05-24 12:43:23

Sampling rate: 384000 Hz

This resembles very closely to a Guassian Wave packet . ( In physics, a wave packet (or wave train) is a short “burst” or “envelope” of localized wave action that travels as a unit )


We believe that the electromagnetic disturbance that is detected during a lightning / spark is a Gaussian wave packet.  (If you believe otherwise let us know why by pinging us at or in the comments section)


Video Demo:

Will be uploaded soon! (subject to environmental weather conditions ;P)

If you found this post interesting, check out

Part- II-  Detect switching ON/OFF of Tube-light with a simple headphone jack

Part – III – Cigarette lighter spark detection using headphone jack

Part – IV –  The Headphone jack meets an actual spark gap!!!!!!

Thunder Audio file (44100 Hz): GoogleDrive link

Thunder Audio file (384000 Hz): GoogleDrive link

Testing basic headphone jack functions (Android)

In this post we will be trying to emulate basic functions as prescribed in the Android Documentation.  In order to understand the functions we first need to understand the circuit layout of the headphone jack which is as follows:

Screenshot from 2017-05-22 11:11:21

Now with this circuit in hand you can perform a series of tasks. This has been summarized in the table below.

Screenshot from 2017-05-22 11:03:00

This post will primarily focus on the Function A since that is the one that is commonplace in all mobile phones with a headphone jack. In the following video, we demonstrate how to emulate the following functions using a headphone jack

  • Play/Pause
  • Open Google Voice
  • Next song
  • Radio
  • Emulating Google voice

Every speaker is also a microphone!

Sometimes it comes as a surprise as a people to people when I tell them that every speaker is also a microphone. This is true because in a speaker you send in electrical signals to change the way a speaker cone moves. This in turn produces various sounds.


With the same setup, if one provides a mechanical vibration to the diaphragm, this will generate audio signals corresponding to that mechanical vibration.



We have made a video demonstrating this using a headphone jack and a phone. In order to loop the sound from the mic to the speaker in the video, we use the following command on Linux:

pactl load-module module-loopback latency_msec=1