1f7c85330SMatthias Ringwald /* 2f7c85330SMatthias Ringwald * Copyright (C) 2016 BlueKitchen GmbH 3f7c85330SMatthias Ringwald * 4f7c85330SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5f7c85330SMatthias Ringwald * modification, are permitted provided that the following conditions 6f7c85330SMatthias Ringwald * are met: 7f7c85330SMatthias Ringwald * 8f7c85330SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9f7c85330SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10f7c85330SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11f7c85330SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12f7c85330SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13f7c85330SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14f7c85330SMatthias Ringwald * contributors may be used to endorse or promote products derived 15f7c85330SMatthias Ringwald * from this software without specific prior written permission. 16f7c85330SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17f7c85330SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18f7c85330SMatthias Ringwald * monetary gain. 19f7c85330SMatthias Ringwald * 20f7c85330SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21f7c85330SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22f7c85330SMatthias 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, 25f7c85330SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26f7c85330SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27f7c85330SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28f7c85330SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29f7c85330SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30f7c85330SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31f7c85330SMatthias Ringwald * SUCH DAMAGE. 32f7c85330SMatthias Ringwald * 33f7c85330SMatthias Ringwald * Please inquire about commercial licensing options at 34f7c85330SMatthias Ringwald * [email protected] 35f7c85330SMatthias Ringwald * 36f7c85330SMatthias Ringwald */ 37ab2c6ae4SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "sco_demo_util.c" 39f7c85330SMatthias Ringwald 40f7c85330SMatthias Ringwald /* 41f7c85330SMatthias Ringwald * sco_demo_util.c - send/receive test data via SCO, used by hfp_*_demo and hsp_*_demo 42f7c85330SMatthias Ringwald */ 43f7c85330SMatthias Ringwald 442ec72fbbSMilanka Ringwald #include <stdio.h> 452ec72fbbSMilanka Ringwald 46f7c85330SMatthias Ringwald #include "sco_demo_util.h" 47379c5f5fSMatthias Ringwald 48379c5f5fSMatthias Ringwald #include "btstack_audio.h" 49fcb08cdbSMilanka Ringwald #include "btstack_debug.h" 50379c5f5fSMatthias Ringwald #include "btstack_ring_buffer.h" 5135fd3fb9SMatthias Ringwald #include "classic/btstack_cvsd_plc.h" 52379c5f5fSMatthias Ringwald #include "classic/btstack_sbc.h" 5335fd3fb9SMatthias Ringwald #include "classic/hfp.h" 54f0d95bdcSMatthias Ringwald #include "classic/hfp_codec.h" 55fcb08cdbSMilanka Ringwald 56681cb550SMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 57681cb550SMatthias Ringwald #include "btstack_lc3.h" 58681cb550SMatthias Ringwald #include "btstack_lc3_google.h" 59681cb550SMatthias Ringwald #endif 60681cb550SMatthias Ringwald 61681cb550SMatthias Ringwald 62f89e874bSMatthias Ringwald #ifdef _MSC_VER 63f89e874bSMatthias Ringwald // ignore deprecated warning for fopen 64f89e874bSMatthias Ringwald #pragma warning(disable : 4996) 65f89e874bSMatthias Ringwald #endif 66f89e874bSMatthias Ringwald 6735fd3fb9SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 68fbc7c9f2SMilanka Ringwald #include "wav_util.h" 6935fd3fb9SMatthias Ringwald #endif 70fbc7c9f2SMilanka Ringwald 71c4e666bcSMatthias Ringwald // test modes 72f7c85330SMatthias Ringwald #define SCO_DEMO_MODE_SINE 0 7394381a69SMatthias Ringwald #define SCO_DEMO_MODE_MICROPHONE 1 741762039cSMatthias Ringwald #define SCO_DEMO_MODE_MODPLAYER 2 75f7c85330SMatthias Ringwald 76f7c85330SMatthias Ringwald // SCO demo configuration 77d365bb51SMatthias Ringwald #define SCO_DEMO_MODE SCO_DEMO_MODE_MICROPHONE 78c4e666bcSMatthias Ringwald 79c4e666bcSMatthias Ringwald // number of sco packets until 'report' on console 80f7c85330SMatthias Ringwald #define SCO_REPORT_PERIOD 100 81f7c85330SMatthias Ringwald 82f55ac442SMatthias Ringwald 83d4f907a6SMatthias Ringwald #ifdef HAVE_POSIX_FILE_IO 842c7ae6e1SMatthias Ringwald // length and name of wav file on disk 85c4e666bcSMatthias Ringwald #define SCO_WAV_DURATION_IN_SECONDS 15 868b29cfc6SMatthias Ringwald #define SCO_WAV_FILENAME "sco_input.wav" 87859f2bc5SMatthias Ringwald #endif 88859f2bc5SMatthias Ringwald 89c4e666bcSMatthias Ringwald // constants 90c4e666bcSMatthias Ringwald #define NUM_CHANNELS 1 91b150a479SMatthias Ringwald #define SAMPLE_RATE_8KHZ 8000 92b150a479SMatthias Ringwald #define SAMPLE_RATE_16KHZ 16000 93681cb550SMatthias Ringwald #define SAMPLE_RATE_32KHZ 32000 94379c5f5fSMatthias Ringwald #define BYTES_PER_FRAME 2 95f7c85330SMatthias Ringwald 96681cb550SMatthias Ringwald // audio pre-buffer - also defines latency 97b150a479SMatthias Ringwald #define SCO_PREBUFFER_MS 50 98b150a479SMatthias Ringwald #define PREBUFFER_BYTES_8KHZ (SCO_PREBUFFER_MS * SAMPLE_RATE_8KHZ/1000 * BYTES_PER_FRAME) 99b150a479SMatthias Ringwald #define PREBUFFER_BYTES_16KHZ (SCO_PREBUFFER_MS * SAMPLE_RATE_16KHZ/1000 * BYTES_PER_FRAME) 100681cb550SMatthias Ringwald #define PREBUFFER_BYTES_32KHZ (SCO_PREBUFFER_MS * SAMPLE_RATE_32KHZ/1000 * BYTES_PER_FRAME) 101f7c85330SMatthias Ringwald 102681cb550SMatthias Ringwald #if defined(ENABLE_HFP_SUPER_WIDE_BAND_SPEECH) 103681cb550SMatthias Ringwald #define PREBUFFER_BYTES_MAX PREBUFFER_BYTES_32KHZ 104681cb550SMatthias Ringwald #define SAMPLES_PER_FRAME_MAX 240 105681cb550SMatthias Ringwald #elif defined(ENABLE_HFP_WIDE_BAND_SPEECH) 1069582ae64SMatthias Ringwald #define PREBUFFER_BYTES_MAX PREBUFFER_BYTES_16KHZ 107f0d95bdcSMatthias Ringwald #define SAMPLES_PER_FRAME_MAX 120 1089582ae64SMatthias Ringwald #else 1099582ae64SMatthias Ringwald #define PREBUFFER_BYTES_MAX PREBUFFER_BYTES_8KHZ 110f0d95bdcSMatthias Ringwald #define SAMPLES_PER_FRAME_MAX 60 1119582ae64SMatthias Ringwald #endif 1129582ae64SMatthias Ringwald 113bf958e42SMatthias Ringwald static uint16_t audio_prebuffer_bytes; 114bf958e42SMatthias Ringwald 1152b89dbfcSMatthias Ringwald // output 116d861f4bfSMatthias Ringwald static int audio_output_paused = 0; 1179582ae64SMatthias Ringwald static uint8_t audio_output_ring_buffer_storage[2 * PREBUFFER_BYTES_MAX]; 118379c5f5fSMatthias Ringwald static btstack_ring_buffer_t audio_output_ring_buffer; 1192b89dbfcSMatthias Ringwald 1202b89dbfcSMatthias Ringwald // input 1212b89dbfcSMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_MICROPHONE 122379c5f5fSMatthias Ringwald #define USE_AUDIO_INPUT 1233cf06407SMatthias Ringwald #else 1243cf06407SMatthias Ringwald #define USE_ADUIO_GENERATOR 1253cf06407SMatthias Ringwald static void (*sco_demo_audio_generator)(uint16_t num_samples, int16_t * data); 1266fb1424bSMatthias Ringwald #endif 127681cb550SMatthias Ringwald 128379c5f5fSMatthias Ringwald static int audio_input_paused = 0; 1299582ae64SMatthias Ringwald static uint8_t audio_input_ring_buffer_storage[2 * PREBUFFER_BYTES_MAX]; 130379c5f5fSMatthias Ringwald static btstack_ring_buffer_t audio_input_ring_buffer; 131f7c85330SMatthias Ringwald 1321762039cSMatthias Ringwald // mod player 1331762039cSMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_MODPLAYER 1341762039cSMatthias Ringwald #include "hxcmod.h" 1351762039cSMatthias Ringwald #include "mods/mod.h" 1361762039cSMatthias Ringwald static modcontext mod_context; 1371762039cSMatthias Ringwald #endif 1381762039cSMatthias Ringwald 139fcb08cdbSMilanka Ringwald static int count_sent = 0; 140fcb08cdbSMilanka Ringwald static int count_received = 0; 141c4e666bcSMatthias Ringwald 142681cb550SMatthias Ringwald static btstack_cvsd_plc_state_t cvsd_plc_state; 143681cb550SMatthias Ringwald 1441bbecc2bSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 1450a4f399aSMatthias Ringwald static btstack_sbc_encoder_state_t msbc_encoder_state; 146681cb550SMatthias Ringwald static btstack_sbc_decoder_state_t msbc_decoder_state; 1471bbecc2bSMatthias Ringwald #endif 1481bbecc2bSMatthias Ringwald 149681cb550SMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 150681cb550SMatthias Ringwald static const btstack_lc3_decoder_t * lc3_decoder; 151681cb550SMatthias Ringwald static btstack_lc3_decoder_google_t lc3_decoder_context; 152aeb2ea11SMatthias Ringwald static btstack_lc3_encoder_google_t lc3_encoder_context; 153681cb550SMatthias Ringwald static hfp_h2_sync_t hfp_h2_sync; 154681cb550SMatthias Ringwald #endif 155379c5f5fSMatthias Ringwald 1562b89dbfcSMatthias Ringwald int num_samples_to_write; 1572b89dbfcSMatthias Ringwald int num_audio_frames; 1582b89dbfcSMatthias Ringwald 159bf958e42SMatthias Ringwald // generic codec support 160bf958e42SMatthias Ringwald typedef struct { 161bf958e42SMatthias Ringwald void (*init)(void); 162bf958e42SMatthias Ringwald void(*receive)(const uint8_t * packet, uint16_t size); 163bf958e42SMatthias Ringwald void (*fill_payload)(uint8_t * payload_buffer, uint16_t sco_payload_length); 164bf958e42SMatthias Ringwald void (*close)(void); 16579cc780fSMatthias Ringwald // 16679cc780fSMatthias Ringwald uint16_t sample_rate; 167bf958e42SMatthias Ringwald } codec_support_t; 16879cc780fSMatthias Ringwald 16979cc780fSMatthias Ringwald // current configuration 170bf958e42SMatthias Ringwald static const codec_support_t * codec_current = NULL; 171bf958e42SMatthias Ringwald 172f0d95bdcSMatthias Ringwald // hfp_codec 1737555fa8bSMatthias Ringwald #if defined(ENABLE_HFP_WIDE_BAND_SPEECH) || defined(ENABLE_HFP_SUPER_WIDE_BAND_SPEECH) 174f0d95bdcSMatthias Ringwald static hfp_codec_t hfp_codec; 1757555fa8bSMatthias Ringwald #endif 176d6a06398SMatthias Ringwald 1779ad691b0SMatthias Ringwald // Sine Wave 1789ad691b0SMatthias Ringwald 1799ad691b0SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 1809ad691b0SMatthias Ringwald static uint16_t sine_wave_phase; 1819ad691b0SMatthias Ringwald static uint16_t sine_wave_steps_per_sample; 182681cb550SMatthias Ringwald #define SINE_WAVE_SAMPLE_RATE SAMPLE_RATE_32KHZ 18394381a69SMatthias Ringwald 184681cb550SMatthias Ringwald // input signal: pre-computed int16 sine wave, 32000 Hz at 266 Hz 185681cb550SMatthias Ringwald static const int16_t sine_int16[] = { 186681cb550SMatthias Ringwald 0, 1715, 3425, 5126, 6813, 8481, 10126, 11743, 13328, 14876, 187681cb550SMatthias Ringwald 16383, 17846, 19260, 20621, 21925, 23170, 24351, 25465, 26509, 27481, 188681cb550SMatthias Ringwald 28377, 29196, 29934, 30591, 31163, 31650, 32051, 32364, 32587, 32722, 189681cb550SMatthias Ringwald 32767, 32722, 32587, 32364, 32051, 31650, 31163, 30591, 29934, 29196, 190681cb550SMatthias Ringwald 28377, 27481, 26509, 25465, 24351, 23170, 21925, 20621, 19260, 17846, 191681cb550SMatthias Ringwald 16383, 14876, 13328, 11743, 10126, 8481, 6813, 5126, 3425, 1715, 192681cb550SMatthias Ringwald 0, -1715, -3425, -5126, -6813, -8481, -10126, -11743, -13328, -14876, 193681cb550SMatthias Ringwald -16384, -17846, -19260, -20621, -21925, -23170, -24351, -25465, -26509, -27481, 194681cb550SMatthias Ringwald -28377, -29196, -29934, -30591, -31163, -31650, -32051, -32364, -32587, -32722, 195681cb550SMatthias Ringwald -32767, -32722, -32587, -32364, -32051, -31650, -31163, -30591, -29934, -29196, 196681cb550SMatthias Ringwald -28377, -27481, -26509, -25465, -24351, -23170, -21925, -20621, -19260, -17846, 197681cb550SMatthias Ringwald -16384, -14876, -13328, -11743, -10126, -8481, -6813, -5126, -3425, -1715, 19835fd3fb9SMatthias Ringwald }; 19935fd3fb9SMatthias Ringwald 2009ad691b0SMatthias Ringwald static void sco_demo_sine_wave_host_endian(uint16_t num_samples, int16_t * data){ 201249d94cfSMatthias Ringwald unsigned int i; 20259c97ae1SMatthias Ringwald for (i=0; i < num_samples; i++){ 203681cb550SMatthias Ringwald data[i] = sine_int16[sine_wave_phase]; 2049ad691b0SMatthias Ringwald sine_wave_phase += sine_wave_steps_per_sample; 205681cb550SMatthias Ringwald if (sine_wave_phase >= (sizeof(sine_int16) / sizeof(int16_t))){ 2069ad691b0SMatthias Ringwald sine_wave_phase = 0; 20735fd3fb9SMatthias Ringwald } 20835fd3fb9SMatthias Ringwald } 20935fd3fb9SMatthias Ringwald } 2101bbecc2bSMatthias Ringwald #endif 211dbb41bfeSMilanka Ringwald 2121762039cSMatthias Ringwald // Mod Player 2131762039cSMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_MODPLAYER 2141762039cSMatthias Ringwald #define NUM_SAMPLES_GENERATOR_BUFFER 30 2151762039cSMatthias Ringwald static void sco_demo_modplayer(uint16_t num_samples, int16_t * data){ 2161762039cSMatthias Ringwald // mix down stereo 2171762039cSMatthias Ringwald signed short samples[NUM_SAMPLES_GENERATOR_BUFFER * 2]; 2181762039cSMatthias Ringwald while (num_samples > 0){ 2191762039cSMatthias Ringwald uint16_t next_samples = btstack_min(num_samples, NUM_SAMPLES_GENERATOR_BUFFER); 2201762039cSMatthias Ringwald hxcmod_fillbuffer(&mod_context, (unsigned short *) samples, next_samples, NULL); 2211762039cSMatthias Ringwald num_samples -= next_samples; 2221762039cSMatthias Ringwald uint16_t i; 2231762039cSMatthias Ringwald for (i=0;i<next_samples;i++){ 2241762039cSMatthias Ringwald int32_t left = samples[2*i + 0]; 2251762039cSMatthias Ringwald int32_t right = samples[2*i + 1]; 2261762039cSMatthias Ringwald data[i] = (int16_t)((left + right) / 2); 2271762039cSMatthias Ringwald } 2281762039cSMatthias Ringwald } 2291762039cSMatthias Ringwald } 2301762039cSMatthias Ringwald #endif 2311762039cSMatthias Ringwald 23294381a69SMatthias Ringwald // Audio Playback / Recording 233d861f4bfSMatthias Ringwald 23494381a69SMatthias Ringwald static void audio_playback_callback(int16_t * buffer, uint16_t num_samples){ 2352b89dbfcSMatthias Ringwald 236c4e666bcSMatthias Ringwald // fill with silence while paused 237379c5f5fSMatthias Ringwald if (audio_output_paused){ 238bf958e42SMatthias Ringwald if (btstack_ring_buffer_bytes_available(&audio_output_ring_buffer) < audio_prebuffer_bytes){ 239f55ac442SMatthias Ringwald memset(buffer, 0, num_samples * BYTES_PER_FRAME); 240379c5f5fSMatthias Ringwald return; 241dbb41bfeSMilanka Ringwald } else { 242c4e666bcSMatthias Ringwald // resume playback 243379c5f5fSMatthias Ringwald audio_output_paused = 0; 244dbb41bfeSMilanka Ringwald } 245c4e666bcSMatthias Ringwald } 246c4e666bcSMatthias Ringwald 247c4e666bcSMatthias Ringwald // get data from ringbuffer 248c4e666bcSMatthias Ringwald uint32_t bytes_read = 0; 249f55ac442SMatthias Ringwald btstack_ring_buffer_read(&audio_output_ring_buffer, (uint8_t *) buffer, num_samples * BYTES_PER_FRAME, &bytes_read); 250f55ac442SMatthias Ringwald num_samples -= bytes_read / BYTES_PER_FRAME; 251f55ac442SMatthias Ringwald buffer += bytes_read / BYTES_PER_FRAME; 252c4e666bcSMatthias Ringwald 253c4e666bcSMatthias Ringwald // fill with 0 if not enough 254f55ac442SMatthias Ringwald if (num_samples){ 255f55ac442SMatthias Ringwald memset(buffer, 0, num_samples * BYTES_PER_FRAME); 256379c5f5fSMatthias Ringwald audio_output_paused = 1; 257c4e666bcSMatthias Ringwald } 258379c5f5fSMatthias Ringwald } 2598b29cfc6SMatthias Ringwald 260379c5f5fSMatthias Ringwald #ifdef USE_AUDIO_INPUT 26194381a69SMatthias Ringwald static void audio_recording_callback(const int16_t * buffer, uint16_t num_samples){ 262379c5f5fSMatthias Ringwald btstack_ring_buffer_write(&audio_input_ring_buffer, (uint8_t *)buffer, num_samples * 2); 263c4e666bcSMatthias Ringwald } 264379c5f5fSMatthias Ringwald #endif 265c4e666bcSMatthias Ringwald 266c4e666bcSMatthias Ringwald // return 1 if ok 267379c5f5fSMatthias Ringwald static int audio_initialize(int sample_rate){ 268c4e666bcSMatthias Ringwald 269d365bb51SMatthias Ringwald // -- output -- // 270d365bb51SMatthias Ringwald 271379c5f5fSMatthias Ringwald // init buffers 272379c5f5fSMatthias Ringwald memset(audio_output_ring_buffer_storage, 0, sizeof(audio_output_ring_buffer_storage)); 273379c5f5fSMatthias Ringwald btstack_ring_buffer_init(&audio_output_ring_buffer, audio_output_ring_buffer_storage, sizeof(audio_output_ring_buffer_storage)); 2742b89dbfcSMatthias Ringwald 275d365bb51SMatthias Ringwald // config and setup audio playback 276d365bb51SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 277e0c8f71bSMatthias Ringwald if (audio_sink != NULL){ 27894381a69SMatthias Ringwald audio_sink->init(1, sample_rate, &audio_playback_callback); 279d365bb51SMatthias Ringwald audio_sink->start_stream(); 280379c5f5fSMatthias Ringwald 281379c5f5fSMatthias Ringwald audio_output_paused = 1; 282e0c8f71bSMatthias Ringwald } 283d365bb51SMatthias Ringwald 284d365bb51SMatthias Ringwald // -- input -- // 285d365bb51SMatthias Ringwald 286d365bb51SMatthias Ringwald // init buffers 287d365bb51SMatthias Ringwald memset(audio_input_ring_buffer_storage, 0, sizeof(audio_input_ring_buffer_storage)); 288d365bb51SMatthias Ringwald btstack_ring_buffer_init(&audio_input_ring_buffer, audio_input_ring_buffer_storage, sizeof(audio_input_ring_buffer_storage)); 2896fb1424bSMatthias Ringwald audio_input_paused = 1; 290d365bb51SMatthias Ringwald 2916fb1424bSMatthias Ringwald #ifdef USE_AUDIO_INPUT 292d365bb51SMatthias Ringwald // config and setup audio recording 293d365bb51SMatthias Ringwald const btstack_audio_source_t * audio_source = btstack_audio_source_get_instance(); 294e0c8f71bSMatthias Ringwald if (audio_source != NULL){ 29594381a69SMatthias Ringwald audio_source->init(1, sample_rate, &audio_recording_callback); 296d365bb51SMatthias Ringwald audio_source->start_stream(); 297e0c8f71bSMatthias Ringwald } 2982b89dbfcSMatthias Ringwald #endif 2992b89dbfcSMatthias Ringwald 300c4e666bcSMatthias Ringwald return 1; 301c4e666bcSMatthias Ringwald } 3022b89dbfcSMatthias Ringwald 303379c5f5fSMatthias Ringwald static void audio_terminate(void){ 304d365bb51SMatthias Ringwald const btstack_audio_sink_t * audio_sink = btstack_audio_sink_get_instance(); 305d365bb51SMatthias Ringwald if (!audio_sink) return; 306d365bb51SMatthias Ringwald audio_sink->close(); 307d365bb51SMatthias Ringwald 308d365bb51SMatthias Ringwald #ifdef USE_AUDIO_INPUT 309d365bb51SMatthias Ringwald const btstack_audio_source_t * audio_source= btstack_audio_source_get_instance(); 310d365bb51SMatthias Ringwald if (!audio_source) return; 311d365bb51SMatthias Ringwald audio_source->close(); 312d365bb51SMatthias Ringwald #endif 3132b89dbfcSMatthias Ringwald } 314c4e666bcSMatthias Ringwald 315d861f4bfSMatthias Ringwald 31694381a69SMatthias Ringwald // CVSD - 8 kHz 317fcb08cdbSMilanka Ringwald 318bf958e42SMatthias Ringwald static void sco_demo_cvsd_init(void){ 319c4e666bcSMatthias Ringwald printf("SCO Demo: Init CVSD\n"); 320fbc7c9f2SMilanka Ringwald btstack_cvsd_plc_init(&cvsd_plc_state); 321fbc7c9f2SMilanka Ringwald } 322fbc7c9f2SMilanka Ringwald 323bf958e42SMatthias Ringwald static void sco_demo_cvsd_receive(const uint8_t * packet, uint16_t size){ 3242c7ae6e1SMatthias Ringwald 3255303ddeeSMatthias Ringwald int16_t audio_frame_out[128]; // 3261f8694ccSMatthias Ringwald 3271f8694ccSMatthias Ringwald if (size > sizeof(audio_frame_out)){ 328bf958e42SMatthias Ringwald printf("sco_demo_cvsd_receive: SCO packet larger than local output buffer - dropping data.\n"); 3291f8694ccSMatthias Ringwald return; 3301f8694ccSMatthias Ringwald } 3312c7ae6e1SMatthias Ringwald 332c4e666bcSMatthias Ringwald const int audio_bytes_read = size - 3; 333379c5f5fSMatthias Ringwald const int num_samples = audio_bytes_read / BYTES_PER_FRAME; 3345303ddeeSMatthias Ringwald 3355303ddeeSMatthias Ringwald // convert into host endian 3365303ddeeSMatthias Ringwald int16_t audio_frame_in[128]; 3375303ddeeSMatthias Ringwald int i; 3385303ddeeSMatthias Ringwald for (i=0;i<num_samples;i++){ 3395303ddeeSMatthias Ringwald audio_frame_in[i] = little_endian_read_16(packet, 3 + i * 2); 3405303ddeeSMatthias Ringwald } 3415303ddeeSMatthias Ringwald 3425f4f94c7SMatthias Ringwald // treat packet as bad frame if controller does not report 'all good' 3435f4f94c7SMatthias Ringwald bool bad_frame = (packet[1] & 0x30) != 0; 3445f4f94c7SMatthias Ringwald 3455f4f94c7SMatthias Ringwald btstack_cvsd_plc_process_data(&cvsd_plc_state, bad_frame, audio_frame_in, num_samples, audio_frame_out); 3465303ddeeSMatthias Ringwald 3472c7ae6e1SMatthias Ringwald #ifdef SCO_WAV_FILENAME 3482c7ae6e1SMatthias Ringwald // Samples in CVSD SCO packet are in little endian, ready for wav files (take shortcut) 3492c7ae6e1SMatthias Ringwald const int samples_to_write = btstack_min(num_samples, num_samples_to_write); 3502c7ae6e1SMatthias Ringwald wav_writer_write_le_int16(samples_to_write, audio_frame_out); 3512c7ae6e1SMatthias Ringwald num_samples_to_write -= samples_to_write; 3522c7ae6e1SMatthias Ringwald if (num_samples_to_write == 0){ 3532c7ae6e1SMatthias Ringwald wav_writer_close(); 3542c7ae6e1SMatthias Ringwald } 3552c7ae6e1SMatthias Ringwald #endif 3562c7ae6e1SMatthias Ringwald 357379c5f5fSMatthias Ringwald btstack_ring_buffer_write(&audio_output_ring_buffer, (uint8_t *)audio_frame_out, audio_bytes_read); 358fcb08cdbSMilanka Ringwald } 359fcb08cdbSMilanka Ringwald 360bf958e42SMatthias Ringwald static void sco_demo_cvsd_fill_payload(uint8_t * payload_buffer, uint16_t sco_payload_length){ 3616fb1424bSMatthias Ringwald uint16_t bytes_to_copy = sco_payload_length; 3626fb1424bSMatthias Ringwald 3632b89dbfcSMatthias Ringwald // get data from ringbuffer 3642b89dbfcSMatthias Ringwald uint16_t pos = 0; 365379c5f5fSMatthias Ringwald if (!audio_input_paused){ 3666fb1424bSMatthias Ringwald uint16_t samples_to_copy = sco_payload_length / 2; 3672b89dbfcSMatthias Ringwald uint32_t bytes_read = 0; 3686fb1424bSMatthias Ringwald btstack_ring_buffer_read(&audio_input_ring_buffer, payload_buffer, bytes_to_copy, &bytes_read); 3698fd6902dSMatthias Ringwald // flip 16 on big endian systems 3708fd6902dSMatthias Ringwald // @note We don't use (uint16_t *) casts since all sample addresses are odd which causes crahses on some systems 3718fd6902dSMatthias Ringwald if (btstack_is_big_endian()){ 3726fb1424bSMatthias Ringwald uint16_t i; 3736fb1424bSMatthias Ringwald for (i=0;i<samples_to_copy/2;i+=2){ 3746fb1424bSMatthias Ringwald uint8_t tmp = payload_buffer[i*2]; 3756fb1424bSMatthias Ringwald payload_buffer[i*2] = payload_buffer[i*2+1]; 3766fb1424bSMatthias Ringwald payload_buffer[i*2+1] = tmp; 3778fd6902dSMatthias Ringwald } 3788fd6902dSMatthias Ringwald } 3792b89dbfcSMatthias Ringwald bytes_to_copy -= bytes_read; 3802b89dbfcSMatthias Ringwald pos += bytes_read; 3812b89dbfcSMatthias Ringwald } 3822b89dbfcSMatthias Ringwald 3832b89dbfcSMatthias Ringwald // fill with 0 if not enough 3842b89dbfcSMatthias Ringwald if (bytes_to_copy){ 3856fb1424bSMatthias Ringwald memset(payload_buffer + pos, 0, bytes_to_copy); 386379c5f5fSMatthias Ringwald audio_input_paused = 1; 3872b89dbfcSMatthias Ringwald } 3882b89dbfcSMatthias Ringwald } 38994381a69SMatthias Ringwald 390bf958e42SMatthias Ringwald static void sco_demo_cvsd_close(void){ 391bf958e42SMatthias Ringwald printf("Used CVSD with PLC, number of proccesed frames: \n - %d good frames, \n - %d bad frames.\n", cvsd_plc_state.good_frames_nr, cvsd_plc_state.bad_frames_nr); 392bf958e42SMatthias Ringwald } 393bf958e42SMatthias Ringwald 394bf958e42SMatthias Ringwald static const codec_support_t codec_cvsd = { 395bf958e42SMatthias Ringwald .init = &sco_demo_cvsd_init, 396bf958e42SMatthias Ringwald .receive = &sco_demo_cvsd_receive, 397bf958e42SMatthias Ringwald .fill_payload = &sco_demo_cvsd_fill_payload, 39879cc780fSMatthias Ringwald .close = &sco_demo_cvsd_close, 39979cc780fSMatthias Ringwald .sample_rate = SAMPLE_RATE_8KHZ 400bf958e42SMatthias Ringwald }; 401bf958e42SMatthias Ringwald 402681cb550SMatthias Ringwald // encode using hfp_codec 403681cb550SMatthias Ringwald #if defined(ENABLE_HFP_WIDE_BAND_SPEECH) || defined(ENABLE_HFP_SUPER_WIDE_BAND_SPEECH) 404d4872f4eSMatthias Ringwald static void sco_demo_codec_fill_payload(uint8_t * payload_buffer, uint16_t sco_payload_length){ 405681cb550SMatthias Ringwald if (!audio_input_paused){ 406681cb550SMatthias Ringwald int num_samples = hfp_codec_num_audio_samples_per_frame(&hfp_codec); 407681cb550SMatthias Ringwald btstack_assert(num_samples <= SAMPLES_PER_FRAME_MAX); 408681cb550SMatthias Ringwald uint16_t samples_available = btstack_ring_buffer_bytes_available(&audio_input_ring_buffer) / BYTES_PER_FRAME; 409681cb550SMatthias Ringwald if (hfp_codec_can_encode_audio_frame_now(&hfp_codec) && samples_available >= num_samples){ 410681cb550SMatthias Ringwald int16_t sample_buffer[SAMPLES_PER_FRAME_MAX]; 411681cb550SMatthias Ringwald uint32_t bytes_read; 412681cb550SMatthias Ringwald btstack_ring_buffer_read(&audio_input_ring_buffer, (uint8_t*) sample_buffer, num_samples * BYTES_PER_FRAME, &bytes_read); 413681cb550SMatthias Ringwald hfp_codec_encode_audio_frame(&hfp_codec, sample_buffer); 414681cb550SMatthias Ringwald num_audio_frames++; 415681cb550SMatthias Ringwald } 416681cb550SMatthias Ringwald } 417681cb550SMatthias Ringwald // get data from encoder, fill with 0 if not enough 418681cb550SMatthias Ringwald if (audio_input_paused || hfp_codec_num_bytes_available(&hfp_codec) < sco_payload_length){ 419681cb550SMatthias Ringwald // just send '0's 420681cb550SMatthias Ringwald memset(payload_buffer, 0, sco_payload_length); 421681cb550SMatthias Ringwald audio_input_paused = 1; 422681cb550SMatthias Ringwald } else { 423681cb550SMatthias Ringwald hfp_codec_read_from_stream(&hfp_codec, payload_buffer, sco_payload_length); 424681cb550SMatthias Ringwald } 425681cb550SMatthias Ringwald } 426681cb550SMatthias Ringwald #endif 427681cb550SMatthias Ringwald 42894381a69SMatthias Ringwald // mSBC - 16 kHz 42994381a69SMatthias Ringwald 43094381a69SMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 43194381a69SMatthias Ringwald 43294381a69SMatthias Ringwald static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 43394381a69SMatthias Ringwald UNUSED(context); 43494381a69SMatthias Ringwald UNUSED(sample_rate); 43594381a69SMatthias Ringwald UNUSED(data); 43694381a69SMatthias Ringwald UNUSED(num_samples); 43794381a69SMatthias Ringwald UNUSED(num_channels); 43894381a69SMatthias Ringwald 43994381a69SMatthias Ringwald // samples in callback in host endianess, ready for playback 44094381a69SMatthias Ringwald btstack_ring_buffer_write(&audio_output_ring_buffer, (uint8_t *)data, num_samples*num_channels*2); 44194381a69SMatthias Ringwald 44294381a69SMatthias Ringwald #ifdef SCO_WAV_FILENAME 44394381a69SMatthias Ringwald if (!num_samples_to_write) return; 44494381a69SMatthias Ringwald num_samples = btstack_min(num_samples, num_samples_to_write); 44594381a69SMatthias Ringwald num_samples_to_write -= num_samples; 44694381a69SMatthias Ringwald wav_writer_write_int16(num_samples, data); 44794381a69SMatthias Ringwald if (num_samples_to_write == 0){ 44894381a69SMatthias Ringwald wav_writer_close(); 449f7c85330SMatthias Ringwald } 45094381a69SMatthias Ringwald #endif /* SCO_WAV_FILENAME */ 4511a919128SMatthias Ringwald } 45294381a69SMatthias Ringwald 453bf958e42SMatthias Ringwald static void sco_demo_msbc_init(void){ 45494381a69SMatthias Ringwald printf("SCO Demo: Init mSBC\n"); 455681cb550SMatthias Ringwald btstack_sbc_decoder_init(&msbc_decoder_state, SBC_MODE_mSBC, &handle_pcm_data, NULL); 4560a4f399aSMatthias Ringwald hfp_codec_init_msbc(&hfp_codec, &msbc_encoder_state); 45794381a69SMatthias Ringwald } 45894381a69SMatthias Ringwald 459bf958e42SMatthias Ringwald static void sco_demo_msbc_receive(const uint8_t * packet, uint16_t size){ 460681cb550SMatthias Ringwald btstack_sbc_decoder_process_data(&msbc_decoder_state, (packet[1] >> 4) & 3, packet + 3, size - 3); 46194381a69SMatthias Ringwald } 46294381a69SMatthias Ringwald 463bf958e42SMatthias Ringwald static void sco_demo_msbc_close(void){ 464681cb550SMatthias Ringwald printf("Used mSBC with PLC, number of processed frames: \n - %d good frames, \n - %d zero frames, \n - %d bad frames.\n", msbc_decoder_state.good_frames_nr, msbc_decoder_state.zero_frames_nr, msbc_decoder_state.bad_frames_nr); 465bf958e42SMatthias Ringwald } 466bf958e42SMatthias Ringwald 467bf958e42SMatthias Ringwald static const codec_support_t codec_msbc = { 468bf958e42SMatthias Ringwald .init = &sco_demo_msbc_init, 469bf958e42SMatthias Ringwald .receive = &sco_demo_msbc_receive, 470681cb550SMatthias Ringwald .fill_payload = &sco_demo_codec_fill_payload, 47179cc780fSMatthias Ringwald .close = &sco_demo_msbc_close, 47279cc780fSMatthias Ringwald .sample_rate = SAMPLE_RATE_16KHZ 473bf958e42SMatthias Ringwald }; 474bf958e42SMatthias Ringwald 47594381a69SMatthias Ringwald #endif /* ENABLE_HFP_WIDE_BAND_SPEECH */ 47694381a69SMatthias Ringwald 477681cb550SMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 478681cb550SMatthias Ringwald 479681cb550SMatthias Ringwald #define LC3_SWB_SAMPLES_PER_FRAME 240 480681cb550SMatthias Ringwald #define LC3_SWB_OCTETS_PER_FRAME 58 481681cb550SMatthias Ringwald 482681cb550SMatthias Ringwald static bool sco_demo_lc3swb_frame_callback(bool bad_frame, const uint8_t * frame_data, uint16_t frame_len){ 483681cb550SMatthias Ringwald 484681cb550SMatthias Ringwald // skip H2 header for good frames 485681cb550SMatthias Ringwald if (bad_frame == false){ 486681cb550SMatthias Ringwald btstack_assert(frame_data != NULL); 487681cb550SMatthias Ringwald frame_data += 2; 488681cb550SMatthias Ringwald } 489681cb550SMatthias Ringwald 490681cb550SMatthias Ringwald uint8_t tmp_BEC_detect = 0; 491681cb550SMatthias Ringwald uint8_t BFI = bad_frame ? 1 : 0; 492681cb550SMatthias Ringwald int16_t samples[LC3_SWB_SAMPLES_PER_FRAME]; 493681cb550SMatthias Ringwald (void) lc3_decoder->decode_signed_16(&lc3_decoder_context, frame_data, BFI, 494681cb550SMatthias Ringwald samples, 1, &tmp_BEC_detect); 495681cb550SMatthias Ringwald 496681cb550SMatthias Ringwald // samples in callback in host endianess, ready for playback 497681cb550SMatthias Ringwald btstack_ring_buffer_write(&audio_output_ring_buffer, (uint8_t *)samples, LC3_SWB_SAMPLES_PER_FRAME*2); 498681cb550SMatthias Ringwald 499681cb550SMatthias Ringwald #ifdef SCO_WAV_FILENAME 500681cb550SMatthias Ringwald if (num_samples_to_write > 0){ 501681cb550SMatthias Ringwald uint16_t num_samples = btstack_min(LC3_SWB_SAMPLES_PER_FRAME, num_samples_to_write); 502681cb550SMatthias Ringwald num_samples_to_write -= num_samples; 503681cb550SMatthias Ringwald wav_writer_write_int16(num_samples, samples); 504681cb550SMatthias Ringwald if (num_samples_to_write == 0){ 505681cb550SMatthias Ringwald wav_writer_close(); 506681cb550SMatthias Ringwald } 507681cb550SMatthias Ringwald } 508681cb550SMatthias Ringwald #endif /* SCO_WAV_FILENAME */ 509681cb550SMatthias Ringwald 510681cb550SMatthias Ringwald // frame is good, if it isn't a bad frame and we didn't detect other errors 511681cb550SMatthias Ringwald return (bad_frame == false) && (tmp_BEC_detect == 0); 512681cb550SMatthias Ringwald } 513681cb550SMatthias Ringwald 514681cb550SMatthias Ringwald static void sco_demo_lc3swb_init(void){ 515681cb550SMatthias Ringwald 516681cb550SMatthias Ringwald printf("SCO Demo: Init LC3-SWB\n"); 517681cb550SMatthias Ringwald 5183b7e9cecSMatthias Ringwald hfp_codec.lc3_encoder_context = &lc3_encoder_context; 519aeb2ea11SMatthias Ringwald const btstack_lc3_encoder_t * lc3_encoder = btstack_lc3_encoder_google_init_instance((btstack_lc3_encoder_google_t *) hfp_codec.lc3_encoder_context); 520aeb2ea11SMatthias Ringwald hfp_codec_init_lc3_swb(&hfp_codec, lc3_encoder, &lc3_encoder_context); 521681cb550SMatthias Ringwald 522681cb550SMatthias Ringwald // init lc3 decoder 523681cb550SMatthias Ringwald lc3_decoder = btstack_lc3_decoder_google_init_instance(&lc3_decoder_context); 524681cb550SMatthias Ringwald lc3_decoder->configure(&lc3_decoder_context, SAMPLE_RATE_32KHZ, BTSTACK_LC3_FRAME_DURATION_7500US, LC3_SWB_OCTETS_PER_FRAME); 525681cb550SMatthias Ringwald 526681cb550SMatthias Ringwald // init HPF H2 framing 527681cb550SMatthias Ringwald hfp_h2_sync_init(&hfp_h2_sync, &sco_demo_lc3swb_frame_callback); 528681cb550SMatthias Ringwald } 529681cb550SMatthias Ringwald 530681cb550SMatthias Ringwald static void sco_demo_lc3swb_receive(const uint8_t * packet, uint16_t size){ 531681cb550SMatthias Ringwald uint8_t packet_status = (packet[1] >> 4) & 3; 532681cb550SMatthias Ringwald bool bad_frame = packet_status != 0; 533681cb550SMatthias Ringwald hfp_h2_sync_process(&hfp_h2_sync, bad_frame, &packet[3], size-3); 534681cb550SMatthias Ringwald } 535681cb550SMatthias Ringwald 536681cb550SMatthias Ringwald static void sco_demo_lc3swb_close(void){ 537681cb550SMatthias Ringwald // TODO: report 538681cb550SMatthias Ringwald } 539681cb550SMatthias Ringwald 540681cb550SMatthias Ringwald static const codec_support_t codec_lc3swb = { 541681cb550SMatthias Ringwald .init = &sco_demo_lc3swb_init, 542681cb550SMatthias Ringwald .receive = &sco_demo_lc3swb_receive, 543681cb550SMatthias Ringwald .fill_payload = &sco_demo_codec_fill_payload, 544681cb550SMatthias Ringwald .close = &sco_demo_lc3swb_close, 545681cb550SMatthias Ringwald .sample_rate = SAMPLE_RATE_32KHZ 546681cb550SMatthias Ringwald }; 547681cb550SMatthias Ringwald #endif 548681cb550SMatthias Ringwald 54994381a69SMatthias Ringwald void sco_demo_init(void){ 55094381a69SMatthias Ringwald 55194381a69SMatthias Ringwald #ifdef ENABLE_CLASSIC_LEGACY_CONNECTIONS_FOR_SCO_DEMOS 55294381a69SMatthias Ringwald printf("Disable BR/EDR Secure Connctions due to incompatibilities with SCO connections\n"); 55394381a69SMatthias Ringwald gap_secure_connections_enable(false); 55494381a69SMatthias Ringwald #endif 55594381a69SMatthias Ringwald 556681cb550SMatthias Ringwald // Set SCO for CVSD (mSBC or other codecs automatically use 8-bit transparent mode) 557681cb550SMatthias Ringwald hci_set_sco_voice_setting(0x60); // linear, unsigned, 16-bit, CVSD 558681cb550SMatthias Ringwald 55994381a69SMatthias Ringwald // status 56094381a69SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_MICROPHONE 56194381a69SMatthias Ringwald printf("SCO Demo: Sending and receiving audio via btstack_audio.\n"); 56294381a69SMatthias Ringwald #endif 56394381a69SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 56494381a69SMatthias Ringwald printf("SCO Demo: Sending sine wave, audio output via btstack_audio.\n"); 56594381a69SMatthias Ringwald #endif 5661762039cSMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_MODPLAYER 567681cb550SMatthias Ringwald printf("SCO Demo: Sending modplayer wave, audio output via btstack_audio.\n"); 5681762039cSMatthias Ringwald // init mod 5691762039cSMatthias Ringwald int hxcmod_initialized = hxcmod_init(&mod_context); 5701762039cSMatthias Ringwald btstack_assert(hxcmod_initialized != 0); 5711762039cSMatthias Ringwald #endif 57294381a69SMatthias Ringwald } 57394381a69SMatthias Ringwald 574bf958e42SMatthias Ringwald void sco_demo_set_codec(uint8_t negotiated_codec){ 57594381a69SMatthias Ringwald switch (negotiated_codec){ 57694381a69SMatthias Ringwald case HFP_CODEC_CVSD: 577bf958e42SMatthias Ringwald codec_current = &codec_cvsd; 57894381a69SMatthias Ringwald break; 57994381a69SMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 58094381a69SMatthias Ringwald case HFP_CODEC_MSBC: 581bf958e42SMatthias Ringwald codec_current = &codec_msbc; 58294381a69SMatthias Ringwald break; 58394381a69SMatthias Ringwald #endif 584681cb550SMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 585681cb550SMatthias Ringwald case HFP_CODEC_LC3_SWB: 586681cb550SMatthias Ringwald codec_current = &codec_lc3swb; 587681cb550SMatthias Ringwald break; 588681cb550SMatthias Ringwald #endif 58994381a69SMatthias Ringwald default: 59094381a69SMatthias Ringwald btstack_assert(false); 59194381a69SMatthias Ringwald break; 59294381a69SMatthias Ringwald } 593bf958e42SMatthias Ringwald 594bf958e42SMatthias Ringwald codec_current->init(); 5959ad691b0SMatthias Ringwald 5969ad691b0SMatthias Ringwald audio_initialize(codec_current->sample_rate); 5979ad691b0SMatthias Ringwald 5989ad691b0SMatthias Ringwald audio_prebuffer_bytes = SCO_PREBUFFER_MS * (codec_current->sample_rate/1000) * BYTES_PER_FRAME; 5999ad691b0SMatthias Ringwald 6009ad691b0SMatthias Ringwald #ifdef SCO_WAV_FILENAME 6019ad691b0SMatthias Ringwald num_samples_to_write = codec_current->sample_rate * SCO_WAV_DURATION_IN_SECONDS; 6029ad691b0SMatthias Ringwald wav_writer_open(SCO_WAV_FILENAME, 1, codec_current->sample_rate); 6039ad691b0SMatthias Ringwald #endif 6049ad691b0SMatthias Ringwald 6059ad691b0SMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_SINE 6069ad691b0SMatthias Ringwald sine_wave_steps_per_sample = SINE_WAVE_SAMPLE_RATE / codec_current->sample_rate; 6079ad691b0SMatthias Ringwald sco_demo_audio_generator = &sco_demo_sine_wave_host_endian; 6089ad691b0SMatthias Ringwald #endif 6091762039cSMatthias Ringwald 6101762039cSMatthias Ringwald #if SCO_DEMO_MODE == SCO_DEMO_MODE_MODPLAYER 6111762039cSMatthias Ringwald // load mod 6121762039cSMatthias Ringwald hxcmod_setcfg(&mod_context, codec_current->sample_rate, 16, 1, 1, 1); 6131762039cSMatthias Ringwald hxcmod_load(&mod_context, (void *) &mod_data, mod_len); 6141762039cSMatthias Ringwald sco_demo_audio_generator = &sco_demo_modplayer; 6151762039cSMatthias Ringwald #endif 61694381a69SMatthias Ringwald } 61794381a69SMatthias Ringwald 61894381a69SMatthias Ringwald void sco_demo_receive(uint8_t * packet, uint16_t size){ 61994381a69SMatthias Ringwald static uint32_t packets = 0; 62094381a69SMatthias Ringwald static uint32_t crc_errors = 0; 62194381a69SMatthias Ringwald static uint32_t data_received = 0; 62294381a69SMatthias Ringwald static uint32_t byte_errors = 0; 62394381a69SMatthias Ringwald 62494381a69SMatthias Ringwald count_received++; 62594381a69SMatthias Ringwald 62694381a69SMatthias Ringwald data_received += size - 3; 62794381a69SMatthias Ringwald packets++; 62894381a69SMatthias Ringwald if (data_received > 100000){ 62994381a69SMatthias Ringwald printf("Summary: data %07u, packets %04u, packet with crc errors %0u, byte errors %04u\n", (unsigned int) data_received, (unsigned int) packets, (unsigned int) crc_errors, (unsigned int) byte_errors); 63094381a69SMatthias Ringwald crc_errors = 0; 63194381a69SMatthias Ringwald byte_errors = 0; 63294381a69SMatthias Ringwald data_received = 0; 63394381a69SMatthias Ringwald packets = 0; 63494381a69SMatthias Ringwald } 63594381a69SMatthias Ringwald 636bf958e42SMatthias Ringwald codec_current->receive(packet, size); 63794381a69SMatthias Ringwald } 63894381a69SMatthias Ringwald 63994381a69SMatthias Ringwald void sco_demo_send(hci_con_handle_t sco_handle){ 64094381a69SMatthias Ringwald 64194381a69SMatthias Ringwald if (sco_handle == HCI_CON_HANDLE_INVALID) return; 64294381a69SMatthias Ringwald 643*b8543c7aSMatthias Ringwald int sco_packet_length = hci_get_sco_packet_length_for_connection(sco_handle); 64494381a69SMatthias Ringwald int sco_payload_length = sco_packet_length - 3; 64594381a69SMatthias Ringwald 64694381a69SMatthias Ringwald hci_reserve_packet_buffer(); 64794381a69SMatthias Ringwald uint8_t * sco_packet = hci_get_outgoing_packet_buffer(); 64894381a69SMatthias Ringwald 649bf958e42SMatthias Ringwald #ifdef USE_ADUIO_GENERATOR 650bf958e42SMatthias Ringwald #define REFILL_SAMPLES 16 651bf958e42SMatthias Ringwald // re-fill audio buffer 652bf958e42SMatthias Ringwald uint16_t samples_free = btstack_ring_buffer_bytes_free(&audio_input_ring_buffer) / 2; 653bf958e42SMatthias Ringwald while (samples_free > 0){ 654bf958e42SMatthias Ringwald int16_t samples_buffer[REFILL_SAMPLES]; 655bf958e42SMatthias Ringwald uint16_t samples_to_add = btstack_min(samples_free, REFILL_SAMPLES); 656bf958e42SMatthias Ringwald (*sco_demo_audio_generator)(samples_to_add, samples_buffer); 657bf958e42SMatthias Ringwald btstack_ring_buffer_write(&audio_input_ring_buffer, (uint8_t *)samples_buffer, samples_to_add * 2); 658bf958e42SMatthias Ringwald samples_free -= samples_to_add; 65994381a69SMatthias Ringwald } 660bf958e42SMatthias Ringwald #endif 661bf958e42SMatthias Ringwald 662bf958e42SMatthias Ringwald // resume if pre-buffer is filled 663bf958e42SMatthias Ringwald if (audio_input_paused){ 664bf958e42SMatthias Ringwald if (btstack_ring_buffer_bytes_available(&audio_input_ring_buffer) >= audio_prebuffer_bytes){ 665bf958e42SMatthias Ringwald // resume sending 666bf958e42SMatthias Ringwald audio_input_paused = 0; 667bf958e42SMatthias Ringwald } 668bf958e42SMatthias Ringwald } 669bf958e42SMatthias Ringwald 670bf958e42SMatthias Ringwald // fill payload by codec 671bf958e42SMatthias Ringwald codec_current->fill_payload(&sco_packet[3], sco_payload_length); 6722b89dbfcSMatthias Ringwald 673c4e666bcSMatthias Ringwald // set handle + flags 674c4e666bcSMatthias Ringwald little_endian_store_16(sco_packet, 0, sco_handle); 675c4e666bcSMatthias Ringwald // set len 676c4e666bcSMatthias Ringwald sco_packet[2] = sco_payload_length; 677c4e666bcSMatthias Ringwald // finally send packet 678f7c85330SMatthias Ringwald hci_send_sco_packet_buffer(sco_packet_length); 679f7c85330SMatthias Ringwald 680f7c85330SMatthias Ringwald // request another send event 681f7c85330SMatthias Ringwald hci_request_sco_can_send_now_event(); 682f7c85330SMatthias Ringwald 6834a96141eSMatthias Ringwald count_sent++; 68494381a69SMatthias Ringwald if ((count_sent % SCO_REPORT_PERIOD) == 0) { 68594381a69SMatthias Ringwald printf("SCO: sent %u, received %u\n", count_sent, count_received); 68694381a69SMatthias Ringwald } 687f7c85330SMatthias Ringwald } 688f7c85330SMatthias Ringwald 68994381a69SMatthias Ringwald void sco_demo_close(void){ 69094381a69SMatthias Ringwald printf("SCO demo close\n"); 6911a919128SMatthias Ringwald 69294381a69SMatthias Ringwald printf("SCO demo statistics: "); 693bf958e42SMatthias Ringwald codec_current->close(); 694bf958e42SMatthias Ringwald codec_current = NULL; 69594381a69SMatthias Ringwald 69694381a69SMatthias Ringwald #if defined(SCO_WAV_FILENAME) 69794381a69SMatthias Ringwald wav_writer_close(); 6988b29cfc6SMatthias Ringwald #endif 6998b29cfc6SMatthias Ringwald 70094381a69SMatthias Ringwald audio_terminate(); 701f7c85330SMatthias Ringwald } 702