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