Skip to content

Commit

Permalink
Allow build to exclude internal avresample (#77)
Browse files Browse the repository at this point in the history
This allows the build to complete without using FFmpeg-derived code,
though this disables input resampling and thus requires that input
audio to the fingerprinter is already at the configured fingerprint
sample rate.

Co-authored-by: Lukáš Lalinský <lalinsky@gmail.com>
  • Loading branch information
johnsheu and lalinsky committed Dec 23, 2021
1 parent d0128e5 commit da9c9ae
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -15,6 +15,8 @@ set_property(CACHE AUDIO_PROCESSOR_LIB PROPERTY STRINGS avresample swresample)
set(FFT_LIB CACHE STRING "Library to use for FFT calculations")
set_property(CACHE FFT_LIB PROPERTY STRINGS avfft fftw3 fftw3f kissfft vdsp)

option(USE_INTERNAL_AVRESAMPLE "Use internal copy of avresample from ffmpeg for input conversion" ON)

include(CMakePushCheckState)
include(CheckFunctionExists)
include(CheckSymbolExists)
Expand Down
1 change: 1 addition & 0 deletions config.h.in
Expand Up @@ -8,6 +8,7 @@

#cmakedefine USE_SWRESAMPLE 1
#cmakedefine USE_AVRESAMPLE 1
#cmakedefine USE_INTERNAL_AVRESAMPLE 1

#cmakedefine USE_AVFFT 1
#cmakedefine USE_FFTW3 1
Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -25,7 +25,6 @@ set(chromaprint_SOURCES
utils/scope_exit.h
utils/rolling_integral_image.h
audio/audio_slicer.h
avresample/resample2.c
)

set(chromaprint_PUBLIC_SOURCES chromaprint.cpp)
Expand Down Expand Up @@ -65,6 +64,10 @@ if(USE_KISSFFT)
include_directories(${KISSFFT_INCLUDE_DIRS})
endif()

if (USE_INTERNAL_AVRESAMPLE)
set(chromaprint_SOURCES avresample/resample2.c ${chromaprint_SOURCES})
endif()

add_library(chromaprint_objs OBJECT ${chromaprint_SOURCES})
if(BUILD_SHARED_LIBS)
set_target_properties(chromaprint_objs PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
Expand Down
59 changes: 41 additions & 18 deletions src/audio_processor.cpp
Expand Up @@ -4,9 +4,16 @@
#include <assert.h>
#include <algorithm>
#include <stdio.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if USE_INTERNAL_AVRESAMPLE
extern "C" {
#include "avresample/avcodec.h"
}
#endif

#include "debug.h"
#include "audio_processor.h"

Expand All @@ -15,11 +22,13 @@ namespace chromaprint {
static const int kMinSampleRate = 1000;
static const int kMaxBufferSize = 1024 * 32;

#if USE_INTERNAL_AVRESAMPLE
// Resampler configuration
static const int kResampleFilterLength = 16;
static const int kResamplePhaseShift = 8;
static const int kResampleLinear = 0;
static const double kResampleCutoff = 0.8;
#endif

AudioProcessor::AudioProcessor(int sample_rate, AudioConsumer *consumer)
: m_buffer(kMaxBufferSize),
Expand All @@ -33,9 +42,11 @@ AudioProcessor::AudioProcessor(int sample_rate, AudioConsumer *consumer)

AudioProcessor::~AudioProcessor()
{
#if USE_INTERNAL_AVRESAMPLE
if (m_resample_ctx) {
av_resample_close(m_resample_ctx);
}
#endif
}

void AudioProcessor::LoadMono(const int16_t *input, int length)
Expand Down Expand Up @@ -90,27 +101,30 @@ int AudioProcessor::Load(const int16_t *input, int length)

void AudioProcessor::Resample()
{
if (!m_resample_ctx) {
#if USE_INTERNAL_AVRESAMPLE
if (m_resample_ctx) {
int consumed = 0;
int length = av_resample(m_resample_ctx, m_resample_buffer.data(), m_buffer.data(), &consumed, int(m_buffer_offset), kMaxBufferSize, 1);
if (length > kMaxBufferSize) {
DEBUG("chromaprint::AudioProcessor::Resample() -- Resampling overwrote output buffer.");
length = kMaxBufferSize;
}
m_consumer->Consume(m_resample_buffer.data(), length);
int remaining = int(m_buffer_offset - consumed);
if (remaining > 0) {
std::copy(m_buffer.begin() + consumed, m_buffer.begin() + m_buffer_offset, m_buffer.begin());
}
else if (remaining < 0) {
DEBUG("chromaprint::AudioProcessor::Resample() -- Resampling overread input buffer.");
remaining = 0;
}
m_buffer_offset = remaining;
} else
#endif
{
m_consumer->Consume(m_buffer.data(), int(m_buffer_offset));
m_buffer_offset = 0;
return;
}
int consumed = 0;
int length = av_resample(m_resample_ctx, m_resample_buffer.data(), m_buffer.data(), &consumed, int(m_buffer_offset), kMaxBufferSize, 1);
if (length > kMaxBufferSize) {
DEBUG("chromaprint::AudioProcessor::Resample() -- Resampling overwrote output buffer.");
length = kMaxBufferSize;
}
m_consumer->Consume(m_resample_buffer.data(), length);
int remaining = int(m_buffer_offset - consumed);
if (remaining > 0) {
std::copy(m_buffer.begin() + consumed, m_buffer.begin() + m_buffer_offset, m_buffer.begin());
}
else if (remaining < 0) {
DEBUG("chromaprint::AudioProcessor::Resample() -- Resampling overread input buffer.");
remaining = 0;
}
m_buffer_offset = remaining;
}


Expand All @@ -126,6 +140,8 @@ bool AudioProcessor::Reset(int sample_rate, int num_channels)
return false;
}
m_buffer_offset = 0;

#if USE_INTERNAL_AVRESAMPLE
if (m_resample_ctx) {
av_resample_close(m_resample_ctx);
m_resample_ctx = 0;
Expand All @@ -138,6 +154,13 @@ bool AudioProcessor::Reset(int sample_rate, int num_channels)
kResampleLinear,
kResampleCutoff);
}
#else
if (sample_rate != m_target_sample_rate) {
DEBUG("chromaprint::AudioProcessor::Reset() -- Cannot resample from " << sample_rate
<< " to " << m_target_sample_rate << " without internal avresample.");
return false;
}
#endif
m_num_channels = num_channels;
return true;
}
Expand Down

0 comments on commit da9c9ae

Please sign in to comment.