1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 * 4 */ 5 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <stdint.h> 9 #include <string.h> 10 #include <stdarg.h> 11 #include <signal.h> 12 13 #include "uvoice_os.h" 14 15 #include "uvoice_audio.h" 16 #include "audio_common.h" 17 18 bits_to_pcm_format(int bits)19enum pcm_format bits_to_pcm_format(int bits) 20 { 21 switch (bits) { 22 case 32: 23 return PCM_FORMAT_S32_LE; 24 case 16: 25 return PCM_FORMAT_S16_LE; 26 case 24: 27 return PCM_FORMAT_S24_LE; 28 case 8: 29 return PCM_FORMAT_S8; 30 } 31 return PCM_FORMAT_S16_LE; 32 } 33 34 #ifndef UVOICE_ON_XR871 pcm_format_to_bits(enum pcm_format format)35int pcm_format_to_bits(enum pcm_format format) 36 { 37 switch (format) { 38 case PCM_FORMAT_S32_LE: 39 return 32; 40 case PCM_FORMAT_S24_LE: 41 return 32; 42 case PCM_FORMAT_S24_3LE: 43 return 24; 44 case PCM_FORMAT_S16_LE: 45 return 16; 46 case PCM_FORMAT_S8: 47 return 8; 48 }; 49 return 0; 50 } 51 #endif 52 pcm_to_decibel(struct pcm_config * config,uint8_t * buffer,int nbytes)53int pcm_to_decibel(struct pcm_config *config, uint8_t *buffer, int nbytes) 54 { 55 double ampl_sum = 0.0; 56 int decibel; 57 int samples; 58 int i; 59 int bits = pcm_format_to_bits(config->format) >> 3; 60 61 if (bits == 0) { 62 return 0; 63 } 64 65 samples = nbytes / bits; 66 67 for (i = 0; i < samples; i += config->channels) 68 ampl_sum += abs(buffer[i]); 69 70 ampl_sum /= samples; 71 decibel = (int)(20.0 * log10(ampl_sum)); 72 73 return decibel; 74 } 75 76