我正在尝试记录麦克风输入,执行一些终结语音的终结算法,并仅保存包含语音的新波形文件。
我设法保存了一个文件,但当我播放录音时,它播放了一半的语音序列。
1)数组应该采用什么格式才能成功保存?
2) 如何将其转换为该格式?
我正在使用以下算法进行麦克风录音,错误出在我保存文件的方式中:
如果我调用 writeframes(frames),它可以很好地保存完整的 3 秒麦克风输入。
FORMAT = pyaudio.paInt16 # We use 16bit format per sample
CHANNELS = 1
RATE = 44100
CHUNK = 1024 # 1024bytes of data red from a buffer
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print "recording..."
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print "finished recording"
# stop Recordings
stream.stop_stream()
stream.close()
audio.terminate()
frames = ''.join(frames)
# important! convert from string to int
amplitudeSamples = np.fromstring(frames, np.int16)
# Perform endpointing algorithm where I compute start and end indexes
# within amplitudeSamples array
voiceSample = amplitudeSamples[start:end]
# Here lies the problem
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(1)
waveFile.setsampwidth(2)
waveFile.setframerate(RATE)
waveFile.writeframes(voiceSample)
#waveFile.writeframesraw(voiceSample) # doesn't work also
waveFile.close()
最佳答案
在写入之前将 numpy 数组转换为字符串:
wavFile.writeframes(voiceSample.tostring())
关于python - 如何在Python中保存修剪后的波形文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443788/