How to generate audio from a numpy array?

后端 未结 8 866
囚心锁ツ
囚心锁ツ 2020-11-28 04:09

I want to create \"heart rate monitor\" effect from a 2D array in numpy and want the tone to reflect the values in the array.

相关标签:
8条回答
  • 2020-11-28 04:52

    PyGame has the module pygame.sndarray which can play numpy data as audio. The other answers are probably better, as PyGame can be difficult to get up and running. Then again, scipy and numpy come with their own difficulties, so maybe it isn't a large step to add PyGame into the mix.

    http://www.pygame.org/docs/ref/sndarray.html

    0 讨论(0)
  • 2020-11-28 04:59

    For the people coming here in 2016 scikits.audiolab doesn't really seem to work anymore. I was able to get a solution using sounddevice.

    import numpy as np
    import sounddevice as sd
    
    fs = 44100
    data = np.random.uniform(-1, 1, fs)
    sd.play(data, fs)
    
    0 讨论(0)
  • 2020-11-28 05:00

    I had some problems using scikit.audiolabs, so I looked for some other options for this task. I came up with sounddevice, which seems a lot more up-to-date. I have not checked if it works with Python 3.

    A simple way to perform what you want is this:

    import numpy as np
    import sounddevice as sd
    
    sd.default.samplerate = 44100
    
    time = 2.0
    frequency = 440
    
    # Generate time of samples between 0 and two seconds
    samples = np.arange(44100 * time) / 44100.0
    # Recall that a sinusoidal wave of frequency f has formula w(t) = A*sin(2*pi*f*t)
    wave = 10000 * np.sin(2 * np.pi * frequency * samples)
    # Convert it to wav format (16 bits)
    wav_wave = np.array(wave, dtype=np.int16)
    
    sd.play(wav_wave, blocking=True)
    
    0 讨论(0)
  • 2020-11-28 05:01

    In addition, you could try scikits.audiolab. It features file IO and the ability to 'play' arrays. Arrays don't have to be integers. To mimick dbaupp's example:

    import numpy as np
    import scikits.audiolab
    
    data = np.random.uniform(-1,1,44100)
    # write array to file:
    scikits.audiolab.wavwrite(data, 'test.wav', fs=44100, enc='pcm16')
    # play the array:
    scikits.audiolab.play(data, fs=44100)
    
    0 讨论(0)
  • 2020-11-28 05:03

    If you are using Jupyter, the best option is:

    from IPython.display import Audio
    Audio(numpy.sin(numpy.linspace(0, 3000, 20000)), rate=20000)
    
    0 讨论(0)
  • 2020-11-28 05:06

    You can use the write function from scipy.io.wavfile to create a wav file which you can then play however you wish. Note that the array must be integers, so if you have floats, you might want to scale them appropriately:

    import numpy as np
    from scipy.io.wavfile import write
    
    data = np.random.uniform(-1,1,44100) # 44100 random samples between -1 and 1
    scaled = np.int16(data/np.max(np.abs(data)) * 32767)
    write('test.wav', 44100, scaled)
    

    If you want Python to actually play audio, then this page provides an overview of some of the packages/modules.

    0 讨论(0)
提交回复
热议问题