Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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

I have a wav conversation of 2 people(customer and tech support) I have 3 separate functions that extract 1 voice, cut 10 seconds and transform it to embedding.

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)

function below cuts 10 seconds of wav file from function above.

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])

how to pass it as wav or other format without saving it to some directory? It's to be used in rest api, i don't know where it will save that wav, so preferably it should be passed somehow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I figured it out - the function below just works without saving, buffer etc. It receives a wav file and edits it and just sends straight to the get math embedding function:

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)

the key is tobytes() in Audio segment, it just assembles all them together in 1 track again


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...