生成正弦波,将其硬平移到一个扬声器,然后用pyaudio播放

时间:2017-11-16 21:57:37

标签: python channel pyaudio panning

我下载了Pyaudio并尝试使用它来生成双耳节拍,但是我在寻找如何将每个正弦波分成它自己的频道时遇到了极大的困难。基本上,我希望能够将每个耳朵的正弦波硬化,以产生双耳节拍效果。这是我要做的代码:

import pyaudio
import numpy as np

p = pyaudio.PyAudio()

volume = 0.5    # range [0.0, 1.0]
fs = 44100       # sampling rate, Hz, must be integer
duration = 2.0   # in seconds, may be float
f = 240.0        # sine frequency, Hz, may be float

# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)

# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=fs,
                output=True)

# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)

stream.stop_stream()
stream.close()

p.terminate()

或者,我尝试过使用sounddevice,它可以很好地进行平移,但是我无法创建持续时间超过一秒的正弦波。

非常感谢任何帮助!

0 个答案:

没有答案