summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2020-03-10 10:15:01 -0500
committerTom Barrett <tom@tombarrett.xyz>2020-03-10 10:15:01 -0500
commit79cb6caee1513c6b70a1f2201b73c222af3ec007 (patch)
tree1463f16d4bfcc026ae1ce458663db4208faaea64
parentf57de5cd6075c071fe89bb7af898072b0a8daab6 (diff)
plots *a waveform*, not timed correctly
-rw-r--r--src/audio.rs15
-rw-r--r--src/main.rs8
-rw-r--r--src/tom.rs61
3 files changed, 47 insertions, 37 deletions
diff --git a/src/audio.rs b/src/audio.rs
index ac6bc12..e51504b 100644
--- a/src/audio.rs
+++ b/src/audio.rs
@@ -4,24 +4,35 @@ use std::fs::File;
use crate::constants;
-pub fn init() -> StreamingSource {
+pub fn init() -> (StreamingSource, Vec<i16>) {
let mut source = OggStreamReader::new(File::open("data/djbLUETOOTH.ogg").unwrap()).unwrap();
let alto = Alto::load_default().unwrap();
let device = alto.open(None).unwrap();
let audio_context = device.new_context(None).unwrap();
let mut stream = audio_context.new_streaming_source().unwrap();
let sample_rate = source.ident_hdr.audio_sample_rate as i32;
+ 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 _track_length = 0.0;
while let Ok(Some(mut samples)) = source.read_dec_packet_itl() {
samples = samples
.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::<Stereo<i16>, _>(&samples, sample_rate)
.unwrap();
stream.queue_buffer(audio_buffer).unwrap();
+
+ if samples.len() > 1 {
+ toms_samples.push(samples[0])
+ }
}
- stream
+ (stream, toms_samples)
}
diff --git a/src/main.rs b/src/main.rs
index d81d45c..8651f56 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,8 +1,8 @@
-pub mod constants;
pub mod audio;
-pub mod vertex;
+pub mod constants;
pub mod n1ck;
pub mod tom;
+pub mod vertex;
use alto::Source;
use luminance::context::GraphicsContext;
@@ -61,10 +61,10 @@ fn main() {
.unwrap()
.ignore_warnings();
- let mut stream = audio::init();
+ let (mut stream, toms_samples) = audio::init();
stream.play();
- let mut tom = Tom::new();
+ let mut tom = Tom::new(toms_samples);
let mut n1ck = N1ck::new();
let viewports = gen_viewports();
diff --git a/src/tom.rs b/src/tom.rs
index b899b08..a8fd4a4 100644
--- a/src/tom.rs
+++ b/src/tom.rs
@@ -1,5 +1,3 @@
-use std::f32::consts::PI;
-
use luminance::context::GraphicsContext;
use luminance::framebuffer::Framebuffer;
use luminance::pipeline::PipelineState;
@@ -55,50 +53,30 @@ fn relative(x: f32, in_min: f32, in_max: f32, out_min: f32, out_max: f32) -> f32
(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
}
-fn gen_sin(start: f32) -> Vec<Vertex> {
- let mut vertices: Vec<Vertex> = Vec::new();
-
- let end = start + (2.0 * PI);
- let mut x = start;
- while x < end {
- vertices.push(Vertex {
- position: VertexPosition::new([relative(x, start, end, -0.9, 0.9), x.sin() * 0.9]),
- color: VertexRGB::new(constants::C64_GREEN),
- });
-
- x += 0.4;
- }
-
- vertices
-}
-
#[derive(Default)]
pub struct Tom {
border: Vec<Vec<Vertex>>,
wave: Vec<Vertex>,
- last_input: f32,
+ last_x: usize,
+ samples: Vec<i16>,
tessalations: Vec<Tess>,
}
impl Tom {
- pub fn new() -> Tom {
- let border = gen_border();
- let wave = gen_sin(0.0);
- let tessalations = Vec::new();
-
+ pub fn new(samples: Vec<i16>) -> Tom {
Tom {
- border,
- wave,
- last_input: 0.0,
- tessalations,
+ samples,
+ border: gen_border(),
+ wave: Vec::new(),
+ last_x: 0,
+ tessalations: Vec::new(),
}
}
pub fn update<T: GraphicsContext>(&mut self, mut surface: T) -> T {
self.tessalations.clear();
- self.last_input += 0.1;
- self.wave = gen_sin(self.last_input);
+ self.update_wave();
for vertices in self.border.iter() {
self.tessalations.push(
@@ -144,4 +122,25 @@ impl Tom {
surface
}
+
+ fn update_wave(&mut self) {
+ self.wave.clear();
+
+ let start = self.last_x;
+ let end = start + 10;
+
+ let max_y = 200.0;
+ let min_y = -200.0;
+ 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),
+ ]),
+ color: VertexRGB::new(constants::C64_GREEN),
+ });
+ }
+
+ self.last_x += 1;
+ }
}