#๐Ÿ”’ Audio File Queue

29 messages ยท Page 1 of 1 (latest)

finite cairn
#

In my project, i have list of strings and a function that creates a text to speech audio file given a string. I also have a function that plays audio files. e.g.

string_list = ['example','strings']
def tts(string): tts
def play_audio(file): plays audio

The problem is combining these functions either:
Does not wait for the previous audio file to finish before playing the next.
Waits for the audio to finish playing before computing TTS.

The behavior I want is for the program to TTS a string and play the audio, and while the audio is playing it can start inferencing the second string to save a lot of time, but wait until the first file is done playing before playing the next. I have tried asyncio with some suggestions from AI, but nothing seems to be working.

maiden lionBOT
#

@finite cairn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tranquil heath
#

hi I am making an app using TTS as well and I will be playing multiple audio files eventually, but right now I'm stuck at not hearing the audio from the python file when it plays the .mp3. I was wondering what audio playback python library you are using? and are you on a Windows computer?

finite cairn
#

sounddevice and soundfile

tranquil heath
#

thanks for responding! Are you on a Windows machine using native Python or WSL?

autumn estuary
#

I understand you don't want to wait for all the words to be converted, but you could do something in the middle: Convert a series of strings without playing them, and then when you're sure you'll always have another file to play according to some heuristic, start playing while converting the rest

#

I'm not sure what you're doing to convert and play, but if the actions are CPU-bound, then asyncio and threading won't be much help to you

#

If that's enough for you to go from here then great, otherwise I'll need more info

finite cairn
#

creating the next audio file is faster than playing the previous file, so it will interrupt the previous audio and play the new file. the fix is to add sounddevice.wait() but then the program will halt until the audio is complete before inferencing the next string into the next audio file

#

i was thinking asyncio would be the solution but i am not familiar and the generated suggestions didnt seem to work

autumn estuary
#

What I would do is have a queue of ready sound files, then one coroutine/thread/process would fill the queue, and another would take from the queue and play the sound. I'm not familiar with the library, if asyncio/multithreading doesn't work you can try multiprocessing

finite cairn
#

yes i am trying to use the async queue modules but im not sure about the syntax

#

the functions are add string to list, pop a string and tts, and play audio file

#

the tts function just creates the audio file

#

i want to process the next tts WHILE an audio file is being played, but not interrupt when finished. so maybe somehow queue the audio files to be played

#

like how you said but im not sure how to write it if you could an example for me

autumn estuary
#

show what you currently have

finite cairn
#

def play_audio(file):
audio_device = 'Audio Device'
data,fs = soundfile.read(file)
sounddevice.play(data,fs,device=audio_device)

maiden lionBOT
#

Hey @finite cairn!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
finite cairn
#

playing the audio just takes a filename input and plays the audio of that file

#

def process_string_list(string_list):
first_string = string_list.pop(0)
file = generate_audio_file(first_string)

autumn estuary
#

you said you tried asyncio

finite cairn
#

yes. these are them before since im not sure what im doing with aynsc. i tried def aysnc play_audio and adding await play_audio but it didnt work and wouldnt even play the file

autumn estuary
#

You don't need the rnd_sleep, you can just do await asyncio.sleep(0) just to release the event loop (meaning, let the other function run)

#

You also need only one producer and one consumer

maiden lionBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.