shiguredo_audio_device 2026.1.0

Cross-platform audio device library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#import <AudioToolbox/AudioToolbox.h>
#import <CoreAudio/CoreAudio.h>
#import <Foundation/Foundation.h>
#import <mach/mach_time.h>
#include <stdatomic.h>

#include "audio_c.h"

// AudioDevice 構造体
struct AudioDevice {
    char* name;
    char* unique_id;
    int channels;
    int sample_rate;
    int device_type;
};

// AudioSession 構造体
struct AudioSession {
    AudioQueueRef queue;
    AudioQueueBufferRef buffers[3];
    AudioStreamBasicDescription format;
    AudioFrameCallback callback;
    void* user_data;
    atomic_int running;
};

static void audio_input_callback(void* user_data,
                                  AudioQueueRef queue,
                                  AudioQueueBufferRef buffer,
                                  const AudioTimeStamp* start_time,
                                  UInt32 num_packets,
                                  const AudioStreamPacketDescription* packet_desc
                                      __attribute__((unused))) {
    struct AudioSession* session = (struct AudioSession*)user_data;

    if (session->callback && num_packets > 0) {
        // タイムスタンプをマイクロ秒に変換
        int64_t timestamp_us = 0;
        if (start_time->mFlags & kAudioTimeStampHostTimeValid) {
            // ホストタイムをナノ秒に変換してからマイクロ秒に
            mach_timebase_info_data_t timebase;
            mach_timebase_info(&timebase);
            uint64_t nanos =
                start_time->mHostTime * timebase.numer / timebase.denom;
            timestamp_us = (int64_t)(nanos / 1000);
        }

        int format = (session->format.mBitsPerChannel == 16) ? AUDIO_FORMAT_S16
                                                              : AUDIO_FORMAT_F32;

        session->callback(session->user_data, buffer->mAudioData, num_packets,
                          session->format.mChannelsPerFrame,
                          (int)session->format.mSampleRate, format,
                          timestamp_us);
    }

    // バッファを再エンキュー
    if (session->running) {
        AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);
    }
}

// デバイス情報(名前、UID)を取得するヘルパー関数
// 成功時は 0 を返し、deviceName と deviceUID に値を設定する
// 失敗時は -1 を返す
static int get_device_info(AudioDeviceID deviceID, CFStringRef* deviceName,
                            CFStringRef* deviceUID) {
    // デバイス名を取得
    AudioObjectPropertyAddress nameAddress = {
        kAudioDevicePropertyDeviceNameCFString,
        kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain};
    UInt32 nameSize = sizeof(CFStringRef);
    OSStatus status = AudioObjectGetPropertyData(deviceID, &nameAddress, 0,
                                                  NULL, &nameSize, deviceName);

    if (status != noErr || !*deviceName) {
        return -1;
    }

    // デバイス UID を取得
    AudioObjectPropertyAddress uidAddress = {
        kAudioDevicePropertyDeviceUID, kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMain};
    UInt32 uidSize = sizeof(CFStringRef);
    status = AudioObjectGetPropertyData(deviceID, &uidAddress, 0, NULL,
                                         &uidSize, deviceUID);

    if (status != noErr || !*deviceUID) {
        CFRelease(*deviceName);
        *deviceName = NULL;
        return -1;
    }

    return 0;
}

// 指定スコープのチャンネル数を取得するヘルパー関数
static int get_channel_count(AudioDeviceID deviceID,
                              AudioObjectPropertyScope scope) {
    AudioObjectPropertyAddress channelAddress = {
        kAudioDevicePropertyStreamConfiguration, scope,
        kAudioObjectPropertyElementMain};
    UInt32 channelSize = 0;
    OSStatus status = AudioObjectGetPropertyDataSize(
        deviceID, &channelAddress, 0, NULL, &channelSize);

    int totalChannels = 0;
    if (status == noErr && channelSize > 0) {
        AudioBufferList* bufferList = (AudioBufferList*)malloc(channelSize);
        if (bufferList) {
            status = AudioObjectGetPropertyData(
                deviceID, &channelAddress, 0, NULL, &channelSize, bufferList);
            if (status == noErr) {
                for (UInt32 j = 0; j < bufferList->mNumberBuffers; j++) {
                    totalChannels += bufferList->mBuffers[j].mNumberChannels;
                }
            }
            free(bufferList);
        }
    }

    return totalChannels;
}

