Skip to content

Commit

Permalink
csv working...gotta think about it
Browse files Browse the repository at this point in the history
  • Loading branch information
xasopheno committed Dec 23, 2022
1 parent 8c76919 commit 480a746
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
80 changes: 52 additions & 28 deletions ast/src/datagen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Scale {
}
}

pub fn csv_to_normalform(filename: &str, scales: Vec<Scale>) -> Result<NormalForm, Error> {
pub fn csv_to_normalform(filename: &str, scales: Vec<Scale>) -> Result<Term, Error> {
let data = get_data(filename.into())?;
let path = Path::new(&filename);
Ok(csv_data_to_normal_form(
Expand All @@ -55,12 +55,30 @@ pub fn csv_to_normalform(filename: &str, scales: Vec<Scale>) -> Result<NormalFor
))
}

fn csv_data_to_normal_form(data: &Vec<Vec<f32>>, scales: Vec<Scale>, filename: &str) -> NormalForm {
let mut length_ratio = Rational64::new(0, 1);
// fn vec_eeg_data_to_normal_form(
// data: &Vec<Vec<f32>>,
// scales: Vec<Scale>,
// filename: &str,
// ) -> NormalForm {
// let nf = csv_data_to_normal_form(data, scales, filename);

// let overlay = Op::Overlay {
// operations: vec![Term::Nf(nf.to_owned())],
// };

// let mut nf = NormalForm::init();
// overlay
// .apply_to_normal_form(&mut nf, &mut Defs::new())
// .expect("unable to normalize");
// nf
// }

fn csv_data_to_normal_form(data: &Vec<Vec<f32>>, scales: Vec<Scale>, filename: &str) -> Term {
// let mut length_ratio = Rational64::new(0, 1);

// let mut buffer = RingBuffer::<f32>::new(50);

let point_ops: Vec<PointOp> = data
let point_ops: Vec<Term> = data
.iter()
.map(|value| {
let op = point_to_point_op(
Expand All @@ -71,16 +89,16 @@ fn csv_data_to_normal_form(data: &Vec<Vec<f32>>, scales: Vec<Scale>, filename: &
// },
&scales, filename,
);
length_ratio += op.l;
// length_ratio += op.l;
op
})
.collect();
dbg!(&point_ops);
// dbg!(&point_ops);

NormalForm {
length_ratio,
operations: vec![point_ops],
}
Term::Op(Op::Sequence {
// length_ratio,
operations: point_ops,
})
}

pub fn f32_to_rational(mut float: f32) -> Rational64 {
Expand All @@ -101,7 +119,7 @@ fn point_to_point_op(
// buffers: Buffers,
scales: &Vec<Scale>,
filename: &str,
) -> PointOp {
) -> Term {
let mut nameset = NameSet::new();
nameset.insert(filename.to_string());
let result: Vec<Rational64> = scales
Expand Down Expand Up @@ -134,23 +152,29 @@ fn point_to_point_op(
// }

// let fa = f32_to_rational(fa);
PointOp {
// fm,
fm: Rational64::new(1, 1),
fa,
l: lm,
// l: Rational64::new(2, 100),
g: Rational64::new(1, 1),
pm: Rational64::new(1, 1),
pa: Rational64::new(0, 1),
asr: ASR::Long,
portamento: Rational64::new(1, 1),
attack: Rational64::new(1, 1),
decay: Rational64::new(1, 1),
reverb: None,
osc_type: OscType::None,
names: nameset,
}
Term::Op(Op::Compose {
operations: vec![
Term::Op(Op::TransposeA { a: fa }),
Term::Op(Op::Length { m: lm }),
],
})
// PointOp {
// // fm,
// fm: Rational64::new(1, 1),
// fa,
// l: lm,
// // l: Rational64::new(2, 100),
// g: Rational64::new(1, 1),
// pm: Rational64::new(1, 1),
// pa: Rational64::new(0, 1),
// asr: ASR::Long,
// portamento: Rational64::new(1, 1),
// attack: Rational64::new(1, 1),
// decay: Rational64::new(1, 1),
// reverb: None,
// osc_type: OscType::None,
// names: nameset,
// }
}

fn get_data(filename: String) -> Result<Vec<Vec<f32>>, Error> {
Expand Down
1 change: 1 addition & 0 deletions core/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub fn write_composition_to_mp3(mut composition: StereoWaveform) -> Result<Vec<u
let mp3buf = &mut vec![0_u8; length.ceil() as usize];

let mut l = Lame::new().ok_or(weresocool_lame::Error::InternalError)?;

l.init_params()?;
l.encode_f32(l_buffer.as_slice(), r_buffer.as_slice(), mp3buf)?;

Expand Down
5 changes: 5 additions & 0 deletions lame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ impl Lame {
pub fn new() -> Option<Self> {
let ctx = unsafe { ffi::lame_init() };

unsafe {
crate::ffi::lame_set_in_samplerate(ctx, 48000);
dbg!(crate::ffi::lame_get_in_samplerate(ctx));
}

if ctx.is_null() {
None
} else {
Expand Down
6 changes: 6 additions & 0 deletions test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
100.0,
200.0,
100.0,
50.0,
100.0,
200.0,

0 comments on commit 480a746

Please sign in to comment.