about summary refs log tree commit diff
path: root/gaudio/src
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2023-10-14 17:02:09 -0500
committergennyble <gen@nyble.dev>2023-10-14 17:02:09 -0500
commit2106c47cc9b16aaf4831d9005dfeafdd3b078db2 (patch)
tree09d07d603556cd6ab90d8b2836dc00462db3b984 /gaudio/src
parent8f89665a63b9d027905f3a1303e5cde8e2359685 (diff)
downloadgifed-2106c47cc9b16aaf4831d9005dfeafdd3b078db2.tar.gz
gifed-2106c47cc9b16aaf4831d9005dfeafdd3b078db2.zip
need to work on gifed's api
sorry the audio_extension doc is rambly
Diffstat (limited to 'gaudio/src')
-rw-r--r--gaudio/src/mp3/mod.rs39
1 files changed, 33 insertions, 6 deletions
diff --git a/gaudio/src/mp3/mod.rs b/gaudio/src/mp3/mod.rs
index 952e9b2..fe6433f 100644
--- a/gaudio/src/mp3/mod.rs
+++ b/gaudio/src/mp3/mod.rs
@@ -1,4 +1,7 @@
-use std::io::{BufRead, BufReader, Cursor, ErrorKind, Read};
+use std::{
+	io::{BufRead, BufReader, Cursor, ErrorKind, Read},
+	time::Duration,
+};
 
 use crate::mp3::bitrate::Bitrate;
 
@@ -49,15 +52,17 @@ impl Breaker {
 				let mut data = vec![0; dat_len];
 				reader.read_exact(&mut data)?;
 				consumed += dat_len;
+				let frame = Frame { header, data };
 
 				println!(
-					"{}kbps {}kHz {}bytes",
-					header.bitrate.kbps().unwrap(),
-					header.samplerate.freq() / 1000,
-					header.length()
+					"{}kbps {}kHz {:<4}bytes [{}ms]",
+					frame.header.bitrate.kbps().unwrap(),
+					frame.header.samplerate.freq() / 1000,
+					frame.header.length(),
+					frame.duration().as_millis()
 				);
 
-				self.frames.push(Frame { header, data });
+				self.frames.push(frame);
 			} else {
 				println!("unsynced!");
 				panic!()
@@ -114,6 +119,28 @@ pub struct Frame {
 	pub data: Vec<u8>,
 }
 
+impl Frame {
+	/// The number of moments-in-time this frame represents. This is constant
+	/// and related to the [Layer]
+	pub fn sample_count(&self) -> usize {
+		// http://www.datavoyage.com/mpgscript/mpeghdr.htm
+		// > Frame size is the number of samples contained in a frame. It is
+		// > constant and always 384 samples for Layer I and 1152 samples for
+		// > Layer II and Layer III.
+		match self.header.layer {
+			Layer::Reserved => panic!(),
+			Layer::Layer1 => 384,
+			Layer::Layer2 | Layer::Layer3 => 1152,
+		}
+	}
+
+	/// Compute the duration of this audio frame
+	pub fn duration(&self) -> Duration {
+		let millis = (self.sample_count() * 1000) / self.header.samplerate.freq();
+		Duration::from_millis(millis as u64)
+	}
+}
+
 pub struct Header {
 	// I only want to parse what i need, but we need this for writing out, so
 	pub raw: [u8; 4],