我正在努力创建一个嵌入式压缩系统,类似于专业音频混音器中的那些。我正在使用PyAudio
通过the given "wire" example捕获音频样本。
这些样本被分成“块”,这要归功于图书馆并在录制后不久进行了流式传输。如果输入信号变得太大,我只是试图压缩块。但是,类型不匹配。
正在使用的类型是:
<type 'str'>
- Unicode字符串 <type 'int'>
- 始终返回1024 <type 'NoneType'>
<class 'pydub.audio_segment.AudioSegment'>
PyAudio从方法string
返回stream.read()
,该方法存储在data
中。我需要能够将这些字符串示例转换为AudioSegment对象,以便使用压缩功能。
因此,最终发生的事情是我得到了几个与类型转换相关的错误,具体取决于我如何设置所有内容。我知道这不是正确的类型。那么如何才能使这种类型的转换工作呢?我的源代码是here
以下是我尝试在for i in range
循环中进行转化的两种方式
1。在压缩前创建“wave”对象
wave_file = wave.open(f="compress.wav", mode="wb")
wave_file.writeframes(data)
frame_rate = wave_file.getframerate()
wave_file.setnchannels(2)
# Create the proper file
compressed = AudioSegment.from_raw(wave_file)
compress(compressed) # Calling compress_dynamic_range in Pydub
异常wave.Error:&gt;错误('未指定#个频道')忽略
2. 将RAW PyAudio数据发送到压缩方法
data = stream.read(chunk)
compress(chunk) # Calling compress_dynamic_range in Pydub
thresh_rms = seg.max_possible_amplitude * db_to_float(threshold) AttributeError:'int'对象没有属性'max_possible_amplitude'
答案 0 :(得分:1)
由于在设置# of channels
之前写入波形文件而引发的第一个错误可以修复如下:
# inside for i in range loop
wave_file = wave.open(f="compress.wav(%s)" %i, mode="wb")
wave_file.setnchannels(channels)
wave_file.setsampwidth(sample_width)
wave_file.setframerate(sample_rate)
wave_file.writeframesraw(data) # place this after all attributes are set
wave_file.close()
# send temp files to compressor
compressed = AudioSegment.from_raw(wave_file)
compress(compressed)
然后可以将其发送到PyDub功能compress_dynamic_range
。
更有效的方法 - 无需创建临时 wav 文件 - 就是以下列方式创建一个简单的AudioSegment对象。也可以使用stream.write()
将压缩后的声音传回PyAudio。
sound = AudioSegment(data, sample_width=2, channels=2, frame_rate=44100)
stream.write(sound.raw_data, chunk) # stream via speakers / headphones