2
#include <chromaprint.h>
4
#include "musicbrainz/chromaprinter.h"
5
#include "soundsourceproxy.h"
8
chromaprinter::chromaprinter(QObject* parent)
12
QString chromaprinter::getFingerPrint(TrackPointer pTrack){
13
SoundSourceProxy soundSource(pTrack);
14
return calcFingerPrint(soundSource);
17
QString chromaprinter::getFingerPrint(QString location){
18
SoundSourceProxy soundSource(location);
19
return calcFingerPrint(soundSource);
22
QString chromaprinter::calcFingerPrint(SoundSourceProxy& soundSource){
24
m_SampleRate = soundSource.getSampleRate();
25
unsigned int length = soundSource.length();
26
if (m_SampleRate == 0 ){
27
qDebug() << "Skipping invalid file:" << soundSource.getFilename();
31
// this is worth 2min of audio, multiply by 2 because we have 2 channels
32
// AcoustID only stores a fingerprint for the first two minutes of a song
33
// on their server so we need only a fingerprint of the first two minutes
35
m_NumSamples = 120*2*m_SampleRate;
36
// check that the song is actually longer then the amount of audio we use
37
if (m_NumSamples > length) {
38
m_NumSamples = length;
41
SAMPLE *pData = new SAMPLE[m_NumSamples];
42
QTime timerReadingFile;
43
timerReadingFile.start();
44
int read = soundSource.read(m_NumSamples, pData);
46
if (read!=m_NumSamples) {
47
qDebug() << "oh that's embarrasing I couldn't read the track";
50
qDebug("reading file took: %d ms" , timerReadingFile.elapsed());
52
ChromaprintContext* ctx = chromaprint_new(CHROMAPRINT_ALGORITHM_DEFAULT);
53
// we have 2 channels in mixxx always
54
chromaprint_start(ctx, m_SampleRate, 2);
56
QTime timerGeneratingFingerPrint;
57
timerGeneratingFingerPrint.start();
58
int success = chromaprint_feed(ctx, pData, m_NumSamples);
60
qDebug() << "could not generate fingerprint";
63
chromaprint_finish(ctx);
67
int ret = chromaprint_get_raw_fingerprint(ctx, &fprint, &size);
68
QByteArray fingerprint;
72
chromaprint_encode_fingerprint(fprint, size,
73
CHROMAPRINT_ALGORITHM_DEFAULT,
77
fingerprint.append(reinterpret_cast<char*>(encoded), encoded_size);
79
chromaprint_dealloc(fprint);
80
chromaprint_dealloc(encoded);
82
chromaprint_free(ctx);
85
qDebug("generating fingerprint took: %d ms" , timerGeneratingFingerPrint.elapsed());