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
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH 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"
46b0e4fcd5SMatthias Ringwald #include <stdio.h>
4769fa7fc6SMatthias Ringwald
4869fa7fc6SMatthias Ringwald #define TABLE_SIZE_441HZ 100
4969fa7fc6SMatthias Ringwald
50*2336d2edSMilanka Ringwald // change to 1 for mono output
51*2336d2edSMilanka Ringwald #define NUM_CHANNELS 2
52*2336d2edSMilanka Ringwald
5369fa7fc6SMatthias Ringwald static int sine_phase;
5469fa7fc6SMatthias Ringwald
5569fa7fc6SMatthias Ringwald static const int16_t sine_int16[] = {
5669fa7fc6SMatthias Ringwald 0, 2057, 4107, 6140, 8149, 10126, 12062, 13952, 15786, 17557,
5769fa7fc6SMatthias Ringwald 19260, 20886, 22431, 23886, 25247, 26509, 27666, 28714, 29648, 30466,
5869fa7fc6SMatthias Ringwald 31163, 31738, 32187, 32509, 32702, 32767, 32702, 32509, 32187, 31738,
5969fa7fc6SMatthias Ringwald 31163, 30466, 29648, 28714, 27666, 26509, 25247, 23886, 22431, 20886,
6069fa7fc6SMatthias Ringwald 19260, 17557, 15786, 13952, 12062, 10126, 8149, 6140, 4107, 2057,
6169fa7fc6SMatthias Ringwald 0, -2057, -4107, -6140, -8149, -10126, -12062, -13952, -15786, -17557,
6269fa7fc6SMatthias Ringwald -19260, -20886, -22431, -23886, -25247, -26509, -27666, -28714, -29648, -30466,
6369fa7fc6SMatthias Ringwald -31163, -31738, -32187, -32509, -32702, -32767, -32702, -32509, -32187, -31738,
6469fa7fc6SMatthias Ringwald -31163, -30466, -29648, -28714, -27666, -26509, -25247, -23886, -22431, -20886,
6569fa7fc6SMatthias Ringwald -19260, -17557, -15786, -13952, -12062, -10126, -8149, -6140, -4107, -2057,
6669fa7fc6SMatthias Ringwald };
6769fa7fc6SMatthias Ringwald
audio_playback(int16_t * pcm_buffer,uint16_t num_samples_to_write)6869fa7fc6SMatthias Ringwald static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){
6969fa7fc6SMatthias Ringwald int count;
7069fa7fc6SMatthias Ringwald for (count = 0; count < num_samples_to_write ; count++){
71*2336d2edSMilanka Ringwald unsigned int channel;
72*2336d2edSMilanka Ringwald for (channel = 0; channel < NUM_CHANNELS ; channel++){
73*2336d2edSMilanka Ringwald pcm_buffer[count * NUM_CHANNELS + channel] = sine_int16[sine_phase];
74*2336d2edSMilanka Ringwald }
7569fa7fc6SMatthias Ringwald sine_phase++;
7669fa7fc6SMatthias Ringwald if (sine_phase >= TABLE_SIZE_441HZ){
7769fa7fc6SMatthias Ringwald sine_phase -= TABLE_SIZE_441HZ;
7869fa7fc6SMatthias Ringwald }
7969fa7fc6SMatthias Ringwald }
8069fa7fc6SMatthias Ringwald }
8169fa7fc6SMatthias Ringwald
8269fa7fc6SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])8369fa7fc6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
8469fa7fc6SMatthias Ringwald (void)argc;
8569fa7fc6SMatthias Ringwald (void)argv;
8669fa7fc6SMatthias Ringwald
8769fa7fc6SMatthias Ringwald // setup audio playback
88d365bb51SMatthias Ringwald const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
8969fa7fc6SMatthias Ringwald if (!audio){
90d365bb51SMatthias Ringwald printf("BTstack Audio Sink not setup\n");
9169fa7fc6SMatthias Ringwald return 10;
9269fa7fc6SMatthias Ringwald }
93*2336d2edSMilanka Ringwald audio->init(NUM_CHANNELS, 44100, &audio_playback);
9469fa7fc6SMatthias Ringwald audio->start_stream();
9569fa7fc6SMatthias Ringwald
9669fa7fc6SMatthias Ringwald return 0;
9769fa7fc6SMatthias Ringwald }
98ec8ae085SMilanka Ringwald
99ec8ae085SMilanka Ringwald /* EXAMPLE_END */