xref: /btstack/platform/embedded/btstack_audio_embedded.c (revision ff0081ee81686a0867861906d3bff5cdb1c3ed01)
1 /*
2  * Copyright (C) 2018 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 MATTHIAS
24  * RINGWALD 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_audio_embedded.c"
39 
40 /*
41  *  btstack_audio_embedded.c
42  *
43  *  Implementation of btstack_audio.h using hal_audio.h for embedded run loop
44  *
45  */
46 
47 #include "btstack_config.h"
48 #include "btstack_debug.h"
49 #include "btstack_audio.h"
50 #include "btstack_run_loop_embedded.h"
51 #include "hal_audio.h"
52 
53 // allow to compile all files in embedded folder even if there's no audio support
54 #ifdef HAVE_HAL_AUDIO
55 
56 #define DRIVER_POLL_INTERVAL_MS          5
57 
58 // client
59 static void (*playback_callback)(int16_t * buffer, uint16_t num_samples);
60 // static void (*recording_callback)(const int16_t * buffer, uint16_t num_samples);
61 
62 // timer to fill output ring buffer
63 static btstack_timer_source_t  driver_timer_sink;
64 // static btstack_timer_source_t  driver_timer_source;
65 
66 static volatile unsigned int output_buffer_to_play;
67 static          unsigned int output_buffer_to_fill;
68 static          unsigned int output_buffer_count;
69 static          unsigned int output_buffer_samples;
70 
71 static void btstack_audio_audio_played(uint8_t buffer_index){
72     output_buffer_to_play = (buffer_index + 1) % output_buffer_count;
73 }
74 
75 #if 0
76 static void btstack_audio_audio_recorded(const int16_t * samples, uint16_t num_samples){
77     UNUSED(samples);
78     UNUSED(num_samples);
79 }
80 #endif
81 
82 static void driver_timer_handler_sink(btstack_timer_source_t * ts){
83 
84     // playback buffer ready to fill
85     if (output_buffer_to_play != output_buffer_to_fill){
86         int16_t * buffer = hal_audio_sink_get_output_buffer(output_buffer_to_fill);
87         (*playback_callback)(buffer, output_buffer_samples);
88 
89         // next
90         output_buffer_to_fill = (output_buffer_to_fill + 1 ) % output_buffer_count;
91     }
92 
93     // re-set timer
94     btstack_run_loop_set_timer(ts, DRIVER_POLL_INTERVAL_MS);
95     btstack_run_loop_add_timer(ts);
96 }
97 
98 #if 0
99 static void driver_timer_handler_source(btstack_timer_source_t * ts){
100     // TODO
101 }
102 #endif
103 
104 static int btstack_audio_embedded_sink_init(
105     uint8_t channels,
106     uint32_t samplerate,
107     void (*playback)(int16_t * buffer, uint16_t num_samples)
108 ){
109     if (!playback){
110         log_error("No playback callback");
111         return 1;
112     }
113 
114     playback_callback  = playback;
115 
116     hal_audio_sink_init(channels, samplerate, &btstack_audio_audio_played);
117 
118     return 0;
119 }
120 
121 static int btstack_audio_embedded_source_init(
122     uint8_t channels,
123     uint32_t samplerate,
124     void (*recording)(const int16_t * buffer, uint16_t num_samples)
125 ){
126     if (!recording){
127         log_error("No recording callback");
128         return 1;
129     }
130     return 0;
131 }
132 
133 static void btstack_audio_embedded_sink_start_stream(void){
134 
135     output_buffer_count   = hal_audio_sink_get_num_output_buffers();
136     output_buffer_samples = hal_audio_sink_get_num_output_buffer_samples();
137 
138     // pre-fill HAL buffers
139     uint16_t i;
140     for (i=0;i<output_buffer_count;i++){
141         int16_t * buffer = hal_audio_sink_get_output_buffer(i);
142         (*playback_callback)(buffer, output_buffer_samples);
143     }
144 
145     output_buffer_to_play = 0;
146     output_buffer_to_fill = 0;
147 
148     // start playback
149     hal_audio_sink_start();
150 
151     // start timer
152     btstack_run_loop_set_timer_handler(&driver_timer_sink, &driver_timer_handler_sink);
153     btstack_run_loop_set_timer(&driver_timer_sink, DRIVER_POLL_INTERVAL_MS);
154     btstack_run_loop_add_timer(&driver_timer_sink);
155 }
156 
157 static void btstack_audio_embedded_source_start_stream(void){
158     // TODO
159 }
160 
161 static void btstack_audio_embedded_sink_close(void){
162     // stop timer
163     btstack_run_loop_remove_timer(&driver_timer_sink);
164     // close HAL
165     hal_audio_sink_close();
166 }
167 
168 static void btstack_audio_embedded_source_close(void){
169     // TODO
170 }
171 
172 static const btstack_audio_sink_t btstack_audio_embedded_sink = {
173     /* int (*init)(..);*/                                       &btstack_audio_embedded_sink_init,
174     /* void (*start_stream(void));*/                            &btstack_audio_embedded_sink_start_stream,
175     /* void (*close)(void); */                                  &btstack_audio_embedded_sink_close
176 };
177 
178 static const btstack_audio_source_t btstack_audio_embedded_source = {
179     /* int (*init)(..);*/                                       &btstack_audio_embedded_source_init,
180     /* void (*start_stream(void));*/                            &btstack_audio_embedded_source_start_stream,
181     /* void (*close)(void); */                                  &btstack_audio_embedded_source_close
182 };
183 
184 const btstack_audio_sink_t * btstack_audio_embedded_sink_get_instance(void){
185     return &btstack_audio_embedded_sink;
186 }
187 
188 const btstack_audio_source_t * btstack_audio_embedded_source_get_instance(void){
189     return &btstack_audio_embedded_source;
190 }
191 
192 #endif
193