1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_AUDIO_POWER_H 18 #define ANDROID_AUDIO_POWER_H 19 20 #include <math.h> 21 #include <stdint.h> 22 #include <sys/cdefs.h> 23 #include <system/audio.h> 24 25 /** \cond */ 26 __BEGIN_DECLS 27 /** \endcond */ 28 29 /** 30 * \brief Compute signal power on a scale of 0 dBFS. 31 * 32 * \param buffer buffer of samples. 33 * \param format one of AUDIO_FORMAT_PCM_8_BIT, AUDIO_FORMAT_PCM_16_BIT, 34 * AUDIO_FORMAT_PCM_24_BIT_PACKED, AUDIO_FORMAT_PCM_8_24_BIT, 35 * AUDIO_FORMAT_PCM_32_BIT, AUDIO_FORMAT_PCM_FLOAT. 36 * \param samples number of samples in buffer. This is not audio frames; 37 * usually the number of samples is the number of audio frames 38 * multiplied by channel count. 39 * 40 * \return 41 * signal power of the samples in the buffer. It is possible to return negative infinity 42 * if the power is zero. 43 */ 44 45 float audio_utils_compute_power_mono(const void *buffer, audio_format_t format, size_t samples); 46 47 /** 48 * \brief Compute signal energy (sum of squared amplitudes). 49 * 50 * \param buffer buffer of samples. 51 * \param format one of AUDIO_FORMAT_PCM_8_BIT, AUDIO_FORMAT_PCM_16_BIT, 52 * AUDIO_FORMAT_PCM_24_BIT_PACKED, AUDIO_FORMAT_PCM_8_24_BIT, 53 * AUDIO_FORMAT_PCM_32_BIT, AUDIO_FORMAT_PCM_FLOAT. 54 * \param samples number of samples in buffer. This is not audio frames; 55 * usually the number of samples is the number of audio frames 56 * multiplied by channel count. 57 * 58 * \return 59 * signal energy of the samples in the buffer (sum of squares) where each sample is 60 * normalized to peak to peak range of 1.f. 61 */ 62 63 float audio_utils_compute_energy_mono(const void *buffer, audio_format_t format, size_t samples); 64 65 /** 66 * \brief Returns true if the format is supported for compute_energy_for_mono() 67 * and compute_power_for_mono(). 68 * \param format format under consideration. 69 * \return true if supported. 70 */ 71 bool audio_utils_is_compute_power_format_supported(audio_format_t format); 72 73 /** 74 * \brief Returns the signal power from amplitude. 75 * \param amplitude the signal amplitude. A negative amplitude is treated 76 * the same as a positive amplitude. 77 * \return signal power in dB. It is possible to return negative infinity 78 * if the input is zero. 79 */ 80 static inline float audio_utils_power_from_amplitude(float amplitude) 81 { 82 return 20.f * log10f(fabsf(amplitude)); 83 } 84 85 /** 86 * \brief Returns the signal power from energy. 87 * \param energy the signal energy. This should be non-negative. 88 * \return signal power in dB. It is possible to return NaN if the input is 89 * negative, or negative infinity if the input is zero. 90 */ 91 static inline float audio_utils_power_from_energy(float energy) 92 { 93 return 10.f * log10f(energy); 94 } 95 96 /** \cond */ 97 __END_DECLS 98 /** \endcond */ 99 100 #endif // !ANDROID_AUDIO_POWER_H 101