First attempt at writing the generator for this

This commit is contained in:
Gwilym Kuiper 2021-10-17 21:59:01 +01:00
parent 6f804d884b
commit 58262bf0f5
3 changed files with 112 additions and 0 deletions

54
agb-sound-converter/Cargo.lock generated Normal file
View file

@ -0,0 +1,54 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "agb_sound_converter"
version = "0.1.0"
dependencies = [
"hound",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "hound"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549"
[[package]]
name = "proc-macro2"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"

View file

@ -0,0 +1,16 @@
[package]
name = "agb_sound_converter"
version = "0.1.0"
authors = ["Gwilym Kuiper <gw@ilym.me>"]
edition = "2018"
license = "MPL-2.0"
description = "Library for converting wavs for use on the Game Boy Advance"
[lib]
proc-macro = true
[dependencies]
hound = "3.4.0"
syn = "1.0.73"
proc-macro2 = "1.0.27"
quote = "1.0.9"

View file

@ -0,0 +1,42 @@
use hound;
use proc_macro::TokenStream;
use quote::quote;
use std::path::Path;
use syn::parse_macro_input;
#[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();
let mut wav_reader = hound::WavReader::open(&path)
.unwrap_or_else(|_| panic!("Failed to load file {}", include_path));
assert_eq!(
wav_reader.spec().sample_rate,
10512,
"agb currently only supports sample rate of 10512Hz"
);
let samples = wav_reader
.samples::<i8>()
.map(|sample| sample.unwrap() as u8);
let result = quote! {
{
const _: &[u8] = include_bytes!(#include_path);
&[
#(#samples),*
]
}
};
TokenStream::from(result)
}