use num_enum::{IntoPrimitive, TryFromPrimitive};
use serde::{Deserialize, Serialize};
#[derive(Default, Serialize, Deserialize, Copy, Clone, IntoPrimitive, TryFromPrimitive, Debug)]
#[repr(u8)]
pub enum LoopType {
#[default]
No = 0,
Forward = 1,
PingPong = 2,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum SampleDataType {
Depth8(Vec<i8>),
Depth16(Vec<i16>),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Sample {
pub name: String,
pub loop_start: u32, pub loop_length: u32, pub volume: f32,
pub finetune: f32, pub flags: LoopType,
pub panning: f32,
pub relative_note: i8,
pub data: SampleDataType,
}
impl Sample {
pub fn len(&self) -> usize {
match &self.data {
SampleDataType::Depth8(v) => v.len(),
SampleDataType::Depth16(v) => v.len(),
}
}
pub fn at(&self, seek: usize) -> f32 {
match &self.data {
SampleDataType::Depth8(v) => v[seek] as f32 / 128.0,
SampleDataType::Depth16(v) => v[seek] as f32 / 32768.0,
}
}
pub fn bits(&self) -> u8 {
match &self.data {
SampleDataType::Depth8(_) => 8,
SampleDataType::Depth16(_) => 16,
}
}
}