xref: /btstack/test/sbc/sbc_decoder_sine.c (revision d40c3de009bce6994e726da5a427e08951b353d9)
1 /*
2  * Copyright (C) 2014 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 // *****************************************************************************
39 //
40 // SBC decoder tests
41 //
42 // *****************************************************************************
43 
44 #include "btstack_config.h"
45 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include "btstack.h"
52 #include "classic/btstack_sbc.h"
53 #include "data_sine_stereo_sbc.h"
54 
55 #ifdef HAVE_AUDIO_DMA
56 #include "hal_audio_dma.h"
57 #endif
58 
59 #define SAMPLE_RATE 44100
60 
61 static int total_num_samples = 0;
62 static int frame_count = 0;
63 
64 #define NUM_SAMPLES 128
65 #define NUM_CHANNELS 2
66 #define BYTES_PER_SAMPLE 2
67 
68 static uint16_t audio_samples0[NUM_SAMPLES*2];
69 static uint16_t audio_samples1[NUM_SAMPLES*2];
70 static volatile int playback_buffer;
71 
72 #ifdef HAVE_AUDIO_DMA
73 void hal_audio_dma_done(void){
74 	uint16_t bytes_to_copy = sizeof(audio_samples1);
75 	playback_buffer = 1 - playback_buffer;
76 //	printf("Play %u\n", playback_buffer);
77 	hal_audio_dma_play(playback_buffer ? audio_samples1 : audio_samples0, NUM_SAMPLES * NUM_CHANNELS * BYTES_PER_SAMPLE);
78 }
79 #endif
80 
81 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
82 #ifdef HAVE_AUDIO_DMA
83 //	printf("Write %u (%u, %u)\n", playback_buffer, num_samples, num_channels);
84 	int write_buffer = 1 - playback_buffer;
85 	playback_buffer ? audio_samples0 : audio_samples1;
86 	memcpy(write_buffer ? audio_samples1 : audio_samples0, data, NUM_SAMPLES * NUM_CHANNELS * BYTES_PER_SAMPLE);
87 #else
88 	printf("Samples: num_samples %u, num_channels %u, sample_rate %u\n", num_samples, num_channels, sample_rate);
89 	// printf_hexdump(data, num_samples * num_channels * 2);
90     int i;
91     for (i=0;i<num_channels*num_samples;i += 2){
92         if ((i%24) == 0) printf("\n");
93         printf ("%12d ", data[i]);
94     }
95     printf("\n");
96 #endif
97     total_num_samples+=num_samples*num_channels;
98     frame_count++;
99 }
100 
101 int btstack_main (int argc, const char * argv[]){
102 	(void) argc;
103 	(void) argv;
104 
105 
106     btstack_sbc_mode_t mode = SBC_MODE_STANDARD;
107     btstack_sbc_decoder_state_t state;
108     btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
109     btstack_sbc_decoder_test_disable_plc();
110     uint32_t t_start = btstack_run_loop_get_time_ms();
111     playback_buffer = 1;
112     uint32_t offset = 0;
113 
114     uint32_t sbc_len = data_sine_stereo_sbc_len;
115     uint8_t * sbc_data = data_sine_stereo_sbc;
116 
117     btstack_sbc_decoder_process_data(&state, 0, &sbc_data[offset], 74);
118     offset += 74;
119 
120 #ifdef HAVE_AUDIO_DMA
121 	hal_audio_dma_init(SAMPLE_RATE);
122 	hal_audio_dma_set_audio_played(&hal_audio_dma_done);
123     hal_audio_dma_done();
124 #endif
125 
126     while (1){
127 
128 	    btstack_sbc_decoder_process_data(&state, 0, &sbc_data[offset], 74);
129 	    offset += 74;
130 
131 	    if (offset >= sbc_len){
132 	    	offset = 0;
133 	    }
134 
135 	    while (playback_buffer == 0){
136 			__asm__("wfe");
137 		}
138 
139 	    btstack_sbc_decoder_process_data(&state, 0, &sbc_data[offset], 74);
140 	    offset += 74;
141 
142 	    if (offset >= sbc_len){
143 	    	offset = 0;
144 	    }
145 
146 	    while (playback_buffer == 1){
147 			__asm__("wfe");
148 		}
149 
150     }
151 #if 0
152     uint32_t t_end = btstack_run_loop_get_time_ms();
153     printf("Decoding done. Processed %d frames:\n - %d good\n - %d bad\n - %d zero frames\n", frame_count, state.good_frames_nr, state.bad_frames_nr, state.zero_frames_nr);
154     printf("Time for 100 frames %u ms\n", t_end - t_start);	// frame len 74 in this sbc test sample
155 #endif
156     return 0;
157 }
158