1 /*
2 ** Copyright 2008, The Android Open-Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16 
17 #ifndef ANDROID_RESAMPLER_H
18 #define ANDROID_RESAMPLER_H
19 
20 #include <stdint.h>
21 #include <sys/time.h>
22 
23 __BEGIN_DECLS
24 
25 
26 #define RESAMPLER_QUALITY_MAX 10
27 #define RESAMPLER_QUALITY_MIN 0
28 #define RESAMPLER_QUALITY_DEFAULT 4
29 #define RESAMPLER_QUALITY_VOIP 3
30 #define RESAMPLER_QUALITY_DESKTOP 5
31 
32 struct resampler_buffer {
33     union {
34         void*       raw;
35         short*      i16;
36         int8_t*     i8;
37     };
38     size_t frame_count;
39 };
40 
41 /** call back interface used by the resampler to get new data */
42 struct resampler_buffer_provider
43 {
44     /**
45      *  get a new buffer of data:
46      *   as input: buffer->frame_count is the number of frames requested
47      *   as output: buffer->frame_count is the number of frames returned
48      *              buffer->raw points to data returned
49      */
50     int (*get_next_buffer)(struct resampler_buffer_provider *provider,
51             struct resampler_buffer *buffer);
52     /**
53      *  release a consumed buffer of data:
54      *   as input: buffer->frame_count is the number of frames released
55      *             buffer->raw points to data released
56      */
57     void (*release_buffer)(struct resampler_buffer_provider *provider,
58             struct resampler_buffer *buffer);
59 };
60 
61 /** resampler interface */
62 struct resampler_itfe {
63     /**
64      * reset resampler state
65      */
66     void (*reset)(struct resampler_itfe *resampler);
67     /**
68      * resample input from buffer provider and output at most *outFrameCount to out buffer.
69      * *outFrameCount is updated with the actual number of frames produced.
70      */
71     int (*resample_from_provider)(struct resampler_itfe *resampler,
72                     int16_t *out,
73                     size_t *outFrameCount);
74     /**
75      * resample at most *inFrameCount frames from in buffer and output at most
76      * *outFrameCount to out buffer. *inFrameCount and *outFrameCount are updated respectively
77      * with the number of frames remaining in input and written to output.
78      */
79     int (*resample_from_input)(struct resampler_itfe *resampler,
80                     int16_t *in,
81                     size_t *inFrameCount,
82                     int16_t *out,
83                     size_t *outFrameCount);
84     /**
85      * \return the latency introduced by the resampler in ns.
86      */
87     int32_t (*delay_ns)(struct resampler_itfe *resampler);
88 };
89 
90 /**
91  * create a resampler according to input parameters passed.
92  * If resampler_buffer_provider is not NULL only resample_from_provider() can be called.
93  * If resampler_buffer_provider is NULL only resample_from_input() can be called.
94  */
95 int create_resampler(uint32_t inSampleRate,
96           uint32_t outSampleRate,
97           uint32_t channelCount,
98           uint32_t quality,
99           struct resampler_buffer_provider *provider,
100           struct resampler_itfe **);
101 
102 /**
103  * release resampler resources.
104  */
105 void release_resampler(struct resampler_itfe *);
106 
107 __END_DECLS
108 
109 #endif // ANDROID_RESAMPLER_H
110