This is How I Played a Sinus Tone in My Jupyter Notebook (Python)

What/Why? I want to write a simple Python script that warns me if crypto price data (e.g., BTC) crosses a certain threshold. This can be useful for trading or some other apps, so I thought it would be fun to do it.

The tutorial in front of you simply documents my learnings on creating a sinus tone in my Jupyter Notebook—so it may benefit you as well.

If you want the whole tutorial on my mini project, you can check it out here on the Finxter blog:

πŸ‘‰ Recommended Tutorial: I Made a Python Script That Beeps When BTC or ETH Prices Drop

Challenge

πŸ’¬ Challenge: Write Python code in a Jupyter Notebook that creates a sinus tone when executed.

Solution

The easy way to solve this challenge is the following. πŸ‘‡

This code creates a sine wave with a frequency of 500 Hz and plays it in the IPython environment. The wave is created using the NumPy library by specifying the frequency and the length of the wave (15000*2). The rate of the wave is set to 10000 Hz and autoplay is set to True so that the wave will start playing immediately.

import numpy as np
from IPython.display import Audio

# Create the tone as a NumPy Sinus Wave
wave = np.sin(2*np.pi*500*np.arange(15000*2)/15000)

# Play the Sinus Wave (tone)
Audio(wave, rate=10000, autoplay=True)

This generates the following beep sound in your Jupyter Notebook:

What happens if you change the rate argument of the Audio() function call to be 20000 instead of 10000?

# Play the Sinus Wave (tone)
Audio(wave, rate=20000, autoplay=True)

The beep sound tone gets higher:

You can play around with the Jupyter notebook here:

But what if you don’t have a Jupyter notebook but a normal Python script (Win/Linux/macOS)?

In that case, you cannot use the IPython library. Instead, follow the steps outlined in the following tutorial on the Finxter blog—you still can play beep sounds!

πŸ‘‰ Recommended Tutorial: How to Make a Beep Sound in Python?

Thanks for Reading! β™₯️

You’re welcome to join our free email academy where I share all our coding projects and cheat sheets on a weekly basis: