this post was submitted on 03 Sep 2023
19 points (100.0% liked)
Rust Programming
8159 readers
4 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
If you want the source to repeat indefinitely, you can try calling
repeat_infinite
on it. Combine that withpausable
/stoppable
, and useperiodic_access
to occasionally check whether the audio source should be paused/stopped probably by using anArc[AtomicBool]
(using square brackets because Lemmy hates angle ones).It could look something like this:
periodic_access
is also howSink
controls the source when you want to pause/stop/etc it. You could probably useSink
directly if you want more control over the source.I dont want to pause/play the source. I simply want to replay it over and over again but with a delay in between. The audio is a click of a game wheel spinning, its duration is somewhere in the low milliseconds. I have an exponential function which tells the program how much time to sleep until the next click on the wheel. I just want to play the sound each time the wheel clicks. Do you think appending the sound to a sink and removing it once Sink.sleep_until_end() finishes would produce that outcome.
In this case, I don't think
Sink
will let you selectively remove sources (although you can clear the sink if you want), but whenever you want to play a click you could clear the sink and append the clicking source to it. Alternatively, you could create a source that chains the clicking sound with something likeZero
and have it repeat indefinitely, but have theZero
source play until you receive a new signal to play the click audio (and stop the source once you're done clicking).I think how you should approach this depends on your architecture, so I'll give a couple approaches I would consider if I were trying to do this myself:
For a blocking approach: I'd play the click sound once (using
.append
on the sink, for example), then useInstant::now() - last_instant
and pass whatever duration is left to wait off tothread::sleep
. This would look something like this (pseudo-ish code):For a non-blocking approach where you have a separate thread managing the audio, I'd use a channel or similar as a signal for when to play the click. Your thread could then wait until that signal is received and append the click sound to the sink. You'd basically have a thread dedicated to managing the audio in this case. If you want a more complicated version this as an example, here's a project where we used
rodio
withtauri
(like you) to queue up audio sources to be played on demand whenever the user clicks certain buttons in the UI. The general architecture is the same - just afor
loop listening for events from a channel, and then using those events to add sources to our output stream (though you can just use aSink
I believe to keep things simple).Man I implemented something like the Sink option and it is playing the audio clips at inputted intervals just as I wanted. The only thing missing for me now is just the intervals, which means i just need to tinker with my exponential time functions. Thank you so much for your help.