从 PyAudio str 到 AudioSegment 的类型转换 returns 错误
Type Conversion from PyAudio str to AudioSegment returns errors
我正在致力于创建一个嵌入式压缩系统,类似于专业混音器上的系统。我正在通过 the given "wire" example.
使用 PyAudio
捕获音频样本
应该发生什么
感谢图书馆,这些样本被分成“块”,并在录制后不久流式传输。如果传入信号变得太大,我只是试图压缩这些块。但是,存在不匹配的类型。
正在使用的类型是:
- 数据 = 来自流的样本
<type 'str'>
- Unicode 字符串
- chunk = 一批音频字节
<type 'int'>
- 总是 returns 1024
- stream.write(数据,块)
<type 'NoneType'>
- compressed_segment = 待压缩
<class 'pydub.audio_segment.AudioSegment'>
发生了什么事
PyAudio returns 作为方法 stream.read()
的 string
存储在 data
中。我需要能够将这些字符串样本转换为 AudioSegment 对象,以便使用压缩功能。
因此,最终发生的是我遇到了几个与类型转换相关的错误,具体取决于我如何设置所有内容。我知道这不是正确的类型。那么我怎样才能使这种类型转换起作用呢?
这是我尝试在 for i in range
循环中进行转换的 2 种方法
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
Exception wave.Error: Error('# channels not specified',) in <bound method Wave_write.del of <wave.Wave_write instance at 0x000000000612FE88>> ignored
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' object has no attribute 'max_possible_amplitude'
第一个错误是在设置 # of channels
之前写入 wave 文件而引发的第一个错误可以修复如下:
# 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
我正在致力于创建一个嵌入式压缩系统,类似于专业混音器上的系统。我正在通过 the given "wire" example.
使用PyAudio
捕获音频样本
应该发生什么
感谢图书馆,这些样本被分成“块”,并在录制后不久流式传输。如果传入信号变得太大,我只是试图压缩这些块。但是,存在不匹配的类型。
正在使用的类型是:
- 数据 = 来自流的样本
<type 'str'>
- Unicode 字符串 - chunk = 一批音频字节
<type 'int'>
- 总是 returns 1024 - stream.write(数据,块)
<type 'NoneType'>
- compressed_segment = 待压缩
<class 'pydub.audio_segment.AudioSegment'>
发生了什么事
PyAudio returns 作为方法 stream.read()
的 string
存储在 data
中。我需要能够将这些字符串样本转换为 AudioSegment 对象,以便使用压缩功能。
因此,最终发生的是我遇到了几个与类型转换相关的错误,具体取决于我如何设置所有内容。我知道这不是正确的类型。那么我怎样才能使这种类型转换起作用呢?
这是我尝试在 for i in range
循环中进行转换的 2 种方法
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
Exception wave.Error: Error('# channels not specified',) in <bound method Wave_write.del of <wave.Wave_write instance at 0x000000000612FE88>> ignored
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' object has no attribute 'max_possible_amplitude'
第一个错误是在设置 # of channels
之前写入 wave 文件而引发的第一个错误可以修复如下:
# 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()
.
sound = AudioSegment(data, sample_width=2, channels=2, frame_rate=44100)
stream.write(sound.raw_data, chunk) # stream via speakers / headphones