Skip to content

Commit

Permalink
Remove App lifetimes in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Aug 28, 2022
1 parent a81bc65 commit 8198c39
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 48 deletions.
16 changes: 8 additions & 8 deletions examples/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mono32>,
}

impl App<'_> {
impl App {
/// Speaker is ready to play more audio.
fn play(&mut self, mut sink: SpeakersSink<Mono32>) -> Poll<()> {
sink.stream(self.buffer.drain());
Expand All @@ -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,
Expand All @@ -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
}
}
10 changes: 5 additions & 5 deletions examples/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stereo32>) -> Poll<()> {
sink.stream(&mut self.synth);
Expand All @@ -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;
}
}
10 changes: 5 additions & 5 deletions examples/play/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stereo32>) -> Poll<()> {
sink.stream(&mut self.synth);
Expand All @@ -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;
}
}
12 changes: 7 additions & 5 deletions examples/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mono32>,
}

impl App<'_> {
impl App {
/// Event loop. Return false to stop program.
fn record(&mut self, stream: MicrophoneStream<Mono32>) -> Poll<()> {
self.buffer.extend(stream);
Expand All @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions examples/record/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mono32>,
}

impl App<'_> {
impl App {
/// Speaker is ready to play more audio.
fn play(&mut self, mut sink: SpeakersSink<Mono32>) -> Poll<()> {
sink.stream(self.buffer.drain());
Expand All @@ -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,
Expand All @@ -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
}
}
5 changes: 2 additions & 3 deletions src/ffi/linux/microphone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Microphone {
) -> MicrophoneStream<F> {
// 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::<F>(inner)
.expect("Microphone::record() called with invalid configuration");
Expand Down Expand Up @@ -289,8 +289,7 @@ impl<F: Frame<Chan = Ch32>> Iterator for MicrophoneStream<F> {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/wasm/speakers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Speakers {
pub(crate) fn play<F: Frame<Chan = Ch32>>(&mut self) -> SpeakersSink<F> {
// 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::<F>() == TypeId::of::<Mono32>() {
inner.buffer.resize(super::BUFFER_SIZE.into(), 0.0);
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mono32>,
//! }
//!
//! impl App<'_> {
//! impl App {
//! /// Speaker is ready to play more audio.
//! fn play(&mut self, mut sink: SpeakersSink<Mono32>) -> Poll<()> {
//! sink.stream(self.buffer.drain());
Expand All @@ -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,
Expand All @@ -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
//! }
//! }
Expand Down
10 changes: 5 additions & 5 deletions src/speakers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stereo32>) -> Poll<()> {
/// sink.stream(&mut self.synth);
Expand All @@ -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;
/// }
/// }
/// ```
Expand Down

0 comments on commit 8198c39

Please sign in to comment.