Generate lookup and add waveform to interop

This commit is contained in:
Gwilym Inzani 2024-07-10 10:28:15 +01:00
parent 9c2d02f161
commit 0daeba1876
5 changed files with 82 additions and 0 deletions

View file

@ -77,6 +77,15 @@ pub enum PatternEffect {
PitchBend(Num<u32, 8>),
}
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum Waveform {
#[default]
None,
Sine,
Saw,
Square,
}
#[cfg(feature = "quote")]
impl quote::ToTokens for Track {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
@ -333,3 +342,21 @@ impl quote::ToTokens for PatternEffect {
});
}
}
#[cfg(feature = "quote")]
impl quote::ToTokens for Waveform {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
let name = match self {
Waveform::None => quote!(None),
Waveform::Sine => quote!(Sine),
Waveform::Saw => quote!(Saw),
Waveform::Square => quote!(Square),
};
tokens.append_all(quote! {
agb_tracker::__private::agb_tracker_interop::Waveform::#name
});
}
}

View file

@ -23,6 +23,9 @@ agb = { version = "0.20.5", path = "../../agb", optional = true }
agb_fixnum = { version = "0.20.5", path = "../../agb-fixnum" }
agb_tracker_interop = { version = "0.20.5", path = "../agb-tracker-interop", default-features = false }
[build-dependencies]
agb_fixnum = { version = "0.20.5", path = "../../agb-fixnum" }
[profile.dev]
opt-level = 3
debug = true

View file

@ -0,0 +1,48 @@
use agb_fixnum::Num;
use std::env;
use std::fs;
use std::path::Path;
fn main() {
let sine = (0..64).map(|i| (Num::<i32, 8>::new(i) / 64).sin());
let square = (0..64).map(|i| {
if i < 32 {
Num::<i32, 8>::new(-1)
} else {
Num::<i32, 8>::new(1)
}
});
let saw = (0..64).map(|i| (Num::<i32, 8>::new(i) - 32) / 32);
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("lookups.rs");
fs::write(
&dest_path,
format!(
"
pub(crate) static SINE_LOOKUP: [agb_fixnum::Num<i32, 8>; 64] = [{sine_lookup}];
pub(crate) static SQUARE_LOOKUP: [agb_fixnum::Num<i32, 8>; 64] = [{square_lookup}];
pub(crate) static SAW_LOOKUP: [agb_fixnum::Num<i32, 8>; 64] = [{saw_lookup}];
",
sine_lookup = gen_lookup(sine),
square_lookup = gen_lookup(square),
saw_lookup = gen_lookup(saw),
),
)
.unwrap();
println!("cargo::rerun-if-changed=build.rs");
}
fn gen_lookup(input: impl IntoIterator<Item = Num<i32, 8>>) -> String {
let output: Vec<_> = input
.into_iter()
.map(|v| format!("agb_fixnum::Num::from_raw({})", v.to_raw()))
.collect();
output.join(", ")
}

View file

@ -65,6 +65,7 @@
extern crate alloc;
mod lookups;
mod mixer;
use agb_tracker_interop::{PatternEffect, Sample};

View file

@ -0,0 +1,3 @@
pub(crate) mod lookups {
include!(concat!(env!("OUT_DIR"), "/lookups.rs"));
}