add audio

This commit is contained in:
Nilton Constantino
2026-01-14 17:55:06 +00:00
parent b0f4a865ad
commit acd09a6468
11 changed files with 861 additions and 49 deletions
+132
View File
@@ -0,0 +1,132 @@
use prometeu_core::peripherals::{AudioCommand, Channel, LoopMode, MAX_CHANNELS, OUTPUT_SAMPLE_RATE};
use std::time::Duration;
pub struct AudioMixer {
voices: [Channel; MAX_CHANNELS],
pub last_processing_time: Duration,
}
impl AudioMixer {
pub fn new() -> Self {
Self {
voices: Default::default(),
last_processing_time: Duration::ZERO,
}
}
pub fn process_command(&mut self, cmd: AudioCommand) {
match cmd {
AudioCommand::Play {
sample,
voice_id,
volume,
pan,
pitch,
priority,
loop_mode,
} => {
if voice_id < MAX_CHANNELS {
self.voices[voice_id] = Channel {
sample: Some(sample),
pos: 0.0,
pitch,
volume,
pan,
loop_mode,
priority,
};
}
}
AudioCommand::Stop { voice_id } => {
if voice_id < MAX_CHANNELS {
self.voices[voice_id].sample = None;
}
}
AudioCommand::SetVolume { voice_id, volume } => {
if voice_id < MAX_CHANNELS {
self.voices[voice_id].volume = volume;
}
}
AudioCommand::SetPan { voice_id, pan } => {
if voice_id < MAX_CHANNELS {
self.voices[voice_id].pan = pan;
}
}
AudioCommand::SetPitch { voice_id, pitch } => {
if voice_id < MAX_CHANNELS {
self.voices[voice_id].pitch = pitch;
}
}
}
}
pub fn fill_buffer(&mut self, buffer: &mut [f32]) {
let start = std::time::Instant::now();
// Zera o buffer (estéreo)
for sample in buffer.iter_mut() {
*sample = 0.0;
}
for voice in self.voices.iter_mut() {
let sample_data = match &voice.sample {
Some(s) => s,
None => continue,
};
let pitch_ratio = sample_data.sample_rate as f64 / OUTPUT_SAMPLE_RATE as f64;
let step = voice.pitch * pitch_ratio;
let vol_f = voice.volume as f32 / 255.0;
let pan_f = voice.pan as f32 / 255.0;
let vol_l = vol_f * (1.0 - pan_f).sqrt();
let vol_r = vol_f * pan_f.sqrt();
for frame in buffer.chunks_exact_mut(2) {
let pos_int = voice.pos as usize;
let pos_fract = voice.pos - pos_int as f64;
if pos_int >= sample_data.data.len() {
voice.sample = None;
break;
}
// Interpolação Linear
let s1 = sample_data.data[pos_int] as f32 / 32768.0;
let s2 = if pos_int + 1 < sample_data.data.len() {
sample_data.data[pos_int + 1] as f32 / 32768.0
} else if voice.loop_mode == LoopMode::On {
let loop_start = sample_data.loop_start.unwrap_or(0) as usize;
sample_data.data[loop_start] as f32 / 32768.0
} else {
0.0
};
let sample_val = s1 + (s2 - s1) * pos_fract as f32;
frame[0] += sample_val * vol_l;
frame[1] += sample_val * vol_r;
voice.pos += step;
let end_pos = sample_data.loop_end.map(|e| e as f64).unwrap_or(sample_data.data.len() as f64);
if voice.pos >= end_pos {
if voice.loop_mode == LoopMode::On {
let loop_start = sample_data.loop_start.unwrap_or(0) as f64;
voice.pos = loop_start + (voice.pos - end_pos);
} else {
voice.sample = None;
break;
}
}
}
}
// Clamp final para evitar clipping (opcional se usarmos f32, mas bom para fidelidade)
for sample in buffer.iter_mut() {
*sample = sample.clamp(-1.0, 1.0);
}
self.last_processing_time = start.elapsed();
}
}
+98 -6
View File
@@ -1,8 +1,15 @@
use pixels::{Pixels, SurfaceTexture};
use std::time::{Duration, Instant};
use pixels::{Pixels, SurfaceTexture};
use prometeu_core::peripherals::InputSignals;
mod audio_mixer;
use audio_mixer::AudioMixer;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use prometeu_core::peripherals::{AudioCommand, InputSignals, OUTPUT_SAMPLE_RATE};
use prometeu_core::Machine;
use ringbuf::traits::{Consumer, Producer, Split};
use ringbuf::HeapRb;
use std::sync::Arc;
use winit::{
application::ApplicationHandler,
dpi::LogicalSize,
@@ -35,6 +42,12 @@ struct PrometeuApp {
last_stats_update: Instant,
frames_since_last_update: u64,
audio_load_accum_us: u64,
audio_load_samples: u64,
audio_perf_consumer: Option<ringbuf::wrap::CachingCons<Arc<HeapRb<u64>>>>,
audio_producer: Option<ringbuf::wrap::CachingProd<Arc<HeapRb<AudioCommand>>>>,
_audio_stream: Option<cpal::Stream>,
}
impl PrometeuApp {
@@ -55,9 +68,61 @@ impl PrometeuApp {
last_stats_update: Instant::now(),
frames_since_last_update: 0,
audio_load_accum_us: 0,
audio_load_samples: 0,
audio_perf_consumer: None,
audio_producer: None,
_audio_stream: None,
}
}
fn init_audio(&mut self) {
let host = cpal::default_host();
let device = host
.default_output_device()
.expect("no output device available");
let config = cpal::StreamConfig {
channels: 2,
sample_rate: cpal::SampleRate(OUTPUT_SAMPLE_RATE),
buffer_size: cpal::BufferSize::Default,
};
let rb = HeapRb::<AudioCommand>::new(1024);
let (prod, mut cons) = rb.split();
self.audio_producer = Some(prod);
let mut mixer = AudioMixer::new();
// Para passar dados de performance da thread de áudio para a principal
let audio_perf_rb = HeapRb::<u64>::new(64);
let (mut perf_prod, perf_cons) = audio_perf_rb.split();
let stream = device
.build_output_stream(
&config,
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
// Consome comandos da ringbuffer
while let Some(cmd) = cons.try_pop() {
mixer.process_command(cmd);
}
// Mixa áudio
mixer.fill_buffer(data);
// Envia tempo de processamento em microssegundos
let _ = perf_prod.try_push(mixer.last_processing_time.as_micros() as u64);
},
|err| eprintln!("audio stream error: {}", err),
None,
)
.expect("failed to build audio stream");
stream.play().expect("failed to play audio stream");
self._audio_stream = Some(stream);
self.audio_perf_consumer = Some(perf_cons);
}
fn window(&self) -> &'static Window {
self.window.expect("window not created yet")
}
@@ -82,7 +147,7 @@ impl PrometeuApp {
impl ApplicationHandler for PrometeuApp {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let attrs = WindowAttributes::default()
.with_title(format!("PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Frame: {}", 0.0, 0.0, 0))
.with_title(format!("PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {}% | Frame: {}", 0.0, 0.0, 0.0, 0))
.with_inner_size(LogicalSize::new(960.0, 540.0))
.with_min_inner_size(LogicalSize::new(320.0, 180.0));
@@ -102,6 +167,8 @@ impl ApplicationHandler for PrometeuApp {
self.pixels = Some(pixels);
self.init_audio();
event_loop.set_control_flow(ControlFlow::Poll);
}
@@ -202,10 +269,26 @@ impl ApplicationHandler for PrometeuApp {
// 🔥 O coração do determinismo: consome o tempo em fatias exatas de 60Hz
while self.accumulator >= self.frame_target_dt {
self.machine.step_frame(&self.input_signals);
// Envia comandos de áudio gerados neste frame para a thread de áudio
if let Some(producer) = &mut self.audio_producer {
for cmd in self.machine.audio.commands.drain(..) {
let _ = producer.try_push(cmd);
}
}
self.accumulator -= self.frame_target_dt;
self.frames_since_last_update += 1;
}
// Drena tempos de performance do áudio
if let Some(cons) = &mut self.audio_perf_consumer {
while let Some(us) = cons.try_pop() {
self.audio_load_accum_us += us;
self.audio_load_samples += 1;
}
}
// Atualiza estatísticas a cada 1 segundo real
let stats_elapsed = now.duration_since(self.last_stats_update);
if stats_elapsed >= Duration::from_secs(1) {
@@ -214,17 +297,26 @@ impl ApplicationHandler for PrometeuApp {
let kb = self.machine.gfx.memory_usage_bytes() as f64 / 1024.0;
let frame_budget_us = 16666.0;
let cpu_load = (self.machine.last_frame_cpu_time_us as f64 / frame_budget_us) * 100.0;
let cpu_load_core = (self.machine.last_frame_cpu_time_us as f64 / frame_budget_us) * 100.0;
let cpu_load_audio = if self.audio_load_samples > 0 {
// O load real é (tempo total processando) / (tempo total de parede).
(self.audio_load_accum_us as f64 / stats_elapsed.as_micros() as f64) * 100.0
} else {
0.0
};
let title = format!(
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {:.1}% | Frame: {}",
kb, fps, cpu_load, self.machine.frame_index
"PROMETEU | GFX: {:.1} KB | FPS: {:.1} | Load: {:.1}% (C) + {:.1}% (A) | Frame: {}",
kb, fps, cpu_load_core, cpu_load_audio, self.machine.frame_index
);
window.set_title(&title);
}
self.last_stats_update = now;
self.frames_since_last_update = 0;
self.audio_load_accum_us = 0;
self.audio_load_samples = 0;
}
self.request_redraw();