File: mainprint.cpp

package info (click to toggle)
libofa 0.9.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,920 kB
  • ctags: 460
  • sloc: cpp: 24,470; sh: 8,366; makefile: 45; ansic: 14
file content (328 lines) | stat: -rw-r--r-- 8,465 bytes parent folder | download | duplicates (8)
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
/* ------------------------------------------------------------------

   libofa -- the Open Fingerprint Architecture library

   Copyright (C) 2006 MusicIP Corporation
   All rights reserved.

-------------------------------------------------------------------*/
// FILE: "mainprint.cpp"
// MODULE: Top level calling code and main functions
// AUTHOR: Frode Holm
// DATE CREATED: 1/12/06

#include <vector>
#include "ofa1/ofa.h"
#include "signal_op.h"
#include "fft_op.h"
#include "frametracker_op.h"
#include "error_op.h"

#include "JAMA/jama_svd.h"

using namespace TNT;
using namespace JAMA;

typedef float Real;

// Print size
const int Dim = 7;
const int Res = 40;

const long SongLen = 120000;	// length to analyze (ms)
const int FrameSize = 8192;	// FFT framesize

void preprocessing(short* samples, long size, int sRate, bool stereo, Signal_op& sig);
void core_print(Signal_op& sig, unsigned char *out);
void pitch_print(Signal_op& sig, unsigned char *out);
char *base64encode(const char *input, int lentext);

// Retreive the version of the library
extern "C"
void ofa_get_version(int *major, int *minor, int *rev)
{
    sscanf(VERSION, "%d.%d.%d", major, minor, rev);
}

// ofa_create_print is the top level function generating the fingerprint.
// NOTE THAT THE PASSED IN DATA MAY BE BYTE SWAPPED DURING THE METHOD.
// ASSUME THAT DATA IN THE INPUT BUFFER IS DESTROYED AS A SIDE-EFFECT OF 
// CALLING THIS FUNCTION
//
// data: a buffer of 16-bit samples in interleaved format (if stereo), i.e. L,R,L,R, etc.
//		This buffer is destroyed during processing.
//		Ideally, this buffer should contain the entire song to be analyzed, but the process will only
//		need the first 2min + 10sec + any silence prepending the actual audio. Since the precise silence
//		interval will only be known after a couple of processing steps, the caller must make adequate
//		allowance for this. Caveat emptor.
// byteOrder: OFA_LITTLE_ENDIAN, or OFA_BIG_ENDIAN - indicates the byte
//            order of the data being passed in.
// size: the size of the buffer, in number of samples.
// sRate: the sample rate of the signal. This can be an arbitrary rate, as long as it can be expressed
//		as an integer (in samples per second). If this is different from 44100, rate conversion will
//		be performed during preprocessing, which will add significantly to the overhead.
// stereo: 1 if there are left and right channels stored, 0 if the data is mono
//
// On success, a valid text representation of the fingerprint is returned.
// The returned buffer will remain valid until the next call to ofa_create_print

extern "C"
const char *ofa_create_print(unsigned char *data, int byteOrder, long size, int sRate, int stereo)
{
    short *samples = (short *) data;
#ifdef BIG_ENDIAN
    if (byteOrder == OFA_LITTLE_ENDIAN) {
	for (int i = 0; i < size; ++i) {
	    samples[i] = data[2*i+1] << 8 | data[2*i];
	}
    }
#else
    if (byteOrder == OFA_BIG_ENDIAN) {
	for (int i = 0; i < size; ++i) {
	    samples[i] = data[2*i] << 8 | data[2*i+1];
	}
    }
#endif
    try {
	Signal_op sig;
	unsigned char bytes[Dim * Res * 2 + 5];

	preprocessing(samples, size, sRate, stereo, sig);
	bytes[0] = 1; // version marker
	core_print(sig, bytes + 1);
	pitch_print(sig, bytes + (Dim * Res * 2 + 1));
	return base64encode((char*) bytes, Dim * Res * 2 + 5);
    } catch (OnePrintError e) {
	return 0;
    }
}


void
preprocessing(short* samples, long size, int sRate, bool stereo, Signal_op& sig)
{
	int ch = stereo ? 2 : 1;
	long sec135 = 135 * sRate * ch;
	if (size > sec135) size = sec135; 

	sig.Load(samples, size, sRate, stereo);

	if (stereo)
		sig.PrepareStereo(44100, 50);
	else
		sig.PrepareMono(44100, 50);

	if (sig.GetDuration() > SongLen+10000)
		sig.CutSignal(10000, SongLen);
}

