add audio
This commit is contained in:
+114
-1
@@ -1,14 +1,22 @@
|
||||
use crate::model::Color;
|
||||
use crate::peripherals::{Gfx, InputSignals, Pad, Touch};
|
||||
use crate::peripherals::{Gfx, InputSignals, Pad, Touch, Audio, LoopMode};
|
||||
use std::sync::Arc;
|
||||
use crate::model::Sample;
|
||||
|
||||
/// PROMETEU "hardware lógico" (v0).
|
||||
/// O Host alimenta INPUT SIGNALS e chama `step_frame()` em 60Hz.
|
||||
pub struct Machine {
|
||||
pub gfx: Gfx,
|
||||
pub audio: Audio,
|
||||
pub pad: Pad,
|
||||
pub touch: Touch,
|
||||
pub frame_index: u64,
|
||||
pub last_frame_cpu_time_us: u64,
|
||||
|
||||
// Assets de exemplo
|
||||
pub sample_square: Option<Arc<Sample>>,
|
||||
pub sample_kick: Option<Arc<Sample>>,
|
||||
pub sample_snare: Option<Arc<Sample>>,
|
||||
}
|
||||
|
||||
impl Machine {
|
||||
@@ -18,10 +26,14 @@ impl Machine {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
gfx: Gfx::new(Self::W, Self::H),
|
||||
audio: Audio::new(),
|
||||
pad: Pad::default(),
|
||||
touch: Touch::default(),
|
||||
frame_index: 0,
|
||||
last_frame_cpu_time_us: 0,
|
||||
sample_square: None,
|
||||
sample_kick: None,
|
||||
sample_snare: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +52,11 @@ impl Machine {
|
||||
pub fn begin_frame(&mut self, signals: &InputSignals) {
|
||||
self.frame_index += 1;
|
||||
|
||||
// Limpa comandos de áudio do frame anterior.
|
||||
// Nota: O Host deve consumir esses comandos ANTES ou DURANTE o step_frame se quiser processá-los.
|
||||
// Como o Host atual consome logo após o step_frame, limpar aqui no início do PRÓXIMO frame está correto.
|
||||
self.audio.clear_commands();
|
||||
|
||||
// Flags transitórias do TOUCH devem durar 1 frame.
|
||||
self.touch.begin_frame(signals);
|
||||
|
||||
@@ -178,6 +195,91 @@ impl Machine {
|
||||
// Se soltar o Start, podemos pausar algo futuramente
|
||||
}
|
||||
|
||||
// --- DEMO DE ÁUDIO ---
|
||||
// Inicializa assets de áudio se não existirem
|
||||
if self.sample_square.is_none() {
|
||||
// 1. Onda Quadrada com Loop (Instrumento)
|
||||
let freq = 440.0;
|
||||
let sample_rate = 44100;
|
||||
let period = (sample_rate as f32 / freq) as usize;
|
||||
let mut data = Vec::with_capacity(period);
|
||||
for i in 0..period {
|
||||
data.push(if i < period / 2 { 4000 } else { -4000 });
|
||||
}
|
||||
self.sample_square = Some(Arc::new(Sample::new(sample_rate, data).with_loop(0, period as u32)));
|
||||
|
||||
// 2. "Kick" simples (Pitch descent)
|
||||
let mut kick_data = Vec::new();
|
||||
for i in 0..4000 {
|
||||
let t = i as f32 / 4000.0;
|
||||
let f = 100.0 * (1.0 - t * 0.9);
|
||||
let val = (i as f32 * f * 2.0 * std::f32::consts::PI / 44100.0).sin();
|
||||
kick_data.push((val * 12000.0 * (1.0 - t)) as i16);
|
||||
}
|
||||
self.sample_kick = Some(Arc::new(Sample::new(44100, kick_data)));
|
||||
|
||||
// 3. "Snare" simples (White noise)
|
||||
let mut snare_data = Vec::new();
|
||||
for i in 0..3000 {
|
||||
let t = i as f32 / 3000.0;
|
||||
let noise = Self::rand_f32(i as u32) * 2.0 - 1.0;
|
||||
snare_data.push((noise * 8000.0 * (1.0 - t)) as i16);
|
||||
}
|
||||
self.sample_snare = Some(Arc::new(Sample::new(44100, snare_data)));
|
||||
}
|
||||
|
||||
// --- SEQUENCER SIMPLES (60Hz) ---
|
||||
let bpm = 125;
|
||||
let frames_per_beat = (60 * 60) / bpm;
|
||||
let frames_per_step = frames_per_beat / 4; // 16th notes
|
||||
|
||||
let step = (self.frame_index / frames_per_step) % 16;
|
||||
let is_new_step = self.frame_index % frames_per_step == 0;
|
||||
|
||||
if is_new_step {
|
||||
// Bassline (Square)
|
||||
// Notas: C-2, C-2, Eb2, F-2, C-2, C-2, G-2, Bb2...
|
||||
let melody = [
|
||||
36, 36, 39, 41,
|
||||
36, 36, 43, 46,
|
||||
36, 36, 39, 41,
|
||||
34, 35, 36, 0,
|
||||
];
|
||||
|
||||
let note = melody[step as usize];
|
||||
if note > 0 {
|
||||
// Cálculo de pitch otimizado (tabela simples ou aproximação rápida se necessário,
|
||||
// mas powf uma vez por step está ok. O problema é se fosse todo frame)
|
||||
let freq = 440.0 * 2.0f64.powf((note as f64 - 69.0) / 12.0);
|
||||
let pitch = freq / 440.0;
|
||||
if let Some(s) = &self.sample_square {
|
||||
self.audio.play(s.clone(), 0, 160, 100, pitch, 0, LoopMode::On);
|
||||
}
|
||||
}
|
||||
|
||||
// Drums
|
||||
let kick_pattern = 0b0101100100011001u16; // Invertido para facilitar bitshift
|
||||
let snare_pattern = 0b0001000000001000u16;
|
||||
|
||||
if (kick_pattern >> step) & 1 == 1 {
|
||||
if let Some(s) = &self.sample_kick {
|
||||
self.audio.play(s.clone(), 1, 255, 127, 1.0, 1, LoopMode::Off);
|
||||
}
|
||||
}
|
||||
if (snare_pattern >> step) & 1 == 1 {
|
||||
if let Some(s) = &self.sample_snare {
|
||||
self.audio.play(s.clone(), 2, 140, 150, 1.0, 1, LoopMode::Off);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Toca um "beep" extra se apertar A (mantendo interatividade)
|
||||
if self.pad.a.pressed {
|
||||
if let Some(s) = &self.sample_square {
|
||||
self.audio.play(s.clone(), 3, 255, 60, 2.0, 10, LoopMode::Off);
|
||||
}
|
||||
}
|
||||
|
||||
// Post-FX Fade Pulsante
|
||||
let pulse = (self.frame_index / 4) % 64;
|
||||
let level = if pulse > 31 { 63 - pulse } else { pulse };
|
||||
@@ -190,4 +292,15 @@ impl Machine {
|
||||
pub fn end_frame(&mut self) {
|
||||
self.gfx.present();
|
||||
}
|
||||
|
||||
/// Helper para noise determinístico
|
||||
fn rand_f32(seed: u32) -> f32 {
|
||||
let mut x = seed.wrapping_add(0x9E3779B9);
|
||||
x = x ^ (x >> 16);
|
||||
x = x.wrapping_mul(0x85EBCA6B);
|
||||
x = x ^ (x >> 13);
|
||||
x = x.wrapping_mul(0xC2B2AE35);
|
||||
x = x ^ (x >> 16);
|
||||
(x as f32) / (u32::MAX as f32)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ mod tile;
|
||||
mod tile_layer;
|
||||
mod tile_bank;
|
||||
mod sprite;
|
||||
mod sample;
|
||||
|
||||
pub use button::Button;
|
||||
pub use color::Color;
|
||||
@@ -11,3 +12,4 @@ pub use tile::Tile;
|
||||
pub use tile_bank::{TileBank, TileSize};
|
||||
pub use tile_layer::{HudTileLayer, ScrollableTileLayer, TileMap};
|
||||
pub use sprite::Sprite;
|
||||
pub use sample::Sample;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
pub struct Sample {
|
||||
pub sample_rate: u32,
|
||||
pub data: Vec<i16>,
|
||||
pub loop_start: Option<u32>,
|
||||
pub loop_end: Option<u32>,
|
||||
}
|
||||
|
||||
impl Sample {
|
||||
pub fn new(sample_rate: u32, data: Vec<i16>) -> Self {
|
||||
Self {
|
||||
sample_rate,
|
||||
data,
|
||||
loop_start: None,
|
||||
loop_end: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_loop(mut self, start: u32, end: u32) -> Self {
|
||||
self.loop_start = Some(start);
|
||||
self.loop_end = Some(end);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn frames_len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
use crate::model::Sample;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub const MAX_CHANNELS: usize = 16;
|
||||
pub const OUTPUT_SAMPLE_RATE: u32 = 48000;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum LoopMode {
|
||||
Off,
|
||||
On,
|
||||
}
|
||||
|
||||
pub struct Channel {
|
||||
pub sample: Option<Arc<Sample>>,
|
||||
pub pos: f64,
|
||||
pub pitch: f64,
|
||||
pub volume: u8, // 0..255
|
||||
pub pan: u8, // 0..255
|
||||
pub loop_mode: LoopMode,
|
||||
pub priority: u8,
|
||||
}
|
||||
|
||||
impl Default for Channel {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sample: None,
|
||||
pos: 0.0,
|
||||
pitch: 1.0,
|
||||
volume: 255,
|
||||
pan: 127,
|
||||
loop_mode: LoopMode::Off,
|
||||
priority: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum AudioCommand {
|
||||
Play {
|
||||
sample: Arc<Sample>,
|
||||
voice_id: usize,
|
||||
volume: u8,
|
||||
pan: u8,
|
||||
pitch: f64,
|
||||
priority: u8,
|
||||
loop_mode: LoopMode,
|
||||
},
|
||||
Stop {
|
||||
voice_id: usize,
|
||||
},
|
||||
SetVolume {
|
||||
voice_id: usize,
|
||||
volume: u8,
|
||||
},
|
||||
SetPan {
|
||||
voice_id: usize,
|
||||
pan: u8,
|
||||
},
|
||||
SetPitch {
|
||||
voice_id: usize,
|
||||
pitch: f64,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct Audio {
|
||||
pub voices: [Channel; MAX_CHANNELS],
|
||||
pub commands: Vec<AudioCommand>,
|
||||
}
|
||||
|
||||
impl Audio {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
voices: Default::default(),
|
||||
commands: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn play(&mut self, sample: Arc<Sample>, voice_id: usize, volume: u8, pan: u8, pitch: f64, priority: u8, loop_mode: LoopMode) {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.commands.push(AudioCommand::Play {
|
||||
sample,
|
||||
voice_id,
|
||||
volume,
|
||||
pan,
|
||||
pitch,
|
||||
priority,
|
||||
loop_mode,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&mut self, voice_id: usize) {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.commands.push(AudioCommand::Stop { voice_id });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_volume(&mut self, voice_id: usize, volume: u8) {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.commands.push(AudioCommand::SetVolume { voice_id, volume });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_pan(&mut self, voice_id: usize, pan: u8) {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.commands.push(AudioCommand::SetPan { voice_id, pan });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_pitch(&mut self, voice_id: usize, pitch: f64) {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.commands.push(AudioCommand::SetPitch { voice_id, pitch });
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_playing(&self, voice_id: usize) -> bool {
|
||||
if voice_id < MAX_CHANNELS {
|
||||
self.voices[voice_id].sample.is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Limpa a fila de comandos. O Host deve consumir isso a cada frame.
|
||||
pub fn clear_commands(&mut self) {
|
||||
self.commands.clear();
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,9 @@ pub struct Gfx {
|
||||
pub scene_fade_color: Color,
|
||||
pub hud_fade_level: u8, // 0..31
|
||||
pub hud_fade_color: Color,
|
||||
|
||||
// Cache de prioridades para evitar alocações por frame
|
||||
priority_buckets: [Vec<usize>; 5],
|
||||
}
|
||||
|
||||
impl Gfx {
|
||||
@@ -65,6 +68,13 @@ impl Gfx {
|
||||
scene_fade_color: Color::BLACK,
|
||||
hud_fade_level: 31,
|
||||
hud_fade_color: Color::BLACK,
|
||||
priority_buckets: [
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,33 +135,29 @@ impl Gfx {
|
||||
/// Segue a ordem de prioridade do manual (Capítulo 4.11).
|
||||
pub fn render_all(&mut self) {
|
||||
// 0. Fase de Preparação: Organiza quem deve ser desenhado em cada camada
|
||||
// Criamos listas de índices para evitar percorrer os 512 sprites 5 vezes
|
||||
let mut priority_buckets: [Vec<usize>; 5] = [
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
Vec::with_capacity(128),
|
||||
];
|
||||
// Limpa os buckets sem desalocar memória
|
||||
for bucket in self.priority_buckets.iter_mut() {
|
||||
bucket.clear();
|
||||
}
|
||||
|
||||
for (idx, sprite) in self.sprites.iter().enumerate() {
|
||||
if sprite.active && sprite.priority < 5 {
|
||||
priority_buckets[sprite.priority as usize].push(idx);
|
||||
self.priority_buckets[sprite.priority as usize].push(idx);
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Sprites de prioridade 0 (Atrás da Layer 0 - o fundo do fundo)
|
||||
self.draw_bucket(&priority_buckets[0]);
|
||||
Self::draw_bucket_on_buffer(&mut self.back, self.w, self.h, &self.priority_buckets[0], &self.sprites, &self.banks);
|
||||
|
||||
for i in 0..self.layers.len() {
|
||||
// 2. Layers de jogo (0 a 3)
|
||||
self.render_layer(i);
|
||||
let bank_id = self.layers[i].bank_id as usize;
|
||||
if let Some(Some(bank)) = self.banks.get(bank_id) {
|
||||
Self::draw_tile_map(&mut self.back, self.w, self.h, &self.layers[i].map, bank, self.layers[i].scroll_x, self.layers[i].scroll_y);
|
||||
}
|
||||
|
||||
// 3. Sprites de acordo com prioridade
|
||||
// i=0 desenha sprites priority 1 (sobre layer 0)
|
||||
// i=1 desenha sprites priority 2 (sobre layer 1)
|
||||
// i=2 desenha sprites priority 3 (sobre layer 2)
|
||||
// i=3 desenha sprites priority 4 (sobre layer 3 - à frente de tudo)
|
||||
self.draw_bucket(&priority_buckets[i + 1]);
|
||||
Self::draw_bucket_on_buffer(&mut self.back, self.w, self.h, &self.priority_buckets[i + 1], &self.sprites, &self.banks);
|
||||
}
|
||||
|
||||
// 4. Aplica Scene Fade (Afeta tudo desenhado até agora)
|
||||
@@ -262,15 +268,20 @@ impl Gfx {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_bucket(&mut self, bucket: &[usize]) {
|
||||
fn draw_bucket_on_buffer(
|
||||
back: &mut [u16],
|
||||
screen_w: usize,
|
||||
screen_h: usize,
|
||||
bucket: &[usize],
|
||||
sprites: &[Sprite],
|
||||
banks: &[Option<TileBank>],
|
||||
) {
|
||||
for &idx in bucket {
|
||||
let sprite = &self.sprites[idx];
|
||||
let bank = match self.banks.get(sprite.bank_id as usize) {
|
||||
Some(Some(b)) => b,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
Self::draw_sprite_pixel_by_pixel(&mut self.back, self.w, self.h, sprite, bank);
|
||||
let s = &sprites[idx];
|
||||
let bank_id = s.bank_id as usize;
|
||||
if let Some(Some(bank)) = banks.get(bank_id) {
|
||||
Self::draw_sprite_pixel_by_pixel(back, screen_w, screen_h, s, bank);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@ mod gfx;
|
||||
mod pad;
|
||||
mod touch;
|
||||
mod input_signal;
|
||||
mod audio;
|
||||
|
||||
pub use gfx::Gfx;
|
||||
pub use gfx::BlendMode;
|
||||
pub use input_signal::InputSignals;
|
||||
pub use pad::Pad;
|
||||
pub use touch::Touch;
|
||||
pub use audio::{Audio, Channel, AudioCommand, LoopMode, MAX_CHANNELS, OUTPUT_SAMPLE_RATE};
|
||||
|
||||
@@ -6,4 +6,6 @@ edition = "2021"
|
||||
[dependencies]
|
||||
prometeu-core = { path = "../core" }
|
||||
winit = "0.30.12"
|
||||
pixels = "0.15.0"
|
||||
pixels = "0.15.0"
|
||||
cpal = "0.15.3"
|
||||
ringbuf = "0.4.7"
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user