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)
  4. Construct the audio sample using the input arguments, such as amplitude and frequency:
        # Construct the audio signal
        audio = amp * np.sin(2 * np.pi * freq * t)
    
        return audio.astype(np.int16) 
  5. Let's define the main function. You have been provided with a JSON file called tone_freq_map.json, which contains some notes along with their frequencies:
    if __name__=='__main__':
        tone_map_file = 'tone_freq_map.json'
  6. Load that file:
        # Read the frequency map
        with open(tone_map_file, 'r') as f:
            tone_freq_map = json.loads(f.read())
  7. Let's assume that we want to generate a G note for a duration of 2 seconds:
        # Set input parameters to generate 'G' tone
        input_tone = 'G'
        duration = 2     # seconds
        amplitude = 10000
        sampling_freq = 44100    # Hz
  8. Call the function with the following parameters:
        # Generate the tone
        synthesized_tone = synthesizer(tone_freq_map[input_tone], duration, amplitude, sampling_freq)
  9. Write the generated signal into the output file:
        # Write to the output file
        write('output_tone.wav', sampling_freq, synthesized_tone)
  10. Open this file in a media player and listen to it. That's the G note! Let's do something more interesting. Let's generate some notes in sequence to give it a musical feel. Define a note sequence along with their durations in seconds:
        # Tone-duration sequence
        tone_seq = [('D', 0.3), ('G', 0.6), ('C', 0.5), ('A', 0.3), ('Asharp', 0.7)]
  11. Iterate through this list and call the synthesizer function for each of them:
        # Construct the audio signal based on the chord sequence
        output = np.array([])
        for item in tone_seq:
            input_tone = item[0]
            duration = item[1]
            synthesized_tone = synthesizer(tone_freq_map[input_tone], duration, amplitude, sampling_freq)
            output = np.append(output, synthesized_tone, axis=0)
  12. Write the signal to the output file:
        # Write to the output file
        write('output_tone_seq.wav', sampling_freq, output)
  13. The full code is in the synthesize_music.py file. You can open the output_tone_seq.wav file in your media player and listen to it. You can feel the music!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181