1 /* Copyright 2020 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #ifndef EWMA_POWER_H_ 7 #define EWMA_POWER_H_ 8 9 #include <stdbool.h> 10 #include <stdint.h> 11 12 #include "cras_audio_area.h" 13 14 /* 15 * The exponentially weighted moving average power module used to 16 * calculate the energe level in audio stream. 17 * Members: 18 * power_set - Flag to note if the first power value has set. 19 * enabled - Flag to enable ewma calculation. Set to false to 20 * make all calculations no-ops. 21 * power - The power value. 22 * step_fr - How many frames to sample one for EWMA calculation. 23 */ 24 struct ewma_power { 25 bool power_set; 26 bool enabled; 27 float power; 28 unsigned int step_fr; 29 }; 30 31 /* 32 * Disables the ewma instance. 33 */ 34 void ewma_power_disable(struct ewma_power *ewma); 35 36 /* 37 * Initializes the ewma_power object. 38 * Args: 39 * ewma - The ewma_power object to initialize. 40 * rate - The sample rate of the audio data that the ewma object 41 * will calculate power from. 42 */ 43 void ewma_power_init(struct ewma_power *ewma, unsigned int rate); 44 45 /* 46 * Feeds an audio buffer to ewma_power object to calculate the 47 * latest power value. 48 * Args: 49 * ewma - The ewma_power object to calculate power. 50 * buf - Pointer to the audio data. 51 * channels - Number of channels of the audio data. 52 * size - Length in frames of the audio data. 53 */ 54 void ewma_power_calculate(struct ewma_power *ewma, const int16_t *buf, 55 unsigned int channels, unsigned int size); 56 57 /* 58 * Feeds non-interleaved audio data to ewma_power to calculate the 59 * latest power value. This is similar to ewma_power_calculate but 60 * accepts cras_audio_area. 61 */ 62 void ewma_power_calculate_area(struct ewma_power *ewma, const int16_t *buf, 63 struct cras_audio_area *area, unsigned int size); 64 65 #endif /* EWMA_POWER_H_ */ 66