agb/tracker/agb-tracker-interop/src/lib.rs

307 lines
9.5 KiB
Rust
Raw Normal View History

2023-07-12 21:18:02 +10:00
#![cfg_attr(not(feature = "std"), no_std)]
2023-07-13 00:38:09 +10:00
use agb_fixnum::Num;
#[derive(Debug)]
pub struct Track<'a> {
pub samples: &'a [Sample<'a>],
2023-08-05 08:17:59 +10:00
pub envelopes: &'a [Envelope<'a>],
2023-07-13 00:38:09 +10:00
pub pattern_data: &'a [PatternSlot],
pub patterns: &'a [Pattern],
2023-07-13 04:06:55 +10:00
pub patterns_to_play: &'a [usize],
2023-07-13 02:36:41 +10:00
2023-07-13 03:52:29 +10:00
pub num_channels: usize,
pub frames_per_tick: Num<u32, 8>,
pub ticks_per_step: u32,
pub repeat: usize,
2023-07-13 00:38:09 +10:00
}
#[derive(Debug)]
pub struct Sample<'a> {
pub data: &'a [u8],
2023-07-13 02:36:41 +10:00
pub should_loop: bool,
2023-07-17 08:12:42 +10:00
pub restart_point: u32,
2023-07-24 07:10:25 +10:00
pub volume: Num<i16, 8>,
2023-08-05 08:19:07 +10:00
pub volume_envelope: Option<usize>,
2023-07-13 00:38:09 +10:00
}
#[derive(Debug)]
pub struct Pattern {
2023-07-13 02:36:41 +10:00
pub length: usize,
pub start_position: usize,
2023-07-13 00:38:09 +10:00
}
#[derive(Debug)]
pub struct PatternSlot {
pub speed: Num<u16, 8>,
pub sample: u16,
2023-07-17 08:57:11 +10:00
pub effect1: PatternEffect,
pub effect2: PatternEffect,
2023-07-13 00:38:09 +10:00
}
2023-08-05 08:17:59 +10:00
#[derive(Debug)]
pub struct Envelope<'a> {
pub amount: &'a [Num<i16, 8>],
pub sustain: Option<usize>,
pub loop_start: Option<usize>,
pub loop_end: Option<usize>,
2023-08-05 08:17:59 +10:00
}
2023-07-17 08:57:11 +10:00
#[derive(Debug, Default)]
pub enum PatternEffect {
/// Don't play an effect
#[default]
None,
/// Stops playing the current note
Stop,
/// Plays an arpeggiation of three notes in one row, cycling betwen the current note, current note + first speed, current note + second speed
Arpeggio(Num<u16, 8>, Num<u16, 8>),
Panning(Num<i16, 4>),
2023-07-24 07:10:25 +10:00
Volume(Num<i16, 8>),
VolumeSlide(Num<i16, 8>),
FineVolumeSlide(Num<i16, 8>),
2023-07-19 22:22:26 +10:00
NoteCut(u32),
Portamento(Num<u16, 12>),
2023-07-24 06:36:02 +10:00
/// Slide each tick the first amount to at most the second amount
TonePortamento(Num<u16, 12>, Num<u16, 12>),
2023-08-05 07:30:49 +10:00
SetTicksPerStep(u32),
SetFramesPerTick(Num<u32, 8>),
2023-07-17 08:57:11 +10:00
}
2023-07-13 08:41:30 +10:00
2023-07-13 00:38:09 +10:00
#[cfg(feature = "quote")]
impl<'a> quote::ToTokens for Track<'a> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
2023-07-13 02:36:41 +10:00
let Track {
samples,
2023-08-05 08:17:59 +10:00
envelopes,
2023-07-13 02:36:41 +10:00
pattern_data,
patterns,
frames_per_tick,
2023-07-13 03:52:29 +10:00
num_channels,
2023-07-13 04:06:55 +10:00
patterns_to_play,
ticks_per_step,
repeat,
2023-07-13 02:36:41 +10:00
} = self;
2023-07-13 00:38:09 +10:00
let frames_per_tick = frames_per_tick.to_raw();
2023-07-13 00:38:09 +10:00
tokens.append_all(quote! {
{
const SAMPLES: &[agb_tracker::__private::agb_tracker_interop::Sample<'static>] = &[#(#samples),*];
const PATTERN_DATA: &[agb_tracker::__private::agb_tracker_interop::PatternSlot] = &[#(#pattern_data),*];
const PATTERNS: &[agb_tracker::__private::agb_tracker_interop::Pattern] = &[#(#patterns),*];
2023-07-13 04:06:55 +10:00
const PATTERNS_TO_PLAY: &[usize] = &[#(#patterns_to_play),*];
2023-08-05 08:17:59 +10:00
const ENVELOPES: &[agb_tracker::__private::agb_tracker_interop::Envelope<'static>] = &[#(#envelopes),*];
2023-07-13 00:38:09 +10:00
agb_tracker::Track {
2023-07-13 00:38:09 +10:00
samples: SAMPLES,
2023-08-05 08:17:59 +10:00
envelopes: ENVELOPES,
2023-07-13 00:38:09 +10:00
pattern_data: PATTERN_DATA,
patterns: PATTERNS,
2023-07-13 04:06:55 +10:00
patterns_to_play: PATTERNS_TO_PLAY,
2023-07-13 02:36:41 +10:00
frames_per_tick: agb_tracker::__private::Num::from_raw(#frames_per_tick),
2023-07-13 03:52:29 +10:00
num_channels: #num_channels,
ticks_per_step: #ticks_per_step,
repeat: #repeat,
2023-07-13 00:38:09 +10:00
}
}
})
}
}
2023-08-05 08:17:59 +10:00
#[cfg(feature = "quote")]
impl quote::ToTokens for Envelope<'_> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
let Envelope {
amount,
sustain,
loop_start,
loop_end,
} = self;
let amount = amount.iter().map(|value| {
let value = value.to_raw();
quote! { agb_tracker::__private::Num::from_raw(#value) }
2023-08-05 08:17:59 +10:00
});
let sustain = match sustain {
Some(value) => quote!(Some(#value)),
None => quote!(None),
};
let loop_start = match loop_start {
Some(value) => quote!(Some(#value)),
None => quote!(None),
};
let loop_end = match loop_end {
Some(value) => quote!(Some(#value)),
None => quote!(None),
};
tokens.append_all(quote! {
{
const AMOUNTS: &[agb_tracker::__private::Num<i16, 8>] = &[#(#amount),*];
2023-08-05 08:17:59 +10:00
agb_tracker::__private::agb_tracker_interop::Envelope {
amount: AMOUNTS,
sustain: #sustain,
loop_start: #loop_start,
loop_end: #loop_end,
}
2023-08-05 08:17:59 +10:00
}
});
}
}
2023-07-13 02:36:41 +10:00
#[cfg(feature = "quote")]
struct ByteString<'a>(&'a [u8]);
#[cfg(feature = "quote")]
impl quote::ToTokens for ByteString<'_> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::TokenStreamExt;
tokens.append(proc_macro2::Literal::byte_string(self.0));
}
}
2023-07-13 00:38:09 +10:00
#[cfg(feature = "quote")]
impl<'a> quote::ToTokens for Sample<'a> {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
2023-07-19 06:36:37 +10:00
let Sample {
data,
should_loop,
restart_point,
volume,
2023-08-05 08:19:07 +10:00
volume_envelope,
2023-07-19 06:36:37 +10:00
} = self;
2023-08-05 08:19:07 +10:00
let volume_envelope = match volume_envelope {
2023-08-05 08:17:59 +10:00
Some(index) => quote!(Some(#index)),
None => quote!(None),
};
2023-07-24 04:08:51 +10:00
let samples = ByteString(data);
2023-07-19 06:36:37 +10:00
let volume = volume.to_raw();
2023-07-13 00:38:09 +10:00
tokens.append_all(quote! {
{
2023-07-13 02:36:41 +10:00
#[repr(align(4))]
struct AlignmentWrapper<const N: usize>([u8; N]);
const SAMPLE_DATA: &[u8] = &AlignmentWrapper(*#samples).0;
2023-07-19 06:36:37 +10:00
agb_tracker::__private::agb_tracker_interop::Sample {
data: SAMPLE_DATA,
should_loop: #should_loop,
restart_point: #restart_point,
volume: agb_tracker::__private::Num::from_raw(#volume),
2023-08-05 08:19:07 +10:00
volume_envelope: #volume_envelope,
2023-07-19 06:36:37 +10:00
}
2023-07-13 00:38:09 +10:00
}
});
}
}
#[cfg(feature = "quote")]
impl quote::ToTokens for PatternSlot {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
let PatternSlot {
speed,
sample,
2023-07-17 08:57:11 +10:00
effect1,
effect2,
2023-07-13 00:38:09 +10:00
} = &self;
let speed = speed.to_raw();
tokens.append_all(quote! {
agb_tracker::__private::agb_tracker_interop::PatternSlot {
speed: agb_tracker::__private::Num::from_raw(#speed),
sample: #sample,
2023-07-17 08:57:11 +10:00
effect1: #effect1,
effect2: #effect2,
2023-07-13 00:38:09 +10:00
}
});
}
}
#[cfg(feature = "quote")]
impl quote::ToTokens for Pattern {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
2023-07-13 02:36:41 +10:00
let Pattern {
length,
start_position,
} = self;
2023-07-13 00:38:09 +10:00
tokens.append_all(quote! {
agb_tracker::__private::agb_tracker_interop::Pattern {
length: #length,
start_position: #start_position,
2023-07-13 00:38:09 +10:00
}
})
}
}
2023-07-17 08:57:11 +10:00
#[cfg(feature = "quote")]
impl quote::ToTokens for PatternEffect {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
let type_bit = match self {
PatternEffect::None => quote! { None },
PatternEffect::Stop => quote! { Stop },
PatternEffect::Arpeggio(first, second) => {
let first = first.to_raw();
let second = second.to_raw();
quote! { Arpeggio(agb_tracker::__private::Num::from_raw(#first), agb_tracker::__private::Num::from_raw(#second)) }
}
PatternEffect::Panning(panning) => {
let panning = panning.to_raw();
quote! { Panning(agb_tracker::__private::Num::from_raw(#panning))}
}
PatternEffect::Volume(volume) => {
let volume = volume.to_raw();
quote! { Volume(agb_tracker::__private::Num::from_raw(#volume))}
}
2023-07-17 09:45:58 +10:00
PatternEffect::VolumeSlide(amount) => {
let amount = amount.to_raw();
quote! { VolumeSlide(agb_tracker::__private::Num::from_raw(#amount))}
}
2023-07-24 07:03:32 +10:00
PatternEffect::FineVolumeSlide(amount) => {
let amount = amount.to_raw();
quote! { FineVolumeSlide(agb_tracker::__private::Num::from_raw(#amount))}
}
2023-07-19 22:22:26 +10:00
PatternEffect::NoteCut(wait) => quote! { NoteCut(#wait) },
2023-07-19 22:38:32 +10:00
PatternEffect::Portamento(amount) => {
let amount = amount.to_raw();
quote! { Portamento(agb_tracker::__private::Num::from_raw(#amount))}
}
2023-07-24 06:36:02 +10:00
PatternEffect::TonePortamento(amount, target) => {
let amount = amount.to_raw();
let target = target.to_raw();
quote! { TonePortamento(agb_tracker::__private::Num::from_raw(#amount), agb_tracker::__private::Num::from_raw(#target))}
}
2023-08-05 07:30:49 +10:00
PatternEffect::SetTicksPerStep(new_ticks) => {
quote! { SetTicksPerStep(#new_ticks) }
}
PatternEffect::SetFramesPerTick(new_frames_per_tick) => {
let amount = new_frames_per_tick.to_raw();
quote! { SetFramesPerTick(agb_tracker::__private::Num::from_raw(#amount)) }
}
2023-07-17 08:57:11 +10:00
};
tokens.append_all(quote! {
agb_tracker::__private::agb_tracker_interop::PatternEffect::#type_bit
});
}
}