xref: /btstack/example/sine_player.c (revision ec8ae085b4a7cad9458241027c59738d9647e24d)
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 
40*ec8ae085SMilanka Ringwald /* EXAMPLE_START(audio_duplex): Audio Driver - Play Sine
4169fa7fc6SMatthias Ringwald  *
42*ec8ae085SMilanka Ringwald  * @text  Play sine to test and validate audio output with simple wave form.
4369fa7fc6SMatthias Ringwald  */
4469fa7fc6SMatthias Ringwald 
4569fa7fc6SMatthias Ringwald #include "btstack.h"
4669fa7fc6SMatthias Ringwald 
4769fa7fc6SMatthias Ringwald #define TABLE_SIZE_441HZ            100
4869fa7fc6SMatthias Ringwald 
4969fa7fc6SMatthias Ringwald static int sine_phase;
5069fa7fc6SMatthias Ringwald 
5169fa7fc6SMatthias Ringwald static const int16_t sine_int16[] = {
5269fa7fc6SMatthias Ringwald      0,    2057,    4107,    6140,    8149,   10126,   12062,   13952,   15786,   17557,
5369fa7fc6SMatthias Ringwald  19260,   20886,   22431,   23886,   25247,   26509,   27666,   28714,   29648,   30466,
5469fa7fc6SMatthias Ringwald  31163,   31738,   32187,   32509,   32702,   32767,   32702,   32509,   32187,   31738,
5569fa7fc6SMatthias Ringwald  31163,   30466,   29648,   28714,   27666,   26509,   25247,   23886,   22431,   20886,
5669fa7fc6SMatthias Ringwald  19260,   17557,   15786,   13952,   12062,   10126,    8149,    6140,    4107,    2057,
5769fa7fc6SMatthias Ringwald      0,   -2057,   -4107,   -6140,   -8149,  -10126,  -12062,  -13952,  -15786,  -17557,
5869fa7fc6SMatthias Ringwald -19260,  -20886,  -22431,  -23886,  -25247,  -26509,  -27666,  -28714,  -29648,  -30466,
5969fa7fc6SMatthias Ringwald -31163,  -31738,  -32187,  -32509,  -32702,  -32767,  -32702,  -32509,  -32187,  -31738,
6069fa7fc6SMatthias Ringwald -31163,  -30466,  -29648,  -28714,  -27666,  -26509,  -25247,  -23886,  -22431,  -20886,
6169fa7fc6SMatthias Ringwald -19260,  -17557,  -15786,  -13952,  -12062,  -10126,   -8149,   -6140,   -4107,   -2057,
6269fa7fc6SMatthias Ringwald };
6369fa7fc6SMatthias Ringwald 
6469fa7fc6SMatthias Ringwald static void audio_playback(int16_t * pcm_buffer, uint16_t num_samples_to_write){
6569fa7fc6SMatthias Ringwald     int count;
6669fa7fc6SMatthias Ringwald     for (count = 0; count < num_samples_to_write ; count++){
6769fa7fc6SMatthias Ringwald         pcm_buffer[count * 2]     = sine_int16[sine_phase];
6869fa7fc6SMatthias Ringwald         pcm_buffer[count * 2 + 1] = sine_int16[sine_phase];
6969fa7fc6SMatthias Ringwald         sine_phase++;
7069fa7fc6SMatthias Ringwald         if (sine_phase >= TABLE_SIZE_441HZ){
7169fa7fc6SMatthias Ringwald             sine_phase -= TABLE_SIZE_441HZ;
7269fa7fc6SMatthias Ringwald         }
7369fa7fc6SMatthias Ringwald     }
7469fa7fc6SMatthias Ringwald }
7569fa7fc6SMatthias Ringwald 
7669fa7fc6SMatthias Ringwald int btstack_main(int argc, const char * argv[]);
7769fa7fc6SMatthias Ringwald int btstack_main(int argc, const char * argv[]){
7869fa7fc6SMatthias Ringwald     (void)argc;
7969fa7fc6SMatthias Ringwald     (void)argv;
8069fa7fc6SMatthias Ringwald 
8169fa7fc6SMatthias Ringwald     // setup audio playback
82d365bb51SMatthias Ringwald     const btstack_audio_sink_t * audio = btstack_audio_sink_get_instance();
8369fa7fc6SMatthias Ringwald     if (!audio){
84d365bb51SMatthias Ringwald         printf("BTstack Audio Sink not setup\n");
8569fa7fc6SMatthias Ringwald         return 10;
8669fa7fc6SMatthias Ringwald     }
87d365bb51SMatthias Ringwald     audio->init(2, 44100, &audio_playback);
8869fa7fc6SMatthias Ringwald     audio->start_stream();
8969fa7fc6SMatthias Ringwald 
9069fa7fc6SMatthias Ringwald     return 0;
9169fa7fc6SMatthias Ringwald }
92*ec8ae085SMilanka Ringwald 
93*ec8ae085SMilanka Ringwald /* EXAMPLE_END */