Skip to content

Commit

Permalink
Merge pull request #1189 from rhysd/queue-return-result
Browse files Browse the repository at this point in the history
Return `Result` from `AudioQueue::queue` instead of `bool`
  • Loading branch information
Cobrand authored Dec 31, 2021
2 parents 443c9d9 + 5d8ba4a commit 7011c6b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/audio-queue-squarewave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() -> Result<(), String> {

let target_bytes = 48_000 * 4;
let wave = gen_wave(target_bytes);
device.queue(&wave);
device.queue_audio(&wave)?;
// Start playback
device.resume();

Expand Down
21 changes: 21 additions & 0 deletions src/sdl2/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ impl<'a, Channel: AudioFormatNum> AudioQueue<Channel> {

/// Adds data to the audio queue.
#[doc(alias = "SDL_QueueAudio")]
#[deprecated(
since = "0.36.0",
note = "Users should instead use AudioQueue::queue_audio"
)]
pub fn queue(&self, data: &[Channel]) -> bool {
let result = unsafe {
sys::SDL_QueueAudio(
Expand All @@ -761,6 +765,23 @@ impl<'a, Channel: AudioFormatNum> AudioQueue<Channel> {
result == 0
}

/// Adds data to the audio queue.
#[doc(alias = "SDL_QueueAudio")]
pub fn queue_audio(&self, data: &[Channel]) -> Result<(), String> {
let result = unsafe {
sys::SDL_QueueAudio(
self.device_id.id(),
data.as_ptr() as *const c_void,
(data.len() * mem::size_of::<Channel>()) as u32,
)
};
if result == 0 {
Ok(())
} else {
Err(get_error())
}
}

#[doc(alias = "SDL_GetQueuedAudioSize")]
pub fn size(&self) -> u32 {
unsafe { sys::SDL_GetQueuedAudioSize(self.device_id.id()) }
Expand Down

0 comments on commit 7011c6b

Please sign in to comment.