xref: /btstack/src/btstack_sample_rate_compensation.c (revision 1990056b3170817e571191fb89b768db4ddf911c)
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 <inttypes.h>
41 
42 #include "btstack.h"
43 #include "btstack_sample_rate_compensation.h"
44 
btstack_sample_rate_compensation_reset(btstack_sample_rate_compensation_t * self,uint32_t timestamp_ms)45 void btstack_sample_rate_compensation_reset(btstack_sample_rate_compensation_t *self, uint32_t timestamp_ms) {
46     self->count = 0;
47     self->last  = timestamp_ms;
48 }
49 
btstack_sample_rate_compensation_init(btstack_sample_rate_compensation_t * self,uint32_t timestamp_ms,uint32_t sample_rate,uint32_t ratio_Q15)50 void btstack_sample_rate_compensation_init(btstack_sample_rate_compensation_t *self, uint32_t timestamp_ms, uint32_t sample_rate, uint32_t ratio_Q15) {
51     btstack_sample_rate_compensation_reset( self, timestamp_ms );
52     self->ratio_state = ratio_Q15 << 1; // Q15 to Q16 is one left shift
53     self->rate_state = sample_rate << 8;
54 #ifdef DEBUG_RATIO_CALCULATION
55     self->ratio = Q15_TO_FLOAT(ratioQ15);
56     self->sample_rate = sample_rate;
57 #endif
58 }
59 
btstack_sample_rate_compensation_update(btstack_sample_rate_compensation_t * self,uint32_t timestamp_ms,uint32_t samples,uint32_t playback_sample_rate)60 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) {
61     int32_t delta = timestamp_ms - self->last;
62     if( delta >= 1000 ) {
63         log_debug("current playback sample rate: %" PRId32 "", playback_sample_rate );
64 
65 #ifdef DEBUG_RATIO_CALCULATION
66         {
67             double current_sample_rate = self->count*(1000./delta);
68             double current_ratio = self->sample_rate/playback_sample_rate;
69 
70             // exponential weighted moving average
71             const double rate_decay = 0.025;
72             self->sample_rate += rate_decay * (current_sample_rate-self->sample_rate);
73 
74             // exponential weighted moving average
75             static const double ratio_decay = 1.3;
76             self->ratio += ratio_decay * (current_ratio-self->ratio);
77 
78             log_debug("current l2cap sample rate: %f (%d %d)", current_sample_rate, delta, self->count );
79             log_debug("current ratio:             %f", current_ratio);
80             log_debug("calculated ratio:          %f", self->ratio );
81         }
82 #endif
83         uint32_t fixed_rate = (self->count*(UINT16_C(1)<<15))/delta*1000;  // sample rate as Q15
84         uint32_t fixed_ratio = (self->rate_state<<7)/playback_sample_rate; // Q15
85         log_debug("fp current l2cap sample rate: %f (%" PRId32 " %" PRId32 ")", Q15_TO_FLOAT(fixed_rate), delta, self->count);
86 
87         self->last = timestamp_ms;
88         self->count = 0;
89 
90         // fixed point exponential weighted moving average
91         const int16_t rate_decay = FLOAT_TO_Q15(0.025f);
92         uint32_t rate = self->rate_state >> 8; // integer part only
93         self->rate_state += (rate_decay * (int32_t)((fixed_rate>>15)-rate)) >> (15-8); // Q8;
94 
95         // fixed point exponential weighted moving average
96         const int16_t ratio_decay = FLOAT_TO_Q8(1.3f);
97         self->ratio_state += (ratio_decay * (int32_t)((fixed_ratio<<1)-self->ratio_state)) >> (16-8); // Q16
98 
99         log_debug("fp current ratio :            %f", Q15_TO_FLOAT(fixed_ratio));
100         log_debug("fp calculated ratio:          %f", Q16_TO_FLOAT(self->ratio_state));
101         log_debug("scale factor Q16:             %" PRId32 "", self->ratio_state);
102     }
103 
104     self->count += samples;
105     return self->ratio_state;
106 }
107