我有两个 Byte 对象。一个来自使用 Wave 模块读取数据“块”:
def get_wave_from_file(filename):
import wave
original_wave = wave.open(filename, 'rb')
return original_wave
另一个使用 MIDI 信息和合成器模块 (fluidsynth)
def create_wave_from_midi_info(sound_font_path, notes):
import fluidsynth
s = []
fl = fluidsynth.Synth()
sfid = fl.sfload(sound_font_path) # Loads a soundfont
fl.program_select(track=0, soundfontid=sfid, banknum=0, presetnum=0) # Selects the soundfont
for n in notes:
fl.noteon(0, n['midi_num'], n['velocity'])
s = np.append(s, fl.get_samples(int(44100 * n['duration']))) # Gives the note the correct duration, based on a sample rate of 44.1Khz
fl.noteoff(0, n['midi_num'])
fl.delete()
samps = fluidsynth.raw_audio_string(s)
return samps
这两个文件的长度不同。我想结合这两个波,以便同时听到两者。具体来说,我想“一次一块”做这个。
这是我的设置:
def get_a_chunk_from_each(wave_object, bytes_from_midi, chunk_size=1024, starting_sample=0)):
from_wav_data = wave_object.readframes(chunk_size)
from_midi_data = bytes_from_midi[starting_sample:starting_sample + chunk_size]
return from_wav_data, from_midi_data
从 get_a_chunk_from_each() 返回的信息: type(from_wav_data), type(from_midi_data) len(from_wav_data), type(from_midi_data) 4096 1024
首先,我对为什么长度不同感到困惑(从 wave_object.readframes(1024) 生成的长度正好是手动切片 bytes_from_midi[0:1024] 生成的长度的 4 倍。这可能是部分原因我一直不成功。
其次,我想创建结合这两个块的函数。以下“伪代码”说明了我想要发生的事情:
def combine_chunks(chunk1, chunk2):
mixed = chunk1 + chunk2
# OR, probably more like:
mixed = (chunk1 + chunk2) / 2
# To prevent clipping?
return mixed