169fa7fc6SMatthias Ringwald /* 269fa7fc6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 369fa7fc6SMatthias Ringwald * 469fa7fc6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 569fa7fc6SMatthias Ringwald * modification, are permitted provided that the following conditions 669fa7fc6SMatthias Ringwald * are met: 769fa7fc6SMatthias Ringwald * 869fa7fc6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 969fa7fc6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 1069fa7fc6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 1169fa7fc6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 1269fa7fc6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 1369fa7fc6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 1469fa7fc6SMatthias Ringwald * contributors may be used to endorse or promote products derived 1569fa7fc6SMatthias Ringwald * from this software without specific prior written permission. 1669fa7fc6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 1769fa7fc6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 1869fa7fc6SMatthias Ringwald * monetary gain. 1969fa7fc6SMatthias Ringwald * 2069fa7fc6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 2169fa7fc6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2269fa7fc6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2369fa7fc6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 2469fa7fc6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2569fa7fc6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 2669fa7fc6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 2769fa7fc6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 2869fa7fc6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 2969fa7fc6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 3069fa7fc6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3169fa7fc6SMatthias Ringwald * SUCH DAMAGE. 3269fa7fc6SMatthias Ringwald * 3369fa7fc6SMatthias Ringwald * Please inquire about commercial licensing options at 3469fa7fc6SMatthias Ringwald * [email protected] 3569fa7fc6SMatthias Ringwald * 3669fa7fc6SMatthias Ringwald */ 3769fa7fc6SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "sine_player.c" 39bb2a7656SMatthias Ringwald 40ec8ae085SMilanka Ringwald /* EXAMPLE_START(audio_duplex): Audio Driver - Play Sine 4169fa7fc6SMatthias Ringwald * 42ec8ae085SMilanka Ringwald * @text Play sine to test and validate audio output with simple wave form. 4369fa7fc6SMatthias Ringwald */ 4469fa7fc6SMatthias Ringwald 4569fa7fc6SMatthias Ringwald #include "btstack.h" 46*b0e4fcd5SMatthias Ringwald #include <stdio.h> 4769fa7fc6SMatthias Ringwald 4869fa7fc6SMatthias Ringwald #define TABLE_SIZE_441HZ 100 4969fa7fc6SMatthias Ringwald 5069fa7fc6SMatthias Ringwald static int sine_phase; 5169fa7fc6SMatthias Ringwald 5269fa7fc6SMatthias Ringwald static const int16_t sine_int16[] = { 5369fa7fc6SMatthias Ringwald 0, 2057, 4107, 6140, 8149, 10126, 12062, 13952, 15786, 17557, 5469fa7fc6SMatthias Ringwald 19260, 20886, 22431, 23886, 25247, 26509, 27666, 28714, 29648, 30466, 5569fa7fc6SMatthias Ringwald 31163, 31738, 32187, 32509, 32702, 32767, 32702, 32509, 32187, 31738, 5669fa7fc6SMatthias Ringwald 31163, 30466, 29648, 28714, 27666, 26509, 25247, 23886, 22431, 20886, 5769fa7fc6SMatthias Ringwald 19260, 17557, 15786, 13952, 12062, 10126, 8149, 6140, 4107, 2057, 5869fa7fc6SMatthias Ringwald 0, -2057, -4107, -6140, -8149, -10126, -12062, -13952, -15786, -17557, 5969fa7fc6SMatthias Ringwald -19260, -20886, -22431, -23886, -25247, -26509, -27666, -28714, -29648, -30466, 6069fa7fc6SMatthias Ringwald -31163, -31738, -32187, -32509, -32702, -32767, -32702, -32509, -32187, -31738, 6169fa7fc6SMatthias Ringwald -31163, -30466, -29648, -28714, -27666, -26509, -25247, -23886, -22431, -20886, 6269fa7fc6SMatthias Ringwald -19260, -17557, -15786, -13952, -12062, -10126, -8149, -6140, -4107, -2057, 6369fa7fc6SMatthias Ringwald }; 6469fa7fc6SMatthias Ringwald 6569fa7fc6SMatthias Ringwald static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){ 6669fa7fc6SMatthias Ringwald int count; 6769fa7fc6SMatthias Ringwald for (count = 0; count < num_samples_to_write ; count++){ 6869fa7fc6SMatthias Ringwald pcm_buffer[count * 2] = sine_int16[sine_phase]; 6969fa7fc6SMatthias Ringwald pcm_buffer[count * 2 + 1] = sine_int16[sine_phase]; 7069fa7fc6SMatthias Ringwald sine_phase++; 7169fa7fc6SMatthias Ringwald if (sine_phase >= TABLE_SIZE_441HZ){ 7269fa7fc6SMatthias Ringwald sine_phase -= TABLE_SIZE_441HZ; 7369fa7fc6SMatthias Ringwald } 7469fa7fc6SMatthias Ringwald } 7569fa7fc6SMatthias Ringwald } 7669fa7fc6SMatthias Ringwald 7769fa7fc6SMatthias Ringwald int btstack_main(int argc, const char * argv[]); 7869fa7fc6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 7969fa7fc6SMatthias Ringwald (void)argc; 8069fa7fc6SMatthias Ringwald (void)argv; 8169fa7fc6SMatthias Ringwald 8269fa7fc6SMatthias Ringwald // setup audio playback 83d365bb51SMatthias Ringwald const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance(); 8469fa7fc6SMatthias Ringwald if (!audio){ 85d365bb51SMatthias Ringwald printf("BTstack Audio Sink not setup\n"); 8669fa7fc6SMatthias Ringwald return 10; 8769fa7fc6SMatthias Ringwald } 88d365bb51SMatthias Ringwald audio->init(2, 44100, &audio_playback); 8969fa7fc6SMatthias Ringwald audio->start_stream(); 9069fa7fc6SMatthias Ringwald 9169fa7fc6SMatthias Ringwald return 0; 9269fa7fc6SMatthias Ringwald } 93ec8ae085SMilanka Ringwald 94ec8ae085SMilanka Ringwald /* EXAMPLE_END */