TEST(API, Test2SilenceFp) { short zeroes[1024]; std::fill(zeroes, zeroes + 1024, 0); ChromaprintContext *ctx = chromaprint_new(CHROMAPRINT_ALGORITHM_TEST2); ASSERT_NE(nullptr, ctx); SCOPE_EXIT(chromaprint_free(ctx)); ASSERT_EQ(1, chromaprint_start(ctx, 44100, 1)); for (int i = 0; i < 130; i++) { ASSERT_EQ(1, chromaprint_feed(ctx, zeroes, 1024)); } char *fp; uint32_t fp_hash; ASSERT_EQ(1, chromaprint_finish(ctx)); ASSERT_EQ(1, chromaprint_get_fingerprint(ctx, &fp)); SCOPE_EXIT(chromaprint_dealloc(fp)); ASSERT_EQ(1, chromaprint_get_fingerprint_hash(ctx, &fp_hash)); ASSERT_EQ(18, strlen(fp)); EXPECT_EQ(std::string("AQAAA0mUaEkSRZEGAA"), std::string(fp)); ASSERT_EQ(627964279, fp_hash); }
TEST(API, Test2SilenceRawFp) { short zeroes[1024]; std::fill(zeroes, zeroes + 1024, 0); ChromaprintContext *ctx = chromaprint_new(CHROMAPRINT_ALGORITHM_TEST2); ASSERT_NE(nullptr, ctx); SCOPE_EXIT(chromaprint_free(ctx)); ASSERT_EQ(1, chromaprint_start(ctx, 44100, 1)); for (int i = 0; i < 130; i++) { ASSERT_EQ(1, chromaprint_feed(ctx, zeroes, 1024)); } uint32_t *fp; int length; ASSERT_EQ(1, chromaprint_finish(ctx)); ASSERT_EQ(1, chromaprint_get_raw_fingerprint(ctx, &fp, &length)); SCOPE_EXIT(chromaprint_dealloc(fp)); ASSERT_EQ(3, length); EXPECT_EQ(627964279, fp[0]); EXPECT_EQ(627964279, fp[1]); EXPECT_EQ(627964279, fp[2]); }
static int write_trailer(AVFormatContext *s) { ChromaprintMuxContext *cpr = s->priv_data; AVIOContext *pb = s->pb; void *fp = NULL, *enc_fp = NULL; int size, enc_size, ret = AVERROR(EINVAL); if (!chromaprint_finish(cpr->ctx)) { av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n"); goto fail; } if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) { av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n"); goto fail; } switch (cpr->fp_format) { case FINGERPRINT_RAW: avio_write(pb, fp, size); break; case FINGERPRINT_COMPRESSED: case FINGERPRINT_BASE64: if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size, cpr->fp_format == FINGERPRINT_BASE64)) { av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n"); goto fail; } avio_write(pb, enc_fp, enc_size); break; } ret = 0; fail: if (fp) chromaprint_dealloc(fp); if (enc_fp) chromaprint_dealloc(enc_fp); cleanup(cpr); return ret; }
static void gst_chromaprint_reset (GstChromaprint * chromaprint) { if (chromaprint->fingerprint) { chromaprint_dealloc (chromaprint->fingerprint); chromaprint->fingerprint = NULL; } chromaprint->nsamples = 0; chromaprint->duration = 0; chromaprint->record = TRUE; }
TEST(API, TestEncodeFingerprintBase64) { uint32_t fingerprint[] = { 1, 0 }; char expected[] = "NwAAAkEA"; char *encoded; int encoded_size; ASSERT_EQ(1, chromaprint_encode_fingerprint(fingerprint, 2, 55, &encoded, &encoded_size, 1)); SCOPE_EXIT(chromaprint_dealloc(encoded)); ASSERT_EQ(8, encoded_size); ASSERT_STREQ(expected, encoded); }
TEST(API, TestDecodeFingerprint) { char data[] = { 55, 0, 0, 2, 65, 0 }; uint32_t *fingerprint; int size; int algorithm; ASSERT_EQ(1, chromaprint_decode_fingerprint(data, 6, &fingerprint, &size, &algorithm, 0)); SCOPE_EXIT(chromaprint_dealloc(fingerprint)); ASSERT_EQ(2, size); ASSERT_EQ(55, algorithm); ASSERT_EQ(1, fingerprint[0]); ASSERT_EQ(0, fingerprint[1]); }
TEST(API, TestEncodeFingerprint) { uint32_t fingerprint[] = { 1, 0 }; char expected[] = { 55, 0, 0, 2, 65, 0 }; char *encoded; int encoded_size; ASSERT_EQ(1, chromaprint_encode_fingerprint(fingerprint, 2, 55, &encoded, &encoded_size, 0)); SCOPE_EXIT(chromaprint_dealloc(encoded)); ASSERT_EQ(6, encoded_size); for (int i = 0; i < encoded_size; i++) { ASSERT_EQ(expected[i], encoded[i]) << "Different at " << i; } }
static void gst_chromaprint_finalize (GObject * object) { GstChromaprint *chromaprint = GST_CHROMAPRINT (object); chromaprint->record = FALSE; if (chromaprint->context) { chromaprint_free (chromaprint->context); chromaprint->context = NULL; } if (chromaprint->fingerprint) { chromaprint_dealloc (chromaprint->fingerprint); chromaprint->fingerprint = NULL; } G_OBJECT_CLASS (parent_class)->finalize (object); }
TEST(API, TestFp) { std::vector<short> data = LoadAudioFile("data/test_stereo_44100.raw"); ChromaprintContext *ctx = chromaprint_new(CHROMAPRINT_ALGORITHM_TEST2); ASSERT_NE(nullptr, ctx); SCOPE_EXIT(chromaprint_free(ctx)); ASSERT_EQ(1, chromaprint_get_num_channels(ctx)); ASSERT_EQ(11025, chromaprint_get_sample_rate(ctx)); ASSERT_EQ(1, chromaprint_start(ctx, 44100, 1)); ASSERT_EQ(1, chromaprint_feed(ctx, data.data(), data.size())); char *fp; uint32_t fp_hash; ASSERT_EQ(1, chromaprint_finish(ctx)); ASSERT_EQ(1, chromaprint_get_fingerprint(ctx, &fp)); SCOPE_EXIT(chromaprint_dealloc(fp)); ASSERT_EQ(1, chromaprint_get_fingerprint_hash(ctx, &fp_hash)); EXPECT_EQ(std::string("AQAAC0kkZUqYREkUnFAXHk8uuMZl6EfO4zu-4ABKFGESWIIMEQE"), std::string(fp)); ASSERT_EQ(3732003127, fp_hash); }
static void Finish( sout_stream_t *p_stream ) { sout_stream_sys_t *p_sys = p_stream->p_sys; char *psz_fingerprint = NULL; if ( p_sys->b_finished && chromaprint_finish( p_sys->p_chromaprint_ctx ) ) { chromaprint_get_fingerprint( p_sys->p_chromaprint_ctx, &psz_fingerprint ); if ( psz_fingerprint ) { p_sys->p_data->i_duration = p_sys->i_total_samples / p_sys->id->i_samplerate; p_sys->p_data->psz_fingerprint = strdup( psz_fingerprint ); chromaprint_dealloc( psz_fingerprint ); msg_Dbg( p_stream, "DURATION=%u;FINGERPRINT=%s", p_sys->p_data->i_duration, p_sys->p_data->psz_fingerprint ); } } else { msg_Dbg( p_stream, "Cannot create %us fingerprint (not enough samples?)", p_sys->i_duration ); } p_sys->b_done = true; msg_Dbg( p_stream, "Fingerprinting finished" ); }
void groove_fingerprinter_free_info(struct GrooveFingerprinterInfo *info) { if (!info->fingerprint) return; chromaprint_dealloc((void*)info->fingerprint); info->fingerprint = NULL; }
QString Chromaprinter::CreateFingerprint() { Q_ASSERT(QThread::currentThread() != qApp->thread()); buffer_.open(QIODevice::WriteOnly); GMainContext* context = g_main_context_new(); g_main_context_push_thread_default(context); event_loop_ = g_main_loop_new(context, FALSE); pipeline_ = gst_pipeline_new("pipeline"); GstElement* src = CreateElement("filesrc", pipeline_); GstElement* decode = CreateElement("decodebin2", 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 floats at a sample rate of 11025Hz. GstCaps* caps = gst_caps_new_simple( "audio/x-raw-int", "width", G_TYPE_INT, 16, "channels", G_TYPE_INT, kDecodeChannels, "rate", G_TYPE_INT, kDecodeRate, nullptr); gst_element_link_filtered(resample, sink, caps); gst_caps_unref(caps); GstAppSinkCallbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.new_buffer = NewBufferCallback; gst_app_sink_set_callbacks(reinterpret_cast<GstAppSink*>(sink), &callbacks, this, nullptr); 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 CHECKED_GCONNECT(decode, "new-decoded-pad", &NewPadCallback, this); gst_bus_set_sync_handler(gst_pipeline_get_bus(GST_PIPELINE(pipeline_)), BusCallbackSync, this); guint bus_callback_id = gst_bus_add_watch( gst_pipeline_get_bus(GST_PIPELINE(pipeline_)), BusCallback, this); QTime time; time.start(); // Start playing gst_element_set_state(pipeline_, GST_STATE_PLAYING); g_main_loop_run(event_loop_); g_main_loop_unref(event_loop_); g_main_context_unref(context); int decode_time = time.restart(); buffer_.close(); QByteArray data = buffer_.data(); ChromaprintContext* chromaprint = chromaprint_new(CHROMAPRINT_ALGORITHM_DEFAULT); chromaprint_start(chromaprint, kDecodeRate, kDecodeChannels); chromaprint_feed(chromaprint, reinterpret_cast<void*>(data.data()), data.size() / 2); chromaprint_finish(chromaprint); void* fprint = nullptr; int size = 0; int ret = chromaprint_get_raw_fingerprint(chromaprint, &fprint, &size); QByteArray fingerprint; if (ret == 1) { void* encoded = nullptr; 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_buffer = nullptr; gst_app_sink_set_callbacks(reinterpret_cast<GstAppSink*>(sink), &callbacks, this, nullptr); gst_bus_set_sync_handler(gst_pipeline_get_bus(GST_PIPELINE(pipeline_)), nullptr, nullptr); g_source_remove(bus_callback_id); gst_element_set_state(pipeline_, GST_STATE_NULL); gst_object_unref(pipeline_); return fingerprint; }
int decode_audio_file(ChromaprintContext *chromaprint_ctx, int16_t *buffer1, int16_t *buffer2, const char *file_name, int max_length, int *duration) { int i, ok = 0, remaining, length, consumed, buffer_size, codec_ctx_opened = 0; AVFormatContext *format_ctx = NULL; AVCodecContext *codec_ctx = NULL; AVCodec *codec = NULL; AVStream *stream = NULL; AVPacket packet, packet_temp; #ifdef HAVE_AV_AUDIO_CONVERT AVAudioConvert *convert_ctx = NULL; #endif int16_t *buffer; if (!strcmp(file_name, "-")) { file_name = "pipe:0"; } #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 2, 0) if (av_open_input_file(&format_ctx, file_name, NULL, 0, NULL) != 0) { #else if (avformat_open_input(&format_ctx, file_name, NULL, NULL) != 0) { #endif fprintf(stderr, "ERROR: couldn't open the file\n"); goto done; } if (av_find_stream_info(format_ctx) < 0) { fprintf(stderr, "ERROR: couldn't find stream information in the file\n"); goto done; } for (i = 0; i < format_ctx->nb_streams; i++) { codec_ctx = format_ctx->streams[i]->codec; if (codec_ctx && codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { stream = format_ctx->streams[i]; break; } } if (!stream) { fprintf(stderr, "ERROR: couldn't find any audio stream in the file\n"); goto done; } codec = avcodec_find_decoder(codec_ctx->codec_id); if (!codec) { fprintf(stderr, "ERROR: unknown codec\n"); goto done; } if (avcodec_open(codec_ctx, codec) < 0) { fprintf(stderr, "ERROR: couldn't open the codec\n"); goto done; } codec_ctx_opened = 1; if (codec_ctx->channels <= 0) { fprintf(stderr, "ERROR: no channels found in the audio stream\n"); goto done; } if (codec_ctx->sample_fmt != AV_SAMPLE_FMT_S16) { #ifdef HAVE_AV_AUDIO_CONVERT convert_ctx = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, codec_ctx->channels, codec_ctx->sample_fmt, codec_ctx->channels, NULL, 0); if (!convert_ctx) { fprintf(stderr, "ERROR: couldn't create sample format converter\n"); goto done; } #else fprintf(stderr, "ERROR: unsupported sample format\n"); goto done; #endif } *duration = stream->time_base.num * stream->duration / stream->time_base.den; av_init_packet(&packet); av_init_packet(&packet_temp); remaining = max_length * codec_ctx->channels * codec_ctx->sample_rate; chromaprint_start(chromaprint_ctx, codec_ctx->sample_rate, codec_ctx->channels); while (1) { if (av_read_frame(format_ctx, &packet) < 0) { break; } packet_temp.data = packet.data; packet_temp.size = packet.size; while (packet_temp.size > 0) { buffer_size = BUFFER_SIZE; #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 23, 0) consumed = avcodec_decode_audio2(codec_ctx, buffer1, &buffer_size, packet_temp.data, packet_temp.size); #else consumed = avcodec_decode_audio3(codec_ctx, buffer1, &buffer_size, &packet_temp); #endif if (consumed < 0) { break; } packet_temp.data += consumed; packet_temp.size -= consumed; if (buffer_size <= 0) { if (buffer_size < 0) { fprintf(stderr, "WARNING: size returned from avcodec_decode_audioX is too small\n"); } continue; } if (buffer_size > BUFFER_SIZE) { fprintf(stderr, "WARNING: size returned from avcodec_decode_audioX is too large\n"); continue; } #ifdef HAVE_AV_AUDIO_CONVERT if (convert_ctx) { const void *ibuf[6] = { buffer1 }; void *obuf[6] = { buffer2 }; #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(51, 8, 0) int istride[6] = { av_get_bits_per_sample_format(codec_ctx->sample_fmt) / 8 }; #else int istride[6] = { av_get_bytes_per_sample(codec_ctx->sample_fmt) }; #endif int ostride[6] = { 2 }; int len = buffer_size / istride[0]; if (av_audio_convert(convert_ctx, obuf, ostride, ibuf, istride, len) < 0) { break; } buffer = buffer2; buffer_size = len * ostride[0]; } else { buffer = buffer1; } #else buffer = buffer1; #endif length = MIN(remaining, buffer_size / 2); if (!chromaprint_feed(chromaprint_ctx, buffer, length)) { fprintf(stderr, "ERROR: fingerprint calculation failed\n"); goto done; } if (max_length) { remaining -= length; if (remaining <= 0) { goto finish; } } } if (packet.data) { av_free_packet(&packet); } } finish: if (!chromaprint_finish(chromaprint_ctx)) { fprintf(stderr, "ERROR: fingerprint calculation failed\n"); goto done; } ok = 1; done: if (codec_ctx_opened) { avcodec_close(codec_ctx); } if (format_ctx) { av_close_input_file(format_ctx); } #ifdef HAVE_AV_AUDIO_CONVERT if (convert_ctx) { av_audio_convert_free(convert_ctx); } #endif return ok; } int fpcalc_main(int argc, char **argv) { int i, j, max_length = 120, num_file_names = 0, raw = 0, raw_fingerprint_size, duration; int16_t *buffer1, *buffer2; int32_t *raw_fingerprint; char *file_name, *fingerprint, **file_names; ChromaprintContext *chromaprint_ctx; int algo = CHROMAPRINT_ALGORITHM_DEFAULT; file_names = malloc(argc * sizeof(char *)); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!strcmp(arg, "-length") && i + 1 < argc) { max_length = atoi(argv[++i]); } else if (!strcmp(arg, "-version") || !strcmp(arg, "-v")) { printf("fpcalc version %s\n", chromaprint_get_version()); return 0; } else if (!strcmp(arg, "-raw")) { raw = 1; } else if (!strcmp(arg, "-algo") && i + 1 < argc) { const char *v = argv[++i]; if (!strcmp(v, "test1")) { algo = CHROMAPRINT_ALGORITHM_TEST1; } else if (!strcmp(v, "test2")) { algo = CHROMAPRINT_ALGORITHM_TEST2; } else if (!strcmp(v, "test3")) { algo = CHROMAPRINT_ALGORITHM_TEST3; } else if (!strcmp(v, "test4")) { algo = CHROMAPRINT_ALGORITHM_TEST4; } else { fprintf(stderr, "WARNING: unknown algorithm, using the default\n"); } } else if (!strcmp(arg, "-set") && i + 1 < argc) { i += 1; } else { file_names[num_file_names++] = argv[i]; } } if (!num_file_names) { printf("usage: %s [OPTIONS] FILE...\n\n", argv[0]); printf("Options:\n"); printf(" -version print version information\n"); printf(" -length SECS length of the audio data used for fingerprint calculation (default 120)\n"); printf(" -raw output the raw uncompressed fingerprint\n"); printf(" -algo NAME version of the fingerprint algorithm\n"); return 2; } av_register_all(); av_log_set_level(AV_LOG_ERROR); buffer1 = av_malloc(BUFFER_SIZE + 16); buffer2 = av_malloc(BUFFER_SIZE + 16); chromaprint_ctx = chromaprint_new(algo); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!strcmp(arg, "-set") && i + 1 < argc) { char *name = argv[++i]; char *value = strchr(name, '='); if (value) { *value++ = '\0'; chromaprint_set_option(chromaprint_ctx, name, atoi(value)); } } } for (i = 0; i < num_file_names; i++) { file_name = file_names[i]; if (!decode_audio_file(chromaprint_ctx, buffer1, buffer2, file_name, max_length, &duration)) { fprintf(stderr, "ERROR: unable to calculate fingerprint for file %s, skipping\n", file_name); continue; } if (i > 0) { printf("\n"); } printf("FILE=%s\n", file_name); printf("DURATION=%d\n", duration); if (raw) { if (!chromaprint_get_raw_fingerprint(chromaprint_ctx, (void **)&raw_fingerprint, &raw_fingerprint_size)) { fprintf(stderr, "ERROR: unable to calculate fingerprint for file %s, skipping\n", file_name); continue; } printf("FINGERPRINT="); for (j = 0; j < raw_fingerprint_size; j++) { printf("%d%s", raw_fingerprint[j], j + 1 < raw_fingerprint_size ? "," : ""); } printf("\n"); chromaprint_dealloc(raw_fingerprint); } else { if (!chromaprint_get_fingerprint(chromaprint_ctx, &fingerprint)) { fprintf(stderr, "ERROR: unable to calculate fingerprint for file %s, skipping\n", file_name); continue; } printf("FINGERPRINT=%s\n", fingerprint); chromaprint_dealloc(fingerprint); } } chromaprint_free(chromaprint_ctx); av_free(buffer1); av_free(buffer2); free(file_names); return 0; }
Chroma::Result Chroma::operator() (const QString& filename) { std::shared_ptr<AVFormatContext> formatCtx; { AVFormatContext *formatCtxRaw = nullptr; if (avformat_open_input (&formatCtxRaw, filename.toLatin1 ().constData (), nullptr, nullptr)) throw std::runtime_error ("error opening file"); formatCtx.reset (formatCtxRaw, [] (AVFormatContext *ctx) { avformat_close_input (&ctx); }); } { QMutexLocker locker (&CodecMutex_); if (avformat_find_stream_info (formatCtx.get (), nullptr) < 0) throw std::runtime_error ("could not find stream"); } AVCodec *codec = nullptr; const auto streamIndex = av_find_best_stream (formatCtx.get (), AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0); if (streamIndex < 0) throw std::runtime_error ("could not find audio stream"); auto stream = formatCtx->streams [streamIndex]; bool codecOpened = false; std::shared_ptr<AVCodecContext> codecCtx (stream->codec, [&codecOpened] (AVCodecContext *ctx) { if (codecOpened) avcodec_close (ctx); }); { QMutexLocker locker (&CodecMutex_); if (avcodec_open2 (codecCtx.get (), codec, nullptr) < 0) throw std::runtime_error ("couldn't open the codec"); } codecOpened = true; if (codecCtx->channels <= 0) throw std::runtime_error ("no channels found"); std::shared_ptr<SwrContext> swr; if (codecCtx->sample_fmt != AV_SAMPLE_FMT_S16) { swr.reset (swr_alloc (), [] (SwrContext *ctx) { if (ctx) swr_free (&ctx); }); av_opt_set_int (swr.get (), "in_channel_layout", codecCtx->channel_layout, 0); av_opt_set_int (swr.get (), "out_channel_layout", codecCtx->channel_layout, 0); av_opt_set_int (swr.get (), "in_sample_rate", codecCtx->sample_rate, 0); av_opt_set_int (swr.get (), "out_sample_rate", codecCtx->sample_rate, 0); av_opt_set_sample_fmt (swr.get (), "in_sample_fmt", codecCtx->sample_fmt, 0); av_opt_set_sample_fmt (swr.get (), "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); swr_init (swr.get ()); } AVPacket packet; av_init_packet (&packet); const int maxLength = 120; auto remaining = maxLength * codecCtx->channels * codecCtx->sample_rate; chromaprint_start (Ctx_, codecCtx->sample_rate, codecCtx->channels); std::shared_ptr<AVFrame> frame (av_frame_alloc (), [] (AVFrame *frame) { av_frame_free (&frame); }); auto maxDstNbSamples = 0; uint8_t *dstData [1] = { nullptr }; std::shared_ptr<void> dstDataGuard (nullptr, [&dstData] (void*) { if (dstData [0]) av_freep (&dstData [0]); }); while (true) { if (av_read_frame (formatCtx.get (), &packet) < 0) break; std::shared_ptr<void> guard (nullptr, [&packet] (void*) { if (packet.data) av_free_packet (&packet); }); if (packet.stream_index != streamIndex) continue; av_frame_unref (frame.get ()); int gotFrame = false; auto consumed = avcodec_decode_audio4 (codecCtx.get (), frame.get (), &gotFrame, &packet); if (consumed < 0 || !gotFrame) continue; uint8_t **data = nullptr; if (swr) { if (frame->nb_samples > maxDstNbSamples) { if (dstData [0]) av_freep (&dstData [0]); int linesize = 0; if (av_samples_alloc (dstData, &linesize, codecCtx->channels, frame->nb_samples, AV_SAMPLE_FMT_S16, 1) < 0) throw std::runtime_error ("cannot allocate memory for resampling"); } if (swr_convert (swr.get (), dstData, frame->nb_samples, const_cast<const uint8_t**> (frame->data), frame->nb_samples) < 0) throw std::runtime_error ("cannot resample audio"); data = dstData; } else data = frame->data; auto length = std::min (remaining, frame->nb_samples * codecCtx->channels); if (!chromaprint_feed (Ctx_, data [0], length)) throw std::runtime_error ("cannot feed data"); bool finished = false; if (maxLength) { remaining -= length; if (remaining <= 0) finished = true; } if (finished) break; } if (!chromaprint_finish (Ctx_)) throw std::runtime_error ("fingerprint calculation failed"); char *fingerprint = 0; if (!chromaprint_get_fingerprint (Ctx_, &fingerprint)) throw std::runtime_error ("unable to get fingerprint"); QByteArray result (fingerprint); chromaprint_dealloc (fingerprint); const double divideFactor = 1. / av_q2d (stream->time_base); const double duration = stream->duration / divideFactor; return { result, static_cast<int> (duration) }; }
int fpcalc_main(int argc, char **argv) { int i, j, max_length = 120, num_file_names = 0, raw = 0, raw_fingerprint_size, duration; int32_t *raw_fingerprint; char *file_name, *fingerprint, **file_names; ChromaprintContext *chromaprint_ctx; int algo = CHROMAPRINT_ALGORITHM_DEFAULT, num_failed = 0, do_hash = 0; file_names = malloc(argc * sizeof(char *)); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!strcmp(arg, "-length") && i + 1 < argc) { max_length = atoi(argv[++i]); } else if (!strcmp(arg, "-version") || !strcmp(arg, "-v")) { printf("fpcalc version %s\n", chromaprint_get_version()); return 0; } else if (!strcmp(arg, "-raw")) { raw = 1; } else if (!strcmp(arg, "-hash")) { do_hash = 1; } else if (!strcmp(arg, "-algo") && i + 1 < argc) { const char *v = argv[++i]; if (!strcmp(v, "test1")) { algo = CHROMAPRINT_ALGORITHM_TEST1; } else if (!strcmp(v, "test2")) { algo = CHROMAPRINT_ALGORITHM_TEST2; } else if (!strcmp(v, "test3")) { algo = CHROMAPRINT_ALGORITHM_TEST3; } else if (!strcmp(v, "test4")) { algo = CHROMAPRINT_ALGORITHM_TEST4; } else { fprintf(stderr, "WARNING: unknown algorithm, using the default\n"); } } else if (!strcmp(arg, "-set") && i + 1 < argc) { i += 1; } else { file_names[num_file_names++] = argv[i]; } } if (!num_file_names) { printf("usage: %s [OPTIONS] FILE...\n\n", argv[0]); printf("Options:\n"); printf(" -version print version information\n"); printf(" -length SECS length of the audio data used for fingerprint calculation (default 120)\n"); printf(" -raw output the raw uncompressed fingerprint\n"); printf(" -algo NAME version of the fingerprint algorithm\n"); printf(" -hash calculate also the fingerprint hash\n"); return 2; } av_register_all(); av_log_set_level(AV_LOG_ERROR); chromaprint_ctx = chromaprint_new(algo); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (!strcmp(arg, "-set") && i + 1 < argc) { char *name = argv[++i]; char *value = strchr(name, '='); if (value) { *value++ = '\0'; chromaprint_set_option(chromaprint_ctx, name, atoi(value)); } } } for (i = 0; i < num_file_names; i++) { file_name = file_names[i]; if (!decode_audio_file(chromaprint_ctx, file_name, max_length, &duration)) { fprintf(stderr, "ERROR: unable to calculate fingerprint for file %s, skipping\n", file_name); num_failed++; continue; } if (i > 0) { printf("\n"); } printf("FILE=%s\n", file_name); printf("DURATION=%d\n", duration); if (raw) { if (!chromaprint_get_raw_fingerprint(chromaprint_ctx, (void **)&raw_fingerprint, &raw_fingerprint_size)) { fprintf(stderr, "ERROR: unable to calculate fingerprint for file %s, skipping\n", file_name); num_failed++; continue; } printf("FINGERPRINT="); for (j = 0; j < raw_fingerprint_size; j++) { printf("%d%s", raw_fingerprint[j], j + 1 < raw_fingerprint_size ? "," : ""); } printf("\n"); chromaprint_dealloc(raw_fingerprint); } else { if (!chromaprint_get_fingerprint(chromaprint_ctx, &fingerprint)) { fprintf(stderr, "ERROR: unable to calculate fingerprint for file %s, skipping\n", file_name); num_failed++; continue; } printf("FINGERPRINT=%s\n", fingerprint); chromaprint_dealloc(fingerprint); } if (do_hash) { int32_t hash = 0; chromaprint_get_fingerprint_hash(chromaprint_ctx, &hash); printf("HASH=%d\n", hash); } } chromaprint_free(chromaprint_ctx); free(file_names); return num_failed ? 1 : 0; }
QString chromaprinter::calcFingerPrint(SoundSourceProxy& soundSource){ soundSource.open(); m_SampleRate = soundSource.getSampleRate(); unsigned int length = soundSource.length(); if (m_SampleRate == 0 ){ qDebug() << "Skipping invalid file:" << soundSource.getFilename(); return QString(); } // this is worth 2min of audio, multiply by 2 because we have 2 channels // AcoustID only stores a fingerprint for the first two minutes of a song // on their server so we need only a fingerprint of the first two minutes // --kain88 July 2012 m_NumSamples = 120*2*m_SampleRate; // check that the song is actually longer then the amount of audio we use if (m_NumSamples > length) { m_NumSamples = length; } SAMPLE *pData = new SAMPLE[m_NumSamples]; QTime timerReadingFile; timerReadingFile.start(); unsigned int read = soundSource.read(m_NumSamples, pData); if (read!=m_NumSamples) { qDebug() << "oh that's embarrasing I couldn't read the track"; return QString(); } qDebug("reading file took: %d ms" , timerReadingFile.elapsed()); ChromaprintContext* ctx = chromaprint_new(CHROMAPRINT_ALGORITHM_DEFAULT); // we have 2 channels in mixxx always chromaprint_start(ctx, m_SampleRate, 2); QTime timerGeneratingFingerPrint; timerGeneratingFingerPrint.start(); int success = chromaprint_feed(ctx, pData, m_NumSamples); if (!success) { qDebug() << "could not generate fingerprint"; delete [] pData; return QString(); } chromaprint_finish(ctx); void* fprint = NULL; int size = 0; int ret = chromaprint_get_raw_fingerprint(ctx, &fprint, &size); QByteArray fingerprint; if (ret == 1) { void* encoded = NULL; 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(ctx); delete [] pData; qDebug("generating fingerprint took: %d ms" , timerGeneratingFingerPrint.elapsed()); return fingerprint; }
void groove_fingerprinter_dealloc(void *ptr) { if (!ptr) return; chromaprint_dealloc(ptr); }