如何在 python 中保存修剪过的波形文件?
How to save trimmed wave file in python?
我正在尝试记录麦克风输入,执行一些结束语音的端点算法并保存仅包含语音的新波形文件。
我设法保存了一个文件,但当我播放录音时,它播放了一半的语音序列。
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())
我正在尝试记录麦克风输入,执行一些结束语音的端点算法并保存仅包含语音的新波形文件。
我设法保存了一个文件,但当我播放录音时,它播放了一半的语音序列。
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())