-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgsspu_al.c
192 lines (174 loc) · 4.73 KB
/
vgsspu_al.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
/* (C)2016, SUZUKI PLAN.
*----------------------------------------------------------------------------
* Description: VGS - Sound Processing Unit for OpenAL
* Platform: iOS and Mac OS X
* Author: Yoji Suzuki (SUZUKI PLAN)
*----------------------------------------------------------------------------
*/
#include "vgsspu_al.h"
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#define kOutputBus 0
#define kInputBus 1
#define BUFNUM 2
typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr)(const ALint bid, ALenum format, ALvoid* data, ALsizei size, ALsizei freq);
struct AL {
ALCdevice* sndDev;
ALCcontext* sndCtx;
ALuint sndABuf;
ALuint sndASrc;
alBufferDataStaticProcPtr alBufferDataStaticProc;
};
struct SPU {
struct AL al;
volatile int alive;
void* buffer;
void (*callback)(void* buffer, size_t size);
pthread_t tid;
size_t size;
int sampling;
int bit;
int ch;
int format;
};
static int get_format(int bit, int ch);
static void* sound_thread(void* context);
static int init_al(struct AL* al);
static void term_al(struct AL* al);
void* vgsspu_start(void (*callback)(void* buffer, size_t size))
{
return vgsspu_start2(22050, 16, 1, 1470, callback);
}
void* vgsspu_start2(int sampling, int bit, int ch, size_t size, void (*callback)(void* buffer, size_t size))
{
struct SPU* result;
int format;
format = get_format(bit, ch);
if (-1 == format) {
return NULL;
}
result = (struct SPU*)malloc(sizeof(struct SPU));
if (NULL == result) {
return NULL;
}
memset(result, 0, sizeof(struct SPU));
result->buffer = malloc(size);
if (NULL == result->buffer) {
free(result);
return NULL;
}
result->sampling = sampling;
result->bit = bit;
result->ch = ch;
result->format = format;
result->callback = callback;
result->size = size;
if (init_al(&result->al)) {
free(result->buffer);
free(result);
return NULL;
}
result->alive = 1;
if (pthread_create(&result->tid, NULL, sound_thread, result)) {
free(result->buffer);
free(result);
return NULL;
}
return result;
}
void vgsspu_end(void* context)
{
struct SPU* c = (struct SPU*)context;
c->alive = 0;
pthread_join(c->tid, NULL);
term_al(&c->al);
free(c->buffer);
free(c);
}
static int get_format(int bit, int ch)
{
if (1 == ch) {
if (8 == bit) {
return AL_FORMAT_MONO8;
}
if (16 == bit) {
return AL_FORMAT_MONO16;
}
} else if (2 == ch) {
if (8 == bit) {
return AL_FORMAT_STEREO8;
}
if (16 == bit) {
return AL_FORMAT_STEREO16;
}
}
return -1;
}
static void* sound_thread(void* context)
{
struct SPU* c = (struct SPU*)context;
ALint st;
memset(c->buffer, 0, c->size);
while (c->alive) {
alGetSourcei(c->al.sndASrc, AL_BUFFERS_QUEUED, &st);
if (st < BUFNUM) {
alGenBuffers(1, &c->al.sndABuf);
} else {
alGetSourcei(c->al.sndASrc, AL_SOURCE_STATE, &st);
if (st != AL_PLAYING) {
alSourcePlay(c->al.sndASrc);
}
while ((void)(alGetSourcei(c->al.sndASrc, AL_BUFFERS_PROCESSED, &st)), st == 0) {
usleep(1000);
}
alSourceUnqueueBuffers(c->al.sndASrc, 1, &c->al.sndABuf);
alDeleteBuffers(1, &c->al.sndABuf);
alGenBuffers(1, &c->al.sndABuf);
}
c->callback(c->buffer, c->size);
alBufferData(c->al.sndABuf, c->format, c->buffer, (int)c->size, c->sampling);
alSourceQueueBuffers(c->al.sndASrc, 1, &c->al.sndABuf);
}
do {
usleep(1000);
alGetSourcei(c->al.sndASrc, AL_SOURCE_STATE, &st);
} while (st == AL_PLAYING);
return NULL;
}
static int init_al(struct AL* al)
{
al->sndDev = alcOpenDevice(NULL);
if (NULL == al->sndDev) {
term_al(al);
return -1;
}
al->sndCtx = alcCreateContext(al->sndDev, NULL);
if (NULL == al->sndCtx) {
term_al(al);
return -1;
}
if (!alcMakeContextCurrent(al->sndCtx)) {
term_al(al);
return -1;
}
al->alBufferDataStaticProc = (alBufferDataStaticProcPtr)alcGetProcAddress(NULL, (const ALCchar*)"alBufferDataStatic");
alGenSources(1, &(al->sndASrc));
return 0;
}
static void term_al(struct AL* al)
{
if (al->sndCtx) {
alcDestroyContext(al->sndCtx);
al->sndCtx = NULL;
}
if (al->sndDev) {
alcCloseDevice(al->sndDev);
al->sndDev = NULL;
}
}