From 0e447ca05ff0c0fe91ec9a9a9091940680f967ba Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Wed, 11 Mar 2020 07:15:16 -0500 Subject: waveform is now accurately shown --- src/audio.rs | 19 +++++++++++++------ src/constants.rs | 1 + src/main.rs | 4 ++-- src/tom.rs | 16 ++++++++-------- 4 files changed, 24 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/audio.rs b/src/audio.rs index e51504b..ef92408 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -14,7 +14,7 @@ pub fn init() -> (StreamingSource, Vec) { let sample_channels = source.ident_hdr.audio_channels as f32 * source.ident_hdr.audio_sample_rate as f32; - let mut toms_samples = Vec::new(); + let mut waveform = Vec::new(); let mut _track_length = 0.0; @@ -23,16 +23,23 @@ pub fn init() -> (StreamingSource, Vec) { .into_iter() .map(|s| (s as f32 * constants::VOLUME) as i16) .collect(); - _track_length += samples.len() as f32 / sample_channels; + let audio_buffer = audio_context .new_buffer::, _>(&samples, sample_rate) .unwrap(); stream.queue_buffer(audio_buffer).unwrap(); - if samples.len() > 1 { - toms_samples.push(samples[0]) - } + _track_length += samples.len() as f32 / sample_channels; + + samples = samples + .into_iter() + .enumerate() + .filter(|(i, _)| i % 2 == 0) + .map(|(_, s)| s) + .collect(); + + waveform.append(&mut samples); } - (stream, toms_samples) + (stream, waveform) } diff --git a/src/constants.rs b/src/constants.rs index e981c8c..066684d 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -19,3 +19,4 @@ pub const C64_LIGHT_BLUE: [u8; 3] = [0, 136, 255]; pub const C64_LIGHT_GREY: [u8; 3] = [187, 187, 187]; pub const VOLUME: f32 = 0.02; +pub const SAMPLE_RATE: usize = 44100; diff --git a/src/main.rs b/src/main.rs index 8651f56..5cb2e3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,10 +61,10 @@ fn main() { .unwrap() .ignore_warnings(); - let (mut stream, toms_samples) = audio::init(); + let (mut stream, waveform) = audio::init(); stream.play(); - let mut tom = Tom::new(toms_samples); + let mut tom = Tom::new(waveform); let mut n1ck = N1ck::new(); let viewports = gen_viewports(); diff --git a/src/tom.rs b/src/tom.rs index a8fd4a4..8a55f44 100644 --- a/src/tom.rs +++ b/src/tom.rs @@ -58,14 +58,14 @@ pub struct Tom { border: Vec>, wave: Vec, last_x: usize, - samples: Vec, + waveform: Vec, tessalations: Vec, } impl Tom { - pub fn new(samples: Vec) -> Tom { + pub fn new(waveform: Vec) -> Tom { Tom { - samples, + waveform, border: gen_border(), wave: Vec::new(), last_x: 0, @@ -127,20 +127,20 @@ impl Tom { self.wave.clear(); let start = self.last_x; - let end = start + 10; + let end = start + 3000; - let max_y = 200.0; - let min_y = -200.0; + let max_y = *self.waveform.iter().max().unwrap() as f32; + let min_y = *self.waveform.iter().min().unwrap() as f32; for x in start..end { self.wave.push(Vertex { position: VertexPosition::new([ relative(x as f32, start as f32, end as f32, -0.9, 0.9), - relative(self.samples[x] as f32, min_y, max_y, -0.9, 0.9), + relative(self.waveform[x] as f32, min_y, max_y, -0.9, 0.9), ]), color: VertexRGB::new(constants::C64_GREEN), }); } - self.last_x += 1; + self.last_x += constants::SAMPLE_RATE / 60; } } -- cgit v1.2.3