O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating sounds

A sound can be represented mathematically by a sine wave with a certain amplitude, frequency, and phase. We can randomly select frequencies from a list specified in http://en.wikipedia.org/wiki/Piano_key_frequencies that comply with the following formula:

Generating sounds

Here, n is the number of the piano key. We will number the keys from 1 to 88. We will select the amplitude, duration, and phase at random.

How to do it...

Begin by initializing random values, then generate sine waves, compose a melody, and finally, plot the generated audio data with matplotlib:

  1. Initialize to random values:
    • The amplitude between 200 - 2000
    • The duration to 0.01 - 0.2 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required