From f2484dda91b4d1a35287fa2840e35935bb6f962d Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 13 Nov 2021 19:51:03 +0000 Subject: [PATCH] :construction: wip: FLTK GUI demo --- src/main.rs | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3190b13..191f130 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,9 +8,8 @@ use std::{ }, thread::{ self, - sleep, }, - time::{Duration, Instant}, + time::{Instant}, }; use anyhow::{ @@ -102,7 +101,14 @@ pub struct SharedState { pub quit: bool, } -fn cmd_gui (_args: &[String]) -> Result <()> { +fn cmd_gui (args: &[String]) -> Result <()> { + tracing_subscriber::fmt::init (); + + let filenames: Vec <_> = args.iter () + .skip (1) + .map (|s| s.to_string ()) + .collect (); + let (fltk_tx, fltk_rx) = app::channel:: (); let app = app::App::default (); @@ -128,18 +134,35 @@ fn cmd_gui (_args: &[String]) -> Result <()> { wind.end (); wind.show (); + let shared_state = Arc::new ((Mutex::new (SharedState::default ()), Condvar::new ())); + let pcm_quit = Arc::new ((Mutex::new (false), Condvar::new ())); + + let thread_decoder = DecoderThread::new (Arc::clone (&shared_state), filenames); + let mut audio_output = AudioOutput::new (Arc::clone (&pcm_quit), Arc::clone (&shared_state))?; + while app.wait () { match fltk_rx.recv () { Some (Message::Play) => { tracing::info! ("play"); + audio_output.play ()?; }, Some (Message::Pause) => { tracing::info! ("pause"); + audio_output.pause ()?; }, None => (), } } + tracing::debug! ("Joining decoder thread..."); + thread_decoder.join ()?; + + tracing::debug! ("Joining PCM thread..."); + let (lock, cvar) = &*pcm_quit; + let _ = cvar.wait (lock.lock ().unwrap ()).unwrap (); + + tracing::info! ("Exiting cleanly."); + Ok (()) } @@ -158,18 +181,14 @@ fn cmd_play (args: &[String]) -> Result <()> { let audio_output = AudioOutput::new (Arc::clone (&pcm_quit), Arc::clone (&shared_state))?; tracing::debug! ("Joining decoder thread..."); - thread_decoder.join ()?; tracing::debug! ("Joining PCM thread..."); - let (lock, cvar) = &*pcm_quit; let _ = cvar.wait (lock.lock ().unwrap ()).unwrap (); drop (audio_output); - sleep (Duration::from_secs (1)); - tracing::info! ("Exiting cleanly."); Ok (()) } @@ -226,8 +245,8 @@ impl DecoderThread { } struct AudioOutput { - host: cpal::Host, - device: cpal::Device, + _host: cpal::Host, + _device: cpal::Device, stream: cpal::Stream, } @@ -288,8 +307,8 @@ impl AudioOutput { )?; Ok (Self { - host, - device, + _host: host, + _device: device, stream, }) }