1 /* Copyright (c) 2011 Xiph.Org Foundation
2    Written by Jean-Marc Valin */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
19    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef ANALYSIS_H
29 #define ANALYSIS_H
30 
31 #include "celt.h"
32 #include "opus_private.h"
33 
34 #define NB_FRAMES 8
35 #define NB_TBANDS 18
36 #define ANALYSIS_BUF_SIZE 720 /* 30 ms at 24 kHz */
37 
38 /* At that point we can stop counting frames because it no longer matters. */
39 #define ANALYSIS_COUNT_MAX 10000
40 
41 #define DETECT_SIZE 100
42 
43 /* Uncomment this to print the MLP features on stdout. */
44 /*#define MLP_TRAINING*/
45 
46 typedef struct {
47    int arch;
48    int application;
49    opus_int32 Fs;
50 #define TONALITY_ANALYSIS_RESET_START angle
51    float angle[240];
52    float d_angle[240];
53    float d2_angle[240];
54    opus_val32 inmem[ANALYSIS_BUF_SIZE];
55    int   mem_fill;                      /* number of usable samples in the buffer */
56    float prev_band_tonality[NB_TBANDS];
57    float prev_tonality;
58    int prev_bandwidth;
59    float E[NB_FRAMES][NB_TBANDS];
60    float logE[NB_FRAMES][NB_TBANDS];
61    float lowE[NB_TBANDS];
62    float highE[NB_TBANDS];
63    float meanE[NB_TBANDS+1];
64    float mem[32];
65    float cmean[8];
66    float std[9];
67    float music_prob;
68    float vad_prob;
69    float Etracker;
70    float lowECount;
71    int E_count;
72    int last_music;
73    int count;
74    int analysis_offset;
75    /** Probability of having speech for time i to DETECT_SIZE-1 (and music before).
76        pspeech[0] is the probability that all frames in the window are speech. */
77    float pspeech[DETECT_SIZE];
78    /** Probability of having music for time i to DETECT_SIZE-1 (and speech before).
79        pmusic[0] is the probability that all frames in the window are music. */
80    float pmusic[DETECT_SIZE];
81    float speech_confidence;
82    float music_confidence;
83    int speech_confidence_count;
84    int music_confidence_count;
85    int write_pos;
86    int read_pos;
87    int read_subframe;
88    float hp_ener_accum;
89    opus_val32 downmix_state[3];
90    AnalysisInfo info[DETECT_SIZE];
91 } TonalityAnalysisState;
92 
93 /** Initialize a TonalityAnalysisState struct.
94  *
95  * This performs some possibly slow initialization steps which should
96  * not be repeated every analysis step. No allocated memory is retained
97  * by the state struct, so no cleanup call is required.
98  */
99 void tonality_analysis_init(TonalityAnalysisState *analysis, opus_int32 Fs);
100 
101 /** Reset a TonalityAnalysisState stuct.
102  *
103  * Call this when there's a discontinuity in the data.
104  */
105 void tonality_analysis_reset(TonalityAnalysisState *analysis);
106 
107 void tonality_get_info(TonalityAnalysisState *tonal, AnalysisInfo *info_out, int len);
108 
109 void run_analysis(TonalityAnalysisState *analysis, const CELTMode *celt_mode, const void *analysis_pcm,
110                  int analysis_frame_size, int frame_size, int c1, int c2, int C, opus_int32 Fs,
111                  int lsb_depth, downmix_func downmix, AnalysisInfo *analysis_info);
112 
113 #endif
114