1 /*
2 * Copyright (C) 2016 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 #include <stdio.h>
39 #include <math.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <portaudio.h>
43
44 #include "btstack_ring_buffer.h"
45 #include "classic/btstack_sbc.h"
46 #include "wav_util.h"
47 #include "classic/avdtp.h"
48 #include "classic/avdtp_source.h"
49 #include "btstack_stdin.h"
50
51 #define NUM_CHANNELS 2
52 #define SAMPLE_RATE 44100
53 #define BYTES_PER_AUDIO_SAMPLE (2*NUM_CHANNELS)
54 #define LATENCY 300 // ms
55
56 #ifndef M_PI
57 #define M_PI 3.14159265
58 #endif
59 #define TABLE_SIZE_441HZ 100
60
61 typedef struct {
62 int16_t source[TABLE_SIZE_441HZ];
63 int left_phase;
64 int right_phase;
65 } paTestData;
66
67 static uint8_t pcm_frame[2*8*16*2];
68
69 static paTestData sin_data;
70 // static int total_num_samples = 0;
71
72 static char * output_wav_filename = "test_output_sine.wav";
73 // static char * input_wav_filename = "test_input_sine.wav";
74
75 static btstack_sbc_decoder_state_t state;
76 static btstack_sbc_mode_t mode = SBC_MODE_STANDARD;
77 static btstack_sbc_encoder_state_t sbc_encoder_state;
78
handle_pcm_data(int16_t * data,int num_samples,int num_channels,int sample_rate,void * context)79 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
80 UNUSED(sample_rate);
81 UNUSED(context);
82 wav_writer_write_int16(num_samples*num_channels, data);
83 }
84
fill_sine_frame(void * userData,int num_samples_to_write)85 static void fill_sine_frame(void *userData, int num_samples_to_write){
86 paTestData *data = (paTestData*)userData;
87 int count = 0;
88 int offset = 0;
89 while (count < num_samples_to_write){
90 uint8_t write_data[BYTES_PER_AUDIO_SAMPLE];
91 *(int16_t*)&write_data[0] = data->source[data->left_phase];
92 *(int16_t*)&write_data[2] = data->source[data->right_phase];
93
94 memcpy(pcm_frame+offset, write_data, BYTES_PER_AUDIO_SAMPLE);
95 offset += BYTES_PER_AUDIO_SAMPLE;
96 count++;
97
98 data->left_phase += 1;
99 if (data->left_phase >= TABLE_SIZE_441HZ){
100 data->left_phase -= TABLE_SIZE_441HZ;
101 }
102 data->right_phase += 1;
103 if (data->right_phase >= TABLE_SIZE_441HZ){
104 data->right_phase -= TABLE_SIZE_441HZ;
105 }
106 }
107 }
108
109 int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])110 int btstack_main(int argc, const char * argv[]){
111 (void) argc;
112 (void) argv;
113 btstack_sbc_encoder_init(&sbc_encoder_state, SBC_MODE_STANDARD, 16, 8, SBC_ALLOCATION_METHOD_LOUDNESS, 44100, 53, SBC_CHANNEL_MODE_STEREO);
114
115 /* initialise sinusoidal wavetable */
116 int i;
117 for (i=0; i<TABLE_SIZE_441HZ; i++){
118 sin_data.source[i] = sin(((double)i/(double)TABLE_SIZE_441HZ) * M_PI * 2.)*32767;
119 }
120 sin_data.left_phase = sin_data.right_phase = 0;
121 // wav_writer_open(input_wav_filename, NUM_CHANNELS, SAMPLE_RATE);
122 wav_writer_open(output_wav_filename, NUM_CHANNELS, SAMPLE_RATE);
123 btstack_sbc_decoder_init(&state, mode, handle_pcm_data, NULL);
124
125 for (i=0; i<3500; i++){
126 fill_sine_frame(&sin_data, 128);
127 btstack_sbc_encoder_process_data((int16_t *) pcm_frame);
128 btstack_sbc_decoder_process_data(&state, 0, btstack_sbc_encoder_sbc_buffer(), btstack_sbc_encoder_sbc_buffer_length());
129
130 }
131 wav_writer_close();
132 exit(0);
133 }
134