-
Notifications
You must be signed in to change notification settings - Fork 22
/
musicplayer_bitmapthumbnail.cpp
374 lines (316 loc) · 10.7 KB
/
musicplayer_bitmapthumbnail.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
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
// ffmpeg_bitmapthumbnail.cpp
// part of MusicPlayer, https://github.com/albertz/music-player
// Copyright (c) 2012, Albert Zeyer, www.az2000.de
// All rights reserved.
// This code is under the 2-clause BSD license, see License.txt in the root directory of this project.
#include "musicplayer.h"
#include "PythonHelpers.h"
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avfft.h>
}
// note that each row in imgDataStart is aligned to 4 bytes
#define ALIGN4(n) ((n)+3 - ((n)+3) % 4)
// each single pixel is decoded as BGR
static
PyObject* createBitmap24Bpp(int w, int h, char** imgDataStart) {
assert(imgDataStart);
// http://en.wikipedia.org/wiki/BMP_file_format
static const int FileHeaderSize = 14;
static const int InfoHeaderSize = 40; // format BITMAPINFOHEADER
size_t bmpSize = FileHeaderSize + InfoHeaderSize + ALIGN4(3 * w) * h;
PyObject* bmp = PyBytes_FromStringAndSize(NULL, bmpSize);
if(!bmp) return NULL;
memset(PyBytes_AS_STRING(bmp), 0, bmpSize);
unsigned char* bmpfileheader = (unsigned char*) PyBytes_AS_STRING(bmp);
unsigned char* bmpinfoheader = bmpfileheader + FileHeaderSize;
*imgDataStart = (char*) bmpinfoheader + InfoHeaderSize;
// header field
bmpfileheader[ 0] = 'B';
bmpfileheader[ 1] = 'M';
bmpfileheader[ 2] = (unsigned char)(bmpSize );
bmpfileheader[ 3] = (unsigned char)(bmpSize>> 8);
bmpfileheader[ 4] = (unsigned char)(bmpSize>>16);
bmpfileheader[ 5] = (unsigned char)(bmpSize>>24);
assert(FileHeaderSize + InfoHeaderSize < 256);
bmpfileheader[10] = FileHeaderSize + InfoHeaderSize; // starting address of image data (32bit)
bmpinfoheader[ 0] = InfoHeaderSize; // size of info header. (32bit)
bmpinfoheader[ 4] = (unsigned char)(w );
bmpinfoheader[ 5] = (unsigned char)(w>> 8);
bmpinfoheader[ 6] = (unsigned char)(w>>16);
bmpinfoheader[ 7] = (unsigned char)(w>>24);
bmpinfoheader[ 8] = (unsigned char)(h );
bmpinfoheader[ 9] = (unsigned char)(h>> 8);
bmpinfoheader[10] = (unsigned char)(h>>16);
bmpinfoheader[11] = (unsigned char)(h>>24);
bmpinfoheader[12] = 1; // num of color planes. must be 1 (16bit)
bmpinfoheader[14] = 24; // bpp (16bit)
return bmp;
}
static
void bmpSetPixel(char* img, int w, int x, int y, unsigned char r, unsigned char g, unsigned char b) {
img[y * ALIGN4(3 * w) + x * 3 + 0] = (char) b;
img[y * ALIGN4(3 * w) + x * 3 + 1] = (char) g;
img[y * ALIGN4(3 * w) + x * 3 + 2] = (char) r;
}
// f must be in [0,1]
static
void rainbowColor(float f, unsigned char* r, unsigned char* g, unsigned char* b) {
if(f < 0.0) {
*r = 255;
*g = 0;
*b = 0;
}
else if(f < 1.0/6) {
f *= 6;
*r = 255;
*g = 255 * f;
*b = 0;
}
else if(f < 2.0/6) {
f = f * 6 + 1;
*r = 255 * (1 - f);
*g = 255;
*b = 0;
}
else if(f < 3.0/6) {
f = f * 6 + 2;
*r = 0;
*g = 255;
*b = 255 * f;
}
else if(f < 4.0/6) {
f = f * 6 + 3;
*r = 0;
*g = 255 * (1 - f);
*b = 255;
}
else if(f < 5.0/6) {
f = f * 6 + 4;
*r = 255 * f;
*g = 0;
*b = 255;
}
else if(f < 6.0/6) {
f = f * 6 + 5;
*r = 255;
*g = 0;
*b = 255 * (1 - f);
}
else {
*r = 255;
*g = 0;
*b = 0;
}
}
// idea loosely from:
// http://www.freesound.org/
// https://github.com/endolith/freesound-thumbnailer/blob/master/processing.py
PyObject *
pyCalcBitmapThumbnail(PyObject* self, PyObject* args, PyObject* kws) {
PyObject* songObj = NULL;
int bmpWidth = 400, bmpHeight = 101;
unsigned char bgR = 100, bgG = bgR, bgB = bgR;
unsigned char timeR = 170, timeG = timeR, timeB = timeR;
int timelineSecInterval = 10;
PyObject* procCallback = NULL;
float volume = 1; // better default value here. note that we also do gain handling if it is set
float volumeSmoothClipX1 = 0.95, volumeSmoothClipX2 = 10;
static const char *kwlist[] = {
"song", "width", "height",
"backgroundColor", "timelineColor",
"timelineSecInterval",
"procCallback",
"volume",
"volumeSmoothClip",
NULL};
if(!PyArg_ParseTupleAndKeywords(args, kws, "O|ii(bbb)(bbb)iOf(ff):calcBitmapThumbnail", (char**)kwlist,
&songObj,
&bmpWidth, &bmpHeight,
&bgR, &bgG, &bgB,
&timeR, &timeG, &timeB,
&timelineSecInterval,
&procCallback,
&volume, &volumeSmoothClipX1, &volumeSmoothClipX2))
return NULL;
char* img = NULL;
PyObject* bmp = createBitmap24Bpp(bmpWidth, bmpHeight, &img);
if(!bmp)
return NULL; // out of memory
RDFTContext* fftCtx = NULL;
float* samplesBuf = NULL;
PyObject* returnObj = NULL;
PlayerObject* player = NULL;
unsigned long totalFrameCount = 0;
double songDuration = 0;
double samplesPerPixel = 0;
unsigned long frame = 0;
player = (PlayerObject*) pyCreatePlayer(NULL);
if(!player) goto final;
player->lock.enabled = false;
player->nextSongOnEof = false;
player->skipPyExceptions = false;
player->volume = volume;
player->volumeSmoothClip.setX(volumeSmoothClipX1, volumeSmoothClipX2);
player->playing = true; // otherwise audio_decode_frame() wont read
Py_INCREF(songObj);
player->curSong = songObj;
if(!player->openInStream()) goto final;
// First count totalFrameCount.
while(player->processInStream()) {
if(PyErr_Occurred()) goto final;
totalFrameCount += player->inStreamBuffer()->size() / player->outNumChannels / OUTSAMPLEBYTELEN;
player->inStreamBuffer()->clear();
}
songDuration = (double)totalFrameCount / player->outSamplerate;
// Seek back.
player->seekSong(0.0, false);
if(PyErr_Occurred()) goto final;
// init the processor
#define fftSizeLog2 (11)
#define fftSize (1 << fftSizeLog2)
float freqWindow[fftSize];
for(int i = 0; i < fftSize; ++i)
// Hanning window
freqWindow[i] = (float) (0.5 * (1.0 - cos((2.0 * M_PI * i) / (fftSize - 1))));
fftCtx = av_rdft_init(fftSizeLog2, DFT_R2C);
if(!fftCtx) {
printf("ERROR: av_rdft_init failed\n");
goto final;
}
// Note: We have to use av_mallocz here to have the right mem alignment.
// That is also why we can't allocate it on the stack (without doing alignment).
samplesBuf = (float *)av_mallocz(sizeof(float) * fftSize);
samplesPerPixel = totalFrameCount / (double)bmpWidth;
for(int x = 0; x < bmpWidth; ++x) {
// draw background
for(int y = 0; y < bmpHeight; ++y)
bmpSetPixel(img, bmpWidth, x, y, bgR, bgG, bgB);
// call the callback every 60 secs
if(procCallback && (int)(songDuration * x / bmpWidth / 60) < (int)(songDuration * (x+1) / bmpWidth / 60)) {
PyGILState_STATE gstate = PyGILState_Ensure();
Py_INCREF(bmp);
Py_INCREF(songObj);
PyObject* args = PyTuple_New(4);
PyTuple_SetItem(args, 0, songObj);
PyTuple_SetItem(args, 1, PyFloat_FromDouble((double) x / bmpWidth));
PyTuple_SetItem(args, 2, PyFloat_FromDouble(songDuration));
PyTuple_SetItem(args, 3, bmp);
PyObject* retObj = PyObject_CallObject(procCallback, args);
int stop = 0;
if(PyErr_Occurred()) {
PyErr_Print();
procCallback = NULL; // don't call again
stop = 1; // just break the whole thing
}
else if(retObj)
stop = !PyObject_IsTrue(retObj);
else // retObj == NULL, strange, should be error
stop = 1;
Py_XDECREF(retObj);
Py_DECREF(args); // this also decrefs song and bmp
if(stop) {
Py_DECREF(bmp);
bmp = NULL;
}
PyGILState_Release(gstate);
if(stop) goto final;
}
if((int)(songDuration * x / bmpWidth / timelineSecInterval) < (int)(songDuration * (x+1) / bmpWidth / timelineSecInterval)) {
// draw timeline
for(int y = 0; y < bmpHeight; ++y)
bmpSetPixel(img, bmpWidth, x, y, timeR, timeG, timeB);
}
int samplesBufIndex = 0;
memset(samplesBuf, 0, sizeof(float) * fftSize);
float peakMin = 0, peakMax = 0;
while(frame < (x + 1) * samplesPerPixel) {
if(!player->processInStream())
break; // probably EOF or so
if(PyErr_Occurred()) goto final;
for(auto& it : player->inStreamBuffer()->chunks) {
assert(it.size() % OUTSAMPLEBYTELEN == 0);
for(size_t i = 0; i < it.size() / OUTSAMPLEBYTELEN; ++i) {
OUTSAMPLE_t* sampleAddr = (OUTSAMPLE_t*) it.pt() + i;
OUTSAMPLE_t sample = *sampleAddr; // TODO: endian swap?
float sampleFloat = OutSampleAsFloat(sample);
if(sampleFloat < peakMin) peakMin = sampleFloat;
if(sampleFloat > peakMax) peakMax = sampleFloat;
if(samplesBufIndex < fftSize) {
samplesBuf[samplesBufIndex] += sampleFloat * freqWindow[samplesBufIndex] * 0.5f /* we do this twice for each channel */;
}
if(i % 2 == 1) samplesBufIndex++;
}
frame += it.size() / player->outNumChannels / OUTSAMPLEBYTELEN;
}
player->inStreamBuffer()->clear();
}
av_rdft_calc(fftCtx, samplesBuf);
float absFftData[fftSize / 2 + 1];
float *in_ptr = samplesBuf;
float *out_ptr = absFftData;
out_ptr[0] = in_ptr[0] * in_ptr[0];
out_ptr[fftSize / 2] = in_ptr[1] * in_ptr[1];
out_ptr += 1;
in_ptr += 2;
for(int i = 1; i < fftSize / 2; i++) {
*out_ptr++ = in_ptr[0] * in_ptr[0] + in_ptr[1] * in_ptr[1];
in_ptr += 2;
}
float energy = 0;
for(int i = 0; i < fftSize / 2; ++i)
energy += absFftData[i];
// compute the spectral centroid in hertz
float spectralCentroid = 0;
for(int i = 0; i < fftSize / 2; ++i)
spectralCentroid += absFftData[i] * i;
spectralCentroid /= energy;
spectralCentroid /= fftSize / 2;
spectralCentroid *= player->outSamplerate;
spectralCentroid *= 0.5;
// clip
static const float lowerFreq = 100;
static const float higherFreq = 22050;
if(spectralCentroid < lowerFreq) spectralCentroid = lowerFreq;
if(spectralCentroid > higherFreq) spectralCentroid = higherFreq;
// apply log so it's proportional to human perception of frequency
spectralCentroid = log10(spectralCentroid);
// scale to [0,1]
spectralCentroid -= log10(lowerFreq);
spectralCentroid /= (log10(higherFreq) - log10(lowerFreq));
//printf("x %i, peak %f,%f, spec %f\n", x, peakMin, peakMax, spectralCentroid);
// get color from spectralCentroid
unsigned char r = 0, g = 0, b = 0;
rainbowColor(spectralCentroid, &r, &g, &b);
int y1 = bmpHeight * 0.5 + peakMin * (bmpHeight - 4) * 0.5;
int y2 = bmpHeight * 0.5 + peakMax * (bmpHeight - 4) * 0.5;
if(y1 < 0) y1 = 0;
if(y2 >= bmpHeight) y2 = bmpHeight - 1;
// draw line
for(int y = y1; y <= y2; ++y)
bmpSetPixel(img, bmpWidth, x, y, r, g, b);
}
// We have to hold the Python GIL for this block
// because of the callback, other Python code/threads
// might have references to bmp.
{
PyGILState_STATE gstate = PyGILState_Ensure();
returnObj = PyTuple_New(2);
PyTuple_SetItem(returnObj, 0, PyFloat_FromDouble(songDuration));
PyTuple_SetItem(returnObj, 1, bmp);
bmp = NULL; // returnObj has taken over the reference
PyGILState_Release(gstate);
}
final:
Py_XDECREF(bmp); // this is multithreading safe in all cases where bmp != NULL
if(fftCtx)
av_rdft_end(fftCtx);
if(samplesBuf)
av_free(samplesBuf);
if(!PyErr_Occurred() && !returnObj) {
returnObj = Py_None;
Py_INCREF(returnObj);
}
Py_XDECREF(player);
return returnObj;
}