strawberry-audio-player-win.../src/engine/chromaprinter.cpp

228 lines
6.9 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2012, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <glib-object.h>
2020-06-14 23:54:18 +02:00
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <chromaprint.h>
#include <gst/gst.h>
2018-02-27 18:06:05 +01:00
#include <QtGlobal>
2018-02-27 18:06:05 +01:00
#include <QCoreApplication>
#include <QThread>
#include <QIODevice>
#include <QBuffer>
#include <QByteArray>
#include <QString>
#include <QElapsedTimer>
#include <QtDebug>
2018-02-27 18:06:05 +01:00
#include "chromaprinter.h"
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "core/signalchecker.h"
2018-07-01 01:29:52 +02:00
#ifndef u_int32_t
typedef unsigned int u_int32_t;
#endif
2018-02-27 18:06:05 +01:00
static const int kDecodeRate = 11025;
static const int kDecodeChannels = 1;
static const int kPlayLengthSecs = 30;
static const int kTimeoutSecs = 10;
Chromaprinter::Chromaprinter(const QString &filename)
: filename_(filename),
convert_element_(nullptr) {}
GstElement *Chromaprinter::CreateElement(const QString &factory_name, GstElement *bin) {
2021-06-12 16:06:30 +02:00
GstElement *ret = gst_element_factory_make(factory_name.toLatin1().constData(), factory_name.toLatin1().constData());
2018-02-27 18:06:05 +01:00
if (ret && bin) gst_bin_add(GST_BIN(bin), ret);
if (!ret) {
qLog(Warning) << "Couldn't create the gstreamer element" << factory_name;
}
return ret;
}
QString Chromaprinter::CreateFingerprint() {
Q_ASSERT(QThread::currentThread() != qApp->thread());
buffer_.open(QIODevice::WriteOnly);
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);
if (!src || !decode || !convert || !resample || !sink) {
return QString();
}
convert_element_ = convert;
// Connect the elements
gst_element_link_many(src, decode, nullptr);
gst_element_link_many(convert, resample, nullptr);
// Chromaprint expects mono 16-bit ints at a sample rate of 11025Hz.
GstCaps *caps = gst_caps_new_simple(
"audio/x-raw",
"format", G_TYPE_STRING, "S16LE",
"channels", G_TYPE_INT, kDecodeChannels,
"rate", G_TYPE_INT, kDecodeRate,
2019-09-08 21:18:26 +02:00
nullptr);
2018-02-27 18:06:05 +01:00
gst_element_link_filtered(resample, sink, caps);
gst_caps_unref(caps);
GstAppSinkCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.new_sample = NewBufferCallback;
gst_app_sink_set_callbacks(reinterpret_cast<GstAppSink*>(sink), &callbacks, this, nullptr);
2018-02-27 18:06:05 +01:00
g_object_set(G_OBJECT(sink), "sync", FALSE, nullptr);
g_object_set(G_OBJECT(sink), "emit-signals", TRUE, nullptr);
// Set the filename
g_object_set(src, "location", filename_.toUtf8().constData(), nullptr);
// Connect signals
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
CHECKED_GCONNECT(decode, "pad-added", &NewPadCallback, this);
// Play only first x seconds
gst_element_set_state(pipeline, GST_STATE_PAUSED);
// wait for state change before seeking
gst_element_get_state(pipeline, nullptr, nullptr, kTimeoutSecs * GST_SECOND);
gst_element_seek(pipeline, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, 0 * GST_SECOND, GST_SEEK_TYPE_SET, kPlayLengthSecs * GST_SECOND);
QElapsedTimer time;
2018-02-27 18:06:05 +01:00
time.start();
// Start playing
gst_element_set_state(pipeline, GST_STATE_PLAYING);
// Wait until EOS or error
GstMessage *msg = gst_bus_timed_pop_filtered(bus, kTimeoutSecs * GST_SECOND, static_cast<GstMessageType>(GST_MESSAGE_EOS | GST_MESSAGE_ERROR));
if (msg != nullptr) {
if (msg->type == GST_MESSAGE_ERROR) {
// Report error
GError *error = nullptr;
gchar *debugs = nullptr;
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);
}
int decode_time = time.restart();
buffer_.close();
// Generate fingerprint from recorded buffer data
QByteArray data = buffer_.data();
ChromaprintContext *chromaprint = chromaprint_new(CHROMAPRINT_ALGORITHM_DEFAULT);
chromaprint_start(chromaprint, kDecodeRate, kDecodeChannels);
2021-03-21 18:53:02 +01:00
chromaprint_feed(chromaprint, reinterpret_cast<int16_t*>(data.data()), static_cast<int>(data.size() / 2));
2018-02-27 18:06:05 +01:00
chromaprint_finish(chromaprint);
u_int32_t *fprint = nullptr;
int size = 0;
2018-02-27 18:06:05 +01:00
int ret = chromaprint_get_raw_fingerprint(chromaprint, &fprint, &size);
QByteArray fingerprint;
if (ret == 1) {
char *encoded = nullptr;
2018-02-27 18:06:05 +01:00
int encoded_size = 0;
chromaprint_encode_fingerprint(fprint, size, CHROMAPRINT_ALGORITHM_DEFAULT, &encoded, &encoded_size, 1);
fingerprint.append(reinterpret_cast<char*>(encoded), encoded_size);
chromaprint_dealloc(fprint);
chromaprint_dealloc(encoded);
}
chromaprint_free(chromaprint);
int codegen_time = time.elapsed();
qLog(Debug) << "Decode time:" << decode_time << "Codegen time:" << codegen_time;
// Cleanup
callbacks.new_sample = nullptr;
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return fingerprint;
}
void Chromaprinter::NewPadCallback(GstElement*, GstPad *pad, gpointer data) {
Chromaprinter *instance = reinterpret_cast<Chromaprinter*>(data);
GstPad *const audiopad = gst_element_get_static_pad(instance->convert_element_, "sink");
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);
}
GstFlowReturn Chromaprinter::NewBufferCallback(GstAppSink *app_sink, gpointer self) {
Chromaprinter *me = reinterpret_cast<Chromaprinter*>(self);
GstSample *sample = gst_app_sink_pull_sample(app_sink);
2019-04-22 00:42:03 +02:00
if (!sample) return GST_FLOW_ERROR;
2018-02-27 18:06:05 +01:00
GstBuffer *buffer = gst_sample_get_buffer(sample);
2019-04-22 00:42:03 +02:00
if (buffer) {
GstMapInfo map;
if (gst_buffer_map(buffer, &map, GST_MAP_READ)) {
me->buffer_.write(reinterpret_cast<const char*>(map.data), map.size);
gst_buffer_unmap(buffer, &map);
}
}
gst_sample_unref(sample);
2018-02-27 18:06:05 +01:00
return GST_FLOW_OK;
}