Skip to content

Commit 79166e1

Browse files
committed
Add unsafe readn and writen
While actual hardware mostly only supports interleaved buffers, it can make sense to have available in some cases. For example, using pipewire with pipewire-alsa and capturing data from external jack clients using non-interleaved buffers.
1 parent f13932a commit 79166e1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/pcm.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,30 @@ impl<'a, S: Copy> IO<'a, S> {
404404
acheck!(snd_pcm_readi((self.0).0, buf.as_mut_ptr() as *mut c_void, self.to_frames(buf.len()))).map(|r| r as usize)
405405
}
406406

407+
/// Write non-interleaved frames to pcm. On success, returns number of frames written.
408+
///
409+
/// # Safety
410+
///
411+
/// Caller must ensure that the length of `bufs` is at least the number of
412+
/// channels, and that each element of bufs is a valid pointer to an array
413+
/// of at least `frames` length.
414+
pub unsafe fn writen(&self, bufs: &[*const S], frames: usize) -> Result<usize> {
415+
let frames = frames as alsa::snd_pcm_uframes_t;
416+
acheck!(snd_pcm_writen((self.0).0, bufs.as_ptr() as *mut *mut c_void, frames)).map(|r| r as usize)
417+
}
418+
419+
/// Read non-interleaved frames to pcm. On success, returns number of frames read.
420+
///
421+
/// # Safety
422+
///
423+
/// Caller must ensure that the length of `bufs` is at least the number of
424+
/// channels, and that each element of bufs is a valid pointer to an array
425+
/// of at least `frames` length.
426+
pub unsafe fn readn(&self, bufs: &mut [*mut S], frames: usize) -> Result<usize> {
427+
let frames = frames as alsa::snd_pcm_uframes_t;
428+
acheck!(snd_pcm_readn((self.0).0, bufs.as_mut_ptr() as *mut *mut c_void, frames)).map(|r| r as usize)
429+
}
430+
407431
/// Wrapper around snd_pcm_mmap_begin and snd_pcm_mmap_commit.
408432
///
409433
/// You can read/write into the sound card's buffer during the call to the closure.

0 commit comments

Comments
 (0)