From c4c7fe876c6231e6e4f9b20ad041d0b260b609b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Freitas?= Date: Mon, 26 Aug 2024 21:22:22 +0100 Subject: [PATCH] Play silent sounds --- fpt-egui/src/main.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/fpt-egui/src/main.rs b/fpt-egui/src/main.rs index 4d1f24d..4df46e9 100644 --- a/fpt-egui/src/main.rs +++ b/fpt-egui/src/main.rs @@ -5,6 +5,8 @@ use std::sync::mpsc::{channel, Receiver, Sender}; use std::time::Duration; use clap::{Parser, ValueEnum}; +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use cpal::{Sample, SampleFormat, Stream}; use eframe::Frame; #[allow(unused_imports)] use egui::{ @@ -106,6 +108,8 @@ pub struct FPT { #[allow(dead_code)] rom_channel: (Sender>, Receiver>), + + stream: Stream, } impl Default for FPT { @@ -132,10 +136,39 @@ impl Default for FPT { bg_map_texture: None, rom_channel: channel(), + stream: play_audio(), } } } +fn play_audio() -> Stream { + let host = cpal::default_host(); + let device = host + .default_output_device() + .expect("no output device available"); + let supported_config = device + .default_output_config() + .expect("error while querying configs"); + + let sample_format = supported_config.sample_format(); + + let err_fn = |err| eprintln!("an error occurred on the output audio stream: {}", err); + let config = supported_config.into(); + let stream = match sample_format { + SampleFormat::F32 => device.build_output_stream(&config, write_silence, err_fn, None), + sample_format => panic!("Unsupported sample format '{sample_format}'"), + } + .unwrap(); + + fn write_silence(data: &mut [f32], _: &cpal::OutputCallbackInfo) { + for sample in data.iter_mut() { + *sample = Sample::EQUILIBRIUM; + } + } + + stream +} + impl FPT { /// Called once before the first frame. #[allow(unused_variables)] @@ -159,6 +192,8 @@ impl FPT { } else { fpt.gb.boot_real(); } + + fpt.stream.play().unwrap(); fpt }