Add boilerplate for exporting the envelopes

This commit is contained in:
Gwilym Inzani 2023-08-04 23:25:48 +01:00
parent d29a641a44
commit ca4cb55b39
2 changed files with 42 additions and 8 deletions

View file

@ -42,9 +42,9 @@ pub struct PatternSlot {
#[derive(Debug)] #[derive(Debug)]
pub struct Envelope<'a> { pub struct Envelope<'a> {
pub amount: &'a [Num<i16, 8>], pub amount: &'a [Num<i16, 8>],
pub sustain: Option<u32>, pub sustain: Option<usize>,
pub loop_start: Option<u32>, pub loop_start: Option<usize>,
pub loop_end: Option<u32>, pub loop_end: Option<usize>,
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, error::Error, fs, path::Path}; use std::{collections::HashMap, env, error::Error, fs, path::Path};
use agb_tracker_interop::PatternEffect; use agb_tracker_interop::PatternEffect;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
@ -9,7 +9,7 @@ use syn::LitStr;
use agb_fixnum::Num; use agb_fixnum::Num;
use xmrs::{prelude::*, xm::xmmodule::XmModule}; use xmrs::{envelope, prelude::*, xm::xmmodule::XmModule};
pub fn agb_xm_core(args: TokenStream) -> TokenStream { pub fn agb_xm_core(args: TokenStream) -> TokenStream {
let input = match syn::parse::<LitStr>(args.into()) { let input = match syn::parse::<LitStr>(args.into()) {
@ -56,15 +56,25 @@ pub fn parse_module(module: &Module) -> TokenStream {
relative_note: i8, relative_note: i8,
restart_point: u32, restart_point: u32,
volume: Num<i16, 8>, volume: Num<i16, 8>,
envelope_id: Option<usize>,
} }
let mut samples = vec![]; let mut samples = vec![];
let mut envelopes: Vec<EnvelopeData> = vec![];
for (instrument_index, instrument) in instruments.iter().enumerate() { for (instrument_index, instrument) in instruments.iter().enumerate() {
let InstrumentType::Default(ref instrument) = instrument.instr_type else { let InstrumentType::Default(ref instrument) = instrument.instr_type else {
continue; continue;
}; };
let envelope = &instrument.volume_envelope;
let envelope_id = if envelope.enabled {
envelopes.push(envelope.as_ref().into());
Some(envelopes.len() - 1)
} else {
None
};
for (sample_index, sample) in instrument.sample.iter().enumerate() { for (sample_index, sample) in instrument.sample.iter().enumerate() {
let should_loop = !matches!(sample.flags, LoopType::No); let should_loop = !matches!(sample.flags, LoopType::No);
let fine_tune = sample.finetune as f64 * 128.0; let fine_tune = sample.finetune as f64 * 128.0;
@ -99,6 +109,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
relative_note, relative_note,
restart_point, restart_point,
volume, volume,
envelope_id,
}); });
} }
} }
@ -377,7 +388,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
should_loop: sample.should_loop, should_loop: sample.should_loop,
restart_point: sample.restart_point, restart_point: sample.restart_point,
volume: sample.volume, volume: sample.volume,
volume_envelope: None, volume_envelope: sample.envelope_id,
}) })
.collect(); .collect();
@ -387,7 +398,16 @@ pub fn parse_module(module: &Module) -> TokenStream {
.map(|order| *order as usize) .map(|order| *order as usize)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Number 150 here deduced experimentally let envelopes = envelopes
.iter()
.map(|envelope| agb_tracker_interop::Envelope {
amount: &envelope.amounts,
sustain: envelope.sustain,
loop_start: envelope.loop_start,
loop_end: envelope.loop_end,
})
.collect::<Vec<_>>();
let frames_per_tick = bpm_to_frames_per_tick(module.default_bpm as u32); let frames_per_tick = bpm_to_frames_per_tick(module.default_bpm as u32);
let ticks_per_step = module.default_tempo; let ticks_per_step = module.default_tempo;
@ -397,7 +417,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
patterns: &patterns, patterns: &patterns,
num_channels: module.get_num_channels(), num_channels: module.get_num_channels(),
patterns_to_play: &patterns_to_play, patterns_to_play: &patterns_to_play,
envelopes: &[], envelopes: &envelopes,
frames_per_tick, frames_per_tick,
ticks_per_step: ticks_per_step.into(), ticks_per_step: ticks_per_step.into(),
@ -408,6 +428,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
} }
fn bpm_to_frames_per_tick(bpm: u32) -> Num<u32, 8> { fn bpm_to_frames_per_tick(bpm: u32) -> Num<u32, 8> {
// Number 150 here deduced experimentally
Num::<u32, 8>::new(150) / bpm Num::<u32, 8>::new(150) / bpm
} }
@ -459,3 +480,16 @@ const AMEGA_FREQUENCIES: &[u32] = &[
524, 520, 516, 513, 508, 505, 502, 498, 494, 491, 487, 484, 480, 477, 474, 470, 467, 463, 460, 524, 520, 516, 513, 508, 505, 502, 498, 494, 491, 487, 484, 480, 477, 474, 470, 467, 463, 460,
457, 457,
]; ];
struct EnvelopeData {
amounts: Vec<Num<i16, 8>>,
sustain: Option<usize>,
loop_start: Option<usize>,
loop_end: Option<usize>,
}
impl From<&xmrs::envelope::Envelope> for EnvelopeData {
fn from(value: &xmrs::envelope::Envelope) -> Self {
todo!()
}
}