xref: /btstack/src/btstack_resample.c (revision 4245bf3de9497bbe01192876191db06230cd94a1)
1c21cc0ebSMatthias Ringwald /*
2c21cc0ebSMatthias Ringwald  * Copyright (C) 2018 BlueKitchen GmbH
3c21cc0ebSMatthias Ringwald  *
4c21cc0ebSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5c21cc0ebSMatthias Ringwald  * modification, are permitted provided that the following conditions
6c21cc0ebSMatthias Ringwald  * are met:
7c21cc0ebSMatthias Ringwald  *
8c21cc0ebSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9c21cc0ebSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10c21cc0ebSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11c21cc0ebSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12c21cc0ebSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13c21cc0ebSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14c21cc0ebSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15c21cc0ebSMatthias Ringwald  *    from this software without specific prior written permission.
16c21cc0ebSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17c21cc0ebSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18c21cc0ebSMatthias Ringwald  *    monetary gain.
19c21cc0ebSMatthias Ringwald  *
20c21cc0ebSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21c21cc0ebSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22c21cc0ebSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25c21cc0ebSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26c21cc0ebSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27c21cc0ebSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28c21cc0ebSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29c21cc0ebSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30c21cc0ebSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31c21cc0ebSMatthias Ringwald  * SUCH DAMAGE.
32c21cc0ebSMatthias Ringwald  *
33c21cc0ebSMatthias Ringwald  * Please inquire about commercial licensing options at
34c21cc0ebSMatthias Ringwald  * [email protected]
35c21cc0ebSMatthias Ringwald  *
36c21cc0ebSMatthias Ringwald  */
37c21cc0ebSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_resample.c"
39bb2a7656SMatthias Ringwald 
4001899c8bSMatthias Ringwald #include "btstack_bool.h"
416e821f1cSMatthias Ringwald #include "btstack_debug.h"
42c21cc0ebSMatthias Ringwald #include "btstack_resample.h"
43c21cc0ebSMatthias Ringwald 
btstack_resample_init(btstack_resample_t * context,int num_channels)44c21cc0ebSMatthias Ringwald void btstack_resample_init(btstack_resample_t * context, int num_channels){
45*4245bf3dSMatthias Ringwald     btstack_assert(num_channels <= BTSTACK_RESAMPLE_MAX_CHANNELS);
46*4245bf3dSMatthias Ringwald 
47c21cc0ebSMatthias Ringwald     context->src_pos = 0;
48c21cc0ebSMatthias Ringwald     context->src_step = 0x10000;  // default resampling 1.0
49*4245bf3dSMatthias Ringwald     int i;
50*4245bf3dSMatthias Ringwald     for (i = 0; i < num_channels; i++){
51*4245bf3dSMatthias Ringwald         context->last_sample[i] = 0;
52*4245bf3dSMatthias Ringwald     }
53c21cc0ebSMatthias Ringwald     context->num_channels   = num_channels;
54c21cc0ebSMatthias Ringwald }
55c21cc0ebSMatthias Ringwald 
btstack_resample_set_factor(btstack_resample_t * context,uint32_t src_step)56c21cc0ebSMatthias Ringwald void btstack_resample_set_factor(btstack_resample_t * context, uint32_t src_step){
57c21cc0ebSMatthias Ringwald     context->src_step = src_step;
58c21cc0ebSMatthias Ringwald }
59c21cc0ebSMatthias Ringwald 
btstack_resample_block(btstack_resample_t * context,const int16_t * input_buffer,uint32_t num_frames,int16_t * output_buffer)60c21cc0ebSMatthias Ringwald uint16_t btstack_resample_block(btstack_resample_t * context, const int16_t * input_buffer, uint32_t num_frames, int16_t * output_buffer){
616e821f1cSMatthias Ringwald     btstack_assert(context->num_channels > 0);
626e821f1cSMatthias Ringwald 
63c21cc0ebSMatthias Ringwald     uint16_t dest_frames = 0;
64c21cc0ebSMatthias Ringwald     uint16_t dest_samples = 0;
65c21cc0ebSMatthias Ringwald     // samples between last sample of previous block and first sample in current block
66c21cc0ebSMatthias Ringwald     while (context->src_pos >= 0xffff0000){
674ea43905SMatthias Ringwald         const uint16_t t = context->src_pos & 0xffffu;
68c21cc0ebSMatthias Ringwald         int i;
69c21cc0ebSMatthias Ringwald         for (i=0;i<context->num_channels;i++){
70c21cc0ebSMatthias Ringwald             int s1 = context->last_sample[i];
71c21cc0ebSMatthias Ringwald             int s2 = input_buffer[i];
724ea43905SMatthias Ringwald             int os = ((s1*(0x10000u - t)) + (s2*t)) >> 16u;
73c21cc0ebSMatthias Ringwald             output_buffer[dest_samples++] = os;
74c21cc0ebSMatthias Ringwald         }
75c21cc0ebSMatthias Ringwald         dest_frames++;
76c21cc0ebSMatthias Ringwald         context->src_pos += context->src_step;
77c21cc0ebSMatthias Ringwald     }
78c21cc0ebSMatthias Ringwald     // process current block
79ff3cc4a5SMatthias Ringwald     while (true){
80c21cc0ebSMatthias Ringwald         const uint16_t src_pos = context->src_pos >> 16;
814ea43905SMatthias Ringwald         const uint16_t t       = context->src_pos & 0xffffu;
82c21cc0ebSMatthias Ringwald         int index = src_pos * context->num_channels;
83c21cc0ebSMatthias Ringwald         int i;
844ea43905SMatthias Ringwald         if (src_pos >= (num_frames - 1u)){
85c21cc0ebSMatthias Ringwald             // store last sample
860532fef7SMatthias Ringwald             index = (num_frames-1u) * context->num_channels;
87c21cc0ebSMatthias Ringwald             for (i=0;i<context->num_channels;i++){
88c21cc0ebSMatthias Ringwald                 context->last_sample[i] = input_buffer[index++];
89c21cc0ebSMatthias Ringwald             }
90c21cc0ebSMatthias Ringwald             // samples processed
91c21cc0ebSMatthias Ringwald             context->src_pos -= num_frames << 16;
92c21cc0ebSMatthias Ringwald             break;
93c21cc0ebSMatthias Ringwald         }
94c21cc0ebSMatthias Ringwald         for (i=0;i<context->num_channels;i++){
95c21cc0ebSMatthias Ringwald             int s1 = input_buffer[index];
96c21cc0ebSMatthias Ringwald             int s2 = input_buffer[index+context->num_channels];
974ea43905SMatthias Ringwald             int os = ((s1*(0x10000u - t)) + (s2*t)) >> 16u;
98c21cc0ebSMatthias Ringwald             output_buffer[dest_samples++] = os;
99c21cc0ebSMatthias Ringwald             index++;
100c21cc0ebSMatthias Ringwald         }
101c21cc0ebSMatthias Ringwald         dest_frames++;
102c21cc0ebSMatthias Ringwald         context->src_pos += context->src_step;
103c21cc0ebSMatthias Ringwald     }
104c21cc0ebSMatthias Ringwald     return dest_frames;
105c21cc0ebSMatthias Ringwald }
106