pulse width

This commit is contained in:
Max Bradbury 2022-11-01 19:14:19 +00:00
parent eee3444c4d
commit a36313341d
1 changed files with 60 additions and 2 deletions

View File

@ -1,5 +1,37 @@
use core::fmt;
use std::fmt::Formatter;
use crate::note::Note;
pub enum PulseWidth {
/// 50% duty cycle
Half,
/// 25% duty cycle
Quarter,
/// 12.5% duty cycle
Eighth,
}
impl PulseWidth {
fn from(str: &str) -> Result<PulseWidth, crate::Error> {
match str {
"P2" => Ok(PulseWidth::Half),
"P4" => Ok(PulseWidth::Quarter),
"P8" => Ok(PulseWidth::Eighth),
_ => Err(crate::Error::PulseWidth),
}
}
}
impl fmt::Display for PulseWidth {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "SQR {}", match self {
PulseWidth::Half => "P2",
PulseWidth::Quarter => "P4",
PulseWidth::Eighth => "P8",
})
}
}
/// thanks to Rumple_Frumpkins from Bitsy Talk for his help in figuring out the blip format.
#[derive(Debug, Clone, PartialEq)]
pub struct Blip {
@ -11,8 +43,34 @@ pub struct Blip {
/// first value is milliseconds per note;
/// second value is a modifier to the first note (add or subtract milliseconds)
beat: [i16; 2],
/// I think this is probably pulse width. apparently the potential values are P2, P4 and P8.
square: String,
/// potential values are P2, P4 and P8.
pulse_width: PulseWidth,
/// Notes can sound repeatedly, or just once as the blip fades out.
repeat: bool,
}
impl From<&str> for Blip {
fn from(str: &str) -> Self {
let mut id = String::new();
let mut notes = vec![];
let mut name = None;
let mut envelope = [0; 5];
let mut beat = [0; 2];
let mut pulse_width = PulseWidth::Half;
let mut repeat = false;
for line in str.lines() {
if line.starts_with("BLIP ") {
id = line.replace("BLIP ", "");
} else if line.starts_with("NAME ") {
name = Some(line.replace("NAME ", ""));
} else if line.starts_with("ENV ") {
let envelope_temp: Vec<u8> = line.replace("ENV ", "").split(' ').map(|v| v.parse().unwrap()).collect();
} else {
// notes = line.split(',').map(|n| Note::from(n)).collect().unwrap();
}
}
Self { id, notes, name, envelope, beat, pulse_width, repeat }
}
}