forked from kivutar/rombundler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.c
174 lines (137 loc) · 3.17 KB
/
audio.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif
#define BUFSIZE 1024*8
#define NUMBUFFERS 4
#ifndef MIN
#define MIN(x,y) ((x) <= (y) ? (x) : (y))
#endif
#include "audio.h"
#include "utils.h"
typedef struct al
{
ALuint source;
ALuint *buffers;
ALuint *res_buf;
ALCdevice *handle;
ALCcontext *ctx;
size_t res_ptr;
size_t tmpbuf_ptr;
int rate;
uint8_t tmpbuf[BUFSIZE];
} al_t;
al_t* al = NULL;
static bool unqueue_buffers()
{
ALint val;
alGetSourcei(al->source, AL_BUFFERS_PROCESSED, &val);
if (val <= 0)
return false;
alSourceUnqueueBuffers(al->source, val, &al->res_buf[al->res_ptr]);
al->res_ptr += val;
return true;
}
static bool get_buffer(ALuint *buffer)
{
if (!al->res_ptr)
{
for (;;)
{
if (unqueue_buffers())
break;
}
}
*buffer = al->res_buf[--al->res_ptr];
return true;
}
static size_t fill_internal_buf(const void *buf, size_t size)
{
size_t read_size = MIN(BUFSIZE - al->tmpbuf_ptr, size);
memcpy(al->tmpbuf + al->tmpbuf_ptr, buf, read_size);
al->tmpbuf_ptr += read_size;
return read_size;
}
size_t audio_write(const void *buf_, unsigned size) {
if (!al || !buf_ || !size)
return size;
const uint8_t *buf = (const uint8_t*)buf_;
size_t written = 0;
while (size)
{
ALint val;
ALuint buffer;
size_t rc = fill_internal_buf(buf, size);
written += rc;
buf += rc;
size -= rc;
if (al->tmpbuf_ptr != BUFSIZE)
break;
if (!get_buffer(&buffer))
break;
alBufferData(buffer, AL_FORMAT_STEREO16, al->tmpbuf, BUFSIZE, al->rate);
al->tmpbuf_ptr = 0;
alSourceQueueBuffers(al->source, 1, &buffer);
if (alGetError() != AL_NO_ERROR)
return -1;
alGetSourcei(al->source, AL_SOURCE_STATE, &val);
if (val != AL_PLAYING)
alSourcePlay(al->source);
if (alGetError() != AL_NO_ERROR)
return -1;
}
return written;
}
void audio_deinit() {
if (!al)
return;
alSourceStop(al->source);
alDeleteSources(1, &al->source);
if (al->buffers)
alDeleteBuffers(NUMBUFFERS, al->buffers);
free(al->buffers);
free(al->res_buf);
alcMakeContextCurrent(NULL);
if (al->ctx)
alcDestroyContext(al->ctx);
if (al->handle)
alcCloseDevice(al->handle);
free(al);
}
void audio_init(int rate) {
al = (al_t*)calloc(1, sizeof(al_t));
if (!al)
return;
al->handle = alcOpenDevice(NULL);
if (!al->handle)
die("Could not init audio handle");
al->ctx = alcCreateContext(al->handle, NULL);
if (!al->ctx)
die("Could not init audio context");
alcMakeContextCurrent(al->ctx);
al->rate = rate;
al->buffers = (ALuint*)calloc(NUMBUFFERS, sizeof(ALuint));
al->res_buf = (ALuint*)calloc(NUMBUFFERS, sizeof(ALuint));
if (!al->buffers || !al->res_buf)
die("Could not init audio buffers");
alGenSources(1, &al->source);
alGenBuffers(NUMBUFFERS, al->buffers);
memcpy(al->res_buf, al->buffers, NUMBUFFERS * sizeof(ALuint));
al->res_ptr = NUMBUFFERS;
}
void audio_sample(int16_t left, int16_t right) {
int16_t buf[2] = {left, right};
audio_write(buf, 4);
}
size_t audio_sample_batch(const int16_t *data, size_t frames) {
return audio_write(data, frames*4);
}