Skip to content

Commit

Permalink
Merge pull request #47 from kylophone/master
Browse files Browse the repository at this point in the history
add ebur128_relative_threshold
  • Loading branch information
jiixyj committed Jan 14, 2016
2 parents bc6dd06 + c438237 commit 76d6c79
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 21 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ See also [loudness-scanner tool](https://github.com/jiixyj/loudness-scanner).

News
----
v1.1 Released:

* Add `ebur128_relative_threshold()`

v1.0.3 Released:

Expand Down
2 changes: 1 addition & 1 deletion ebur128/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif()


set(EBUR128_VERSION_MAJOR 1)
set(EBUR128_VERSION 1.0.3)
set(EBUR128_VERSION 1.1.0)

#### static
if(BUILD_STATIC_LIBS)
Expand Down
67 changes: 51 additions & 16 deletions ebur128/ebur128.c
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,41 @@ EBUR128_ADD_FRAMES(int)
EBUR128_ADD_FRAMES(float)
EBUR128_ADD_FRAMES(double)

static int ebur128_calc_relative_threshold(ebur128_state* st,
size_t* above_thresh_counter,
double* relative_threshold) {
struct ebur128_dq_entry* it;
size_t i;
*relative_threshold = 0.0;
*above_thresh_counter = 0;

if (st->d->use_histogram) {
for (i = 0; i < 1000; ++i) {
*relative_threshold += st->d->block_energy_histogram[i] *
histogram_energies[i];
*above_thresh_counter += st->d->block_energy_histogram[i];
}
} else {
SLIST_FOREACH(it, &st->d->block_list, entries) {
++*above_thresh_counter;
*relative_threshold += it->z;
}
}

if (*above_thresh_counter != 0) {
*relative_threshold /= (double) *above_thresh_counter;
*relative_threshold *= relative_gate_factor;
}

return EBUR128_SUCCESS;
}

static int ebur128_gated_loudness(ebur128_state** sts, size_t size,
double* out) {
struct ebur128_dq_entry* it;
double relative_threshold = 0.0;
double gated_loudness = 0.0;
size_t above_thresh_counter = 0;
double relative_threshold;
size_t above_thresh_counter;
size_t i, j, start_index;

for (i = 0; i < size; i++) {
Expand All @@ -722,25 +751,13 @@ static int ebur128_gated_loudness(ebur128_state** sts, size_t size,

for (i = 0; i < size; i++) {
if (!sts[i]) continue;
if (sts[i]->d->use_histogram) {
for (j = 0; j < 1000; ++j) {
relative_threshold += sts[i]->d->block_energy_histogram[j] *
histogram_energies[j];
above_thresh_counter += sts[i]->d->block_energy_histogram[j];
}
} else {
SLIST_FOREACH(it, &sts[i]->d->block_list, entries) {
++above_thresh_counter;
relative_threshold += it->z;
}
}
ebur128_calc_relative_threshold(sts[i], &above_thresh_counter, &relative_threshold);
}
if (!above_thresh_counter) {
*out = -HUGE_VAL;
return EBUR128_SUCCESS;
}
relative_threshold /= (double) above_thresh_counter;
relative_threshold *= relative_gate_factor;

above_thresh_counter = 0;
if (relative_threshold < histogram_energy_boundaries[0]) {
start_index = 0;
Expand Down Expand Up @@ -776,6 +793,24 @@ static int ebur128_gated_loudness(ebur128_state** sts, size_t size,
return EBUR128_SUCCESS;
}

int ebur128_relative_threshold(ebur128_state* st, double* out) {
double relative_threshold;
size_t above_thresh_counter;

if (st && (st->mode & EBUR128_MODE_I) != EBUR128_MODE_I)
return EBUR128_ERROR_INVALID_MODE;

ebur128_calc_relative_threshold(st, &above_thresh_counter, &relative_threshold);

if (!above_thresh_counter) {
*out = -70.0;
return EBUR128_SUCCESS;
}

*out = ebur128_energy_to_loudness(relative_threshold);
return EBUR128_SUCCESS;
}

int ebur128_loudness_global(ebur128_state* st, double* out) {
return ebur128_gated_loudness(&st, 1, out);
}
Expand Down
17 changes: 14 additions & 3 deletions ebur128/ebur128.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extern "C" {
#endif

#define EBUR128_VERSION_MAJOR 1
#define EBUR128_VERSION_MINOR 0
#define EBUR128_VERSION_PATCH 3
#define EBUR128_VERSION_MINOR 1
#define EBUR128_VERSION_PATCH 0

#include <stddef.h> /* for size_t */

Expand Down Expand Up @@ -82,7 +82,7 @@ enum mode {
EBUR128_MODE_M = (1 << 0),
/** can call ebur128_loudness_shortterm */
EBUR128_MODE_S = (1 << 1) | EBUR128_MODE_M,
/** can call ebur128_loudness_global_* */
/** can call ebur128_loudness_global_* and ebur128_relative_threshold */
EBUR128_MODE_I = (1 << 2) | EBUR128_MODE_M,
/** can call ebur128_loudness_range */
EBUR128_MODE_LRA = (1 << 3) | EBUR128_MODE_S,
Expand Down Expand Up @@ -312,6 +312,17 @@ int ebur128_true_peak(ebur128_state* st,
unsigned int channel_number,
double* out);

/** \brief Get relative threshold in LUFS.
*
* @param st library state
* @param out relative threshold in LUFS.
* @return
* - EBUR128_SUCCESS on success.
* - EBUR128_ERROR_INVALID_MODE if mode "EBUR128_MODE_I" has not
* been set.
*/
int ebur128_relative_threshold(ebur128_state* st, double* out);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ set(ENABLE_TESTS OFF CACHE BOOL "Build test binaries, needs libsndfile")

if(ENABLE_TESTS)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SNDFILE REQUIRED sndfile)
find_pkg_config(SNDFILE sndfile REQUIRED)

include_directories(${EBUR128_INCLUDE_DIR})
include_directories(SYSTEM ${SNDFILE_INCLUDE_DIRS})

add_executable(r128-test-library tests)
add_executable(minimal-example minimal-example)
set_property(TARGET r128-test-library APPEND_STRING PROPERTY
Expand Down

0 comments on commit 76d6c79

Please sign in to comment.