Skip to content

Conversation

@anas2660
Copy link
Contributor

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.

@anas2660 anas2660 changed the title Add unsafe readn and writen Add unsafe readn and writen Jul 11, 2025
@diwic
Copy link
Owner

diwic commented Jul 11, 2025

Thanks. So then we need readn and writen.

Can we make it safer somehow? And do we need all the mut even on the write side?

@anas2660
Copy link
Contributor Author

Can we make it safer somehow?

We would need to know the number of channels so we can check the length of the slice.

Then you could make a fully safe version by taking a slice of slices, and individually check that each inner slice is at least frames in length. You would then have to allocate an array of pointers from that on the stack, since it does not transmute.

So it is doable. I just didn't know how to get the number of channels in a sane way in there.

And do we need all the mut even on the write side?

The alsa-sys crate takes mut pointers currently

extern "C" {
    pub fn snd_pcm_readn(
        pcm: *mut snd_pcm_t,
        bufs: *mut *mut ::std::os::raw::c_void,
        size: snd_pcm_uframes_t,
    ) -> snd_pcm_sframes_t;
}

@diwic
Copy link
Owner

diwic commented Jul 11, 2025

It does not make sense for writei to take *const and writen to take *mut. This is probably a mistake on the alsa-lib side?

writen(&self, bufs: &[*const S], frames: usize) would make more sense, or am I missing something?

@anas2660
Copy link
Contributor Author

yeah my mistake, it is this definition from the alsa-sys side

extern "C" {
    pub fn snd_pcm_writen(
        pcm: *mut snd_pcm_t,
        bufs: *mut *mut ::std::os::raw::c_void,
        size: snd_pcm_uframes_t,
    ) -> snd_pcm_sframes_t;
}

So yes it is a mistake from that side. They are not marked as const from the lib-alsa side either.

snd_pcm_sframes_t snd_pcm_writen(snd_pcm_t *pcm, void **bufs, snd_pcm_uframes_t size);

We could just ignore it and risk wonky codegen. I don't think the writen will ever write to the buffers so probably fine in that aspect and also the implicit __restrict__ on mut references probably doesn't matter in this case. Up to you, I just exposed what was there in alsa-sys, but I can change it if you want.

@diwic
Copy link
Owner

diwic commented Jul 11, 2025

Yeah, change the writen function signature. Then just test both functions locally somehow so you know that they work like we expect them to. Thanks!

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.
@anas2660
Copy link
Contributor Author

Seems ok

@diwic diwic merged commit f98f7c5 into diwic:master Jul 11, 2025
1 check passed
@diwic
Copy link
Owner

diwic commented Jul 11, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants