2012-01-06 16:58:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2012-01-09 15:30:59 +01:00
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "chromaprinter.h"
|
|
|
|
|
2012-01-09 15:30:59 +01:00
|
|
|
#include <QCoreApplication>
|
2012-01-06 16:58:46 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QEventLoop>
|
2012-01-09 15:30:59 +01:00
|
|
|
#include <QThread>
|
2012-01-06 16:58:46 +01:00
|
|
|
#include <QtDebug>
|
|
|
|
#include <QTime>
|
|
|
|
|
2012-01-07 16:25:39 +01:00
|
|
|
#include <chromaprint.h>
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
#include "core/logging.h"
|
2012-06-08 15:34:00 +02:00
|
|
|
#include "core/signalchecker.h"
|
2012-01-06 16:58:46 +01:00
|
|
|
|
2012-02-02 14:15:55 +01:00
|
|
|
static const int kDecodeRate = 11025;
|
|
|
|
static const int kDecodeChannels = 1;
|
2015-05-04 15:43:23 +02:00
|
|
|
static const int kPlayLengthSecs = 30;
|
|
|
|
static const int kTimeoutSecs = 10;
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
Chromaprinter::Chromaprinter(const QString& filename)
|
2015-05-04 15:11:31 +02:00
|
|
|
: filename_(filename), convert_element_(nullptr) {}
|
2012-01-06 16:58:46 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Chromaprinter::~Chromaprinter() {}
|
2012-01-06 16:58:46 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GstElement* Chromaprinter::CreateElement(const QString& factory_name,
|
|
|
|
GstElement* bin) {
|
2012-01-06 16:58:46 +01:00
|
|
|
GstElement* ret = gst_element_factory_make(
|
2014-02-07 16:34:20 +01:00
|
|
|
factory_name.toAscii().constData(), factory_name.toAscii().constData());
|
2012-01-06 16:58:46 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (ret && bin) gst_bin_add(GST_BIN(bin), ret);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
qLog(Warning) << "Couldn't create the gstreamer element" << factory_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Chromaprinter::CreateFingerprint() {
|
2012-01-09 15:30:59 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() != qApp->thread());
|
|
|
|
|
|
|
|
buffer_.open(QIODevice::WriteOnly);
|
|
|
|
|
2015-05-04 15:11:31 +02:00
|
|
|
GstElement* pipeline = gst_pipeline_new("pipeline");
|
|
|
|
GstElement* src = CreateElement("filesrc", pipeline);
|
|
|
|
GstElement* decode = CreateElement("decodebin", pipeline);
|
|
|
|
GstElement* convert = CreateElement("audioconvert", pipeline);
|
|
|
|
GstElement* resample = CreateElement("audioresample", pipeline);
|
|
|
|
GstElement* sink = CreateElement("appsink", pipeline);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
if (!src || !decode || !convert || !resample || !sink) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
convert_element_ = convert;
|
|
|
|
|
|
|
|
// Connect the elements
|
2014-02-06 16:49:49 +01:00
|
|
|
gst_element_link_many(src, decode, nullptr);
|
|
|
|
gst_element_link_many(convert, resample, nullptr);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
2013-09-25 15:42:13 +02:00
|
|
|
// Chromaprint expects mono 16-bit ints at a sample rate of 11025Hz.
|
2012-01-06 16:58:46 +01:00
|
|
|
GstCaps* caps = gst_caps_new_simple(
|
2015-05-04 15:11:31 +02:00
|
|
|
"audio/x-raw", "format", G_TYPE_STRING, "S16LE", "channels", G_TYPE_INT,
|
|
|
|
kDecodeChannels, "rate", G_TYPE_INT, kDecodeRate, NULL);
|
2012-01-06 16:58:46 +01:00
|
|
|
gst_element_link_filtered(resample, sink, caps);
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
|
|
|
|
GstAppSinkCallbacks callbacks;
|
|
|
|
memset(&callbacks, 0, sizeof(callbacks));
|
2013-09-25 15:42:13 +02:00
|
|
|
callbacks.new_sample = NewBufferCallback;
|
2014-02-07 16:34:20 +01:00
|
|
|
gst_app_sink_set_callbacks(reinterpret_cast<GstAppSink*>(sink), &callbacks,
|
|
|
|
this, nullptr);
|
2014-02-06 16:49:49 +01:00
|
|
|
g_object_set(G_OBJECT(sink), "sync", FALSE, nullptr);
|
|
|
|
g_object_set(G_OBJECT(sink), "emit-signals", TRUE, nullptr);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
// Set the filename
|
2014-02-06 16:49:49 +01:00
|
|
|
g_object_set(src, "location", filename_.toUtf8().constData(), nullptr);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
// Connect signals
|
2015-05-04 15:11:31 +02:00
|
|
|
GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
|
2013-09-25 15:42:13 +02:00
|
|
|
CHECKED_GCONNECT(decode, "pad-added", &NewPadCallback, this);
|
2015-05-04 15:11:31 +02:00
|
|
|
|
|
|
|
// Play only first x seconds
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_PAUSED);
|
|
|
|
// wait for state change before seeking
|
2015-05-04 15:43:23 +02:00
|
|
|
gst_element_get_state(pipeline, nullptr, nullptr, kTimeoutSecs * GST_SECOND);
|
2015-05-04 15:11:31 +02:00
|
|
|
gst_element_seek(pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
|
|
|
|
GST_SEEK_TYPE_SET, 0 * GST_SECOND, GST_SEEK_TYPE_SET,
|
2015-05-04 15:43:23 +02:00
|
|
|
kPlayLengthSecs * GST_SECOND);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
QTime time;
|
|
|
|
time.start();
|
|
|
|
|
|
|
|
// Start playing
|
2015-05-04 15:11:31 +02:00
|
|
|
gst_element_set_state(pipeline, GST_STATE_PLAYING);
|
|
|
|
|
|
|
|
// Wait until EOS or error
|
|
|
|
GstMessage* msg = gst_bus_timed_pop_filtered(
|
2015-05-04 15:43:23 +02:00
|
|
|
bus, kTimeoutSecs * GST_SECOND,
|
2015-05-04 15:11:31 +02:00
|
|
|
static_cast<GstMessageType>(GST_MESSAGE_EOS | GST_MESSAGE_ERROR));
|
|
|
|
|
|
|
|
if (msg != nullptr) {
|
|
|
|
if (msg->type == GST_MESSAGE_ERROR) {
|
|
|
|
// Report error
|
2015-05-04 15:43:23 +02:00
|
|
|
GError* error = nullptr;
|
|
|
|
gchar* debugs = nullptr;
|
2012-01-06 16:58:46 +01:00
|
|
|
|
2015-05-04 15:11:31 +02:00
|
|
|
gst_message_parse_error(msg, &error, &debugs);
|
|
|
|
QString message = QString::fromLocal8Bit(error->message);
|
|
|
|
|
|
|
|
g_error_free(error);
|
|
|
|
free(debugs);
|
|
|
|
|
|
|
|
qLog(Debug) << "Error processing" << filename_ << ":" << message;
|
|
|
|
}
|
|
|
|
gst_message_unref(msg);
|
|
|
|
}
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
int decode_time = time.restart();
|
|
|
|
|
|
|
|
buffer_.close();
|
2015-05-04 15:11:31 +02:00
|
|
|
|
|
|
|
// Generate fingerprint from recorded buffer data
|
2012-01-06 16:58:46 +01:00
|
|
|
QByteArray data = buffer_.data();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
ChromaprintContext* chromaprint =
|
|
|
|
chromaprint_new(CHROMAPRINT_ALGORITHM_DEFAULT);
|
2012-02-02 14:15:55 +01:00
|
|
|
chromaprint_start(chromaprint, kDecodeRate, kDecodeChannels);
|
2014-02-07 16:34:20 +01:00
|
|
|
chromaprint_feed(chromaprint, reinterpret_cast<void*>(data.data()),
|
|
|
|
data.size() / 2);
|
2012-01-06 16:58:46 +01:00
|
|
|
chromaprint_finish(chromaprint);
|
|
|
|
|
2014-02-06 16:49:49 +01:00
|
|
|
void* fprint = nullptr;
|
2012-01-06 16:58:46 +01:00
|
|
|
int size = 0;
|
|
|
|
int ret = chromaprint_get_raw_fingerprint(chromaprint, &fprint, &size);
|
|
|
|
QByteArray fingerprint;
|
|
|
|
if (ret == 1) {
|
2014-02-06 16:49:49 +01:00
|
|
|
void* encoded = nullptr;
|
2012-01-06 16:58:46 +01:00
|
|
|
int encoded_size = 0;
|
2014-02-07 16:34:20 +01:00
|
|
|
chromaprint_encode_fingerprint(fprint, size, CHROMAPRINT_ALGORITHM_DEFAULT,
|
|
|
|
&encoded, &encoded_size, 1);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
fingerprint.append(reinterpret_cast<char*>(encoded), encoded_size);
|
|
|
|
|
|
|
|
chromaprint_dealloc(fprint);
|
|
|
|
chromaprint_dealloc(encoded);
|
|
|
|
}
|
2012-02-02 14:13:34 +01:00
|
|
|
chromaprint_free(chromaprint);
|
2012-01-06 16:58:46 +01:00
|
|
|
int codegen_time = time.elapsed();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Decode time:" << decode_time
|
|
|
|
<< "Codegen time:" << codegen_time;
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
// Cleanup
|
2014-06-09 08:20:24 +02:00
|
|
|
callbacks.new_sample = nullptr;
|
2015-05-04 15:11:31 +02:00
|
|
|
gst_object_unref(bus);
|
|
|
|
gst_element_set_state(pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref(pipeline);
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
return fingerprint;
|
|
|
|
}
|
|
|
|
|
2013-09-25 15:42:13 +02:00
|
|
|
void Chromaprinter::NewPadCallback(GstElement*, GstPad* pad, gpointer data) {
|
2012-01-06 16:58:46 +01:00
|
|
|
Chromaprinter* instance = reinterpret_cast<Chromaprinter*>(data);
|
2014-02-07 16:34:20 +01:00
|
|
|
GstPad* const audiopad =
|
|
|
|
gst_element_get_static_pad(instance->convert_element_, "sink");
|
2012-01-06 16:58:46 +01:00
|
|
|
|
|
|
|
if (GST_PAD_IS_LINKED(audiopad)) {
|
|
|
|
qLog(Warning) << "audiopad is already linked, unlinking old pad";
|
|
|
|
gst_pad_unlink(audiopad, GST_PAD_PEER(audiopad));
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_pad_link(pad, audiopad);
|
|
|
|
gst_object_unref(audiopad);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GstFlowReturn Chromaprinter::NewBufferCallback(GstAppSink* app_sink,
|
|
|
|
gpointer self) {
|
2012-01-06 16:58:46 +01:00
|
|
|
Chromaprinter* me = reinterpret_cast<Chromaprinter*>(self);
|
|
|
|
|
2013-09-25 15:42:13 +02:00
|
|
|
GstSample* sample = gst_app_sink_pull_sample(app_sink);
|
|
|
|
GstBuffer* buffer = gst_sample_get_buffer(sample);
|
|
|
|
GstMapInfo map;
|
|
|
|
gst_buffer_map(buffer, &map, GST_MAP_READ);
|
|
|
|
me->buffer_.write(reinterpret_cast<const char*>(map.data), map.size);
|
|
|
|
gst_buffer_unmap(buffer, &map);
|
2012-01-06 16:58:46 +01:00
|
|
|
gst_buffer_unref(buffer);
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|