// デバイスを動的配列に追加するヘルパー関数
// 成功時は 0、メモリ確保失敗時は -1 を返す
static int add_device_to_array(struct AudioDevice*** deviceArray,
                                int* deviceCount, int* capacity,
                                CFStringRef deviceName,
                                CFStringRef deviceUID,
                                int channels, int sample_rate,
                                int device_type) {
    // 配列を拡張
    if (*deviceCount >= *capacity) {
        int new_capacity = *capacity == 0 ? 8 : *capacity * 2;
        struct AudioDevice** newArray = (struct AudioDevice**)realloc(
            *deviceArray, sizeof(struct AudioDevice*) * new_capacity);
        if (!newArray) {
            return -1;
        }
        *deviceArray = newArray;
        *capacity = new_capacity;
    }

    struct AudioDevice* device =
        (struct AudioDevice*)malloc(sizeof(struct AudioDevice));
    if (!device) {
        return -1;
    }

    // CFString を C 文字列に変換
    CFIndex nameLength =
        CFStringGetMaximumSizeForEncoding(
            CFStringGetLength(deviceName), kCFStringEncodingUTF8) +
        1;
    device->name = (char*)malloc(nameLength);
    if (!device->name) {
        free(device);
        return -1;
    }
    if (!CFStringGetCString(deviceName, device->name, nameLength,
                             kCFStringEncodingUTF8)) {
        free(device->name);
        free(device);
        return -1;
    }

    CFIndex uidLength =
        CFStringGetMaximumSizeForEncoding(CFStringGetLength(deviceUID),
                                           kCFStringEncodingUTF8) +
        1;
    device->unique_id = (char*)malloc(uidLength);
    if (!device->unique_id) {
        free(device->name);
        free(device);
        return -1;
    }
    if (!CFStringGetCString(deviceUID, device->unique_id, uidLength,
                             kCFStringEncodingUTF8)) {
        free(device->unique_id);
        free(device->name);
        free(device);
        return -1;
    }

    device->channels = channels > 0 ? channels : 2;
    device->sample_rate = sample_rate > 0 ? sample_rate : 48000;
    device->device_type = device_type;

    (*deviceArray)[(*deviceCount)++] = device;
    return 0;
}

