PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/liblastfm2/src/fingerprint/Fingerprint.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 116 lines | 55 code | 21 blank | 40 comment | 1 complexity | d20ef9fa4b0be80c9eba9ba61c695cb4 MD5 | raw file
  1/*
  2   Copyright 2009 Last.fm Ltd. 
  3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4
  5   This file is part of liblastfm.
  6
  7   liblastfm is free software: you can redistribute it and/or modify
  8   it under the terms of the GNU General Public License as published by
  9   the Free Software Foundation, either version 3 of the License, or
 10   (at your option) any later version.
 11
 12   liblastfm is distributed in the hope that it will be useful,
 13   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License
 18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 19*/
 20#ifndef LASTFM_FINGERPRINT_H
 21#define LASTFM_FINGERPRINT_H
 22
 23#include <lastfm/FingerprintId>
 24#include <lastfm/Track>
 25
 26
 27namespace lastfm
 28{
 29    class LASTFM_FINGERPRINT_DLLEXPORT Fingerprint
 30    {
 31        lastfm::Track m_track;
 32        QByteArray m_data;
 33        int m_id;
 34        int m_duration;
 35
 36    protected:
 37        bool m_complete;
 38
 39    public:
 40        /** represents a partial fingerprint of 20 seconds of music, this is 
 41          * considered 99.9999...9999% unique and so we use it for most stuff as 
 42          * it is much quicker than a complete fingerprint, still though, you
 43          * should do the generate step in a thread. */
 44        Fingerprint( const lastfm::Track& );
 45    
 46        /** if the id isNull(), then you'll need to do generate, submit and decode */
 47        FingerprintId id() const { return m_id; }
 48
 49        /** The actual data that is the fingerprint, this is about 70kB or so,
 50          * there isn't anything in it until you call generate. */
 51        QByteArray data() const { return m_data; }
 52
 53        enum Error
 54        {
 55            ReadError = 0,
 56
 57            /** failed to extract samplerate, bitrate, channels, duration etc */
 58            HeadersError,
 59
 60            DecodeError,
 61        
 62            /** there is a minimum track duration for fingerprinting */
 63            TrackTooShortError,
 64            
 65            /** the fingerprint service went wrong, or we submitted bad data, 
 66              * or myabe the request failed, whatever, we couldn't parse the 
 67              * result */
 68            BadResponseError,
 69        
 70            /** sorry, liblastfm sucks, report bug with log! */
 71            InternalError            
 72        };
 73
 74        /** This is CPU intensive, do it in a thread in your GUI application */
 75        void generate( FingerprintableSource* ) throw( Error );
 76
 77        /** Submits the fingerprint data to Last.fm in order to get a FingerprintId
 78          * back. You need to wait for the QNetworkReply to finish before you can
 79          * pass it to decode clearly. */
 80        QNetworkReply* submit() const;
 81
 82        /** Pass a finished reply from submit(), if the response is sound, id()
 83          * will be valid. Otherwise we will throw. You always get a valid id
 84          * or a throw.
 85          */
 86        void decode( QNetworkReply*, bool* lastfm_needs_a_complete_fingerprint = 0 ) throw( Error );
 87    };
 88
 89
 90    class CompleteFingerprint : public Fingerprint
 91    {
 92    public:
 93        CompleteFingerprint( const lastfm::Track& t ) : Fingerprint( t )
 94        {
 95            m_complete = true;
 96        }
 97    };
 98}
 99
100
101inline QDebug operator<<( QDebug d, lastfm::Fingerprint::Error e )
102{
103    #define CASE(x) case lastfm::Fingerprint::x: return d << #x;
104    switch (e)
105    {
106        CASE(ReadError)
107        CASE(HeadersError)
108        CASE(DecodeError)
109        CASE(TrackTooShortError)
110        CASE(BadResponseError)
111        CASE(InternalError)
112    }
113    #undef CASE
114}
115
116#endif