简体   繁体   English

Python-混合两个音频块

[英]Python - Mix two audio chunks

I have two Byte objects. 我有两个字节对象。 One comes from using the Wave module to read a "chunk" of data: 一种是使用Wave模块读取“大块”数据:

def get_wave_from_file(filename):
    import wave
    original_wave = wave.open(filename, 'rb')
    return original_wave

The other uses MIDI information and a Synthesizer module (fluidsynth) 另一个使用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

The two files are of different length. 这两个文件的长度不同。 I want to combine the two waves, so that both are heard simultaneously. 我想将两个波形合并,以便同时听到两个波形。 Specifically, I would like to do this "one chunk at a time". 具体来说,我想“一次做一个块”。

Here is my setup: 这是我的设置:

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

Info about the return from get_a_chunk_from_each(): type(from_wav_data), type(from_midi_data) len(from_wav_data), type(from_midi_data) 4096 1024 有关从get_a_chunk_from_each()返回的信息:type(from_wav_data),type(from_midi_data)len(from_wav_data),type(from_midi_data)4096 1024

Firstly, I'm confused as to why the lengths are different (the one generated from wave_object.readframes(1024) is exactly 4 times longer than the one generated by manually slicing bytes_from_midi[0:1024]. This may be part of the reason I have been unsuccessful. 首先,我对为什么长度不同感到困惑(从wave_object.readframes(1024)生成的长度比手动切片bytes_from_midi [0:1024]生成的长度长4倍。这可能是部分原因)我一直没有成功。

Secondly, I want to create the function which combines the two chunks. 其次,我想创建结合了两个块的函数。 The following "pseudocode" illustrates what I want to happen: 以下“伪代码”说明了我想发生的事情:

def combine_chunks(chunk1, chunk2):
    mixed = chunk1 + chunk2
    # OR, probably more like:
    mixed = (chunk1 + chunk2) / 2
    # To prevent clipping?
    return mixed

It turns out there is a very, very simple solution. 事实证明,有一个非常非常简单的解决方案。 I simply used the library audioop: 我只是使用了库audioop:

https://docs.python.org/3/library/audioop.html https://docs.python.org/3/library/audioop.html

and used their "add" function ("width" is the sample width in bytes. Since this is 16 bit audio, that's 16 / 8 = 2 bytes): 并使用了他们的“添加”功能(“宽度”是样本宽度(以字节为单位。由于这是16位音频,因此16/8 = 2字节)):

    audioop.add(chunk1, chunk2, width=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM