2021-10-19 07:15:11 +11:00
|
|
|
#![deny(clippy::all)]
|
|
|
|
|
2021-10-18 07:59:01 +11:00
|
|
|
use proc_macro::TokenStream;
|
2022-08-05 07:53:25 +10:00
|
|
|
use proc_macro2::Literal;
|
|
|
|
use quote::{quote, ToTokens};
|
|
|
|
use std::path::Path;
|
2021-10-18 07:59:01 +11:00
|
|
|
use syn::parse_macro_input;
|
|
|
|
|
2022-08-05 07:53:25 +10:00
|
|
|
use quote::TokenStreamExt;
|
|
|
|
struct ByteString<'a>(&'a [u8]);
|
|
|
|
impl ToTokens for ByteString<'_> {
|
|
|
|
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
|
|
|
tokens.append(Literal::byte_string(self.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 07:59:01 +11:00
|
|
|
#[proc_macro]
|
|
|
|
pub fn include_wav(input: TokenStream) -> TokenStream {
|
|
|
|
let input = parse_macro_input!(input as syn::LitStr);
|
|
|
|
|
|
|
|
let filename = input.value();
|
|
|
|
|
|
|
|
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
|
|
|
|
let path = Path::new(&root).join(&*filename);
|
|
|
|
|
|
|
|
let include_path = path.to_string_lossy();
|
|
|
|
|
2022-08-05 07:53:25 +10:00
|
|
|
let wav_reader = hound::WavReader::open(&path)
|
2023-02-08 07:32:43 +11:00
|
|
|
.unwrap_or_else(|_| panic!("Failed to load file {include_path}"));
|
2021-10-19 06:22:21 +11:00
|
|
|
|
2022-08-05 07:53:25 +10:00
|
|
|
let samples: Vec<u8> = samples_from_reader(wav_reader).collect();
|
|
|
|
let samples = ByteString(&samples);
|
2021-10-18 09:22:36 +11:00
|
|
|
|
2021-10-18 07:59:01 +11:00
|
|
|
let result = quote! {
|
|
|
|
{
|
2021-10-27 08:14:14 +11:00
|
|
|
#[repr(align(4))]
|
|
|
|
struct AlignmentWrapper<const N: usize>([u8; N]);
|
|
|
|
|
2021-10-18 07:59:01 +11:00
|
|
|
const _: &[u8] = include_bytes!(#include_path);
|
|
|
|
|
2022-08-05 07:53:25 +10:00
|
|
|
&AlignmentWrapper(*#samples).0
|
2021-10-18 07:59:01 +11:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TokenStream::from(result)
|
|
|
|
}
|
2021-10-18 08:37:50 +11:00
|
|
|
|
|
|
|
fn samples_from_reader<'a, R>(reader: hound::WavReader<R>) -> Box<dyn Iterator<Item = u8> + 'a>
|
|
|
|
where
|
|
|
|
R: std::io::Read + 'a,
|
|
|
|
{
|
|
|
|
let bitrate = reader.spec().bits_per_sample;
|
|
|
|
let reduction = bitrate - 8;
|
|
|
|
|
|
|
|
match reader.spec().sample_format {
|
|
|
|
hound::SampleFormat::Float => Box::new(
|
|
|
|
reader
|
|
|
|
.into_samples::<f32>()
|
|
|
|
.map(|sample| (sample.unwrap() * (i8::MAX as f32)) as u8),
|
|
|
|
),
|
|
|
|
hound::SampleFormat::Int => Box::new(
|
|
|
|
reader
|
|
|
|
.into_samples::<i32>()
|
|
|
|
.map(move |sample| (sample.unwrap() >> reduction) as u8),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|