Attempt to make it so that the sound converter doesn't create too many tokens

This commit is contained in:
Gwilym Kuiper 2021-10-17 23:22:36 +01:00
parent 66e201ae39
commit eb26a21629
4 changed files with 59 additions and 5 deletions

View file

@ -9,6 +9,7 @@ dependencies = [
"hound",
"proc-macro2",
"quote",
"siphasher",
"syn",
]
@ -36,6 +37,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "siphasher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]]
name = "syn"
version = "1.0.80"

View file

@ -6,6 +6,14 @@ edition = "2018"
license = "MPL-2.0"
description = "Library for converting wavs for use on the Game Boy Advance"
[profile.dev]
opt-level = 3
debug = true
[profile.release]
lto = true
debug = true
[lib]
proc-macro = true
@ -13,4 +21,5 @@ proc-macro = true
hound = "3.4.0"
syn = "1.0.73"
proc-macro2 = "1.0.27"
quote = "1.0.9"
quote = "1.0.9"
siphasher = "0.3.7"

View file

@ -1,7 +1,13 @@
use hound;
use proc_macro::TokenStream;
use quote::quote;
use std::path::Path;
use siphasher::sip::SipHasher;
use std::{
fs::File,
hash::{Hash, Hasher},
io::Write,
path::Path,
};
use syn::parse_macro_input;
#[proc_macro]
@ -26,13 +32,31 @@ pub fn include_wav(input: TokenStream) -> TokenStream {
let samples = samples_from_reader(wav_reader);
let out_file_path_include = {
let out_dir = std::env::var("OUT_DIR").expect("Expected OUT_DIR");
let out_filename = get_out_filename(&path);
let out_file_path = Path::new(&out_dir).with_file_name(&out_filename);
let mut out_file = File::create(&out_file_path).expect("Failed to open file for writing");
out_file
.write_all(&samples.collect::<Vec<_>>())
.expect("Failed to write to temporary file");
out_file_path
}
.clone()
.canonicalize()
.expect("Failed to canonicalize");
let out_file_path_include = out_file_path_include.to_string_lossy();
let result = quote! {
{
const _: &[u8] = include_bytes!(#include_path);
&[
#(#samples),*
]
include_bytes!(#out_file_path_include)
}
};
@ -59,3 +83,10 @@ where
),
}
}
fn get_out_filename(path: &Path) -> String {
let mut hasher = SipHasher::new();
path.hash(&mut hasher);
format!("{}.raw", hasher.finish())
}

7
agb/Cargo.lock generated
View file

@ -47,6 +47,7 @@ dependencies = [
"hound",
"proc-macro2",
"quote",
"siphasher",
"syn",
]
@ -289,6 +290,12 @@ dependencies = [
"syn",
]
[[package]]
name = "siphasher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]]
name = "syn"
version = "1.0.78"