Synthesizing music

Now that we know how to generate audio, let's use this principle to synthesize some music. You can check out this link, http://www.phy.mtu.edu/~suits/notefreqs.html. This link lists various notes, such as A, G, D, and so on, along with their corresponding frequencies. We will use this to generate some simple music.

How to do it…

  1. Create a new Python file, and import the following packages:
    import json
    import numpy as np
    from scipy.io.wavfile import write
    import matplotlib.pyplot as plt
  2. Define a function to synthesize a tone, based on input parameters:
    # Synthesize tone
    def synthesizer(freq, duration, amp=1.0, sampling_freq=44100):
  3. Build the time axis values:
     # Build the time axis t = np.linspace(0, duration, duration * sampling_freq) ...

Get Python: Real World Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.