This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
coreaudio-play.c
150 lines (121 loc) · 4.04 KB
/
coreaudio-play.c
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
/** Audio API Quick Start Guide: CoreAudio: Play audio from stdin
Link with -framework CoreFoundation -framework CoreAudio */
#include <CoreAudio/CoreAudio.h>
#include <CoreFoundation/CFString.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "ringbuffer.h"
int quit;
ringbuffer *ring_buf;
const AudioObjectPropertyAddress prop_odev_default = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
const AudioObjectPropertyAddress prop_idev_default = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
const AudioObjectPropertyAddress prop_odev_fmt = { kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
const AudioObjectPropertyAddress prop_idev_fmt = { kAudioDevicePropertyStreamFormat, kAudioDevicePropertyScopeInput, kAudioObjectPropertyElementMaster };
OSStatus on_playback(AudioDeviceID device, const AudioTimeStamp *now,
const AudioBufferList *indata, const AudioTimeStamp *intime,
AudioBufferList *outdata, const AudioTimeStamp *outtime,
void *udata)
{
float *d = outdata->mBuffers[0].mData;
size_t n = outdata->mBuffers[0].mDataByteSize;
ringbuf *ring = udata;
ringbuffer_chunk buf;
size_t h = ringbuf_read_begin(ring, n, &buf, NULL);
memcpy(buf.ptr, d, buf.len);
ringbuf_read_finish(ring, h);
d = (char*)d + buf.len;
n -= buf.len;
if (n != 0) {
h = ringbuf_read_begin(ring, n, &buf, NULL);
memcpy(buf.ptr, d, buf.len);
ringbuf_read_finish(ring, h);
d = (char*)d + buf.len;
n -= buf.len;
}
if (n != 0)
memset(d, 0, n);
return 0;
}
void* abuf_create(int playback, void *proc, int *dev_id)
{
AudioObjectID device_id;
if (1) {
// Get default device
const AudioObjectPropertyAddress *a = (playback) ? &prop_odev_default : &prop_idev_default;
u_int size = sizeof(AudioObjectID);
assert(0 == AudioObjectGetPropertyData(kAudioObjectSystemObject, a, 0, NULL, &size, &device_id));
}
// Get audio format
AudioStreamBasicDescription asbd = {};
u_int size = sizeof(asbd);
const AudioObjectPropertyAddress *a = (playback) ? &prop_odev_fmt : &prop_idev_fmt;
assert(0 == AudioObjectGetPropertyData(device_id, a, 0, NULL, &size, &asbd));
fprintf(stderr, "Using format float32, sample rate %u, channels %u\n", (int)asbd.mSampleRate, (int)asbd.mChannelsPerFrame);
// Get buffer size
int buffer_length_msec = 500;
int buf_size = 32/8 * sample_rate * channels * buffer_length_msec / 1000;
// Allocate buffer
ring_buf = ringbuf_alloc(buf_size);
// Register I/O callback
void *io_proc_id = NULL;
void *udata = ring_buf;
assert(0 == AudioDeviceCreateIOProcID(device_id, proc, udata, (AudioDeviceIOProcID*)&io_proc_id)
&& io_proc_id != NULL);
*dev_id = device_id;
return io_proc_id;
}
void on_sigint()
{
quit = 1;
}
void main()
{
int dev;
void *io_proc_id = abuf_create(1, on_playback, &dev);
// Properly handle SIGINT from user
struct sigaction sa = {};
sa.sa_handler = on_sigint;
sigaction(SIGINT, &sa, NULL);
int started = 0;
while (!quit) {
ringbuffer_chunk buf;
size_t h = ringbuf_write_begin(ring_buf, 16*1024, &buf, NULL);
if (buf.len == 0) {
if (!started) {
assert(0 == AudioDeviceStart(dev, io_proc_id));
started = 1;
}
// Buffer is full. Wait.
int period_ms = 100;
usleep(period_ms*1000);
continue;
}
// Read data from stdin
int n = read(0, buf.ptr, buf.len);
ringbuf_write_finish(ring_buf, h);
if (n == 0)
break; // stdin data is complete
}
// Wait until all bufferred data is played by audio device
while (!quit) {
size_t free_space;
ringbuffer_chunk d;
ringbuf_write_begin(ring_buf, 0, &d, &free_space);
if (free_space == ring_buf->cap) {
assert(0 == AudioDeviceStop(dev, io_proc_id));
break;
}
if (!started) {
assert(0 == AudioDeviceStart(dev, io_proc_id));
started = 1;
}
// Buffer isn't empty. Wait.
int period_ms = 100;
usleep(period_ms*1000);
}
AudioDeviceDestroyIOProcID(dev, io_proc_id);
ringbuf_free(ring_buf);
}