🚧 wip: FLTK GUI demo
							parent
							
								
									70e5182dfc
								
							
						
					
					
						commit
						f2484dda91
					
				
							
								
								
									
										41
									
								
								src/main.rs
								
								
								
								
							
							
						
						
									
										41
									
								
								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::<Message> (); | ||||
| 	
 | ||||
| 	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, | ||||
| 		}) | ||||
| 	} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 _
						_