如何在不保存 wav 的情况下在函数之间传递编辑的 wav?

how to pass edited wav between functions without saving wav in between?

我有 2 个人(客户和技术支持)的 wav 对话 我有 3 个独立的函数,提取 1 个语音,剪切 10 秒并将其转换为嵌入。

def get_customer_voice(file):

    print('getting customer voice only')
    wav = wf.read(file)
    ch = wav[1].shape[1]#customer voice always in 1st track
    sr = wav[0]
    c1 = wav[1][:,1]
    #print('c0 %i'%c0.size)

    if ch==1:
        exit()
    vad = VoiceActivityDetection()
    vad.process(c1)
    voice_samples = vad.get_voice_samples()
    #this is trouble - how to pass it without saving anywhere as wav?
    wf.write('%s_customer.wav'%file,sr,voice_samples)

下面的函数从上面的函数中剪切了 10 秒的 wav 文件。

import sys
from pydub import AudioSegment

def get_customer_voice_10_seconds(file):
    voice = AudioSegment.from_wav(file)
    new_voice = voice[0:10000]
    file = str(file) + '_10seconds.wav'
    new_voice.export(file, format='wav')


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('give wav file to process!')
    else:
        print(sys.argv)
        get_customer_voice_10_seconds(sys.argv[1])

如何在不保存到某个目录的情况下将其作为 wav 或其他格式传递?它要在休息时使用api,我不知道它会在哪里保存那个wav,所以最好它应该以某种方式传递。

我明白了 - 下面的功能无需保存、缓冲等即可运行。 它接收一个 wav 文件并对其进行编辑,然后直接发送到获取数学嵌入函数:

def get_customer_voice_and_cutting_10_seconds_embedding(file):

    print('getting customer voice only')
    wav = read(file)
    ch = wav[1].shape[1]
    sr = wav[0]

    c1 = wav[1][:,1]

    vad = VoiceActivityDetection()
    vad.process(c1)
    voice_samples = vad.get_voice_samples()
    audio_segment = AudioSegment(voice_samples.tobytes(), frame_rate=sr,sample_width=voice_samples.dtype.itemsize, channels=1)
    audio_segment = audio_segment[0:10000]
    file = str(file) + '_10seconds.wav'

    return get_embedding(file)

关键是Audio段中的tobytes(),它只是把它们重新组合在1个音轨中