diff options
Diffstat (limited to 'src/scalar.c')
-rw-r--r-- | src/scalar.c | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/src/scalar.c b/src/scalar.c index becf341..74d6cee 100644 --- a/src/scalar.c +++ b/src/scalar.c @@ -889,8 +889,8 @@ int xtract_f0(const double *data, const int N, const void *argv, double *result) if(sr == 0) sr = 44100.0; - input = (double *)malloc(bytes = N * sizeof(double)); - input = memcpy(input, data, bytes); + input = (double*)malloc(bytes = N * sizeof(double)); + input = (double*)memcpy(input, data, bytes); /* threshold_peak = *((double *)argv+1); threshold_centre = *((double *)argv+2); printf("peak: %.2\tcentre: %.2\n", threshold_peak, threshold_centre);*/ @@ -1000,4 +1000,55 @@ int xtract_wavelet_f0(const double *data, const int N, const void *argv, double return XTRACT_SUCCESS; } +int xtract_midicent(const double *data, const int N, const void *argv, double *result) +{ + double f0 = *(double *)argv; + double note = 0.0; + + note = 69 + log(f0 / 440.f) * 17.31234; + note *= 100; + note = floor( 0.5f + note ); // replace -> round(note); + + *result = note; + + if (note > 12700 || note < 0) + { + return XTRACT_ARGUMENT_ERROR; + } + + return XTRACT_SUCCESS; +} + +int xtract_peak(const double *data, const int N, const void *argv, double *result) +{ + double threshold = *(double *)argv; + double current = data[N - 1]; + double average = 0.0; + double maximum = -DBL_MAX; + + for (uint32_t n = 0; n < N; ++n) + { + average += data[n]; + if (data[n] > maximum) + { + maximum = data[n]; + } + } + + average /= (double)N; + + if (current != maximum) + { + return XTRACT_NO_RESULT; + } + + if (current < average + threshold) + { + return XTRACT_NO_RESULT; + } + + return XTRACT_SUCCESS; + +} + |