xref: /btstack/src/btstack_resample.h (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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
23*2fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24*2fca4dadSMilanka 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 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title Lienar Resampling
40fe5a6c4eSMilanka Ringwald  *
41fe5a6c4eSMilanka Ringwald  * Linear resampling for 16-bit audio code samples using 16 bit/16 bit fixed point math.
42fe5a6c4eSMilanka Ringwald  *
43fe5a6c4eSMilanka Ringwald  */
44fe5a6c4eSMilanka Ringwald 
4580e33422SMatthias Ringwald #ifndef BTSTACK_RESAMPLE_H
4680e33422SMatthias Ringwald #define BTSTACK_RESAMPLE_H
47c21cc0ebSMatthias Ringwald 
48c21cc0ebSMatthias Ringwald #include <stdint.h>
49c21cc0ebSMatthias Ringwald 
50c21cc0ebSMatthias Ringwald #if defined __cplusplus
51c21cc0ebSMatthias Ringwald extern "C" {
52c21cc0ebSMatthias Ringwald #endif
53c21cc0ebSMatthias Ringwald 
54c21cc0ebSMatthias Ringwald #define BTSTACK_RESAMPLE_MAX_CHANNELS 2
55c21cc0ebSMatthias Ringwald 
56c21cc0ebSMatthias Ringwald typedef struct {
57c21cc0ebSMatthias Ringwald     uint32_t src_pos;
58c21cc0ebSMatthias Ringwald     uint32_t src_step;
59c21cc0ebSMatthias Ringwald     int16_t  last_sample[BTSTACK_RESAMPLE_MAX_CHANNELS];
60c21cc0ebSMatthias Ringwald     int      num_channels;
61c21cc0ebSMatthias Ringwald } btstack_resample_t;
62c21cc0ebSMatthias Ringwald 
63fe5a6c4eSMilanka Ringwald /* API_START */
64fe5a6c4eSMilanka Ringwald 
65c21cc0ebSMatthias Ringwald /**
66c21cc0ebSMatthias Ringwald  * @brief Init resample context
67c21cc0ebSMatthias Ringwald  * @param num_channels
686b65794dSMilanka Ringwald  * @return btstack_audio implementation
69c21cc0ebSMatthias Ringwald  */
70c21cc0ebSMatthias Ringwald void btstack_resample_init(btstack_resample_t * context, int num_channels);
71c21cc0ebSMatthias Ringwald 
72c21cc0ebSMatthias Ringwald /**
73c21cc0ebSMatthias Ringwald  * @brief Set resampling factor
74c21cc0ebSMatthias Ringwald  * @param factor as fixed point value, identity is 0x10000
75c21cc0ebSMatthias Ringwald  */
76c21cc0ebSMatthias Ringwald void btstack_resample_set_factor(btstack_resample_t * context, uint32_t factor);
77c21cc0ebSMatthias Ringwald 
78c21cc0ebSMatthias Ringwald /**
79c21cc0ebSMatthias Ringwald  * @brief Process block of input samples
80c21cc0ebSMatthias Ringwald  * @note size of output buffer is not checked
81c21cc0ebSMatthias Ringwald  * @param input_buffer
82c21cc0ebSMatthias Ringwald  * @param num_frames
83c21cc0ebSMatthias Ringwald  * @param output_buffer
846b65794dSMilanka Ringwald  * @return number destination frames
85c21cc0ebSMatthias Ringwald  */
86c21cc0ebSMatthias Ringwald uint16_t btstack_resample_block(btstack_resample_t * context, const int16_t * input_buffer, uint32_t num_frames, int16_t * output_buffer);
87c21cc0ebSMatthias Ringwald 
88fe5a6c4eSMilanka Ringwald /* API_END */
89fe5a6c4eSMilanka Ringwald 
90c21cc0ebSMatthias Ringwald #if defined __cplusplus
91c21cc0ebSMatthias Ringwald }
92c21cc0ebSMatthias Ringwald #endif
93c21cc0ebSMatthias Ringwald 
94c21cc0ebSMatthias Ringwald #endif
95