mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
3.96k stars 348 forks source link

Reliably comparing two `ma_device_id` objects for equality #866

Closed vittorioromeo closed 1 month ago

vittorioromeo commented 2 months ago

I am trying to compare to ma_device_id objects to figure out if they refer to the same hardware device. I couldn't find any API function to compare ma_device_id objects, and I considered using std::memcmp, but I am not sure if the ma_device_id object is intended to be compared that way.

Is there a way to reliably compare two ma_device_id for equality, or -- more generally -- check if two hardware devices detected through miniaudio are the same?

vittorioromeo commented 2 months ago

BTW, using the device name is not an option as there could be multiple devices with the same name.

mackron commented 2 months ago

memcmp(&id0, &id1, sizeof(ma_device_id)) should do the job. I thought I actually had a function for comparing device IDs, but looking now it appears that no such function exists. Probably worth adding that. In any case, ma_device_id is explicitly a fixed sized struct, no matter what backends are enabled/disabled, and it should be padded with zeros, so it should be reliable. If I add a function to compare IDs, memcmp() is how I'd be doing it.

mackron commented 1 month ago

I added ma_device_id_equal() to the dev branch.