• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include <sys/mman.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #define MINIMP3_IMPLEMENTATION
12 #include "decode.h"
13 
14 #define MIN(a, b) ((a) < (b) ? (a) : (b))
15 
get_spectrum(decoder * dec,int numch)16 static void get_spectrum(decoder *dec, int numch)
17 {
18     int i, ch, band;
19     const mp3dec_t *d = &dec->mp3d.mp3d;
20     // Average spectrum power for 32 frequency band
21     for (ch = 0; ch < numch; ch++)
22     {
23         for (band = 0; band < 32; band++)
24         {
25             float band_power = 0;
26             for (i = 0; i < 9; i++)
27             {
28                 float sample = d->mdct_overlap[ch][i + band*9];
29                 band_power += sample*sample;
30             }
31             // with averaging
32             //dec->spectrum[band][ch] += (band_power/9 - spectrum[band][ch]) * 0.25;
33 
34             // w/o averaging
35             dec->spectrum[band][ch] = band_power/9;
36         }
37     }
38     // Calculate dB scale from power for better visualization
39     for (ch = 0; ch < numch; ch++)
40     {
41         for (band = 0; band < 32; band++)
42         {
43             float power = dec->spectrum[band][ch];
44             float db_offset = 100;      // to shift dB values from [0..-inf] to [max..0]
45             float db = 10*log10f(power + 1e-15) + db_offset;
46             if (db < 0) db = 0;
47             //printf("% .5f\t", db);
48         }
49         //printf("\n");
50     }
51 }
52 
decode_samples(decoder * dec,uint8_t * buf,int bytes)53 void decode_samples(decoder *dec, uint8_t *buf, int bytes)
54 {
55     memset(buf, 0, bytes);
56     mp3dec_ex_read(&dec->mp3d, (mp3d_sample_t*)buf, bytes/sizeof(mp3d_sample_t));
57 }
58 
open_dec(decoder * dec,const char * file_name)59 int open_dec(decoder *dec, const char *file_name)
60 {
61     if (!dec || !file_name || !*file_name)
62         return 0;
63 
64     memset(dec, 0, sizeof(*dec));
65 
66     mp3dec_ex_open(&dec->mp3d, file_name, MP3D_SEEK_TO_SAMPLE);
67     if (!dec->mp3d.samples)
68         return 0;
69     return 1;
70 }
71 
close_dec(decoder * dec)72 int close_dec(decoder *dec)
73 {
74     mp3dec_ex_close(&dec->mp3d);
75     memset(dec, 0, sizeof(*dec));
76 }
77