File indexing completed on 2024-05-19 04:56:25

0001 /**
0002  * \file fingerprintcalculator.h
0003  * Chromaprint fingerprint calculator.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 21 Jan 2012
0008  *
0009  * Copyright (C) 2012-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QObject>
0030 #include <QString>
0031 #include <chromaprint.h>
0032 
0033 class AbstractFingerprintDecoder;
0034 
0035 /**
0036  * Calculate Chromaprint audio fingerprints for audio files.
0037  */
0038 class FingerprintCalculator : public QObject {
0039   Q_OBJECT
0040 public:
0041   /** Types of error occurring in fingerprint calculation. */
0042   enum Error {
0043     Ok,               /**< Fingerprint calculation OK */
0044     Pending,          /**< Not started */
0045     NoStreamFound,    /**< Format not recognized or no audio stream found */
0046     NoCodecFound,     /**< No codec found */
0047     NoConverterFound, /**< Sample rate conversion failed or unavailable */
0048     FingerprintCalculationFailed, /**< Chromaprint error */
0049     Timeout,          /**< Operation timeout */
0050     DecoderError      /**< Error while decoding */
0051   };
0052 
0053   /**
0054    * Constructor.
0055    */
0056   explicit FingerprintCalculator(QObject* parent = nullptr);
0057 
0058   /**
0059    * Destructor.
0060    */
0061   ~FingerprintCalculator() override;
0062 
0063   /**
0064    * Calculate audio fingerprint for audio file.
0065    * When the calculation is finished, finished() is emitted.
0066    *
0067    * @param fileName path to audio file
0068    */
0069   void start(const QString& fileName);
0070 
0071   /**
0072    * Stop decoder.
0073    */
0074   void stop();
0075 
0076 signals:
0077   /**
0078    * Emitted when the fingerprint calculation is finished.
0079    *
0080    * @param fingerprint Chromaprint fingerprint
0081    * @param duration duration in seconds
0082    * @param error error code, enum FingerprintCalculator::Error
0083    */
0084   void finished(const QString& fingerprint, int duration, int error);
0085 
0086 private slots:
0087   /**
0088    * Called when decoding starts.
0089    * @param sampleRate sample rate of the audio stream (in Hz)
0090    * @param channelCount numbers of channels in the audio stream (1 or 2)
0091    */
0092   void startChromaprint(int sampleRate, int channelCount);
0093 
0094   /**
0095    * Called when decoded data is available.
0096    * @param data 16-bit signed integers in native byte-order
0097    */
0098   void feedChromaprint(QByteArray data);
0099 
0100   /**
0101    * Called when an error occurs.
0102    * @param err error code, enum FingerprintCalculator::Error
0103    */
0104   void receiveError(int err);
0105 
0106   /**
0107    * Called when decoding finished successfully.
0108    * @param duration duration of stream in seconds
0109    */
0110   void finishChromaprint(int duration);
0111 
0112 private:
0113   ChromaprintContext* m_chromaprintCtx;
0114   AbstractFingerprintDecoder* m_decoder;
0115 };