forked from mikebrady/shairport-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_soundio.c
222 lines (181 loc) · 7.22 KB
/
audio_soundio.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
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
#include "audio.h"
#include "common.h"
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <soundio/soundio.h>
struct SoundIoOutStream *outstream;
struct SoundIo *soundio;
struct SoundIoDevice *device;
struct SoundIoRingBuffer *ring_buffer = NULL;
static int min_int(int a, int b) { return (a < b) ? a : b; }
static void write_callback(struct SoundIoOutStream *outstream, int frame_count_min,
int frame_count_max) {
struct SoundIoChannelArea *areas;
// int frame_count;
int err;
char *read_ptr = soundio_ring_buffer_read_ptr(ring_buffer);
int fill_bytes = soundio_ring_buffer_fill_count(ring_buffer);
int fill_count = fill_bytes / outstream->bytes_per_frame;
debug(3,
"[--->>] frame_count_min: %d , frame_count_max: %d , fill_bytes: %d , fill_count: %d , "
"outstream->bytes_per_frame: %d",
frame_count_min, frame_count_max, fill_bytes, fill_count, outstream->bytes_per_frame);
if (frame_count_min > fill_count) {
int frame_count = frame_count_min;
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
debug(0, "[--->>] begin write error: %s", soundio_strerror(err));
}
for (int frame = 0; frame < frame_count; frame += 1) {
for (int ch = 0; ch < outstream->layout.channel_count; ch += 1) {
memset(areas[ch].ptr, 0, outstream->bytes_per_sample);
areas[ch].ptr += areas[ch].step;
}
}
if ((err = soundio_outstream_end_write(outstream)))
debug(0, "[--->>] end write error: %s", soundio_strerror(err));
return;
}
int read_count = min_int(frame_count_max, fill_count);
int frames_left = read_count;
while (frames_left > 0) {
int frame_count = frames_left;
if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count)))
debug(0, "[--->>] begin write error: %s", soundio_strerror(err));
if (frame_count <= 0)
break;
for (int frame = 0; frame < frame_count; frame += 1) {
for (int ch = 0; ch < outstream->layout.channel_count; ch += 1) {
memcpy(areas[ch].ptr, read_ptr, outstream->bytes_per_sample);
areas[ch].ptr += areas[ch].step;
read_ptr += outstream->bytes_per_sample;
}
}
if ((err = soundio_outstream_end_write(outstream)))
debug(0, "[--->>] end write error: %s", soundio_strerror(err));
frames_left -= frame_count;
}
debug(3, "[--->>] Wrote: %d", read_count * outstream->bytes_per_frame);
soundio_ring_buffer_advance_read_ptr(ring_buffer, read_count * outstream->bytes_per_frame);
}
static void underflow_callback(__attribute__((unused)) struct SoundIoOutStream *outstream) {
static int count = 0;
debug(0, "underflow %d\n", ++count);
}
static int init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
int err;
config.audio_backend_buffer_desired_length = 2.0;
config.audio_backend_latency_offset = 0;
// get settings from settings file
// do the "general" audio options. Note, these options are in the "general" stanza!
parse_general_audio_options();
// get the specific settings
soundio = soundio_create();
if (!soundio) {
debug(0, "out of memory\n");
return 1;
}
if ((err = soundio_connect_backend(soundio, SoundIoBackendCoreAudio))) {
debug(0, "error connecting: %s", soundio_strerror(err));
return 1;
}
soundio_flush_events(soundio);
int default_out_device_index = soundio_default_output_device_index(soundio);
if (default_out_device_index < 0) {
debug(0, "no output device found");
return 1;
}
device = soundio_get_output_device(soundio, default_out_device_index);
if (!device) {
debug(0, "out of memory");
return 1;
}
debug(0, "Output device: %s\n", device->name);
return 0;
}
static void deinit(void) {
soundio_ring_buffer_destroy(ring_buffer);
soundio_device_unref(device);
soundio_destroy(soundio);
debug(0, "soundio audio deinit\n");
}
static void start(int sample_rate, int sample_format) {
int err;
debug(1, "soundion rate: %d, format: %d", sample_rate, sample_format);
// soundio_device_sort_channel_layouts(device);
outstream = soundio_outstream_create(device);
outstream->format = SoundIoFormatS16NE;
outstream->sample_rate = sample_rate;
outstream->layout.channel_count = 2;
outstream->write_callback = write_callback;
outstream->underflow_callback = underflow_callback;
// outstream->software_latency = 0;
if ((err = soundio_outstream_open(outstream))) {
debug(0, "unable to open device: %s", soundio_strerror(err));
}
if (outstream->layout_error)
debug(0, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));
int capacity = outstream->sample_rate * outstream->bytes_per_frame;
ring_buffer = soundio_ring_buffer_create(soundio, capacity);
if (!ring_buffer)
debug(0, "unable to create ring buffer: out of memory");
char *buf = soundio_ring_buffer_write_ptr(ring_buffer);
memset(buf, 0, capacity);
soundio_ring_buffer_advance_write_ptr(ring_buffer, capacity);
if ((err = soundio_outstream_start(outstream))) {
debug(0, "unable to start outstream: %s", soundio_strerror(err));
}
debug(1, "libsoundio output started\n");
}
static int play(void *buf, int samples) {
// int err;
int free_bytes = soundio_ring_buffer_free_count(ring_buffer);
int written_bytes = 0;
int write_bytes = 0;
int left_bytes = samples * outstream->bytes_per_frame;
char *write_ptr = soundio_ring_buffer_write_ptr(ring_buffer);
debug(3, "[<<---] samples: %d , size: %d", samples, left_bytes);
write_bytes = min_int(left_bytes, free_bytes);
debug(3, "[<<---] left_bytes: %d, write_bytes: %d, free_bytes: %d\n", left_bytes, write_bytes,
free_bytes);
if (write_bytes) {
memcpy(write_ptr, buf, write_bytes);
written_bytes += write_bytes;
soundio_ring_buffer_advance_write_ptr(ring_buffer, write_bytes);
debug(3, "[<<---] Written to buffer : %d\n", written_bytes);
}
return 0;
}
static void parameters(audio_parameters *info) {
info->minimum_volume_dB = -30.0;
info->maximum_volume_dB = 0.0;
debug(2, "Parameters\n");
debug(2, "Current Volume dB: %f\n", info->current_volume_dB);
debug(2, "Minimum Volume dB: %d\n", info->minimum_volume_dB);
debug(2, "Maximum Volume dB: %d\n", info->maximum_volume_dB);
}
static void stop(void) {
soundio_outstream_destroy(outstream);
soundio_ring_buffer_clear(ring_buffer);
debug(1, "libsoundio output stopped\n");
}
static void flush(void) {
soundio_ring_buffer_clear(ring_buffer);
debug(1, "libsoundio output flushed\n");
}
static void help(void) { printf(" There are no options for libsoundio.\n"); }
audio_output audio_soundio = {.name = "soundio",
.help = &help,
.init = &init,
.deinit = &deinit,
.prepare = NULL,
.start = &start,
.stop = &stop,
.is_running = NULL,
.flush = &flush,
.delay = NULL,
.play = &play,
.volume = NULL,
.parameters = ¶meters,
.mute = NULL};