Voice Rework -- Events, Track Queues (#806)

This implements a proof-of-concept for an improved audio frontend. The largest change is the introduction of events and event handling: both by time elapsed and by track events, such as ending or looping. Following on from this, the library now includes a basic, event-driven track queue system (which people seem to ask for unusually often). A new sample, `examples/13_voice_events`, demonstrates both the `TrackQueue` system and some basic events via the `~queue` and `~play_fade` commands.

Locks are removed from around the control of `Audio` objects, which should allow the backend to be moved to a more granular futures-based backend solution in a cleaner way.
This commit is contained in:
Kyle Simpson
2020-10-29 20:25:20 +00:00
committed by Alex M. M
commit 7e4392ae68
76 changed files with 8756 additions and 0 deletions

53
src/tracks/command.rs Normal file
View File

@@ -0,0 +1,53 @@
use super::*;
use crate::events::EventData;
use std::time::Duration;
use tokio::sync::oneshot::Sender as OneshotSender;
/// A request from external code using a [`TrackHandle`] to modify
/// or act upon an [`Track`] object.
///
/// [`Track`]: struct.Track.html
/// [`TrackHandle`]: struct.TrackHandle.html
pub enum TrackCommand {
/// Set the track's play_mode to play/resume.
Play,
/// Set the track's play_mode to pause.
Pause,
/// Stop the target track. This cannot be undone.
Stop,
/// Set the track's volume.
Volume(f32),
/// Seek to the given duration.
///
/// On unsupported input types, this can be fatal.
Seek(Duration),
/// Register an event on this track.
AddEvent(EventData),
/// Run some closure on this track, with direct access to the core object.
Do(Box<dyn FnOnce(&mut Track) + Send + Sync + 'static>),
/// Request a read-only view of this track's state.
Request(OneshotSender<Box<TrackState>>),
/// Change the loop count/strategy of this track.
Loop(LoopState),
}
impl std::fmt::Debug for TrackCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
use TrackCommand::*;
write!(
f,
"TrackCommand::{}",
match self {
Play => "Play".to_string(),
Pause => "Pause".to_string(),
Stop => "Stop".to_string(),
Volume(vol) => format!("Volume({})", vol),
Seek(d) => format!("Seek({:?})", d),
AddEvent(evt) => format!("AddEvent({:?})", evt),
Do(_f) => "Do([function])".to_string(),
Request(tx) => format!("Request({:?})", tx),
Loop(loops) => format!("Loop({:?})", loops),
}
)
}
}