temp record audio

This commit is contained in:
Alex Janka 2023-02-18 08:35:40 +11:00
parent cea9262218
commit 433c9bae6c
4 changed files with 49 additions and 2 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target
/test-roms
/bootrom
/wavs

7
Cargo.lock generated
View file

@ -413,6 +413,7 @@ dependencies = [
"cpal",
"futures",
"gilrs",
"hound",
"minifb",
"rand",
"ringbuf",
@ -491,6 +492,12 @@ dependencies = [
"libc",
]
[[package]]
name = "hound"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d13cdbd5dbb29f9c88095bbdc2590c9cba0d0a1269b983fef6b2cdd7e9f4db1"
[[package]]
name = "indexmap"
version = "1.9.2"

View file

@ -16,3 +16,4 @@ cpal = "0.15.0"
ringbuf = "0.3.2"
async-ringbuf = "0.1.2"
futures = "0.3.26"
hound = "3.5.0"

View file

@ -1,3 +1,5 @@
use std::{fs::File, io::BufWriter, path::Path};
use self::{
channels::EnvelopeMode,
types::{Channels, DacSample, Mixer, VinEnable, Volume},
@ -16,6 +18,7 @@ use cpal::{
Device, Stream, StreamConfig,
};
use futures::executor;
use hound::WavWriter;
use samplerate::{ConverterType, Samplerate};
mod channels;
@ -56,8 +59,11 @@ pub struct Apu {
stream: Option<Stream>,
send_rb: Option<AsyncHeapProducer<f32>>,
converter: Samplerate,
writer: Option<WavWriter<BufWriter<File>>>,
}
const IS_SAVING: bool = true;
impl Default for Apu {
fn default() -> Self {
let host = cpal::default_host();
@ -72,8 +78,34 @@ impl Default for Apu {
let config = supported_configs_range
.next()
.expect("no supported config?!")
.with_max_sample_rate()
.config();
.with_max_sample_rate();
let base_filename = "wavs/out";
let mut filename = format!("{base_filename}.wav");
let mut i = 0;
while Path::new(filename.as_str()).exists() {
i += 1;
filename = format!("{base_filename} ({i}).wav");
}
let writer = if IS_SAVING {
Some(
hound::WavWriter::create(
filename,
hound::WavSpec {
channels: config.channels(),
sample_rate: config.sample_rate().0,
bits_per_sample: (config.sample_format().sample_size() * 8) as u16,
sample_format: hound::SampleFormat::Float,
},
)
.unwrap(),
)
} else {
None
};
let config = config.config();
let converter = Samplerate::new(
ConverterType::Linear,
@ -96,6 +128,7 @@ impl Default for Apu {
stream: None,
send_rb: None,
converter,
writer,
}
}
}
@ -182,6 +215,11 @@ impl Apu {
)
.unwrap();
executor::block_on(send.push_slice(&converted)).unwrap();
if let Some(ref mut writer) = self.writer {
for v in converted {
writer.write_sample(v).unwrap();
}
}
}
}