我正在尝试在Python中创建一个无限长的纯正弦音色(以便以后可以添加实时开/关事件),并且虽然我可以演奏出一个音色,但它断断续续。我的假设是它不是从下一个块开始,而不是在波浪周期中与最后一个块结束的位置相同的地方开始,或者是在计算下一个块时存在延迟,因此我不知道它是哪个。
这些事情是否正在发生,或者我是否犯了其他错误?此外,在不牺牲实时更改传入数据的能力的情况下,是否有更好的方法?
import time
import numpy
import pyaudio
import math
CHUNK = 4096
RATE = 44100
def sine(current_time, frequency=440):
length = CHUNK
factor = float(frequency) * (math.pi * 2) / RATE
this_chunk = numpy.arange(length) + current_time
return numpy.sin(this_chunk * factor)
def get_chunk():
data = sine(time.time())
return data * 0.1
def callback(in_data, frame_count, time_info, status):
chunk = get_chunk() * 0.25
data = chunk.astype(numpy.float32).tostring()
return (data, pyaudio.paContinue)
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paFloat32,
channels = 2,
rate = RATE,
output = True,
stream_callback = callback)
stream.start_stream()
while stream.is_active():
time.sleep(0.1)
stream.stop_stream()
stream.close()
最佳答案
看来您需要一些优化,
https://wiki.python.org/moin/PythonSpeed/PerformanceTips
尝试:
import time
import numpy
import pyaudio
import math
CHUNK = 4096
RATE = 44100
MP = math.pi
NA = numpy.arange
NS = numpy.sin
TT = time.time
NF32 = numpy.float32
p = pyaudio.PyAudio()
PO = p.open
PC = pyaudio.paContinue
PF = pyaudio.paFloat32
TS = time.sleep
def sine(current_time, frequency=440):
length = CHUNK
factor = float(frequency) * (MP * 2) / RATE
this_chunk = NA(length) + current_time
return NS(this_chunk * factor)* 0.025
def callback(in_data, frame_count, time_info, status):
chunk = sine(TT())
data = chunk.astype(NF32).tostring()
return (data, PC)
stream = PO(format = PF,
channels = 2,
rate = RATE,
output = True,
stream_callback = callback)
stream.start_stream()
while stream.is_active():
TS(0.1)
stream.stop_stream()
stream.close()
关于python - 在Python中创建无限长的正弦音,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20057306/