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 {
channels: Vec<UIChannel>,
selected_channel: usize,
selected_index: usize,
_stream: OutputStream,
stream_handle: OutputStreamHandle,
quit: bool,
@ -38,7 +38,7 @@ impl App {
.expect("Failed to create output stream");
Self {
channels: Vec::new(),
selected_channel: 0,
selected_index: 0,
_stream,
stream_handle,
quit: false,
@ -85,7 +85,7 @@ impl App {
for (i, channel) in self.channels.iter().enumerate() {
println!(
"{}{}:\n {:.0}",
if i == self.selected_channel { ">" } else { " " },
if i == self.selected_index { ">" } else { " " },
&channel.name,
(channel.volume * 100.0)
);
@ -106,16 +106,28 @@ impl App {
match event.code {
KeyCode::Char('q') => self.quit = true,
KeyCode::Up => {
self.selected_channel = match self.selected_channel {
0 => self.channels.len() - 1,
n => n - 1,
};
}
KeyCode::Down => {
self.selected_channel = (self.selected_channel + 1) % self.channels.len();
}
KeyCode::Up => self.select_prev(),
KeyCode::Down => self.select_next(),
KeyCode::Right => self.set_channel_volume(0.1),
KeyCode::Left => self.set_channel_volume(-0.1),
_ => (),
}
}
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 {
channels: Vec<SoundChannel>,
sample_rate: u32,
sync_rate: u32,
since_sync: u32,
}
struct SoundChannel {
@ -35,6 +37,11 @@ impl Iterator for Snoud {
type Item = i16;
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;
for c in &mut self.channels {
if c.paused {
@ -66,9 +73,12 @@ impl Source for Snoud {
impl Snoud {
pub fn new(/* filenames: &[String] */) -> Self {
// let channels = filenames.iter().map(SoundChannel::new).collect();
let sample_rate = 48000;
Self {
sample_rate: 48000,
sample_rate,
channels: Vec::new(),
sync_rate: sample_rate / 20,
since_sync: 0,
}
}
@ -78,4 +88,10 @@ impl Snoud {
self.channels.push(new);
volume_sync
}
fn sync(&mut self) {
for c in &mut self.channels {
c.volume = *c.volume_sync.lock().unwrap();
}
}
}