diff --git a/test/hfp/wav_utils.c b/platform/posix/wav_util.c similarity index 54% rename from test/hfp/wav_utils.c rename to platform/posix/wav_util.c index 2618a79a4..8a4175499 100644 --- a/test/hfp/wav_utils.c +++ b/platform/posix/wav_util.c @@ -1,3 +1,40 @@ +/* + * Copyright (C) 2016 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + #include #include #include @@ -5,15 +42,8 @@ #include #include -#include "wav_utils.h" - -#define CVSD_FRAME_SIZE 24 - -// Wav data: 8bit is uint8_t; 16bit is int16 - -static int wav_reader_fd; -static int bytes_per_sample = 2; -static int8_t audio_frame_in[CVSD_FRAME_SIZE]; +#include "wav_util.h" +#include "btstack_util.h" static const uint8_t sine[] = { 0, 15, 31, 46, 61, 74, 86, 97, 107, 114, @@ -24,6 +54,8 @@ static const uint8_t sine[] = { }; static int phase = 0; +static int wav_reader_fd; +static int bytes_per_sample = 2; /* Write wav file utils */ typedef struct wav_writer_state { @@ -36,21 +68,10 @@ typedef struct wav_writer_state { wav_writer_state_t wav_writer_state; -static void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ - buffer[pos++] = value; - buffer[pos++] = value >> 8; -} - -static void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){ - buffer[pos++] = value; - buffer[pos++] = value >> 8; - buffer[pos++] = value >> 16; - buffer[pos++] = value >> 24; -} static void little_endian_fstore_16(FILE *wav_file, uint16_t value){ uint8_t buf[2]; - little_endian_store_32(buf, 0, value); + little_endian_store_16(buf, 0, value); fwrite(&buf, 1, 2, wav_file); } @@ -60,7 +81,21 @@ static void little_endian_fstore_32(FILE *wav_file, uint32_t value){ fwrite(&buf, 1, 4, wav_file); } -void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){ +static ssize_t __read(int fd, void *buf, size_t count){ + ssize_t len, pos = 0; + + while (count > 0) { + len = read(fd, (int8_t * )buf + pos, count); + if (len <= 0) + return pos; + + count -= len; + pos += len; + } + return pos; +} + +static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){ unsigned int bytes_per_sample = 2; /* write RIFF header */ fwrite("RIFF", 1, 4, wav_file); @@ -90,90 +125,32 @@ void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, little_endian_fstore_32(wav_file, data_bytes); } -void init_wav_writer(const char * cvsd_filename){ - FILE * wav_file = fopen(cvsd_filename, "wb"); +int wav_writer_open(const char * filepath, int num_channels, int sampling_frequency){ + FILE * wav_file = fopen(filepath, "wb"); + if (!wav_file) return 1; + wav_writer_state.wav_file = wav_file; wav_writer_state.frame_count = 0; wav_writer_state.total_num_samples = 0; - wav_writer_state.num_channels = 1; - wav_writer_state.sampling_frequency = 8000; - write_wav_header(wav_writer_state.wav_file, 0, 1, 8000); + wav_writer_state.num_channels = num_channels; + wav_writer_state.sampling_frequency = sampling_frequency; + write_wav_header(wav_writer_state.wav_file, 0, num_channels, sampling_frequency); + return 0; } -void close_wav_writer(void){ +int wav_writer_close(void){ rewind(wav_writer_state.wav_file); write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, wav_writer_state.num_channels, wav_writer_state.sampling_frequency); fclose(wav_writer_state.wav_file); + return 0; } -static ssize_t __read(int fd, void *buf, size_t count){ - ssize_t len, pos = 0; - - while (count > 0) { - len = read(fd, (int8_t * )buf + pos, count); - if (len <= 0) - return pos; - - count -= len; - pos += len; - } - return pos; -} - -static uint16_t little_endian_read_16(const uint8_t * buffer, int pos){ - return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8); -} - -static void read_wav_header(int wav_fd){ - uint8_t buf[40]; - __read(wav_fd, buf, sizeof(buf)); - - int num_channels = little_endian_read_16(buf, 22); - int block_align = little_endian_read_16(buf, 32); - bytes_per_sample = block_align/num_channels; - if (bytes_per_sample > 2){ - bytes_per_sample = bytes_per_sample/8; - } - printf("Bytes_per_sample %d\n", bytes_per_sample); - -} - -void init_wav_reader(const char * wav_reader_filename_in){ - wav_reader_fd = open(wav_reader_filename_in, O_RDONLY); //fopen(wav_reader_filename_in, "rb"); - if (!wav_reader_fd) { - printf("Can't open file %s", wav_reader_filename_in); - } - read_wav_header(wav_reader_fd); -} - -void close_wav_reader(void){ - close(wav_reader_fd); -} - -static int read_audio_frame_int8(int audio_samples_per_frame){ - int i; - int bytes_read = 0; - - for (i=0; i < audio_samples_per_frame; i++){ - if (bytes_per_sample == 2){ - uint8_t buf[2]; - bytes_read +=__read(wav_reader_fd, &buf, 2); - audio_frame_in[i] = buf[1]; - } else { - uint8_t buf[1]; - bytes_read +=__read(wav_reader_fd, &buf, 1); - audio_frame_in[i] = buf[0] + 128; - } - } - return bytes_read; -} - -void write_wav_data_int8(int num_samples, int8_t * data){ - if (data == NULL) return; +int wav_writer_write_int8(int num_samples, int8_t * data){ + if (data == NULL) return 1; int i = 0; int8_t zero_byte = 0; - for (i=0; i 2){ + bytes_per_sample = bytes_per_sample/8; + } + return 0; +} + +int wav_reader_close(void){ + close(wav_reader_fd); + return 0; +} + +// Wav data: 8bit is uint8_t; 16bit is int16 +int wav_reader_read_int8(int num_samples, int8_t * data){ + if (!wav_reader_fd) return 1; int i; - for (i=0;i= sizeof(sine)) phase = 0; } - return (int8_t *)&audio_frame_in; } -int8_t * next_audio_frame_int8(void){ - if (!wav_reader_fd) return NULL; - - int bytes_read = read_audio_frame_int8(CVSD_FRAME_SIZE); - if (bytes_read == CVSD_FRAME_SIZE*bytes_per_sample){ - return (int8_t *)&audio_frame_in; - } - return NULL; -} - - -// void fill_audio_frame_with_error(int8_t * buf, int buf_size, int nr_bad_bytes, int bad_byte_value){ -// int i; -// for (i=0;i= buf_size-nr_bad_bytes){ -// buf[i] = bad_byte_value; -// } -// } -// } \ No newline at end of file diff --git a/platform/posix/wav_util.h b/platform/posix/wav_util.h new file mode 100644 index 000000000..910e7c257 --- /dev/null +++ b/platform/posix/wav_util.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2016 BlueKitchen GmbH + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holders nor the names of + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * 4. Any redistribution, use, or modification is done solely for + * personal benefit and not for any commercial purpose or for + * monetary gain. + * + * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS + * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Please inquire about commercial licensing options at + * contact@bluekitchen-gmbh.com + * + */ + +#include +#include +#include +#include +#include +#include + +// return 0 if ok +int wav_writer_open(const char * filepath, int num_channels, int sampling_frequency); +int wav_writer_write_int8(int num_samples, int8_t * data); +int wav_writer_close(void); + + +// return 0 if ok +int wav_reader_open(const char * filepath); +int wav_reader_read_int8(int num_samples, int8_t * data); +int wav_reader_close(void); + +// +void wav_synthesize_sine_wave_int8(int num_samples, int8_t * data); + diff --git a/test/hfp/Makefile b/test/hfp/Makefile index 996c92525..43721ee82 100644 --- a/test/hfp/Makefile +++ b/test/hfp/Makefile @@ -53,7 +53,7 @@ VPATH += ${BTSTACK_ROOT}/src VPATH += ${BTSTACK_ROOT}/src/classic VPATH += ${BTSTACK_ROOT}/platform/posix -CFLAGS = -g -Wall -I. -I../ -I${BTSTACK_ROOT}/src -I${BTSTACK_ROOT}/src/classic -I${BTSTACK_ROOT}/include -I${BTSTACK_ROOT}/ble +CFLAGS = -g -Wall -I. -I../ -I${BTSTACK_ROOT}/src -I${BTSTACK_ROOT}/src/classic -I${BTSTACK_ROOT}/platform/posix -I${BTSTACK_ROOT}/include -I${BTSTACK_ROOT}/ble LDFLAGS += -lCppUTest -lCppUTestExt EXAMPLES = hfp_ag_parser_test hfp_ag_client_test hfp_hf_parser_test hfp_hf_client_test cvsd_plc_test @@ -75,7 +75,7 @@ hfp_hf_client_test: ${MOCK_OBJ} hfp_hf.o hfp.o hfp_hf_client_test.c hfp_ag_client_test: ${MOCK_OBJ} hfp_gsm_model.o hfp_ag.o hfp.o hfp_ag_client_test.c ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ -cvsd_plc_test: ${COMMON_OBJ} btstack_cvsd_plc.o wav_utils.o cvsd_plc_test.c +cvsd_plc_test: ${COMMON_OBJ} btstack_cvsd_plc.o wav_util.o cvsd_plc_test.c ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ test: all diff --git a/test/hfp/cvsd_plc_test.c b/test/hfp/cvsd_plc_test.c index 5760260e0..f2afa1057 100644 --- a/test/hfp/cvsd_plc_test.c +++ b/test/hfp/cvsd_plc_test.c @@ -9,9 +9,10 @@ #include "CppUTest/CommandLineTestRunner.h" #include "btstack_cvsd_plc.h" -#include "wav_utils.h" +#include "wav_util.h" const int audio_samples_per_frame = 24; +static int8_t audio_frame_in[audio_samples_per_frame]; static uint8_t test_data[][audio_samples_per_frame] = { { 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, @@ -42,71 +43,64 @@ static int count_equal_bytes(uint8_t * packet, uint16_t size){ return count; } - -static void create_sine_wav(const char * out_file_name){ +static void create_sine_wav(const char * out_filename){ btstack_cvsd_plc_init(&plc_state); - init_wav_writer(out_file_name); + wav_writer_open(out_filename, 1, 8000); int i; for (i=0; i<2000; i++){ - int8_t * audio_frame_in = next_sine_audio_frame(); - write_wav_data_int8(audio_samples_per_frame, audio_frame_in); + wav_synthesize_sine_wave_int8(audio_samples_per_frame, audio_frame_in); + wav_writer_write_int8(audio_samples_per_frame, audio_frame_in); } - close_wav_writer(); + wav_writer_close(); } static void introduce_bad_frames_to_wav_file(const char * in_filename, const char * out_filename, int corruption_step){ btstack_cvsd_plc_init(&plc_state); - init_wav_writer(out_filename); - init_wav_reader(in_filename); + wav_writer_open(out_filename, 1, 8000); + wav_reader_open(in_filename); - int8_t * audio_frame_in = next_audio_frame_int8(); int fc = 0; - while (audio_frame_in != NULL){ + while (wav_reader_read_int8(audio_samples_per_frame, audio_frame_in)){ if (corruption_step > 0 && fc >= corruption_step && fc%corruption_step == 0){ memset(audio_frame_in, 50, audio_samples_per_frame); } - write_wav_data_int8(audio_samples_per_frame, audio_frame_in); - audio_frame_in = next_audio_frame_int8(); + wav_writer_write_int8(audio_samples_per_frame, audio_frame_in); fc++; } - close_wav_reader(); - close_wav_writer(); + wav_reader_close(); + wav_writer_close(); } static void process_wav_file_with_plc(const char * in_filename, const char * out_filename){ printf("\nProcess %s -> %s\n", in_filename, out_filename); btstack_cvsd_plc_init(&plc_state); - init_wav_writer(out_filename); - init_wav_reader(in_filename); + wav_writer_open(out_filename, 1, 8000); + wav_reader_open(in_filename); - int8_t * audio_frame_in = next_audio_frame_int8(); - while (audio_frame_in != NULL){ + while (wav_reader_read_int8(audio_samples_per_frame, audio_frame_in)){ int8_t audio_frame_out[audio_samples_per_frame]; btstack_cvsd_plc_process_data(&plc_state, audio_frame_in, audio_samples_per_frame, audio_frame_out); - write_wav_data_int8(audio_samples_per_frame, audio_frame_out); - audio_frame_in = next_audio_frame_int8(); + wav_writer_write_int8(audio_samples_per_frame, audio_frame_out); } - close_wav_reader(); - close_wav_writer(); + wav_reader_close(); + wav_writer_close(); btstack_cvsd_dump_statistics(&plc_state); } static void mark_bad_frames_wav_file(const char * in_filename, const char * out_filename){ printf("\nMark bad frame %s -> %s\n", in_filename, out_filename); btstack_cvsd_plc_init(&plc_state); - init_wav_writer(out_filename); - init_wav_reader(in_filename); + wav_writer_open(out_filename, 1, 8000); + wav_reader_open(in_filename); - int8_t * audio_frame_in = next_audio_frame_int8(); - while (audio_frame_in != NULL){ + while (wav_reader_read_int8(audio_samples_per_frame, audio_frame_in)){ int8_t audio_frame_out[audio_samples_per_frame]; btstack_cvsd_plc_mark_bad_frame(&plc_state, audio_frame_in, audio_samples_per_frame, audio_frame_out); - write_wav_data_int8(audio_samples_per_frame, audio_frame_out); - audio_frame_in = next_audio_frame_int8(); + wav_writer_write_int8(audio_samples_per_frame, audio_frame_out); } - close_wav_reader(); - close_wav_writer(); + wav_reader_close(); + wav_writer_close(); btstack_cvsd_dump_statistics(&plc_state); } diff --git a/test/hfp/wav_utils.h b/test/hfp/wav_utils.h deleted file mode 100644 index a33b1a700..000000000 --- a/test/hfp/wav_utils.h +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include -#include -#include -#include - -void init_wav_writer(const char * cvsd_filename); -void close_wav_writer(void); -void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate); -void write_wav_data_int8(int num_samples, int8_t * data); - -void init_wav_reader(const char * wav_reader_filename_in); -void close_wav_reader(void); -int8_t * next_audio_frame_int8(void); - -int8_t * next_sine_audio_frame(void); -