void
core_print(Signal_op& sig, unsigned char *out)
{
	FFT_op fft;

	fft.LoadSignal(&sig);
	fft.SetSize(FrameSize,false);
	fft.SetWindowShape(HAMMING);
	fft.Compute(0);

	fft.ReSample(Res, true);

	if (fft.GetNumFrames() < Res)
		throw OnePrintError(FILETOOSHORT);

	// Compute SVD
	int i,j;
	float* fr;
	int numBins = fft.GetNumBins();
	int numFrames = fft.GetNumFrames();

	Array2D<Real> in2D(numFrames, numBins);
	Array2D<Real> v(numBins, numBins);

	// copy into Array2D
	for (i = 0; i < numFrames; i++)
	{
		fr = fft.GetFrame(i);
		for (j = 0; j < numBins; j++)
			in2D[i][j] = fr[j];
	}

	SVD<Real> s(in2D);
	s.getV(v);

	int pos = 0;
	for (i = 0; i < Dim; i++) {
	    for (j = 0; j < Res; j++) {
		short value = short(v[j][i] * 32767);
		out[pos++] = ((value & 0xff00) >> 8);
		out[pos++] = (value & 0x00ff);
	    }
	}
}


struct pitchPacket {
	pitchPacket() { dur = 0; tracks = 0; amp = 0; }
	double dur;
	int tracks;
	double amp;
};


void 
pitch_print(Signal_op& sig, unsigned char *out)
{
	if (sig.GetDuration() > 40000)
		sig.CutSignal(0, 30000);

	FFT_op fft;

	fft.LoadSignal(&sig);
	fft.SetSize(FrameSize,false);
	fft.SetWindowShape(HAMMING);
	fft.Compute(0.8);

	FrameTracker_op fTrk(0.005f, 0.03f,  0.1f);
	fTrk.Compute(fft);

	vector<pitchPacket> notes(128);
	double loFreq = 50;
	double hiFreq = 1500;

	// Collect track statistics
	TrackList_op* trl = fTrk.getTracks();
	TrackFrame_op* base = trl->getBaseFrame();
	double dur, amp;
	int avPitch;
	int totalTracks = 0;
	while (base != 0) 
	{
		TrackData_op* td = base->getBaseTrack();
		while (td != 0) 
		{
			if (td->isHead() && td->getAvgPitch() > loFreq && td->getAvgPitch() < hiFreq) 
			{
				dur = td->getDuration();
				avPitch = fft.FreqToMidi(td->getAvgPitch());
				amp = td->getAvgAmplitude();
				notes[avPitch].dur += dur;
				notes[avPitch].tracks++;
				notes[avPitch].amp += amp;
				totalTracks++;
			}
			td = td->getHigher();
		}
		base = base->getNext();
	}

	// Find the 4 most prominent notes
	double maxStrength[4];
	int index[4];
	int i;

	for (i=0; i<4; i++)
	{
		maxStrength[i] = 0;
		index[i] = 0;
	}
	for (i=0; i<128; i++)
	{
		if (notes[i].tracks == 0) continue;

		double strength = notes[i].amp + notes[i].dur/10000.0;			// "linear" spread

		// "manual" sort
		if (strength > maxStrength[0])
		{
			maxStrength[3] = maxStrength[2];
			maxStrength[2] = maxStrength[1];
			maxStrength[1] = maxStrength[0];
			maxStrength[0] = strength;
			index[3] = index[2];
			index[2] = index[1];
			index[1] = index[0];
			index[0] = i;
		}
		else if (strength > maxStrength[1])
		{
			maxStrength[3] = maxStrength[2];
			maxStrength[2] = maxStrength[1];
			maxStrength[1] = strength;
			index[3] = index[2];
			index[2] = index[1];
			index[1] = i;
		}
		else if (strength > maxStrength[2])
		{
			maxStrength[3] = maxStrength[2];
			maxStrength[2] = strength;
			index[3] = index[2];
			index[2] = i;
		}
		else if (strength > maxStrength[3])
		{
			maxStrength[3] = strength;
			index[3] = i;
		}
	}

	for (i=0; i<4; i++)
	{
		out[i] = index[i];
	}
}

static char encodingTable[64] = {
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',    
    'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',    
    'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',    
    'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};


// The return buffer is only valid until the next call to this method
char *base64encode(const char *input, int lentext) {
    static char out[758];
    unsigned char inbuf[3], outbuf[4];
    int i, ctcopy, pos = 0, ixtext = 0;
    
    while (true) {
        int ctremaining = lentext - ixtext;
        if (ctremaining <= 0)
            break;
        for (i = 0; i < 3; i++) { 
            int ix = ixtext + i;
            if (ix < lentext)
                inbuf[i] = (unsigned char) input[ix];
            else
                inbuf[i] = 0;
        }
        outbuf[0] = (unsigned char) ((inbuf [0] & 0xFC) >> 2);
        outbuf[1] = (unsigned char) (((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4));
        outbuf[2] = (unsigned char) (((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6));
        outbuf[3] = (unsigned char) (inbuf [2] & 0x3F);
	
        switch (ctremaining) {
        case 1: 
            ctcopy = 2; 
            break;
        case 2: 
            ctcopy = 3; 
            break;
        default:
            ctcopy = 4;
            break;
        }
        for (i = 0; i < ctcopy; i++) {
            out[pos++] = encodingTable[outbuf[i]];
        }
        for (i = ctcopy; i < 4; i++) {
            out[pos++] = '=';
        }
        ixtext += 3;
    }
    out[pos] = 0;
    return out;
}