-
Notifications
You must be signed in to change notification settings - Fork 1
/
ichigo-audio.c
428 lines (351 loc) · 8.49 KB
/
ichigo-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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Ichigo Audio Library - A core audio library for Ichigo
// Copyright (C) 2014 bagnz0r (http://github.com/bagnz0r)
// This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdio.h>
#include <wchar.h>
#include <stdbool.h>
#include <bass.h>
#include <bass_fx.h>
#include <tags.h>
#ifdef _WIN32
#define PF __declspec(dllexport)
#else
#define PF __attribute__((visibility("default")))
#endif
// Effects enabled?
bool fx = false;
// Currently selected audio device.
int current_device = 0;
// Curent stream handle.
int current_stream = -1;
// Has the stream ended?
bool end_of_stream = true;
// Is the stream paused?
bool paused = true;
// Audio volume.
float audio_volume = 1;
// Equalizer bands.
int equalizer[18];
// Equalizer band parameters.
BASS_BFX_PEAKEQ equalizer_params[18];
#ifdef _WIN32
void __stdcall sync_end(HSYNC handle, DWORD channel, DWORD data, void *user)
#else
void sync_end(HSYNC handle, DWORD channel, DWORD data, void *user)
#endif
{
BASS_ChannelStop(current_stream);
BASS_StreamFree(current_stream);
current_stream = -1;
end_of_stream = true;
paused = true;
}
//
// Initialize the Ichigo Audio library
//
PF bool ig_initialize(int device, int freq)
{
if (!BASS_Init(device, freq, BASS_DEVICE_FREQ, 0, NULL))
{
return false;
}
current_device = device;
// load all the plugins here
#ifdef _WIN32
BASS_PluginLoad("bass_aac.dll", 0);
BASS_PluginLoad("bass_alac.dll", 0);
BASS_PluginLoad("bass_fx.dll", 0);
BASS_PluginLoad("bassflac.dll", 0);
BASS_PluginLoad("basswv.dll", 0);
BASS_PluginLoad("bass_mpc.dll", 0);
BASS_PluginLoad("bass_ape.dll", 0);
#elif defined(__linux__)
BASS_PluginLoad("libbass_fx.so", 0);
BASS_PluginLoad("libbassflac.so", 0);
BASS_PluginLoad("libbasswv.so", 0);
BASS_PluginLoad("libbass_mpc.so", 0);
BASS_PluginLoad("libbass_ape.so", 0);
#else
BASS_PluginLoad("libbass_fx.dylib", 0);
BASS_PluginLoad("libbassflac.dylib", 0);
BASS_PluginLoad("libbasswv.dylib", 0);
BASS_PluginLoad("libbass_mpc.dylib", 0);
BASS_PluginLoad("libbass_ape.dylib", 0);
#endif
return true;
}
//
// Enables the equalizer
//
PF void ig_enable_equalizer()
{
fx = true;
}
//
// Disables the equalizer
//
PF void ig_disable_equalizer()
{
for (int i = 0; i < sizeof(equalizer); i++)
{
if (equalizer[i])
BASS_ChannelRemoveFX(current_stream, equalizer[i]);
}
fx = false;
}
//
// Sets gain value on the specified equalizer band
//
// band: 0..n
// freq: 1...n
// gain 0..n
//
PF void ig_set_equalizer(int band, float freq, float gain)
{
if (!fx)
return;
BASS_BFX_PEAKEQ param;
param.lBand = band;
if (!BASS_FXGetParameters(equalizer[band], ¶m))
{
param.fBandwidth = 16;
param.fCenter = freq;
param.fQ = 0;
equalizer[band] = BASS_ChannelSetFX(current_stream, BASS_FX_BFX_PEAKEQ, 0);
}
param.fGain = gain;
BASS_FXSetParameters(equalizer[band], ¶m);
equalizer_params[band] = param;
}
//
// Restores equalizer settings when stream is changed
//
void restore_equalizer()
{
if (!fx)
return;
for (int i = 0; i < sizeof(equalizer_params); i++)
{
equalizer[i] = BASS_ChannelSetFX(current_stream, BASS_FX_BFX_PEAKEQ, 0);
BASS_FXSetParameters(equalizer[i], &equalizer_params[i]);
}
}
//
// Get current volume
//
PF float ig_get_volume()
{
if (current_stream == -1)
return 0;
float volume = 0;
if (!BASS_ChannelGetAttribute(current_stream, BASS_ATTRIB_VOL, &volume))
return 0;
return volume;
}
//
// Set volume
//
PF void ig_set_volume(float volume)
{
if (current_stream == -1)
return;
audio_volume = volume;
BASS_ChannelSetAttribute(current_stream, BASS_ATTRIB_VOL, volume);
}
//
// Returns count of available devices
//
PF int ig_get_device_count()
{
int count = 0;
BASS_DEVICEINFO info;
for (int i = 0; BASS_GetDeviceInfo(i, &info); i++)
{
if (info.flags)
count++;
}
return count;
}
//
// Returns selected device name
//
PF char * ig_get_device_name(int device)
{
BASS_DEVICEINFO info;
BASS_GetDeviceInfo(device, &info);
return info.name;
}
//
// Create audio stream from file
//
#ifdef _WIN32
PF int ig_create_stream(wchar_t * file_name)
#else
PF int ig_create_stream(char * file_name)
#endif
{
if (current_stream != -1)
{
BASS_ChannelStop(current_stream);
BASS_StreamFree(current_stream);
current_stream = -1;
}
#ifdef _WIN32
current_stream = BASS_StreamCreateFile(false, file_name, 0, 0, BASS_SAMPLE_FLOAT | BASS_UNICODE);
#else
current_stream = BASS_StreamCreateFile(false, file_name, 0, 0, BASS_SAMPLE_FLOAT);
#endif
ig_set_volume(audio_volume);
restore_equalizer();
return BASS_ErrorGetCode();
}
//
// Create audio stream from URL
//
PF void ig_create_stream_from_url(char * url)
{
if (current_stream != -1)
{
BASS_ChannelStop(current_stream);
BASS_StreamFree(current_stream);
current_stream = -1;
}
current_stream = BASS_StreamCreateURL(url, 0, BASS_SAMPLE_FLOAT | BASS_STREAM_RESTRATE, NULL, NULL);
ig_set_volume(audio_volume);
restore_equalizer();
}
//
// Play the current stream
//
PF void ig_play()
{
if (current_stream == -1)
return;
BASS_ChannelPlay(current_stream, 0);
BASS_ChannelSetSync(current_stream, BASS_SYNC_END, 0, &sync_end, 0);
end_of_stream = false;
paused = false;
}
//
// Pause the current stream
//
PF void ig_pause()
{
if (current_stream == -1)
return;
BASS_ChannelPause(current_stream);
paused = true;
}
//
// Stop the current stream
//
PF void ig_stop()
{
if (current_stream == -1)
return;
BASS_ChannelStop(current_stream);
BASS_StreamFree(current_stream);
current_stream = -1;
end_of_stream = true;
paused = true;
}
//
// Get current track position
//
PF double ig_get_pos()
{
if (current_stream == -1)
return -1;
return BASS_ChannelBytes2Seconds(current_stream, BASS_ChannelGetPosition(current_stream, BASS_POS_BYTE));
}
//
// Get track length
//
PF double ig_get_len()
{
if (current_stream == -1)
return -1;
return BASS_ChannelBytes2Seconds(current_stream, BASS_ChannelGetLength(current_stream, BASS_POS_BYTE));
}
//
// Set track position
//
PF void ig_set_pos(double position)
{
if (current_stream == -1)
return;
BASS_ChannelSetPosition(current_stream, BASS_ChannelSeconds2Bytes(current_stream, position), BASS_POS_BYTE);
}
//
// Determines whether the stream is active/has ended or not
//
PF bool ig_is_stream_active()
{
return !end_of_stream;
}
//
// Determines whether the stream has paused or not
//
PF bool ig_is_paused()
{
if (current_stream == -1)
return false;
return paused;
}
//
// Reads tags on a current stream using the expression provided in tag_format
// Encapsulate your expression in %UTF8(expression_here) in order to get UTF8 encoded output
//
// For more information on expression format, please see tags-readme.txt in dependencies/{OS}/tags
//
PF char * ig_read_tag_from_current_stream(char * tag_format)
{
return TAGS_Read(current_stream, tag_format);
}
//
// Creates a dummy stream and reads tags using the expression provided in tag_format
// Encapsulate your expression in %UTF8(expression_here) in order to get UTF8 encoded output
//
// For more information on expression format, please see tags-readme.txt in dependencies/{OS}/tags
//
#ifdef _WIN32
PF char * ig_read_tag_from_file(wchar_t * file_name, char * tag_format)
#else
PF char * ig_read_tag_from_file(char * file_name, char * tag_format)
#endif
{
#ifdef _WIN32
int stream = BASS_StreamCreateFile(false, file_name, 0, 0, BASS_SAMPLE_FLOAT | BASS_UNICODE);
#else
int stream = BASS_StreamCreateFile(false, file_name, 0, 0, BASS_SAMPLE_FLOAT);
#endif
char * tag = TAGS_Read(stream, tag_format);
BASS_StreamFree(stream);
return tag;
}
//
// Grabs current stream's FFT data (256 samples).
//
PF float * ig_get_fft()
{
float fft[128];
BASS_ChannelGetData(current_stream, fft, BASS_DATA_FFT256);
return fft;
}
//
// Grabs current stream's FFT data (256 samples)
// and calculates an average.
//
PF float ig_get_fft_avg()
{
float * fft = ig_get_fft();
float avg = 0;
for (int i = 0; i < sizeof(fft); i++)
{
avg += fft[i];
}
avg = avg / sizeof(fft);
return avg;
}