int audio_enumerate_devices(struct AudioDevice*** devices, int* count) {
    if (!devices || !count) {
        return -1;
    }

    // 全デバイスの取得
    AudioObjectPropertyAddress propertyAddress = {
        kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMain};

    UInt32 dataSize = 0;
    OSStatus status = AudioObjectGetPropertyDataSize(
        kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);

    if (status != noErr) {
        return -1;
    }

    UInt32 deviceCount = dataSize / sizeof(AudioDeviceID);
    if (deviceCount == 0) {
        *devices = NULL;
        *count = 0;
        return 0;
    }

    AudioDeviceID* deviceIDs = (AudioDeviceID*)malloc(dataSize);
    if (!deviceIDs) {
        return -2;
    }

    status = AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                         &propertyAddress, 0, NULL, &dataSize,
                                         deviceIDs);

    if (status != noErr) {
        free(deviceIDs);
        return -1;
    }

    struct AudioDevice** deviceArray = NULL;
    int totalDeviceCount = 0;
    int capacity = 0;

    for (UInt32 i = 0; i < deviceCount; i++) {
        // 入力ストリームがあるかチェック
        AudioObjectPropertyAddress inputStreamAddress = {
            kAudioDevicePropertyStreams, kAudioDevicePropertyScopeInput,
            kAudioObjectPropertyElementMain};
        UInt32 inputStreamSize = 0;
        status = AudioObjectGetPropertyDataSize(
            deviceIDs[i], &inputStreamAddress, 0, NULL, &inputStreamSize);
        int has_input = (status == noErr && inputStreamSize > 0);

        // 出力ストリームがあるかチェック
        AudioObjectPropertyAddress outputStreamAddress = {
            kAudioDevicePropertyStreams, kAudioDevicePropertyScopeOutput,
            kAudioObjectPropertyElementMain};
        UInt32 outputStreamSize = 0;
        status = AudioObjectGetPropertyDataSize(
            deviceIDs[i], &outputStreamAddress, 0, NULL, &outputStreamSize);
        int has_output = (status == noErr && outputStreamSize > 0);

        if (!has_input && !has_output) {
            continue;
        }

        // デバイス情報を取得
        CFStringRef deviceName = NULL;
        CFStringRef deviceUID = NULL;
        if (get_device_info(deviceIDs[i], &deviceName, &deviceUID) < 0) {
            continue;
        }

        // サンプルレートを取得
        Float64 sampleRate = 0;
        AudioObjectPropertyAddress rateAddress = {
            kAudioDevicePropertyNominalSampleRate,
            kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain};
        UInt32 rateSize = sizeof(Float64);
        AudioObjectGetPropertyData(deviceIDs[i], &rateAddress, 0, NULL,
                                    &rateSize, &sampleRate);

        // 入力デバイスとして追加
        if (has_input) {
            int inputChannels = get_channel_count(
                deviceIDs[i], kAudioDevicePropertyScopeInput);
            if (add_device_to_array(&deviceArray, &totalDeviceCount,
                                     &capacity, deviceName,
                                     deviceUID, inputChannels,
                                     (int)sampleRate,
                                     AUDIO_DEVICE_TYPE_INPUT) < 0) {
                CFRelease(deviceName);
                CFRelease(deviceUID);
                for (int j = 0; j < totalDeviceCount; j++) {
                    free(deviceArray[j]->name);
                    free(deviceArray[j]->unique_id);
                    free(deviceArray[j]);
                }
                free(deviceArray);
                free(deviceIDs);
                return -2;
            }
        }

        // 出力デバイスとして追加
        if (has_output) {
            int outputChannels = get_channel_count(
                deviceIDs[i], kAudioDevicePropertyScopeOutput);
            if (add_device_to_array(&deviceArray, &totalDeviceCount,
                                     &capacity, deviceName,
                                     deviceUID, outputChannels,
                                     (int)sampleRate,
                                     AUDIO_DEVICE_TYPE_OUTPUT) < 0) {
                CFRelease(deviceName);
                CFRelease(deviceUID);
                for (int j = 0; j < totalDeviceCount; j++) {
                    free(deviceArray[j]->name);
                    free(deviceArray[j]->unique_id);
                    free(deviceArray[j]);
                }
                free(deviceArray);
                free(deviceIDs);
                return -2;
            }
        }

        CFRelease(deviceName);
        CFRelease(deviceUID);
    }

    free(deviceIDs);

    *devices = deviceArray;
    *count = totalDeviceCount;
    return 0;
}

void audio_free_devices(struct AudioDevice** devices, int count) {
    if (!devices) {
        return;
    }

    for (int i = 0; i < count; i++) {
        if (devices[i]) {
            free(devices[i]->name);
            free(devices[i]->unique_id);
            free(devices[i]);
        }
    }
    free(devices);
}

const char* audio_device_name(struct AudioDevice* device) {
    if (!device) {
        return NULL;
    }
    return device->name;
}

const char* audio_device_unique_id(struct AudioDevice* device) {
    if (!device) {
        return NULL;
    }
    return device->unique_id;
}

int audio_device_channels(struct AudioDevice* device) {
    if (!device) {
        return 0;
    }
    return device->channels;
}

int audio_device_sample_rate(struct AudioDevice* device) {
    if (!device) {
        return 0;
    }
    return device->sample_rate;
}

int audio_device_type(struct AudioDevice* device) {
    if (!device) {
        return AUDIO_DEVICE_TYPE_INPUT;
    }
    return device->device_type;
}

static AudioDeviceID find_device_by_uid(const char* uid) {
    AudioObjectPropertyAddress propertyAddress = {
        kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMain};

    UInt32 dataSize = 0;
    OSStatus status = AudioObjectGetPropertyDataSize(
        kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);

    if (status != noErr) {
        return kAudioObjectUnknown;
    }

    UInt32 deviceCount = dataSize / sizeof(AudioDeviceID);
    AudioDeviceID* deviceIDs = (AudioDeviceID*)malloc(dataSize);
    if (!deviceIDs) {
        return kAudioObjectUnknown;
    }

    status = AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                         &propertyAddress, 0, NULL, &dataSize,
                                         deviceIDs);

    if (status != noErr) {
        free(deviceIDs);
        return kAudioObjectUnknown;
    }

    AudioDeviceID foundDevice = kAudioObjectUnknown;
    NSString* targetUID = [NSString stringWithUTF8String:uid];

    for (UInt32 i = 0; i < deviceCount; i++) {
        CFStringRef deviceUID = NULL;
        AudioObjectPropertyAddress uidAddress = {
            kAudioDevicePropertyDeviceUID, kAudioObjectPropertyScopeGlobal,
            kAudioObjectPropertyElementMain};
        UInt32 uidSize = sizeof(CFStringRef);
        status = AudioObjectGetPropertyData(deviceIDs[i], &uidAddress, 0, NULL,
                                             &uidSize, &deviceUID);

        if (status == noErr && deviceUID) {
            if ([(__bridge NSString*)deviceUID isEqualToString:targetUID]) {
                foundDevice = deviceIDs[i];
                CFRelease(deviceUID);
                break;
            }
            CFRelease(deviceUID);
        }
    }

    free(deviceIDs);
    return foundDevice;
}

