Skip to content

Commit

Permalink
Hold the queue in the caller
Browse files Browse the repository at this point in the history
Instead of sleeping and then dropping the queue in `play_sound`, pass the
queue back to the caller for safekeeping.  Not pretty, but it makes the
program work as expected.

I think AudioQueue may just be broken.  I would expect to be able to call
`AudioQueue.queue_audio` repeatedly on the same object and have it play
the sounds in sequence, or immediately, if no sound is currently playing.
That's how it works on macOS.  But on Linux (with PulseAudio), only the
first `queue_audio` call plays anything, and subsequent calls are silent.
Several variations of calls to `.pause`, `.resume`, and `.clear`, didn't
improve matters.

So the fix is to make a new queue for each sound and make sure it isn't
dropped until that sound has finished playing.
  • Loading branch information
piki committed Jan 29, 2024
1 parent 5e7b186 commit f21e522
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/bin/letters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::HashMap;
use std::time::Duration;
use sdl2::{ttf::{self, Font}, pixels::Color, event::Event, keyboard::Mod, render::Canvas, video::Window, rect::{Rect, Point}, audio::{AudioSpecWAV, AudioSpecDesired}, Sdl};
use sdl2::{ttf::{self, Font}, pixels::Color, event::Event, keyboard::Mod, render::Canvas, video::Window, rect::{Rect, Point}, audio::{AudioQueue, AudioSpecDesired, AudioSpecWAV}, Sdl};

fn main() {
let sdl = sdl2::init().unwrap();
Expand All @@ -15,6 +14,8 @@ fn main() {
canvas.clear();
canvas.present();

let mut queue: Option<AudioQueue<u8>> = None;

for event in sdl.event_pump().unwrap().wait_iter() {
match event {
Event::Quit { timestamp: _ } => { break },
Expand All @@ -38,7 +39,7 @@ fn main() {
// display and play sound for any letter or number
if c.is_alphanumeric() {
draw_letter(&mut canvas, &font, c);
play_sound(&sdl, sounds.get(&c).unwrap());
queue = Some(play_sound(&sdl, sounds.get(&c).unwrap()));
}
}
}
Expand All @@ -47,7 +48,7 @@ fn main() {
};
}

println!("quit");
println!("quit: {}", queue.is_some());
}

fn draw_letter(canvas: &mut Canvas<Window>, font: &Font, c: char) {
Expand Down Expand Up @@ -85,7 +86,7 @@ fn init_audio() -> HashMap<char, AudioSpecWAV> {
sounds
}

fn play_sound(sdl: &Sdl, sound: &AudioSpecWAV) {
fn play_sound(sdl: &Sdl, sound: &AudioSpecWAV) -> AudioQueue<u8> {
let desired = AudioSpecDesired{
freq: Some(sound.freq),
channels: Some(sound.channels),
Expand All @@ -95,6 +96,5 @@ fn play_sound(sdl: &Sdl, sound: &AudioSpecWAV) {
let sound_queue = audio.open_queue(None, &desired).unwrap();
sound_queue.queue_audio(sound.buffer()).unwrap();
sound_queue.resume();
let audio_len = sound.buffer().len(); // bytes
std::thread::sleep(Duration::from_millis(audio_len as u64 * 1000 / sound.freq as u64 / sound.channels as u64));
sound_queue
}

0 comments on commit f21e522

Please sign in to comment.