xref: /btstack/src/btstack_sample_rate_compensation.c (revision ce6f85e79d1d141c1b45dfa16b2671762457cbb4)
1 /*
2  * Copyright (C) 2023 BlueKitchen GmbH
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "btstack_sample_rate_comnpensation.h"
39 
40 #include "btstack.h"
41 #include "btstack_sample_rate_compensation.h"
42 
43 void btstack_sample_rate_compensation_reset(btstack_sample_rate_compensation_t *self, uint32_t timestamp_ms) {
44     self->count = 0;
45     self->last  = timestamp_ms;
46 }
47 
48 void btstack_sample_rate_compensation_init(btstack_sample_rate_compensation_t *self, uint32_t timestamp_ms, uint32_t sample_rate, uint32_t ratio_Q15) {
49     btstack_sample_rate_compensation_reset( self, timestamp_ms );
50     self->ratio_state = ratio_Q15 << 1; // Q15 to Q16 is one left shift
51     self->rate_state = sample_rate << 8;
52 #ifdef DEBUG_RATIO_CALCULATION
53     self->ratio = Q15_TO_FLOAT(ratioQ15);
54     self->sample_rate = sample_rate;
55 #endif
56 }
57 
58 uint32_t btstack_sample_rate_compensation_update(btstack_sample_rate_compensation_t *self, uint32_t timestamp_ms, uint32_t samples, uint32_t playback_sample_rate) {
59     int32_t delta = timestamp_ms - self->last;
60     if( delta >= 1000 ) {
61         log_debug("current playback sample rate: %d", playback_sample_rate );
62 
63 #ifdef DEBUG_RATIO_CALCULATION
64         {
65             double current_sample_rate = self->count*(1000./delta);
66             double current_ratio = self->sample_rate/playback_sample_rate;
67 
68             // exponential weighted moving average
69             const double rate_decay = 0.025;
70             self->sample_rate += rate_decay * (current_sample_rate-self->sample_rate);
71 
72             // exponential weighted moving average
73             static const double ratio_decay = 1.3;
74             self->ratio += ratio_decay * (current_ratio-self->ratio);
75 
76             log_debug("current l2cap sample rate: %f (%d %d)", current_sample_rate, delta, self->count );
77             log_debug("current ratio:             %f", current_ratio);
78             log_debug("calculated ratio:          %f", self->ratio );
79         }
80 #endif
81         uint32_t fixed_rate = (self->count*(UINT16_C(1)<<15))/delta*1000;  // sample rate as Q15
82         uint32_t fixed_ratio = (self->rate_state<<7)/playback_sample_rate; // Q15
83         log_debug("fp current l2cap sample rate: %f (%d %d)", Q15_TO_FLOAT(fixed_rate), delta, self->count);
84 
85         self->last = timestamp_ms;
86         self->count = 0;
87 
88         // fixed point exponential weighted moving average
89         const int16_t rate_decay = FLOAT_TO_Q15(0.025f);
90         uint32_t rate = self->rate_state >> 8; // integer part only
91         self->rate_state += (rate_decay * (int32_t)((fixed_rate>>15)-rate)) >> (15-8); // Q8;
92 
93         // fixed point exponential weighted moving average
94         const int16_t ratio_decay = FLOAT_TO_Q8(1.3f);
95         self->ratio_state += (ratio_decay * (int32_t)((fixed_ratio<<1)-self->ratio_state)) >> (16-8); // Q16
96 
97         log_debug("fp current ratio :            %f", Q15_TO_FLOAT(fixed_ratio));
98         log_debug("fp calculated ratio:          %f", Q16_TO_FLOAT(self->ratio_state));
99         log_debug("scale factor Q16:             %d", self->ratio_state);
100     }
101 
102     self->count += samples;
103     return self->ratio_state;
104 }
105