GUI is very much WIP but it does work
parent
f2484dda91
commit
10d3cd62c5
|
@ -322,6 +322,17 @@ version = "1.0.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if 1.0.0",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glob"
|
name = "glob"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
|
@ -469,6 +480,7 @@ dependencies = [
|
||||||
"cpal",
|
"cpal",
|
||||||
"ffmpeg-next",
|
"ffmpeg-next",
|
||||||
"fltk",
|
"fltk",
|
||||||
|
"rand",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
]
|
]
|
||||||
|
@ -697,6 +709,12 @@ version = "0.3.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "12295df4f294471248581bc09bef3c38a5e46f1e36d6a37353621a0c6c357e1f"
|
checksum = "12295df4f294471248581bc09bef3c38a5e46f1e36d6a37353621a0c6c357e1f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro-crate"
|
name = "proc-macro-crate"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
@ -734,6 +752,46 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
"rand_hc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_hc"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.2.10"
|
version = "0.2.10"
|
||||||
|
@ -966,6 +1024,12 @@ dependencies = [
|
||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.10.2+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.78"
|
version = "0.2.78"
|
||||||
|
|
|
@ -11,5 +11,6 @@ byteorder = "1.4.3"
|
||||||
cpal = "0.13.4"
|
cpal = "0.13.4"
|
||||||
ffmpeg-next = "4.4.0"
|
ffmpeg-next = "4.4.0"
|
||||||
fltk = "1.2.15"
|
fltk = "1.2.15"
|
||||||
|
rand = "0.8.4"
|
||||||
tracing = "0.1.29"
|
tracing = "0.1.29"
|
||||||
tracing-subscriber = { version = "0.3.1", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.1", features = ["env-filter"] }
|
||||||
|
|
|
@ -91,9 +91,9 @@ impl PcmBuffers {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Decoder {
|
pub struct Decoder {
|
||||||
pub input_ctx: DemuxContext,
|
input_ctx: DemuxContext,
|
||||||
pub decoder: DecodeContext,
|
decoder: DecodeContext,
|
||||||
pub resampler: ResamplingContext,
|
resampler: ResamplingContext,
|
||||||
|
|
||||||
best_stream_idx: usize,
|
best_stream_idx: usize,
|
||||||
dummy_frame: Option <FfAudioFrame>,
|
dummy_frame: Option <FfAudioFrame>,
|
||||||
|
|
393
src/main.rs
393
src/main.rs
|
@ -5,6 +5,7 @@ use std::{
|
||||||
Arc,
|
Arc,
|
||||||
Condvar,
|
Condvar,
|
||||||
Mutex,
|
Mutex,
|
||||||
|
mpsc,
|
||||||
},
|
},
|
||||||
thread::{
|
thread::{
|
||||||
self,
|
self,
|
||||||
|
@ -36,11 +37,7 @@ use fltk::{
|
||||||
|
|
||||||
mod decoder;
|
mod decoder;
|
||||||
|
|
||||||
#[derive (Clone, Copy)]
|
const BUFFER_SIZE: usize = 5_000;
|
||||||
enum Message {
|
|
||||||
Play,
|
|
||||||
Pause,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main () -> Result <()> {
|
fn main () -> Result <()> {
|
||||||
let args: Vec <_> = std::env::args ().collect ();
|
let args: Vec <_> = std::env::args ().collect ();
|
||||||
|
@ -98,12 +95,68 @@ fn cmd_debug_pipe (args: &[String]) -> Result <()> {
|
||||||
#[derive (Default)]
|
#[derive (Default)]
|
||||||
pub struct SharedState {
|
pub struct SharedState {
|
||||||
pub pcm_buffers: decoder::PcmBuffers,
|
pub pcm_buffers: decoder::PcmBuffers,
|
||||||
pub quit: bool,
|
}
|
||||||
|
|
||||||
|
struct Gui {
|
||||||
|
intend_to_play: bool,
|
||||||
|
can_decode: bool,
|
||||||
|
audio_is_streaming: bool,
|
||||||
|
trying_to_stream: bool,
|
||||||
|
|
||||||
|
but_pause: Button,
|
||||||
|
but_play: Button,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_active <W: WidgetExt> (w: &mut W, b: bool) {
|
||||||
|
if b {
|
||||||
|
w.activate ();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
w.deactivate ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Gui {
|
||||||
|
pub fn sync (&mut self) {
|
||||||
|
set_active (
|
||||||
|
&mut self.but_pause,
|
||||||
|
self.intend_to_play,
|
||||||
|
);
|
||||||
|
set_active (
|
||||||
|
&mut self.but_play,
|
||||||
|
! self.intend_to_play &&
|
||||||
|
self.can_decode
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive (Clone, Copy, Debug)]
|
||||||
|
enum Message {
|
||||||
|
AudioStreamStatus (bool),
|
||||||
|
DecoderStatus (bool),
|
||||||
|
RawPlay,
|
||||||
|
RawPause,
|
||||||
|
RawSet,
|
||||||
|
RawSkip,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cmd_gui (args: &[String]) -> Result <()> {
|
fn cmd_gui (args: &[String]) -> Result <()> {
|
||||||
|
use std::io::BufRead;
|
||||||
|
|
||||||
tracing_subscriber::fmt::init ();
|
tracing_subscriber::fmt::init ();
|
||||||
|
|
||||||
|
let playlist = {
|
||||||
|
let mut files = Vec::with_capacity (2000);
|
||||||
|
let file = File::open ("/home/user/music/playlist.m3u")?;
|
||||||
|
let reader = std::io::BufReader::new (file);
|
||||||
|
|
||||||
|
for line in reader.lines () {
|
||||||
|
files.push (line?);
|
||||||
|
}
|
||||||
|
|
||||||
|
files
|
||||||
|
};
|
||||||
|
|
||||||
let filenames: Vec <_> = args.iter ()
|
let filenames: Vec <_> = args.iter ()
|
||||||
.skip (1)
|
.skip (1)
|
||||||
.map (|s| s.to_string ())
|
.map (|s| s.to_string ())
|
||||||
|
@ -117,49 +170,146 @@ fn cmd_gui (args: &[String]) -> Result <()> {
|
||||||
.with_label (&window_title);
|
.with_label (&window_title);
|
||||||
wind.make_resizable (true);
|
wind.make_resizable (true);
|
||||||
|
|
||||||
let mut row = Flex::default ().row ().size_of_parent ();
|
let mut col = Flex::default ().column ().size_of_parent ();
|
||||||
|
|
||||||
let mut but_play = Button::default ().with_label ("▶️");
|
{
|
||||||
but_play.set_trigger (CallbackTrigger::Release);
|
let mut row = Flex::default ().row ();
|
||||||
but_play.emit (fltk_tx, Message::Play);
|
|
||||||
row.set_size (&mut but_play, 30);
|
|
||||||
|
|
||||||
let mut but_pause = Button::default ().with_label ("■");
|
let mut label = Frame::default ().with_label ("Audio output");
|
||||||
but_pause.set_trigger (CallbackTrigger::Release);
|
let mut but_audio_play = Button::default ().with_label ("Play");
|
||||||
but_pause.emit (fltk_tx, Message::Pause);
|
let mut but_audio_pause = Button::default ().with_label ("Pause");
|
||||||
row.set_size (&mut but_pause, 30);
|
|
||||||
|
|
||||||
row.end ();
|
row.set_size (&mut label, 100);
|
||||||
|
row.set_size (&mut but_audio_play, 100);
|
||||||
|
row.set_size (&mut but_audio_pause, 100);
|
||||||
|
|
||||||
|
but_audio_play.set_trigger (CallbackTrigger::Release);
|
||||||
|
but_audio_pause.set_trigger (CallbackTrigger::Release);
|
||||||
|
|
||||||
|
but_audio_play.emit (fltk_tx, Message::RawPlay);
|
||||||
|
but_audio_pause.emit (fltk_tx, Message::RawPause);
|
||||||
|
|
||||||
|
row.end ();
|
||||||
|
|
||||||
|
col.add (&row);
|
||||||
|
col.set_size (&mut row, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut row = Flex::default ().row ();
|
||||||
|
|
||||||
|
let mut label = Frame::default ().with_label ("Decoder");
|
||||||
|
let mut but_set = Button::default ().with_label ("Set");
|
||||||
|
let mut but_skip = Button::default ().with_label ("Skip");
|
||||||
|
|
||||||
|
row.set_size (&mut label, 100);
|
||||||
|
row.set_size (&mut but_set, 100);
|
||||||
|
row.set_size (&mut but_skip, 100);
|
||||||
|
|
||||||
|
but_set.set_trigger (CallbackTrigger::Release);
|
||||||
|
but_skip.set_trigger (CallbackTrigger::Release);
|
||||||
|
|
||||||
|
but_set.emit (fltk_tx, Message::RawSet);
|
||||||
|
but_skip.emit (fltk_tx, Message::RawSkip);
|
||||||
|
|
||||||
|
row.end ();
|
||||||
|
|
||||||
|
col.add (&row);
|
||||||
|
col.set_size (&mut row, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
col.end ();
|
||||||
|
|
||||||
wind.end ();
|
wind.end ();
|
||||||
wind.show ();
|
wind.show ();
|
||||||
|
/*
|
||||||
|
let mut gui = Gui {
|
||||||
|
can_decode: false,
|
||||||
|
audio_is_streaming: false,
|
||||||
|
intend_to_play: false,
|
||||||
|
trying_to_stream: false,
|
||||||
|
but_pause,
|
||||||
|
but_play,
|
||||||
|
};
|
||||||
|
|
||||||
let shared_state = Arc::new ((Mutex::new (SharedState::default ()), Condvar::new ()));
|
gui.sync ();
|
||||||
let pcm_quit = Arc::new ((Mutex::new (false), Condvar::new ()));
|
*/
|
||||||
|
let mut shared_state = SharedState::default ();
|
||||||
|
let silence = vec! [0; 12_000 * 4 * 2];
|
||||||
|
shared_state.pcm_buffers.produce_bytes (silence);
|
||||||
|
|
||||||
let thread_decoder = DecoderThread::new (Arc::clone (&shared_state), filenames);
|
let shared_state = Arc::new ((Mutex::new (shared_state), Condvar::new ()));
|
||||||
let mut audio_output = AudioOutput::new (Arc::clone (&pcm_quit), Arc::clone (&shared_state))?;
|
|
||||||
|
let thread_decoder = DecoderThread::new (
|
||||||
|
Arc::clone (&shared_state),
|
||||||
|
fltk_tx.clone (),
|
||||||
|
);
|
||||||
|
let mut audio_output = AudioOutput::new (
|
||||||
|
Arc::clone (&shared_state),
|
||||||
|
thread_decoder.tx.clone (),
|
||||||
|
fltk_tx.clone (),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Doesn't work.
|
||||||
|
// audio_output.pause ()?;
|
||||||
|
|
||||||
|
/*
|
||||||
|
for filename in filenames {
|
||||||
|
//gui.can_decode = true;
|
||||||
|
thread_decoder.tx.send (MessageToDecoder::QueueFile (filename))?;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// let mut play_queue = vec! [];
|
||||||
|
|
||||||
|
let mut current_file = None;
|
||||||
|
|
||||||
while app.wait () {
|
while app.wait () {
|
||||||
match fltk_rx.recv () {
|
let msg = match fltk_rx.recv () {
|
||||||
Some (Message::Play) => {
|
None => continue,
|
||||||
tracing::info! ("play");
|
Some (x) => x,
|
||||||
|
};
|
||||||
|
|
||||||
|
match msg {
|
||||||
|
Message::AudioStreamStatus (x) => {
|
||||||
|
|
||||||
|
},
|
||||||
|
Message::DecoderStatus (x) => {
|
||||||
|
if ! x {
|
||||||
|
tracing::info! ("Decoder ran dry");
|
||||||
|
current_file = None;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Message::RawPlay => {
|
||||||
audio_output.play ()?;
|
audio_output.play ()?;
|
||||||
},
|
},
|
||||||
Some (Message::Pause) => {
|
Message::RawPause => {
|
||||||
tracing::info! ("pause");
|
|
||||||
audio_output.pause ()?;
|
audio_output.pause ()?;
|
||||||
},
|
},
|
||||||
None => (),
|
Message::RawSet => {
|
||||||
|
use rand::Rng;
|
||||||
|
let idx = rand::thread_rng ().gen_range (0..playlist.len ());
|
||||||
|
let filename = &playlist [idx];
|
||||||
|
tracing::info! ("Set decoder to {}", filename);
|
||||||
|
thread_decoder.tx.send (MessageToDecoder::SetFile (Some (format! ("/home/user/music/{}", filename))))?;
|
||||||
|
current_file = Some (filename);
|
||||||
|
},
|
||||||
|
Message::RawSkip => {
|
||||||
|
thread_decoder.tx.send (MessageToDecoder::SetFile (None))?;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::info! ("Received FLTK quit signal");
|
||||||
|
|
||||||
|
audio_output.pause ()?;
|
||||||
|
|
||||||
tracing::debug! ("Joining decoder thread...");
|
tracing::debug! ("Joining decoder thread...");
|
||||||
|
thread_decoder.tx.send (MessageToDecoder::Quit)?;
|
||||||
thread_decoder.join ()?;
|
thread_decoder.join ()?;
|
||||||
|
|
||||||
tracing::debug! ("Joining PCM thread...");
|
tracing::debug! ("Joining PCM thread...");
|
||||||
let (lock, cvar) = &*pcm_quit;
|
drop (audio_output);
|
||||||
let _ = cvar.wait (lock.lock ().unwrap ()).unwrap ();
|
|
||||||
|
|
||||||
tracing::info! ("Exiting cleanly.");
|
tracing::info! ("Exiting cleanly.");
|
||||||
|
|
||||||
|
@ -167,6 +317,8 @@ fn cmd_gui (args: &[String]) -> Result <()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cmd_play (args: &[String]) -> Result <()> {
|
fn cmd_play (args: &[String]) -> Result <()> {
|
||||||
|
unimplemented! ();
|
||||||
|
/*
|
||||||
tracing_subscriber::fmt::init ();
|
tracing_subscriber::fmt::init ();
|
||||||
|
|
||||||
let filenames: Vec <_> = args.iter ()
|
let filenames: Vec <_> = args.iter ()
|
||||||
|
@ -174,68 +326,140 @@ fn cmd_play (args: &[String]) -> Result <()> {
|
||||||
.map (|s| s.to_string ())
|
.map (|s| s.to_string ())
|
||||||
.collect ();
|
.collect ();
|
||||||
|
|
||||||
|
// let (decoder_tx, decoder_rx) = mpsc::sync_channel (1);
|
||||||
|
let (main_tx, main_rx) = mpsc::sync_channel (1);
|
||||||
|
|
||||||
let shared_state = Arc::new ((Mutex::new (SharedState::default ()), Condvar::new ()));
|
let shared_state = Arc::new ((Mutex::new (SharedState::default ()), Condvar::new ()));
|
||||||
let pcm_quit = Arc::new ((Mutex::new (false), Condvar::new ()));
|
let pcm_quit = Arc::new ((Mutex::new (false), Condvar::new ()));
|
||||||
|
|
||||||
let thread_decoder = DecoderThread::new (Arc::clone (&shared_state), filenames);
|
let thread_decoder = DecoderThread::new (
|
||||||
let audio_output = AudioOutput::new (Arc::clone (&pcm_quit), Arc::clone (&shared_state))?;
|
Arc::clone (&shared_state),
|
||||||
|
main_tx.clone (),
|
||||||
|
);
|
||||||
|
let audio_output = AudioOutput::new (
|
||||||
|
Arc::clone (&pcm_quit),
|
||||||
|
Arc::clone (&shared_state),
|
||||||
|
thread_decoder.tx.clone (),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
for filename in filenames {
|
||||||
|
thread_decoder.tx.send (MessageToDecoder::QueueFile (filename))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
while let Ok (msg) = main_rx.recv () { match msg {
|
||||||
|
MessageToMain::DecoderEmpty => break,
|
||||||
|
}}
|
||||||
|
|
||||||
|
tracing::debug! ("Asking decoder to quit...");
|
||||||
|
thread_decoder.tx.send (MessageToDecoder::Quit)?;
|
||||||
tracing::debug! ("Joining decoder thread...");
|
tracing::debug! ("Joining decoder thread...");
|
||||||
thread_decoder.join ()?;
|
thread_decoder.join ()?;
|
||||||
|
|
||||||
tracing::debug! ("Joining PCM thread...");
|
tracing::debug! ("Joining PCM thread...");
|
||||||
let (lock, cvar) = &*pcm_quit;
|
|
||||||
let _ = cvar.wait (lock.lock ().unwrap ()).unwrap ();
|
|
||||||
|
|
||||||
drop (audio_output);
|
drop (audio_output);
|
||||||
|
|
||||||
tracing::info! ("Exiting cleanly.");
|
tracing::info! ("Exiting cleanly.");
|
||||||
Ok (())
|
Ok (())
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DecoderThread {
|
struct DecoderThread {
|
||||||
join_handle: thread::JoinHandle <Result <()>>,
|
join_handle: thread::JoinHandle <Result <()>>,
|
||||||
|
pub tx: mpsc::SyncSender <MessageToDecoder>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum MessageToDecoder {
|
||||||
|
NeedPcm,
|
||||||
|
SetFile (Option <String>),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DecoderThread {
|
impl DecoderThread {
|
||||||
pub fn new (
|
pub fn new (
|
||||||
shared_state: Arc <(Mutex <SharedState>, Condvar)>,
|
shared_state: Arc <(Mutex <SharedState>, Condvar)>,
|
||||||
filenames: Vec <String>,
|
main_tx: fltk::app::Sender <Message>,
|
||||||
) -> Self
|
) -> Self
|
||||||
{
|
{
|
||||||
|
let (tx, rx) = mpsc::sync_channel (4);
|
||||||
|
|
||||||
|
let self_tx = tx.clone ();
|
||||||
|
|
||||||
let join_handle = thread::spawn (move|| {
|
let join_handle = thread::spawn (move|| {
|
||||||
let (lock, cvar) = &*shared_state;
|
let mut decoder: Option <decoder::Decoder> = None;
|
||||||
|
|
||||||
'many_files: for filename in &filenames {
|
'thread: while let Ok (msg) = rx.recv () { match msg {
|
||||||
let mut decoder = decoder::Decoder::new (&filename)?;
|
MessageToDecoder::NeedPcm => {
|
||||||
|
// tracing::trace! ("DecoderThread recv'd NeedPcm");
|
||||||
|
let mut bytes_produced = 0;
|
||||||
|
let mut status = false;
|
||||||
|
|
||||||
'one_file: loop {
|
'pipeline: while bytes_produced < BUFFER_SIZE * 4 * 2 {
|
||||||
let frame = match decoder.next ()? {
|
// tracing::trace! ("Pumping multi-file decode pipeline");
|
||||||
Some (x) => x,
|
let frame = if let Some (decoder) = decoder.as_mut () {
|
||||||
None => {
|
decoder.next ()?
|
||||||
tracing::debug! ("Decoder thread finished a file");
|
}
|
||||||
break 'one_file;
|
else {
|
||||||
},
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut decoder_state = cvar.wait_while (lock.lock ().unwrap (), |decoder_state| {
|
if let Some (frame) = frame {
|
||||||
decoder_state.pcm_buffers.samples_available () >= 12_000 &&
|
// tracing::trace! ("Decoded bytes");
|
||||||
! decoder_state.quit
|
status = true;
|
||||||
}).unwrap ();
|
let mut shared_state = shared_state.0.lock ().unwrap ();
|
||||||
|
bytes_produced += frame.data ().len ();
|
||||||
|
shared_state.pcm_buffers.produce_bytes (frame.data ().into ());
|
||||||
|
continue 'pipeline;
|
||||||
|
}
|
||||||
|
else if decoder.is_some () {
|
||||||
|
decoder = None;
|
||||||
|
main_tx.send (Message::DecoderStatus (false));
|
||||||
|
}
|
||||||
|
|
||||||
if decoder_state.quit {
|
{
|
||||||
break 'many_files;
|
// tracing::trace! ("Decoder thread is producing silence");
|
||||||
|
let frame = vec! [0; 2_000 * 4 * 2];
|
||||||
|
{
|
||||||
|
let mut shared_state = shared_state.0.lock ().unwrap ();
|
||||||
|
|
||||||
|
bytes_produced += frame.len ();
|
||||||
|
shared_state.pcm_buffers.produce_bytes (frame);
|
||||||
|
// tracing::trace! ("PCM buffer: {}", shared_state.pcm_buffers.samples_available ());
|
||||||
|
}
|
||||||
|
status = false;
|
||||||
|
continue 'pipeline;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// tracing::trace! ("Left NeedPcm handler");
|
||||||
|
// main_tx.send (Message::DecoderStatus (status));
|
||||||
|
},
|
||||||
|
MessageToDecoder::SetFile (x) => {
|
||||||
|
if let Some (x) = x.as_ref () {
|
||||||
|
decoder = Some (decoder::Decoder::new (x)?);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
decoder = None;
|
||||||
|
}
|
||||||
|
// main_tx.send (Message::DecoderStatus (x.is_some ()));
|
||||||
|
let new_buffers = decoder::PcmBuffers::default ();
|
||||||
|
self_tx.send (MessageToDecoder::NeedPcm)?;
|
||||||
|
|
||||||
decoder_state.pcm_buffers.produce_bytes (frame.data ().into ());
|
let mut shared_state = shared_state.0.lock ().unwrap ();
|
||||||
}
|
shared_state.pcm_buffers = new_buffers;
|
||||||
}
|
},
|
||||||
|
MessageToDecoder::Quit => {
|
||||||
|
tracing::debug! ("DecoderThread recv'd Quit");
|
||||||
|
break 'thread;
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
|
||||||
|
tracing::info! ("Decoder thread quit gracefully");
|
||||||
|
|
||||||
Ok::<_, anyhow::Error> (())
|
Ok::<_, anyhow::Error> (())
|
||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
join_handle,
|
join_handle,
|
||||||
|
tx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,8 +476,9 @@ struct AudioOutput {
|
||||||
|
|
||||||
impl AudioOutput {
|
impl AudioOutput {
|
||||||
pub fn new (
|
pub fn new (
|
||||||
pcm_quit: Arc <(Mutex <bool>, Condvar)>,
|
|
||||||
shared_state: Arc <(Mutex <SharedState>, Condvar)>,
|
shared_state: Arc <(Mutex <SharedState>, Condvar)>,
|
||||||
|
decoder_tx: mpsc::SyncSender <MessageToDecoder>,
|
||||||
|
main_tx: fltk::app::Sender <Message>,
|
||||||
) -> Result <Self>
|
) -> Result <Self>
|
||||||
{
|
{
|
||||||
let host = cpal::default_host ();
|
let host = cpal::default_host ();
|
||||||
|
@ -270,39 +495,47 @@ impl AudioOutput {
|
||||||
let stream = device.build_output_stream (
|
let stream = device.build_output_stream (
|
||||||
&config,
|
&config,
|
||||||
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||||
let (lock, cvar) = &*shared_state;
|
let (lock, _) = &*shared_state;
|
||||||
|
|
||||||
let time_start = Instant::now ();
|
let time_start = Instant::now ();
|
||||||
let mut decoder_state = match lock.lock () {
|
let need_pcm;
|
||||||
Ok (x) => x,
|
let status;
|
||||||
Err (_) => return,
|
{
|
||||||
};
|
let mut decoder_state = match lock.lock () {
|
||||||
let time_stop = Instant::now ();
|
Ok (x) => x,
|
||||||
let dur = time_stop - time_start;
|
Err (_) => return,
|
||||||
if dur.as_micros () > 100 {
|
};
|
||||||
tracing::warn! ("PCM thread waited {} us for a lock", dur.as_micros ());
|
let time_stop = Instant::now ();
|
||||||
}
|
let dur = time_stop - time_start;
|
||||||
|
if dur.as_micros () > 100 {
|
||||||
let pcm_buffers = &mut decoder_state.pcm_buffers;
|
tracing::warn! ("PCM thread waited {} us for a lock", dur.as_micros ());
|
||||||
|
|
||||||
if ! pcm_buffers.consume_exact (data) {
|
|
||||||
tracing::warn! ("PCM buffer underflow");
|
|
||||||
|
|
||||||
for x in data {
|
|
||||||
*x = 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let (lock, cvar) = &*pcm_quit;
|
let pcm_buffers = &mut decoder_state.pcm_buffers;
|
||||||
|
|
||||||
let mut pcm_quit = lock.lock ().unwrap ();
|
if ! pcm_buffers.consume_exact (data) {
|
||||||
*pcm_quit = true;
|
tracing::warn! ("PCM buffer underflow");
|
||||||
cvar.notify_one ();
|
status = false;
|
||||||
|
|
||||||
|
for x in data {
|
||||||
|
*x = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
status = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
need_pcm = pcm_buffers.samples_available () < BUFFER_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
cvar.notify_one ();
|
if need_pcm {
|
||||||
|
decoder_tx.try_send (MessageToDecoder::NeedPcm).ok ();
|
||||||
|
// tracing::trace! ("AudioOutput sent NeedPcm");
|
||||||
|
}
|
||||||
|
// main_tx.send (Message::AudioStreamStatus (status));
|
||||||
},
|
},
|
||||||
move |_err| {
|
move |err| {
|
||||||
// react to errors here.
|
tracing::error! ("{:?}", err);
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue