set channel volumes from menu

This commit is contained in:
Crispy 2023-01-18 23:09:59 +01:00
parent ad4a91e105
commit 2de51f7a48
2 changed files with 41 additions and 13 deletions

View file

@ -26,7 +26,7 @@ struct UIChannel {
struct App { struct App {
channels: Vec<UIChannel>, channels: Vec<UIChannel>,
selected_channel: usize, selected_index: usize,
_stream: OutputStream, _stream: OutputStream,
stream_handle: OutputStreamHandle, stream_handle: OutputStreamHandle,
quit: bool, quit: bool,
@ -38,7 +38,7 @@ impl App {
.expect("Failed to create output stream"); .expect("Failed to create output stream");
Self { Self {
channels: Vec::new(), channels: Vec::new(),
selected_channel: 0, selected_index: 0,
_stream, _stream,
stream_handle, stream_handle,
quit: false, quit: false,
@ -85,7 +85,7 @@ impl App {
for (i, channel) in self.channels.iter().enumerate() { for (i, channel) in self.channels.iter().enumerate() {
println!( println!(
"{}{}:\n {:.0}", "{}{}:\n {:.0}",
if i == self.selected_channel { ">" } else { " " }, if i == self.selected_index { ">" } else { " " },
&channel.name, &channel.name,
(channel.volume * 100.0) (channel.volume * 100.0)
); );
@ -106,16 +106,28 @@ impl App {
match event.code { match event.code {
KeyCode::Char('q') => self.quit = true, KeyCode::Char('q') => self.quit = true,
KeyCode::Up => { KeyCode::Up => self.select_prev(),
self.selected_channel = match self.selected_channel { KeyCode::Down => self.select_next(),
0 => self.channels.len() - 1, KeyCode::Right => self.set_channel_volume(0.1),
n => n - 1, KeyCode::Left => self.set_channel_volume(-0.1),
};
}
KeyCode::Down => {
self.selected_channel = (self.selected_channel + 1) % self.channels.len();
}
_ => (), _ => (),
} }
} }
fn set_channel_volume(&mut self, delta: f32) {
let channel = self.channels.get_mut(self.selected_index).unwrap();
channel.volume = (channel.volume + delta).clamp(0., 2.);
*channel.internal_volume.lock().unwrap() = channel.volume;
}
fn select_prev(&mut self) {
self.selected_index = match self.selected_index {
0 => self.channels.len() - 1,
n => n - 1,
};
}
fn select_next(&mut self) {
self.selected_index = (self.selected_index + 1) % self.channels.len();
}
} }

View file

@ -7,6 +7,8 @@ use std::time::Duration;
pub struct Snoud { pub struct Snoud {
channels: Vec<SoundChannel>, channels: Vec<SoundChannel>,
sample_rate: u32, sample_rate: u32,
sync_rate: u32,
since_sync: u32,
} }
struct SoundChannel { struct SoundChannel {
@ -35,6 +37,11 @@ impl Iterator for Snoud {
type Item = i16; type Item = i16;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.since_sync += 1;
if self.since_sync == self.sync_rate {
self.since_sync = 0;
self.sync();
}
let mut out: Self::Item = 0; let mut out: Self::Item = 0;
for c in &mut self.channels { for c in &mut self.channels {
if c.paused { if c.paused {
@ -66,9 +73,12 @@ impl Source for Snoud {
impl Snoud { impl Snoud {
pub fn new(/* filenames: &[String] */) -> Self { pub fn new(/* filenames: &[String] */) -> Self {
// let channels = filenames.iter().map(SoundChannel::new).collect(); // let channels = filenames.iter().map(SoundChannel::new).collect();
let sample_rate = 48000;
Self { Self {
sample_rate: 48000, sample_rate,
channels: Vec::new(), channels: Vec::new(),
sync_rate: sample_rate / 20,
since_sync: 0,
} }
} }
@ -78,4 +88,10 @@ impl Snoud {
self.channels.push(new); self.channels.push(new);
volume_sync volume_sync
} }
fn sync(&mut self) {
for c in &mut self.channels {
c.volume = *c.volume_sync.lock().unwrap();
}
}
} }