From 63deff31a441647619080ca046d1aa1c8edcea9e Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 10 Mar 2015 00:18:56 +0100 Subject: [PATCH] Use float samples internally. --- audioreader.cpp | 14 +++++++------- audioreader.h | 2 +- decode.cpp | 4 ++-- interpolate.h | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/audioreader.cpp b/audioreader.cpp index f0d07d8..e133afc 100644 --- a/audioreader.cpp +++ b/audioreader.cpp @@ -50,25 +50,25 @@ struct AVSampleDeleter { } }; -void convert_samples(SwrContext *swr, int sample_rate, const uint8_t **data, int nb_samples, std::vector *samples) +void convert_samples(SwrContext *swr, int sample_rate, const uint8_t **data, int nb_samples, std::vector *samples) { int max_out_samples = nb_samples + swr_get_delay(swr, sample_rate); if (max_out_samples == 0) { return; } uint8_t *output; - av_samples_alloc(&output, nullptr, 1, max_out_samples, AV_SAMPLE_FMT_S16, 0); + av_samples_alloc(&output, nullptr, 1, max_out_samples, AV_SAMPLE_FMT_FLT, 0); std::unique_ptr output_deleter(output); int out_samples = swr_convert(swr, &output, max_out_samples, data, nb_samples); if (out_samples > 0) { - const int16_t* start = reinterpret_cast(output); - const int16_t* end = start + out_samples; + const float* start = reinterpret_cast(output); + const float* end = start + out_samples; samples->insert(samples->end(), start, end); } } -int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *swr, AVFrame *audio_frame, AVPacket *packet, int *got_frame, std::vector *samples) +int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *swr, AVFrame *audio_frame, AVPacket *packet, int *got_frame, std::vector *samples) { *got_frame = 0; int len1 = avcodec_decode_audio4(codec_ctx, audio_frame, got_frame, packet); @@ -88,7 +88,7 @@ int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *s } // namespace -bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate) +bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate) { av_register_all(); @@ -141,7 +141,7 @@ bool read_audio_file(const char *filename, std::vector *samples, int *s } SwrContext *swr = swr_alloc_set_opts( nullptr, - AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_S16, codec_ctx->sample_rate, + AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_FLT, codec_ctx->sample_rate, codec_ctx->channel_layout, codec_ctx->sample_fmt, codec_ctx->sample_rate, 0, nullptr); std::unique_ptr swr_deleter(swr); diff --git a/audioreader.h b/audioreader.h index 47bb8f5..c654b61 100644 --- a/audioreader.h +++ b/audioreader.h @@ -5,6 +5,6 @@ #include -bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate); +bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate); #endif // !defined(_AUDIOREADER_H) diff --git a/decode.cpp b/decode.cpp index 8ed2f1b..5bb3b2c 100644 --- a/decode.cpp +++ b/decode.cpp @@ -20,7 +20,7 @@ #define SYNC_TEST_TOLERANCE 1.10 // between [x,x+1] -double find_zerocrossing(const std::vector &pcm, int x) +double find_zerocrossing(const std::vector &pcm, int x) { if (pcm[x] == 0) { return x; @@ -107,7 +107,7 @@ double calibrate(const std::vector &pulses) { int main(int argc, char **argv) { make_lanczos_weight_table(); - std::vector pcm; + std::vector pcm; int sample_rate; if (!read_audio_file(argv[1], &pcm, &sample_rate)) { exit(1); diff --git a/interpolate.h b/interpolate.h index f9aeb11..c71110b 100644 --- a/interpolate.h +++ b/interpolate.h @@ -29,9 +29,9 @@ inline double lanczos_weight(double x) extern double lanczos_table[LANCZOS_RADIUS * LANCZOS_RESOLUTION]; void make_lanczos_weight_table(); -inline double lanczos_weight_table(double x) +inline double lanczos_weight_table(float x) { - int table_id = lrintf(fabs(x) * LANCZOS_RESOLUTION); + int table_id = lrintf(fabsf(x) * LANCZOS_RESOLUTION); if (table_id >= LANCZOS_RADIUS * LANCZOS_RESOLUTION) { return 0.0; } -- 2.39.2