struct AudioSession* audio_session_create(const char* device_id,
                                           int sample_rate,
                                           int channels) {
    struct AudioSession* session =
        (struct AudioSession*)calloc(1, sizeof(struct AudioSession));
    if (!session) {
        return NULL;
    }

    // デフォルト値の設定
    if (sample_rate <= 0) {
        sample_rate = 48000;
    }
    if (channels <= 0) {
        channels = 1;
    }

    // オーディオフォーマットの設定(16-bit signed integer)
    session->format.mSampleRate = sample_rate;
    session->format.mFormatID = kAudioFormatLinearPCM;
    session->format.mFormatFlags =
        kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
    session->format.mBitsPerChannel = 16;
    session->format.mChannelsPerFrame = channels;
    session->format.mBytesPerFrame =
        session->format.mChannelsPerFrame * (session->format.mBitsPerChannel / 8);
    session->format.mFramesPerPacket = 1;
    session->format.mBytesPerPacket = session->format.mBytesPerFrame;

    // AudioQueue を作成
    OSStatus status = AudioQueueNewInput(
        &session->format, audio_input_callback, session, NULL,
        kCFRunLoopCommonModes, 0, &session->queue);

    if (status != noErr) {
        free(session);
        return NULL;
    }

    // デバイスを設定
    if (device_id) {
        AudioDeviceID deviceID = find_device_by_uid(device_id);
        if (deviceID != kAudioObjectUnknown) {
            CFStringRef deviceUID =
                CFStringCreateWithCString(NULL, device_id, kCFStringEncodingUTF8);
            if (deviceUID) {
                AudioQueueSetProperty(session->queue,
                                       kAudioQueueProperty_CurrentDevice,
                                       &deviceUID, sizeof(CFStringRef));
                CFRelease(deviceUID);
            }
        }
    }

    // バッファを作成(10ms 分)
    UInt32 bufferByteSize =
        session->format.mSampleRate * session->format.mBytesPerFrame / 100;

    for (int i = 0; i < 3; i++) {
        status = AudioQueueAllocateBuffer(session->queue, bufferByteSize,
                                           &session->buffers[i]);
        if (status != noErr) {
            AudioQueueDispose(session->queue, true);
            free(session);
            return NULL;
        }
    }

    return session;
}

void audio_session_destroy(struct AudioSession* session) {
    if (!session) {
        return;
    }

    if (session->running) {
        audio_session_stop(session);
    }

    AudioQueueDispose(session->queue, true);
    free(session);
}

int audio_session_start(struct AudioSession* session,
                        AudioFrameCallback callback,
                         void* user_data) {
    if (!session || !callback) {
        return -1;
    }

    if (session->running) {
        return 0;
    }

    session->callback = callback;
    session->user_data = user_data;
    session->running = 1;

    // バッファをエンキュー
    for (int i = 0; i < 3; i++) {
        OSStatus status =
            AudioQueueEnqueueBuffer(session->queue, session->buffers[i], 0, NULL);
        if (status != noErr) {
            session->running = 0;
            return -1;
        }
    }

    // キャプチャを開始
    OSStatus status = AudioQueueStart(session->queue, NULL);
    if (status != noErr) {
        session->running = 0;
        return -1;
    }

    return 0;
}

void audio_session_stop(struct AudioSession* session) {
    if (!session || !session->running) {
        return;
    }

    session->running = 0;
    AudioQueueStop(session->queue, true);
}

int audio_session_sample_rate(struct AudioSession* session) {
    if (!session) {
        return 0;
    }
    return (int)session->format.mSampleRate;
}

int audio_session_channels(struct AudioSession* session) {
    if (!session) {
        return 0;
    }
    return session->format.mChannelsPerFrame;
}