Skip to content

Commit 8f04e4a

Browse files
committed
audio: Clean out all the bitshifting.
Hide the implementation details in something human-readable.
1 parent 01d94ca commit 8f04e4a

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

src/audio/SDL_audio.c

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,28 @@ static SDL_AudioDeviceID AssignAudioDeviceInstanceId(bool recording, bool islogi
367367

368368
bool SDL_IsAudioDevicePhysical(SDL_AudioDeviceID devid)
369369
{
370+
// bit #1 of devid is set for physical devices and unset for logical.
370371
return (devid & (1 << 1)) != 0;
371372
}
372373

374+
bool SDL_IsAudioDeviceLogical(SDL_AudioDeviceID devid)
375+
{
376+
// bit #1 of devid is set for physical devices and unset for logical.
377+
return (devid & (1 << 1)) == 0;
378+
}
379+
373380
bool SDL_IsAudioDevicePlayback(SDL_AudioDeviceID devid)
374381
{
382+
// bit #0 of devid is set for playback devices and unset for recording.
375383
return (devid & (1 << 0)) != 0;
376384
}
377385

386+
bool SDL_IsAudioDeviceRecording(SDL_AudioDeviceID devid)
387+
{
388+
// bit #0 of devid is set for playback devices and unset for recording.
389+
return (devid & (1 << 0)) == 0;
390+
}
391+
378392
static void ObtainPhysicalAudioDeviceObj(SDL_AudioDevice *device) SDL_NO_THREAD_SAFETY_ANALYSIS // !!! FIXMEL SDL_ACQUIRE
379393
{
380394
if (device) {
@@ -405,9 +419,7 @@ static SDL_LogicalAudioDevice *ObtainLogicalAudioDevice(SDL_AudioDeviceID devid,
405419
SDL_AudioDevice *device = NULL;
406420
SDL_LogicalAudioDevice *logdev = NULL;
407421

408-
// bit #1 of devid is set for physical devices and unset for logical.
409-
const bool islogical = !(devid & (1<<1));
410-
if (islogical) { // don't bother looking if it's not a logical device id value.
422+
if (SDL_IsAudioDeviceLogical(devid)) { // don't bother looking if it's not a logical device id value.
411423
SDL_LockRWLockForReading(current_audio.subsystem_rwlock);
412424
SDL_FindInHashTable(current_audio.device_hash_logical, (const void *) (uintptr_t) devid, (const void **) &logdev);
413425
if (logdev) {
@@ -452,9 +464,7 @@ static SDL_AudioDevice *ObtainPhysicalAudioDevice(SDL_AudioDeviceID devid) // !
452464
{
453465
SDL_AudioDevice *device = NULL;
454466

455-
// bit #1 of devid is set for physical devices and unset for logical.
456-
const bool islogical = !(devid & (1<<1));
457-
if (islogical) {
467+
if (SDL_IsAudioDeviceLogical(devid)) {
458468
ObtainLogicalAudioDevice(devid, &device);
459469
} else if (!SDL_GetCurrentAudioDriver()) { // (the `islogical` path, above, checks this in ObtainLogicalAudioDevice.)
460470
SDL_SetError("Audio subsystem is not initialized");
@@ -879,12 +889,8 @@ static bool SDLCALL FindLowestDeviceID(void *userdata, const SDL_HashTable *tabl
879889
{
880890
FindLowestDeviceIDData *data = (FindLowestDeviceIDData *) userdata;
881891
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
882-
// bit #0 of devid is set for playback devices and unset for recording.
883-
// bit #1 of devid is set for physical devices and unset for logical.
884-
const bool devid_recording = !(devid & (1 << 0));
885-
const bool isphysical = !!(devid & (1 << 1));
886-
SDL_assert(isphysical); // should only be iterating device_hash_physical.
887-
if ((devid_recording == data->recording) && (devid < data->highest)) {
892+
SDL_assert(SDL_IsAudioDevicePhysical(devid)); // should only be iterating device_hash_physical.
893+
if ((SDL_IsAudioDeviceRecording(devid) == data->recording) && (devid < data->highest)) {
888894
data->highest = devid;
889895
data->result = (SDL_AudioDevice *) value;
890896
SDL_assert(data->result->instance_id == devid);
@@ -1065,10 +1071,8 @@ bool SDL_InitAudio(const char *driver_name)
10651071

10661072
static bool SDLCALL DestroyOnePhysicalAudioDevice(void *userdata, const SDL_HashTable *table, const void *key, const void *value)
10671073
{
1068-
// bit #1 of devid is set for physical devices and unset for logical.
10691074
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
1070-
const bool isphysical = !!(devid & (1<<1));
1071-
SDL_assert(isphysical);
1075+
SDL_assert(SDL_IsAudioDevicePhysical(devid)); // should only be iterating device_hash_physical.
10721076
SDL_AudioDevice *dev = (SDL_AudioDevice *) value;
10731077
SDL_assert(dev->instance_id == devid);
10741078
DestroyPhysicalAudioDevice(dev);
@@ -1428,12 +1432,8 @@ static bool SDLCALL CountAudioDevices(void *userdata, const SDL_HashTable *table
14281432
{
14291433
CountAudioDevicesData *data = (CountAudioDevicesData *) userdata;
14301434
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
1431-
// bit #0 of devid is set for playback devices and unset for recording.
1432-
// bit #1 of devid is set for physical devices and unset for logical.
1433-
const bool devid_recording = !(devid & (1<<0));
1434-
const bool isphysical = !!(devid & (1<<1));
1435-
SDL_assert(isphysical);
1436-
if (devid_recording == data->recording) {
1435+
SDL_assert(SDL_IsAudioDevicePhysical(devid)); // should only be iterating device_hash_physical.
1436+
if (SDL_IsAudioDeviceRecording(devid) == data->recording) {
14371437
SDL_assert(data->devs_seen < data->num_devices);
14381438
SDL_AudioDevice *device = (SDL_AudioDevice *) value; // this is normally risky, but we hold the subsystem_rwlock here.
14391439
const bool zombie = SDL_GetAtomicInt(&device->zombie) != 0;
@@ -1500,9 +1500,7 @@ static bool SDLCALL FindAudioDeviceByCallback(void *userdata, const SDL_HashTabl
15001500
{
15011501
FindAudioDeviceByCallbackData *data = (FindAudioDeviceByCallbackData *) userdata;
15021502
const SDL_AudioDeviceID devid = (SDL_AudioDeviceID) (uintptr_t) key;
1503-
// bit #1 of devid is set for physical devices and unset for logical.
1504-
const bool isphysical = !!(devid & (1<<1));
1505-
SDL_assert(isphysical);
1503+
SDL_assert(SDL_IsAudioDevicePhysical(devid)); // should only be iterating device_hash_physical.
15061504
SDL_AudioDevice *device = (SDL_AudioDevice *) value;
15071505
if (data->callback(device, data->userdata)) { // found it?
15081506
data->retval = device;
@@ -1545,13 +1543,14 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
15451543
const char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
15461544
{
15471545
// bit #1 of devid is set for physical devices and unset for logical.
1548-
const bool islogical = !(devid & (1<<1));
15491546
const char *result = NULL;
1550-
const void *vdev = NULL;
15511547

15521548
if (!SDL_GetCurrentAudioDriver()) {
15531549
SDL_SetError("Audio subsystem is not initialized");
15541550
} else {
1551+
const bool islogical = SDL_IsAudioDeviceLogical(devid);
1552+
const void *vdev = NULL;
1553+
15551554
// This does not call ObtainPhysicalAudioDevice() because the device's name never changes, so
15561555
// it doesn't have to lock the whole device. However, just to make sure the device pointer itself
15571556
// remains valid (in case the device is unplugged at the wrong moment), we hold the
@@ -1844,8 +1843,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
18441843

18451844
// this will let you use a logical device to make a new logical device on the parent physical device. Could be useful?
18461845
SDL_AudioDevice *device = NULL;
1847-
const bool islogical = (!wants_default && !(devid & (1<<1)));
1848-
if (!islogical) {
1846+
if ((wants_default || SDL_IsAudioDevicePhysical(devid))) {
18491847
device = ObtainPhysicalAudioDeviceDefaultAllowed(devid);
18501848
} else {
18511849
SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid, &device);
@@ -1982,7 +1980,6 @@ bool SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid, SDL_AudioPostmixCallba
19821980

19831981
bool SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *streams, int num_streams)
19841982
{
1985-
const bool islogical = !(devid & (1<<1));
19861983
SDL_AudioDevice *device = NULL;
19871984
SDL_LogicalAudioDevice *logdev = NULL;
19881985
bool result = true;
@@ -1993,7 +1990,7 @@ bool SDL_BindAudioStreams(SDL_AudioDeviceID devid, SDL_AudioStream * const *stre
19931990
return SDL_InvalidParamError("num_streams");
19941991
} else if (!streams) {
19951992
return SDL_InvalidParamError("streams");
1996-
} else if (!islogical) {
1993+
} else if (SDL_IsAudioDevicePhysical(devid)) {
19971994
return SDL_SetError("Audio streams are bound to device ids from SDL_OpenAudioDevice, not raw physical devices");
19981995
}
19991996

@@ -2600,7 +2597,7 @@ void SDL_UpdateAudio(void)
26002597
SDL_zero(event);
26012598
event.type = i->type;
26022599
event.adevice.which = (Uint32) i->devid;
2603-
event.adevice.recording = ((i->devid & (1<<0)) == 0); // bit #0 of devid is set for playback devices and unset for recording.
2600+
event.adevice.recording = SDL_IsAudioDeviceRecording(i->devid); // bit #0 of devid is set for playback devices and unset for recording.
26042601
SDL_PushEvent(&event);
26052602
}
26062603
SDL_free(i);

0 commit comments

Comments
 (0)