From 8198c397719f74c0ebf534d19bd475e7d6a011c9 Mon Sep 17 00:00:00 2001 From: Jeron Aldaron Lau Date: Sun, 28 Aug 2022 01:09:44 -0500 Subject: [PATCH] Remove `App` lifetimes in examples --- examples/monitor.rs | 16 ++++++++-------- examples/play.rs | 10 +++++----- examples/play/src/main.rs | 10 +++++----- examples/record.rs | 12 +++++++----- examples/record/src/main.rs | 16 ++++++++-------- src/ffi/linux/microphone.rs | 5 ++--- src/ffi/wasm/speakers.rs | 2 +- src/lib.rs | 16 ++++++++-------- src/speakers.rs | 10 +++++----- 9 files changed, 49 insertions(+), 48 deletions(-) diff --git a/examples/monitor.rs b/examples/monitor.rs index 4663b92..43eaf30 100644 --- a/examples/monitor.rs +++ b/examples/monitor.rs @@ -9,16 +9,16 @@ use pasts::{prelude::*, Join}; use wavy::{Microphone, MicrophoneStream, Speakers, SpeakersSink}; /// Shared state between tasks on the thread. -struct App<'a> { +struct App { /// Handle to speakers - speakers: &'a mut Speakers<1>, + speakers: Speakers<1>, /// Handle to the microphone - microphone: &'a mut Microphone<1>, + microphone: Microphone<1>, /// Temporary buffer for holding real-time audio samples. buffer: Audio, } -impl App<'_> { +impl App { /// Speaker is ready to play more audio. fn play(&mut self, mut sink: SpeakersSink) -> Poll<()> { sink.stream(self.buffer.drain()); @@ -33,8 +33,8 @@ impl App<'_> { /// Program start. async fn main(_executor: Executor) { - let speakers = &mut Speakers::default(); - let microphone = &mut Microphone::default(); + let speakers = Speakers::default(); + let microphone = Microphone::default(); let buffer = Audio::with_silence(48_000, 0); let mut app = App { speakers, @@ -43,8 +43,8 @@ impl App<'_> { }; Join::new(&mut app) - .on(|s| s.speakers, App::play) - .on(|s| s.microphone, App::record) + .on(|s| &mut s.speakers, App::play) + .on(|s| &mut s.microphone, App::record) .await } } diff --git a/examples/play.rs b/examples/play.rs index e81a824..04b335d 100644 --- a/examples/play.rs +++ b/examples/play.rs @@ -9,14 +9,14 @@ use twang::{Fc, Signal, Synth}; use wavy::{Speakers, SpeakersSink}; /// Shared state between tasks on the thread. -struct App<'a> { +struct App { /// Handle to stereo speakers - speakers: &'a mut Speakers<2>, + speakers: Speakers<2>, /// A streaming synthesizer using Twang. synth: Synth<()>, } -impl App<'_> { +impl App { /// Speaker is ready to play more audio. fn play(&mut self, mut sink: SpeakersSink) -> Poll<()> { sink.stream(&mut self.synth); @@ -29,10 +29,10 @@ impl App<'_> { fc.freq(440.0).sine().gain(0.7) } - let speakers = &mut Speakers::default(); + let speakers = Speakers::default(); let synth = Synth::new((), sine); let mut app = App { speakers, synth }; - Join::new(&mut app).on(|s| s.speakers, App::play).await; + Join::new(&mut app).on(|s| &mut s.speakers, App::play).await; } } diff --git a/examples/play/src/main.rs b/examples/play/src/main.rs index e81a824..04b335d 100644 --- a/examples/play/src/main.rs +++ b/examples/play/src/main.rs @@ -9,14 +9,14 @@ use twang::{Fc, Signal, Synth}; use wavy::{Speakers, SpeakersSink}; /// Shared state between tasks on the thread. -struct App<'a> { +struct App { /// Handle to stereo speakers - speakers: &'a mut Speakers<2>, + speakers: Speakers<2>, /// A streaming synthesizer using Twang. synth: Synth<()>, } -impl App<'_> { +impl App { /// Speaker is ready to play more audio. fn play(&mut self, mut sink: SpeakersSink) -> Poll<()> { sink.stream(&mut self.synth); @@ -29,10 +29,10 @@ impl App<'_> { fc.freq(440.0).sine().gain(0.7) } - let speakers = &mut Speakers::default(); + let speakers = Speakers::default(); let synth = Synth::new((), sine); let mut app = App { speakers, synth }; - Join::new(&mut app).on(|s| s.speakers, App::play).await; + Join::new(&mut app).on(|s| &mut s.speakers, App::play).await; } } diff --git a/examples/record.rs b/examples/record.rs index 4008191..78525b4 100644 --- a/examples/record.rs +++ b/examples/record.rs @@ -8,14 +8,14 @@ use pasts::{prelude::*, Join}; use wavy::{Microphone, MicrophoneStream}; /// Shared state between tasks on the thread. -struct App<'a> { +struct App { /// Handle to the mono microphone - microphone: &'a mut Microphone<1>, + microphone: Microphone<1>, /// Temporary buffer for holding real-time audio samples. buffer: Audio, } -impl App<'_> { +impl App { /// Event loop. Return false to stop program. fn record(&mut self, stream: MicrophoneStream) -> Poll<()> { self.buffer.extend(stream); @@ -27,10 +27,12 @@ impl App<'_> { async fn main(_executor: Executor) { let buffer = Audio::with_silence(48_000, 0); - let microphone = &mut Microphone::default(); + let microphone = Microphone::default(); let mut app = App { buffer, microphone }; - Join::new(&mut app).on(|s| s.microphone, App::record).await; + Join::new(&mut app) + .on(|s| &mut s.microphone, App::record) + .await; write_pcm(&app.buffer); } diff --git a/examples/record/src/main.rs b/examples/record/src/main.rs index 4663b92..43eaf30 100644 --- a/examples/record/src/main.rs +++ b/examples/record/src/main.rs @@ -9,16 +9,16 @@ use pasts::{prelude::*, Join}; use wavy::{Microphone, MicrophoneStream, Speakers, SpeakersSink}; /// Shared state between tasks on the thread. -struct App<'a> { +struct App { /// Handle to speakers - speakers: &'a mut Speakers<1>, + speakers: Speakers<1>, /// Handle to the microphone - microphone: &'a mut Microphone<1>, + microphone: Microphone<1>, /// Temporary buffer for holding real-time audio samples. buffer: Audio, } -impl App<'_> { +impl App { /// Speaker is ready to play more audio. fn play(&mut self, mut sink: SpeakersSink) -> Poll<()> { sink.stream(self.buffer.drain()); @@ -33,8 +33,8 @@ impl App<'_> { /// Program start. async fn main(_executor: Executor) { - let speakers = &mut Speakers::default(); - let microphone = &mut Microphone::default(); + let speakers = Speakers::default(); + let microphone = Microphone::default(); let buffer = Audio::with_silence(48_000, 0); let mut app = App { speakers, @@ -43,8 +43,8 @@ impl App<'_> { }; Join::new(&mut app) - .on(|s| s.speakers, App::play) - .on(|s| s.microphone, App::record) + .on(|s| &mut s.speakers, App::play) + .on(|s| &mut s.microphone, App::record) .await } } diff --git a/src/ffi/linux/microphone.rs b/src/ffi/linux/microphone.rs index dd82ef3..45ee8f5 100644 --- a/src/ffi/linux/microphone.rs +++ b/src/ffi/linux/microphone.rs @@ -145,7 +145,7 @@ impl Microphone { ) -> MicrophoneStream { // Always called after ready, so should be safe let inner = unsafe { self.inner.as_mut().unwrap() }; - + // Change number of channels, if different than last call. self.set_channels::(inner) .expect("Microphone::record() called with invalid configuration"); @@ -289,8 +289,7 @@ impl> Iterator for MicrophoneStream { if self.1 >= mic.endi { return None; } - let frame = - F::from_channels(&mic.buffer[self.1 * self.4 as usize..]); + let frame = F::from_channels(&mic.buffer[self.1 * self.4 as usize..]); self.1 += 1; Some(frame) } diff --git a/src/ffi/wasm/speakers.rs b/src/ffi/wasm/speakers.rs index f948584..ec0c0ef 100644 --- a/src/ffi/wasm/speakers.rs +++ b/src/ffi/wasm/speakers.rs @@ -102,7 +102,7 @@ impl Speakers { pub(crate) fn play>(&mut self) -> SpeakersSink { // Always called after ready, so should be safe let inner = unsafe { self.inner.as_mut().unwrap() }; - + // Adjust buffer size depending on type. if TypeId::of::() == TypeId::of::() { inner.buffer.resize(super::BUFFER_SIZE.into(), 0.0); diff --git a/src/lib.rs b/src/lib.rs index 4728df1..0d92e71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,16 +28,16 @@ //! use wavy::{Microphone, MicrophoneStream, Speakers, SpeakersSink}; //! //! /// Shared state between tasks on the thread. -//! struct App<'a> { +//! struct App { //! /// Handle to speakers -//! speakers: &'a mut Speakers<1>, +//! speakers: Speakers<1>, //! /// Handle to the microphone -//! microphone: &'a mut Microphone<1>, +//! microphone: Microphone<1>, //! /// Temporary buffer for holding real-time audio samples. //! buffer: Audio, //! } //! -//! impl App<'_> { +//! impl App { //! /// Speaker is ready to play more audio. //! fn play(&mut self, mut sink: SpeakersSink) -> Poll<()> { //! sink.stream(self.buffer.drain()); @@ -52,8 +52,8 @@ //! //! /// Program start. //! async fn main(_executor: Executor) { -//! let speakers = &mut Speakers::default(); -//! let microphone = &mut Microphone::default(); +//! let speakers = Speakers::default(); +//! let microphone = Microphone::default(); //! let buffer = Audio::with_silence(48_000, 0); //! let mut app = App { //! speakers, @@ -62,8 +62,8 @@ //! }; //! //! Join::new(&mut app) -//! .on(|s| s.speakers, App::play) -//! .on(|s| s.microphone, App::record) +//! .on(|s| &mut s.speakers, App::play) +//! .on(|s| &mut s.microphone, App::record) //! .await //! } //! } diff --git a/src/speakers.rs b/src/speakers.rs index 9eace22..8a89eac 100644 --- a/src/speakers.rs +++ b/src/speakers.rs @@ -30,14 +30,14 @@ use crate::ffi; /// use wavy::{Speakers, SpeakersSink}; /// /// /// Shared state between tasks on the thread. -/// struct App<'a> { +/// struct App { /// /// Handle to stereo speakers -/// speakers: &'a mut Speakers<2>, +/// speakers: Speakers<2>, /// /// A streaming synthesizer using Twang. /// synth: Synth<()>, /// } /// -/// impl App<'_> { +/// impl App { /// /// Speaker is ready to play more audio. /// fn play(&mut self, mut sink: SpeakersSink) -> Poll<()> { /// sink.stream(&mut self.synth); @@ -50,11 +50,11 @@ use crate::ffi; /// fc.freq(440.0).sine().gain(0.7) /// } /// -/// let speakers = &mut Speakers::default(); +/// let speakers = Speakers::default(); /// let synth = Synth::new((), sine); /// let mut app = App { speakers, synth }; /// -/// Join::new(&mut app).on(|s| s.speakers, App::play).await; +/// Join::new(&mut app).on(|s| &mut s.speakers, App::play).await; /// } /// } /// ```