~mixxxdevelopers/mixxx/trunk

« back to all changes in this revision

Viewing changes to mixxx/src/musicbrainz/chromaprinter.cpp

  • Committer: Max Linke
  • Date: 2013-05-22 07:48:46 UTC
  • mfrom: (3366.1.16 chromaprint)
  • Revision ID: kain88@mixxx.org-20130522074846-e61uzchmtk5vcfhv
merge with chromaprint branch Fix Bug #804700

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QtCore>
 
2
#include <chromaprint.h>
 
3
 
 
4
#include "musicbrainz/chromaprinter.h"
 
5
#include "soundsourceproxy.h"
 
6
#include "defs.h"
 
7
 
 
8
chromaprinter::chromaprinter(QObject* parent)
 
9
             : QObject(parent){
 
10
}
 
11
 
 
12
QString chromaprinter::getFingerPrint(TrackPointer pTrack){
 
13
    SoundSourceProxy soundSource(pTrack);
 
14
    return calcFingerPrint(soundSource);
 
15
}
 
16
 
 
17
QString chromaprinter::getFingerPrint(QString location){
 
18
    SoundSourceProxy soundSource(location);
 
19
    return calcFingerPrint(soundSource);
 
20
}
 
21
 
 
22
QString chromaprinter::calcFingerPrint(SoundSourceProxy& soundSource){
 
23
    soundSource.open();
 
24
    m_SampleRate = soundSource.getSampleRate();
 
25
    unsigned int length = soundSource.length();
 
26
    if (m_SampleRate == 0 ){
 
27
        qDebug() << "Skipping invalid file:" << soundSource.getFilename();
 
28
        return QString();
 
29
    }
 
30
 
 
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
 
34
    // --kain88 July 2012
 
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;
 
39
    }
 
40
 
 
41
    SAMPLE *pData = new SAMPLE[m_NumSamples];
 
42
    QTime timerReadingFile;
 
43
    timerReadingFile.start();
 
44
    int read = soundSource.read(m_NumSamples, pData);
 
45
 
 
46
    if (read!=m_NumSamples) {
 
47
        qDebug() << "oh that's embarrasing I couldn't read the track";
 
48
        return QString();
 
49
    }
 
50
    qDebug("reading file took: %d ms" , timerReadingFile.elapsed());
 
51
 
 
52
    ChromaprintContext* ctx = chromaprint_new(CHROMAPRINT_ALGORITHM_DEFAULT);
 
53
    // we have 2 channels in mixxx always
 
54
    chromaprint_start(ctx, m_SampleRate, 2);
 
55
 
 
56
    QTime timerGeneratingFingerPrint;
 
57
    timerGeneratingFingerPrint.start();
 
58
    int success = chromaprint_feed(ctx, pData, m_NumSamples);
 
59
    if (!success) {
 
60
        qDebug() << "could not generate fingerprint";
 
61
        return QString();
 
62
    }
 
63
    chromaprint_finish(ctx);
 
64
 
 
65
    void* fprint = NULL;
 
66
    int size = 0;
 
67
    int ret = chromaprint_get_raw_fingerprint(ctx, &fprint, &size);
 
68
    QByteArray fingerprint;
 
69
    if (ret == 1) {
 
70
        void* encoded = NULL;
 
71
        int encoded_size = 0;
 
72
        chromaprint_encode_fingerprint(fprint, size,
 
73
                                       CHROMAPRINT_ALGORITHM_DEFAULT,
 
74
                                       &encoded,
 
75
                                       &encoded_size, 1);
 
76
 
 
77
        fingerprint.append(reinterpret_cast<char*>(encoded), encoded_size);
 
78
 
 
79
        chromaprint_dealloc(fprint);
 
80
        chromaprint_dealloc(encoded);
 
81
    }
 
82
    chromaprint_free(ctx);
 
83
    delete pData;
 
84
    
 
85
    qDebug("generating fingerprint took: %d ms" , timerGeneratingFingerPrint.elapsed());
 
86
 
 
87
    return fingerprint;
 
88
}