From 9f4c8929ac2015ec13baf9d8b4f7814ebb35a6b6 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 13 Feb 2016 11:19:07 -0800 Subject: [PATCH] Audio compressor implementation. --- firmware/baseband/audio_compressor.cpp | 53 +++++++++++++ firmware/baseband/audio_compressor.hpp | 102 +++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 firmware/baseband/audio_compressor.cpp create mode 100644 firmware/baseband/audio_compressor.hpp diff --git a/firmware/baseband/audio_compressor.cpp b/firmware/baseband/audio_compressor.cpp new file mode 100644 index 000000000..f04810a1a --- /dev/null +++ b/firmware/baseband/audio_compressor.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc. + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "audio_compressor.hpp" + +float GainComputer::operator()(const float x) const { + const auto abs_x = std::abs(x); + const auto db = (abs_x < lin_floor) ? db_floor : log2_db_k * fast_log2(abs_x); + const auto overshoot_db = db - threshold_db; + if( knee_width_db > 0.0f ) { + const auto w2 = knee_width_db / 2.0f; + const auto a = w2 / (knee_width_db * knee_width_db); + const auto in_transition = (overshoot_db > -w2) && (overshoot_db < w2); + const auto rectified_overshoot = in_transition ? (a * std::pow(overshoot_db + w2, 2.0f)) : std::max(overshoot_db, 0.0f); + return rectified_overshoot * slope; + } else { + const auto rectified_overshoot = std::max(overshoot_db, 0.0f); + return rectified_overshoot * slope; + } +} + +void FeedForwardCompressor::execute_in_place(const buffer_f32_t& buffer) { + constexpr float makeup_gain = std::pow(10.0f, (threshold - (threshold / ratio)) / 20.0f); + for(size_t i=0; i + +/* Code based on article in Journal of the Audio Engineering Society + * Vol. 60, No. 6, 2012 June, by Dimitrios Giannoulis, Michael Massberg, + * Joshua D. Reiss "Digital Dynamic Range Compressor Design – A Tutorial + * and Analysis" + */ + +class GainComputer { +public: + constexpr GainComputer( + float ratio, + float threshold + ) : ratio { ratio }, + slope { 1.0f / ratio - 1.0f }, + threshold_db { threshold } + { + } + + float operator()(const float x) const; + +private: + const float ratio; + const float slope; + const float threshold_db; + + static constexpr float knee_width_db = 0.0f; + + static constexpr float db_floor = -120.0f; + static constexpr float lin_floor = std::pow(10.0f, db_floor / 20.0f); + static constexpr float log2_db_k = 20.0f * std::log10(2.0f); +}; + +class PeakDetectorBranchingSmooth { +public: + constexpr PeakDetectorBranchingSmooth( + float att_a, + float rel_a + ) : att_a { att_a }, + rel_a { rel_a } + { + } + + float operator()(const float db) { + const auto a = (db > state) ? att_a : rel_a; + state = db + a * (state - db); + return state; + } + +private: + float state { 0.0f }; + const float att_a; + const float rel_a; +}; + +class FeedForwardCompressor { +public: + void execute_in_place(const buffer_f32_t& buffer); + +private: + static constexpr float fs = 12000.0f; + static constexpr float ratio = 10.0f; + static constexpr float threshold = -30.0f; + + GainComputer gain_computer { ratio, threshold }; + PeakDetectorBranchingSmooth peak_detector { tau_alpha(0.010f, fs), tau_alpha(0.300f, fs) }; + + float execute_once(const float x); + + static constexpr float tau_alpha(const float tau, const float fs) { + return std::exp(-1.0f / (tau * fs)); + } +}; + +#endif/*__AUDIO_COMPRESSOR_H__*/