Skip to content

Commit

Permalink
testing and experimenting with audio
Browse files Browse the repository at this point in the history
  • Loading branch information
pillowtrucker committed Jan 25, 2024
1 parent 2357b8a commit ea2b4ec
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "brainworms_farting_noises/libymfm.wasm"]
path = brainworms_farting_noises/libymfm.wasm
url = git@github.com:pillowtrucker/libymfm.wasm.git
[submodule "furnace"]
path = furnace
url = git@github.com:pillowtrucker/furnace.git
168 changes: 165 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion brainworms_farting_noises/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"

[dependencies]
cubeb = {git = "https://github.com/mozilla/cubeb-rs"}
libymfm = {path = "./libymfm.wasm"}
libymfm = {path = "./libymfm.wasm"}
ustr = "1"
glicol_synth = "0.13"
101 changes: 92 additions & 9 deletions brainworms_farting_noises/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,97 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
use std::{
fs::File,
io::Read,
ptr::slice_from_raw_parts,
sync::{Arc, Condvar, Mutex},
};

use cubeb::{Context, Result, StereoFrame};
use libymfm::{driver::VgmPlay, sound::SoundSlot};
const SAMPLE_FREQUENCY: u32 = 48_000;
const STREAM_FORMAT: cubeb::SampleFormat = cubeb::SampleFormat::Float32LE;
pub fn init(ctx_name: &str) -> Result<Context> {
let ctx_name = ustr::ustr(ctx_name);
Context::init(Some(ctx_name.as_cstr()), None)
}
const MAX_SAMPLE_SIZE: usize = 2048;

pub async fn play(filepath: &str) -> Result<()> {
let ctx = init("booger")?;
let params = cubeb::StreamParamsBuilder::new()
.format(STREAM_FORMAT)
.rate(SAMPLE_FREQUENCY)
.channels(2)
.layout(cubeb::ChannelLayout::STEREO)
.take();

let mut position = 0u32;

let mut builder = cubeb::StreamBuilder::<StereoFrame<f32>>::new();

#[cfg(test)]
mod tests {
use super::*;
let mut file = File::open(filepath).unwrap();
let mut buffer = Vec::new();
let _ = file.read_to_end(&mut buffer).unwrap();

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
// read vgm
let mut vgmplay = VgmPlay::new(
SoundSlot::new(SAMPLE_FREQUENCY, SAMPLE_FREQUENCY, MAX_SAMPLE_SIZE),
&buffer,
)
.unwrap();
let mut sampling_l;
let mut sampling_r;

let mut out_l = Vec::<f32>::with_capacity(MAX_SAMPLE_SIZE * 2);
let mut out_r = Vec::<f32>::with_capacity(MAX_SAMPLE_SIZE * 2);
let cv_playback_ended = Arc::new((Mutex::new(false), Condvar::new()));
let cv_playback_ended_inside_copy = Arc::clone(&cv_playback_ended);

#[allow(clippy::absurd_extreme_comparisons)]
while vgmplay.play(false) <= 0 {
unsafe {
sampling_l = slice_from_raw_parts(vgmplay.get_sampling_l_ref(), MAX_SAMPLE_SIZE)
.as_ref()
.unwrap();
sampling_r = slice_from_raw_parts(vgmplay.get_sampling_r_ref(), MAX_SAMPLE_SIZE)
.as_ref()
.unwrap();
}
out_l.extend_from_slice(sampling_l);
out_r.extend_from_slice(sampling_r);
}
builder
.name("Cubeb stereo")
.default_output(&params)
.latency(0x1000)
.data_callback(move |_, output| {
for f in output.iter_mut() {
f.l = out_l[position as usize];
f.r = out_r[position as usize];
position += 1;
}
output.len() as isize
})
.state_callback(move |state| {
println!("stream {:?}", state);
match state {
cubeb::State::Started => {}
cubeb::State::Stopped => {}
cubeb::State::Drained => {
let (lock, cvar) = &*cv_playback_ended_inside_copy;
let mut playback_ended = lock.lock().unwrap();
*playback_ended = true;
cvar.notify_one();
}
cubeb::State::Error => panic!("playback error"),
}
});

let stream = builder.init(&ctx).expect("Failed to create cubeb stream");

stream.start()?;
let (lock, cvar) = &*cv_playback_ended;
let _guard = cvar
.wait_while(lock.lock().unwrap(), |ended| !*ended)
.unwrap();
Ok(())
}
2 changes: 1 addition & 1 deletion brainworms_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod the_great_mind_palace_of_theatrical_arts;
// the reexports suck but this stays until I can focus on wrapping this

pub use brainworms_arson::{self, anyhow, egui, egui_winit, nanorand};
pub use brainworms_farting_noises;
pub use cfg_if::cfg_if;

use egui::{Color32, TextStyle, Visuals};
pub use glam;
use glam::{Mat3A, Vec3};
Expand Down
1 change: 1 addition & 0 deletions furnace
Submodule furnace added at 8ca655

0 comments on commit ea2b4ec

Please sign in to comment.