0

Here's my code. The function is often called with the same file name to play. Each call creates several temporary files. How to avoid this? or create one temporary file for each call with the same name?

def play_sound(file_name):    
    path_file=os.path.join(WAV_FLDR, file_name)
    path_file+= ".ogg"    
    if os.path.exists(path_file): 
        ogg_audio = AudioSegment.from_ogg(path_file)
        sound =ogg_audio.apply_gain(-ogg_audio.max_dBFS)
        silence_threshold = -45
        
        nonsilent_ranges=silence.split_on_silence(sound,silence_thresh=silence_threshold,min_silence_len=80)
        cnt=0
        for sound in nonsilent_ranges:
            cnt+=1
            play(sound)
    else:
        print(f"File didnt finder {path_file}")
       

1 Answer 1

0

I'm using a virtual environment. If you have installed PyDub globally then make sure to navigate where the packages are installed on your operating system.

Image Link: Pydub Temporary File Fix

Open path/to/packages/pydub/playback.py and in the function _play_with_ffplay(seg), make the following change:

From: with NamedTemporaryFile("w+b", suffix=".wav") as f:

To: with NamedTemporaryFile("w+b", suffix=".wav", delete=True) as f:

It stops creating the temporary files multiple times for each call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.