-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMoyaiALSound.cpp
303 lines (278 loc) · 9.83 KB
/
MoyaiALSound.cpp
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
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include "common.h"
#include "client.h"
#include "MoyaiALSound.h"
#ifdef USE_MOYAIAL
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/opt.h"
#include "libswresample/swresample.h"
//#include "libswscale/swscale.h"
};
#endif
//static inline int16_t limit_float_conv_int16(float inValue) {
// return (int16_t)((1-2*signbit(inValue)) * atanf(fabs(inValue) * 2.0f) * ((2.0f / 3.14f) * 32767));
//}
static const int FREQ = 48000;
static MoyaiALSound *g_moyaial_sounds[1024];
static int g_moyaial_sounds_used=0;
static void appendSound(MoyaiALSound *snd) {
assert(g_moyaial_sounds_used<=elementof(g_moyaial_sounds));
g_moyaial_sounds[g_moyaial_sounds_used]=snd;
g_moyaial_sounds_used++;
fprintf(stderr,"appendSound: used:%d\n",g_moyaial_sounds_used);
}
MoyaiALSound *MoyaiALSound::create( const char *cpath ) {
ALenum fmt;
void *data;
ALsizei bytesz=0;
ALsizei freq;
int sampleSize;
alutLoadWAVFile((ALbyte*)cpath, &fmt, &data, &bytesz, &freq );
if(bytesz==0) return nullptr;
MoyaiALSound *out = new MoyaiALSound();
out->sampleRate = freq;
switch(fmt) {
case AL_FORMAT_MONO8:
sampleSize=1;
out->numChannels=1;
out->numFrames=bytesz;
break;
case AL_FORMAT_MONO16:
sampleSize=2;
out->numChannels=1;
out->numFrames=bytesz/2;
break;
case AL_FORMAT_STEREO8:
sampleSize = 1;
out->numChannels=2;
out->numFrames=bytesz/2;
break;
case AL_FORMAT_STEREO16:
sampleSize = 2;
out->numChannels=2;
out->numFrames=bytesz/4;
break;
}
fprintf(stderr, "loadwavfile: bytesz:%d fmt:%d numch:%d numframe:%d freq:%d path:%s\n",
bytesz, fmt, out->numChannels, out->numFrames, out->sampleRate, cpath );
out->samples = (float*)MALLOC(out->numFrames*out->numChannels*sizeof(float));
assert(out->samples);
for(int i=0;i<out->numFrames;i++) {
if(sampleSize==1) {
char *ptr=(char*)data;
out->samples[i*out->numChannels+0]=((float)ptr[i*out->numChannels+0])/127.0f;
if(out->numChannels==2) {
out->samples[i*out->numChannels+1]=((float)ptr[i*out->numChannels+1])/127.0f;
}
} else if( sampleSize==2 ) {
short *ptr=(short*)data;
out->samples[i*out->numChannels+0]=((float)ptr[i*out->numChannels+0])/32767.0f;
if(out->numChannels==2) {
out->samples[i*out->numChannels+1]=((float)ptr[i*out->numChannels+1])/32767.0f;
}
}
}
out->resample();
out->normalize();
appendSound(out);
return out;
}
void MoyaiALSound::normalize() {
float maxv=0,minv=0;
for(int i=0;i<numFrames*numChannels;i++) {
float v=samples[i];
if(v<minv)minv=v;
if(v>maxv)maxv=v;
}
if(minv<0)minv*=-1;
float absmax = maxv > minv ? maxv : minv;
float factor=1;
if(absmax>0.5) factor=0.8;
if(absmax>0.7) factor=0.6;
if(absmax>0.9) factor=0.4;
for(int i=0;i<numFrames*numChannels;i++) {
samples[i]=samples[i]*factor;
}
}
void MoyaiALSound::resample() {
if(sampleRate==FREQ) return;
#if USE_MOYAIAL
int layout;
if(numChannels==1) {
layout = AV_CH_LAYOUT_MONO;
} else {
layout = AV_CH_LAYOUT_STEREO;
}
// https://github.com/illuusio/ffmpeg-example/blob/master/example2.c
SwrContext *swr = swr_alloc_set_opts( NULL,
// out
layout,
AV_SAMPLE_FMT_FLT,
FREQ,
// in
layout,
AV_SAMPLE_FMT_FLT,
sampleRate,
0,
NULL);
int ret = swr_init(swr);
assert(ret>=0);
const uint8_t *input = (uint8_t*)samples;
float sec = (float)numFrames / (float)sampleRate;
int needNumFrames = (int)( (float)sec * FREQ);
fprintf(stderr," sec:%f needNumFrames:%d\n", sec,needNumFrames);
uint8_t *output = (uint8_t*)MALLOC( needNumFrames * numChannels * sizeof(float));
ret = swr_convert(swr, &output, needNumFrames, &input, numFrames );
fprintf(stderr, "swr_convert: ret:%d\n",ret);
if(ret<0) {
fprintf(stderr, "swr_convert failed: %d\n",ret);
FREE(output);
return;
}
float *to_free = samples;
samples = (float*)output;
FREE(to_free);
numFrames = ret;
sampleRate = FREQ;
#endif
}
MoyaiALSound *MoyaiALSound::create( int sampleRate, int numChannels, int numFrames, bool loop, float *samples ) {
MoyaiALSound *out = new MoyaiALSound();
out->sampleRate = sampleRate;
out->numChannels = numChannels;
out->numFrames = numFrames;
out->loop = loop;
int bytesize = sizeof(float) * numFrames * numChannels;
out->samples = (float*)MALLOC(bytesize);
assert(out);
for(int i=0;i<numFrames * numChannels;i++) {
out->samples[i]=samples[i];
}
out->resample();
out->normalize();
appendSound(out);
return out;
}
static const int ABUFNUM = 4, ABUFLEN = 512; // 256 not working
static int16_t g_pcmdata[ABUFNUM][ABUFLEN*2]; // stereo
static ALuint g_alsource;
static ALuint g_albuffer[ABUFNUM];
static double t=0,dt=0;
static void (*g_on_before_mix)(int16_t *samples, int numFrames, int numChannels, int freq ) = nullptr;
static void (*g_on_mix_done)(int16_t *samples, int numFrames, int numChannels, int freq ) = nullptr;
void setMoyaiALOnMixDone( void (*cb)( int16_t *samples, int numFrames, int numChannels, int freq ) ) {
g_on_mix_done = cb;
}
void setMoyaiALOnBeforeMix( void (*cb)( int16_t *samples, int numFrames, int numChannels, int freq ) ) {
g_on_before_mix = cb;
}
static void mixFill(int bufind) {
#if 0 // debug sound filler
for(int i=0;i<ABUFLEN;i++) {
t+=0.01+dt;
dt+=0.0000002;
g_pcmdata[bufind][i*2+0] = sin(t)*1000;
g_pcmdata[bufind][i*2+1] = random()%200;
}
#else
for(int i=0;i<ABUFLEN;i++) {
g_pcmdata[bufind][i*2+0] = 0;
g_pcmdata[bufind][i*2+1] = 0;
}
#endif
if(g_on_before_mix) g_on_before_mix( g_pcmdata[bufind], ABUFLEN, 2, FREQ );
for(int i=0;i<g_moyaial_sounds_used;i++) {
MoyaiALSound *snd = g_moyaial_sounds[i];
assert(snd->sampleRate == FREQ);
if(snd->state == MoyaiALSound::PLAYING) {
#if 0
float s = snd->samples[snd->posFrame*snd->numChannels];
fprintf(stderr, "Playing:[%d] ch:%d nf:%d posF:%d(%f) sr:%d v:%f st:%d loop:%d\n",
i, snd->numChannels, snd->numFrames, snd->posFrame, s,
snd->sampleRate, snd->volume, snd->state, snd->loop );
#endif
} else {
continue;
}
for(int i=0;i<ABUFLEN;i++) {
float left_sample = snd->samples[snd->posFrame*snd->numChannels+0] * snd->volume;
if(left_sample<-1)left_sample=-1;
else if(left_sample>1)left_sample=1;
g_pcmdata[bufind][i*2+0] += (short)(left_sample*32767);
float right_sample = left_sample;
if(snd->numChannels==2) {
right_sample = snd->samples[snd->posFrame*snd->numChannels+1] * snd->volume;
if(right_sample<-1)right_sample=-1;
else if(right_sample>1)right_sample=1;
}
g_pcmdata[bufind][i*2+1] += (short)(right_sample*32767);
snd->posFrame++;
if(snd->posFrame > snd->numFrames) {
snd->posFrame = 0;
if(!snd->loop) {
snd->state = MoyaiALSound::STOPPED;
break;
}
}
}
}
if(g_on_mix_done) {
g_on_mix_done(g_pcmdata[bufind], ABUFLEN, 2, FREQ );
}
}
static void *moyaiALThreadFunc(void *arg) {
alGenBuffers(ABUFNUM,g_albuffer);
alGenSources(1,&g_alsource);
fprintf(stderr,"moyaiALsrc:%d\n",g_alsource);
for(int j=0;j<ABUFNUM;j++) {
for(int i=0;i<ABUFLEN;i++) {
t+=0.01+dt;
dt+=0.0000002;
g_pcmdata[j][i*2+0] = sin(t)*1000;
g_pcmdata[j][i*2+1] = random()%200;
}
}
for(int i=0;i<ABUFNUM;i++) {
alBufferData(g_albuffer[i], AL_FORMAT_STEREO16, g_pcmdata[i], ABUFLEN*sizeof(int16_t)*2, FREQ);
alSourceQueueBuffers(g_alsource,1,&g_albuffer[i]);
fprintf(stderr,"alsourcequeuebuffers: %d\n",alGetError());
}
alSourcePlay(g_alsource);
fprintf(stderr,"alsourceplay done:%d\n",alGetError());
static int play_head=0;
while(true) {
ALint proced;
alGetSourcei(g_alsource,AL_BUFFERS_PROCESSED,&proced);
// fprintf(stderr,"proced:%d\n",proced);
if(proced>0) {
for(int proci=0;proci<proced;proci++) {
int bufind = play_head % ABUFNUM;
alSourceUnqueueBuffers(g_alsource,1,&g_albuffer[bufind]);
mixFill(bufind);
alBufferData(g_albuffer[bufind], AL_FORMAT_STEREO16, g_pcmdata[bufind], ABUFLEN*sizeof(int16_t)*2,FREQ);
alSourceQueueBuffers(g_alsource,1,&g_albuffer[bufind]);
play_head++;
}
}
usleep(1*1000);
}
}
void startMoyaiAL() {
static bool init=false;
if(init) return;
init=true;
#ifdef WIN32
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)moyaiALThreadFunc, url, 0, NULL);
#else
pthread_t tid;
int err = pthread_create(&tid,NULL,moyaiALThreadFunc,NULL);
if(err) {
print("moyaiALThreadFunc: pthread_create failed:%d",err);
return;
}
#endif
}