xref: /btstack/test/sbc/sbc_decoder_sine.c (revision 630ffdd469bbec3276322f46b93e6cfdfcb21c27)
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 #define SAMPLE_RATE 44100
56 
57 static int total_num_samples = 0;
58 static int frame_count = 0;
59 
60 #define NUM_SAMPLES 128
61 #define NUM_CHANNELS 2
62 #define BYTES_PER_SAMPLE 2
63 
64 static uint16_t audio_samples0[NUM_SAMPLES*2];
65 static uint16_t audio_samples1[NUM_SAMPLES*2];
66 static volatile int playback_buffer;
67 
68 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
69 	printf("Samples: num_samples %u, num_channels %u, sample_rate %u\n", num_samples, num_channels, sample_rate);
70 	// printf_hexdump(data, num_samples * num_channels * 2);
71     int i;
72     for (i=0;i<num_channels*num_samples;i += 2){
73         if ((i%24) == 0) printf("\n");
74         printf ("%12d ", data[i]);
75     }
76     printf("\n");
77 
78     total_num_samples+=num_samples*num_channels;
79     frame_count++;
80 }
81 
82 int btstack_main (int argc, const char * argv[]){
83 	(void) argc;
84 	(void) argv;
85 
86 
87     btstack_sbc_mode_t mode = SBC_MODE_STANDARD;
88     btstack_sbc_decoder_state_t state;
89     btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
90     btstack_sbc_decoder_test_disable_plc();
91     uint32_t t_start = btstack_run_loop_get_time_ms();
92     playback_buffer = 1;
93     uint32_t offset = 0;
94 
95     uint32_t sbc_len = data_sine_stereo_sbc_len;
96     uint8_t * sbc_data = data_sine_stereo_sbc;
97 
98     btstack_sbc_decoder_process_data(&state, 0, &sbc_data[offset], 74);
99     offset += 74;
100 
101     while (1){
102 
103 	    btstack_sbc_decoder_process_data(&state, 0, &sbc_data[offset], 74);
104 	    offset += 74;
105 
106 	    if (offset >= sbc_len){
107 	    	offset = 0;
108 	    }
109 
110 	    while (playback_buffer == 0){
111 			__asm__("wfe");
112 		}
113 
114 	    btstack_sbc_decoder_process_data(&state, 0, &sbc_data[offset], 74);
115 	    offset += 74;
116 
117 	    if (offset >= sbc_len){
118 	    	offset = 0;
119 	    }
120 
121 	    while (playback_buffer == 1){
122 			__asm__("wfe");
123 		}
124 
125     }
126 #if 0
127     uint32_t t_end = btstack_run_loop_get_time_ms();
128     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);
129     printf("Time for 100 frames %u ms\n", t_end - t_start);	// frame len 74 in this sbc test sample
130 #endif
131     return 0;
132 }
133