mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-14 01:27:41 +00:00
reworked test, extracted wav_utils
This commit is contained in:
parent
26463303c9
commit
202da3172e
@ -64,7 +64,7 @@
|
||||
#define SCO_MSBC_OUT_FILENAME "sco_output.msbc"
|
||||
#define SCO_MSBC_IN_FILENAME "sco_input.mscb"
|
||||
|
||||
#define SCO_WAV_DURATION_IN_SECONDS 30
|
||||
#define SCO_WAV_DURATION_IN_SECONDS 15
|
||||
#endif
|
||||
|
||||
|
||||
@ -278,7 +278,8 @@ static void sco_demo_receive_CVSD(uint8_t * packet, uint16_t size){
|
||||
const int samples_to_write = btstack_min(num_samples, num_samples_to_write);
|
||||
int8_t audio_frame_out[24];
|
||||
|
||||
btstack_cvsd_plc_process_data(&cvsd_plc_state, (int8_t *)(packet+3),num_samples,audio_frame_out);
|
||||
memcpy(audio_frame_out, (int8_t*)(packet+3), 24);
|
||||
// btstack_cvsd_plc_process_data(&cvsd_plc_state, (int8_t *)(packet+3),num_samples,audio_frame_out);
|
||||
|
||||
// convert 8 bit signed to 8 bit unsigned
|
||||
int i;
|
||||
|
@ -216,15 +216,54 @@ static int count_equal_bytes(int8_t * packet, uint16_t size){
|
||||
return count;
|
||||
}
|
||||
|
||||
static int bad_frame(int8_t * frame, uint16_t size){
|
||||
return count_equal_bytes(frame, size) > 20;
|
||||
}
|
||||
|
||||
void btstack_cvsd_plc_process_data(btstack_cvsd_plc_state_t * state, int8_t * in, uint16_t size, int8_t * out){
|
||||
if (size != 24){
|
||||
log_error("btstack_cvsd_plc_process_data: audio frame size is incorrect. Expected %d, got %d", CVSD_FS, size);
|
||||
}
|
||||
if (count_equal_bytes(in, size) > size/2){
|
||||
btstack_cvsd_plc_bad_frame(state, out);
|
||||
state->bad_frames_nr++;
|
||||
state->frame_count++;
|
||||
if (bad_frame(in,size)){
|
||||
memcpy(out, in, size);
|
||||
if (state->good_frames_nr > CVSD_LHIST/CVSD_FS){
|
||||
btstack_cvsd_plc_bad_frame(state, out);
|
||||
state->bad_frames_nr++;
|
||||
} else {
|
||||
memset(out, 0, CVSD_FS);
|
||||
}
|
||||
} else {
|
||||
btstack_cvsd_plc_good_frame(state, in, out);
|
||||
state->good_frames_nr++;
|
||||
if (state->good_frames_nr == 1){
|
||||
printf("First good frame at index %d\n", state->frame_count-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void btstack_cvsd_plc_mark_bad_frame(btstack_cvsd_plc_state_t * state, int8_t * in, uint16_t size, int8_t * out){
|
||||
if (size != 24){
|
||||
log_error("btstack_cvsd_plc_mark_bad_frame: audio frame size is incorrect. Expected %d, got %d", CVSD_FS, size);
|
||||
}
|
||||
state->frame_count++;
|
||||
|
||||
if (bad_frame(in,size)){
|
||||
memcpy(out, in, size);
|
||||
if (state->good_frames_nr > CVSD_LHIST/CVSD_FS){
|
||||
memset(out, 50, size);
|
||||
state->bad_frames_nr++;
|
||||
}
|
||||
} else {
|
||||
memcpy(out, in, size);
|
||||
state->good_frames_nr++;
|
||||
if (state->good_frames_nr == 1){
|
||||
printf("First good frame at index %d\n", state->frame_count-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state){
|
||||
printf("Good frames: %d\n", state->good_frames_nr);
|
||||
printf("Bad frames: %d\n", state->bad_frames_nr);
|
||||
}
|
||||
|
@ -65,6 +65,7 @@ typedef struct cvsd_plc_state {
|
||||
// summary of processed good and bad frames
|
||||
int good_frames_nr;
|
||||
int bad_frames_nr;
|
||||
int frame_count;
|
||||
} btstack_cvsd_plc_state_t;
|
||||
|
||||
void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state);
|
||||
@ -72,6 +73,8 @@ void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *out
|
||||
void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *in, int8_t *out);
|
||||
uint8_t * btstack_cvsd_plc_zero_signal_frame(void);
|
||||
void btstack_cvsd_plc_process_data(btstack_cvsd_plc_state_t * state, int8_t * in, uint16_t size, int8_t * out);
|
||||
void btstack_cvsd_plc_mark_bad_frame(btstack_cvsd_plc_state_t * state, int8_t * in, uint16_t size, int8_t * out);
|
||||
void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state);
|
||||
|
||||
#if defined __cplusplus
|
||||
}
|
||||
|
@ -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: btstack_cvsd_plc.o cvsd_plc_test.c
|
||||
cvsd_plc_test: ${COMMON_OBJ} btstack_cvsd_plc.o wav_utils.o cvsd_plc_test.c
|
||||
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
|
||||
|
||||
test: all
|
||||
|
@ -9,11 +9,9 @@
|
||||
#include "CppUTest/CommandLineTestRunner.h"
|
||||
|
||||
#include "btstack_cvsd_plc.h"
|
||||
#include "wav_utils.h"
|
||||
|
||||
const int audio_samples_per_frame = 24;
|
||||
static int8_t audio_frame_in[audio_samples_per_frame];
|
||||
static int8_t audio_frame_out[audio_samples_per_frame];
|
||||
static int last_value = 0;
|
||||
|
||||
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 },
|
||||
@ -22,38 +20,8 @@ static uint8_t test_data[][audio_samples_per_frame] = {
|
||||
{ 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
};
|
||||
|
||||
// input signal: pre-computed sine wave, 160 Hz at 8 kHz
|
||||
static const uint8_t sine[] = {
|
||||
0, 15, 31, 46, 61, 74, 86, 97, 107, 114,
|
||||
120, 124, 126, 126, 124, 120, 114, 107, 97, 86,
|
||||
74, 61, 46, 31, 15, 0, 241, 225, 210, 195,
|
||||
182, 170, 159, 149, 142, 136, 132, 130, 130, 132,
|
||||
136, 142, 149, 159, 170, 182, 195, 210, 225, 241,
|
||||
};
|
||||
static int phase = 0;
|
||||
|
||||
static btstack_cvsd_plc_state_t plc_state;
|
||||
|
||||
static void next_sine_audio_frame(void){
|
||||
int i;
|
||||
last_value = audio_frame_in[audio_samples_per_frame-1];
|
||||
for (i=0;i<audio_samples_per_frame;i++){
|
||||
audio_frame_in[i] = (int8_t)sine[phase];
|
||||
phase++;
|
||||
if (phase >= sizeof(sine)) phase = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void fill_audio_frame_with_error(int nr_zero_bytes, int mark_bad_frame){
|
||||
int i;
|
||||
int error_value = mark_bad_frame ? 127 : last_value;
|
||||
for (i=0;i<audio_samples_per_frame;i++){
|
||||
if (i >= audio_samples_per_frame-nr_zero_bytes){
|
||||
audio_frame_in[i] = error_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int count_equal_bytes(uint8_t * packet, uint16_t size){
|
||||
int count = 0;
|
||||
int temp_count = 1;
|
||||
@ -73,225 +41,74 @@ static int count_equal_bytes(uint8_t * packet, uint16_t size){
|
||||
}
|
||||
return count;
|
||||
}
|
||||
/* Write wav file utils */
|
||||
typedef struct wav_writer_state {
|
||||
FILE * wav_file;
|
||||
int total_num_samples;
|
||||
int num_channels;
|
||||
int sampling_frequency;
|
||||
int frame_count;
|
||||
} wav_writer_state_t;
|
||||
|
||||
const char * cvsd_filename = "sine_test.wav";
|
||||
wav_writer_state_t wav_writer_state;
|
||||
|
||||
void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
|
||||
buffer[pos++] = value;
|
||||
buffer[pos++] = value >> 8;
|
||||
}
|
||||
|
||||
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);
|
||||
fwrite(&buf, 1, 2, wav_file);
|
||||
}
|
||||
|
||||
static void little_endian_fstore_32(FILE *wav_file, uint32_t value){
|
||||
uint8_t buf[4];
|
||||
little_endian_store_32(buf, 0, value);
|
||||
fwrite(&buf, 1, 4, wav_file);
|
||||
}
|
||||
|
||||
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);
|
||||
// num_samples = blocks * subbands
|
||||
uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels);
|
||||
little_endian_fstore_32(wav_file, data_bytes + 36);
|
||||
fwrite("WAVE", 1, 4, wav_file);
|
||||
|
||||
int byte_rate = sample_rate * num_channels * bytes_per_sample;
|
||||
int bits_per_sample = 8 * bytes_per_sample;
|
||||
int block_align = num_channels * bits_per_sample;
|
||||
int fmt_length = 16;
|
||||
int fmt_format_tag = 1; // PCM
|
||||
|
||||
/* write fmt chunk */
|
||||
fwrite("fmt ", 1, 4, wav_file);
|
||||
little_endian_fstore_32(wav_file, fmt_length);
|
||||
little_endian_fstore_16(wav_file, fmt_format_tag);
|
||||
little_endian_fstore_16(wav_file, num_channels);
|
||||
little_endian_fstore_32(wav_file, sample_rate);
|
||||
little_endian_fstore_32(wav_file, byte_rate);
|
||||
little_endian_fstore_16(wav_file, block_align);
|
||||
little_endian_fstore_16(wav_file, bits_per_sample);
|
||||
|
||||
/* write data chunk */
|
||||
fwrite("data", 1, 4, wav_file);
|
||||
little_endian_fstore_32(wav_file, data_bytes);
|
||||
}
|
||||
|
||||
static void init_wav_writer(const char * cvsd_filename){
|
||||
printf("Open wav file: %s\n", cvsd_filename);
|
||||
FILE * wav_file = fopen(cvsd_filename, "wb");
|
||||
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);
|
||||
}
|
||||
|
||||
static void close_wav_writer(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);
|
||||
}
|
||||
|
||||
static void write_wav_data(int num_samples, int8_t * data){
|
||||
int i = 0;
|
||||
for (i=0; i<num_samples;i++){
|
||||
fwrite(&data[i], 1, 1, wav_writer_state.wav_file);
|
||||
fwrite(&data[i], 1, 1, wav_writer_state.wav_file);
|
||||
}
|
||||
|
||||
wav_writer_state.total_num_samples+=num_samples;
|
||||
wav_writer_state.frame_count++;
|
||||
}
|
||||
|
||||
static int wav_reader_num_frames = 0;
|
||||
const char * wav_reader_filename_in = "data/fanfare-mono.wav";
|
||||
int wav_reader_fd;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
uint16_t little_endian_read_16(const uint8_t * buffer, int pos){
|
||||
return ((uint16_t) buffer[pos]) | (((uint16_t)buffer[(pos)+1]) << 8);
|
||||
}
|
||||
|
||||
static int read_audio_frame(int wav_fd){
|
||||
int i;
|
||||
int bytes_read = 0;
|
||||
for (i=0; i < audio_samples_per_frame; i++){
|
||||
uint8_t buf[2];
|
||||
bytes_read +=__read(wav_fd, &buf, 2);
|
||||
//read_buffer[i] = little_endian_read_16(buf, 0);
|
||||
audio_frame_in[i] = buf[1];
|
||||
}
|
||||
|
||||
wav_reader_num_frames++;
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
static void read_wav_header(int wav_fd){
|
||||
uint8_t buf[40];
|
||||
__read(wav_fd, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
int next_audio_frame(void){
|
||||
if (!wav_reader_fd) return -1;
|
||||
last_value = audio_frame_in[audio_samples_per_frame-1];
|
||||
int bytes_read = read_audio_frame(wav_reader_fd);
|
||||
return bytes_read == audio_samples_per_frame*2;
|
||||
}
|
||||
|
||||
static 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);
|
||||
}
|
||||
|
||||
static void close_wav_reader(void){
|
||||
close(wav_reader_fd);
|
||||
}
|
||||
//
|
||||
|
||||
static void process_wav_file_with_error_rate(const char * file_name, int corruption_step, int num_bad_frames, int plc_enabled, int mark_bad_frame){
|
||||
static void create_sine_wav(const char * out_file_name){
|
||||
btstack_cvsd_plc_init(&plc_state);
|
||||
init_wav_writer(file_name);
|
||||
init_wav_reader(wav_reader_filename_in);
|
||||
|
||||
int i = 0;
|
||||
int num_bf = num_bad_frames;
|
||||
printf("Corruption every %dth step\n", corruption_step);
|
||||
while (next_audio_frame() > 0){
|
||||
if (i > corruption_step && corruption_step > 0 && i%corruption_step == 0) {
|
||||
fill_audio_frame_with_error(24, mark_bad_frame);
|
||||
btstack_cvsd_plc_bad_frame(&plc_state, audio_frame_out);
|
||||
num_bf = num_bad_frames;
|
||||
} else if (i > corruption_step && num_bf > 1) {
|
||||
fill_audio_frame_with_error(24, mark_bad_frame);
|
||||
btstack_cvsd_plc_bad_frame(&plc_state, audio_frame_out);
|
||||
num_bf--;
|
||||
} else {
|
||||
// fill_audio_frame_with_error(0, mark_bad_frame);
|
||||
btstack_cvsd_plc_good_frame(&plc_state, audio_frame_in, (int8_t*)audio_frame_out);
|
||||
}
|
||||
if (plc_enabled){
|
||||
write_wav_data(24, audio_frame_out);
|
||||
} else {
|
||||
write_wav_data(24, audio_frame_in);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
close_wav_writer();
|
||||
close_wav_reader();
|
||||
}
|
||||
|
||||
static void process_sine_with_error_rate(const char * file_name, int corruption_step, int num_bad_frames, int plc_enabled, int mark_bad_frame){
|
||||
btstack_cvsd_plc_init(&plc_state);
|
||||
init_wav_writer(file_name);
|
||||
init_wav_writer(out_file_name);
|
||||
|
||||
int i;
|
||||
int num_bf = num_bad_frames;
|
||||
printf("Corruption every %dth step\n", corruption_step);
|
||||
for (i=0; i<2000; i++){
|
||||
next_sine_audio_frame();
|
||||
if (i > corruption_step && corruption_step > 0 && i%corruption_step == 0) {
|
||||
fill_audio_frame_with_error(24, mark_bad_frame);
|
||||
btstack_cvsd_plc_bad_frame(&plc_state, (int8_t*)audio_frame_out);
|
||||
num_bf = num_bad_frames;
|
||||
} else if (i > corruption_step && num_bf > 1) {
|
||||
fill_audio_frame_with_error(24, mark_bad_frame);
|
||||
btstack_cvsd_plc_bad_frame(&plc_state, audio_frame_out);
|
||||
num_bf--;
|
||||
} else {
|
||||
fill_audio_frame_with_error(0, mark_bad_frame);
|
||||
btstack_cvsd_plc_good_frame(&plc_state, audio_frame_in, audio_frame_out);
|
||||
}
|
||||
if (plc_enabled){
|
||||
write_wav_data(24, audio_frame_out);
|
||||
} else {
|
||||
write_wav_data(24, audio_frame_in);
|
||||
}
|
||||
int8_t * audio_frame_in = next_sine_audio_frame();
|
||||
write_wav_data_int8(audio_samples_per_frame, audio_frame_in);
|
||||
}
|
||||
close_wav_writer();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
int8_t * audio_frame_in = next_audio_frame_int8();
|
||||
int fc = 0;
|
||||
while (audio_frame_in != NULL){
|
||||
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();
|
||||
fc++;
|
||||
}
|
||||
close_wav_reader();
|
||||
close_wav_writer();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
int8_t * audio_frame_in = next_audio_frame_int8();
|
||||
while (audio_frame_in != NULL){
|
||||
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();
|
||||
}
|
||||
close_wav_reader();
|
||||
close_wav_writer();
|
||||
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);
|
||||
|
||||
int8_t * audio_frame_in = next_audio_frame_int8();
|
||||
while (audio_frame_in != NULL){
|
||||
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();
|
||||
}
|
||||
close_wav_reader();
|
||||
close_wav_writer();
|
||||
btstack_cvsd_dump_statistics(&plc_state);
|
||||
}
|
||||
|
||||
TEST_GROUP(CVSD_PLC){
|
||||
|
||||
@ -304,41 +121,34 @@ TEST(CVSD_PLC, CountEqBytes){
|
||||
CHECK_EQUAL(23, count_equal_bytes(test_data[3],24));
|
||||
}
|
||||
|
||||
// TEST(CVSD_PLC, FillAudioFrame){
|
||||
// fill_audio_frame_with_error(15, 1);
|
||||
// CHECK_EQUAL(15, count_equal_bytes(audio_frame_in, 24));
|
||||
// }
|
||||
|
||||
// TEST(CVSD_PLC, SineWithErrorRateSingleBadFrame){
|
||||
// process_sine_with_error_rate("sine_test_10_no_plc.wav", 10, 0, 0);
|
||||
// process_sine_with_error_rate("sine_test_10_plc.wav", 10, 0, 1);
|
||||
// }
|
||||
|
||||
// TEST(CVSD_PLC, SineWithErrorRateMultipleBadFrame){
|
||||
// process_sine_with_error_rate("sine_test_10_2_no_plc.wav", 10, 2, 0);
|
||||
// process_sine_with_error_rate("sine_test_10_2_plc.wav", 10, 2, 1);
|
||||
// }
|
||||
|
||||
|
||||
TEST(CVSD_PLC, WavFileWithErrorRateSingleBadFrame){
|
||||
// process_wav_file_with_error_rate("fanfare_test.wav", 0, 0, 0);
|
||||
// process_sine_with_error_rate("sine_test.wav", 0, 0, 0);
|
||||
TEST(CVSD_PLC, TestLiveWavFile){
|
||||
int corruption_step = 10;
|
||||
introduce_bad_frames_to_wav_file("data/input/sco_input.wav", "data/sco_input.wav", 0);
|
||||
introduce_bad_frames_to_wav_file("data/input/sco_input.wav", "data/sco_input_with_bad_frames.wav", corruption_step);
|
||||
|
||||
process_wav_file_with_error_rate("fanfare_test_20_no_plc.wav", 20, 0, 0, 1);
|
||||
process_wav_file_with_error_rate("fanfare_test_20_1_plc8.wav", 20, 1, 1, 0);
|
||||
// process_wav_file_with_error_rate("fanfare_test_20_2_plc8.wav", 20, 2, 1, 0);
|
||||
// process_wav_file_with_error_rate("fanfare_test_20_4_plc8.wav", 20, 4, 1, 0);
|
||||
|
||||
process_sine_with_error_rate("sine_test_10_no_plc.wav", 10, 0, 0, 1);
|
||||
process_sine_with_error_rate("sine_test_10_1_plc8.wav", 10, 0, 1, 0);
|
||||
// process_sine_with_error_rate("sine_test_10_2_plc8.wav", 10, 2, 1, 0);
|
||||
// process_sine_with_error_rate("sine_test_10_4_plc8.wav", 10, 4, 1, 0);
|
||||
mark_bad_frames_wav_file("data/sco_input.wav", "data/sco_input_detected_frames.wav");
|
||||
process_wav_file_with_plc("data/sco_input.wav", "data/sco_input_after_plc.wav");
|
||||
process_wav_file_with_plc("data/sco_input_with_bad_frames.wav", "data/sco_input_with_bad_frames_after_plc.wav");
|
||||
}
|
||||
|
||||
// TEST(CVSD_PLC, WavFileWithErrorRateMultipleBadFrame){
|
||||
// process_wav_file_with_error_rate("fanfare_test_10_2_no_plc.wav", 10, 2, 0);
|
||||
// process_wav_file_with_error_rate("fanfare_test_10_2_plc.wav", 10, 2, 1);
|
||||
// }
|
||||
TEST(CVSD_PLC, TestFanfareFile){
|
||||
int corruption_step = 10;
|
||||
introduce_bad_frames_to_wav_file("data/input/fanfare_mono.wav", "data/fanfare_mono.wav", 0);
|
||||
introduce_bad_frames_to_wav_file("data/fanfare_mono.wav", "data/fanfare_mono_with_bad_frames.wav", corruption_step);
|
||||
|
||||
mark_bad_frames_wav_file("data/fanfare_mono.wav", "data/fanfare_mono_detected_frames.wav");
|
||||
process_wav_file_with_plc("data/fanfare_mono.wav", "data/fanfare_mono_after_plc.wav");
|
||||
process_wav_file_with_plc("data/fanfare_mono_with_bad_frames.wav", "data/fanfare_mono_with_bad_frames_after_plc.wav");
|
||||
}
|
||||
|
||||
TEST(CVSD_PLC, TestSineWave){
|
||||
int corruption_step = 10;
|
||||
create_sine_wav("data/sine_test.wav");
|
||||
introduce_bad_frames_to_wav_file("data/sine_test.wav", "data/sine_test_with_bad_frames.wav", corruption_step);
|
||||
|
||||
process_wav_file_with_plc("data/sine_test.wav", "data/sine_test_after_plc.wav");
|
||||
process_wav_file_with_plc("data/sine_test_with_bad_frames.wav", "data/sine_test_with_bad_frames_after_plc.wav");
|
||||
}
|
||||
|
||||
int main (int argc, const char * argv[]){
|
||||
return CommandLineTestRunner::RunAllTests(argc, argv);
|
||||
|
214
test/hfp/wav_utils.c
Normal file
214
test/hfp/wav_utils.c
Normal file
@ -0,0 +1,214 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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];
|
||||
|
||||
static const uint8_t sine[] = {
|
||||
0, 15, 31, 46, 61, 74, 86, 97, 107, 114,
|
||||
120, 124, 126, 126, 124, 120, 114, 107, 97, 86,
|
||||
74, 61, 46, 31, 15, 0, 241, 225, 210, 195,
|
||||
182, 170, 159, 149, 142, 136, 132, 130, 130, 132,
|
||||
136, 142, 149, 159, 170, 182, 195, 210, 225, 241,
|
||||
};
|
||||
static int phase = 0;
|
||||
|
||||
|
||||
/* Write wav file utils */
|
||||
typedef struct wav_writer_state {
|
||||
FILE * wav_file;
|
||||
int total_num_samples;
|
||||
int num_channels;
|
||||
int sampling_frequency;
|
||||
int frame_count;
|
||||
} wav_writer_state_t;
|
||||
|
||||
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);
|
||||
fwrite(&buf, 1, 2, wav_file);
|
||||
}
|
||||
|
||||
static void little_endian_fstore_32(FILE *wav_file, uint32_t value){
|
||||
uint8_t buf[4];
|
||||
little_endian_store_32(buf, 0, value);
|
||||
fwrite(&buf, 1, 4, wav_file);
|
||||
}
|
||||
|
||||
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);
|
||||
// num_samples = blocks * subbands
|
||||
uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels);
|
||||
little_endian_fstore_32(wav_file, data_bytes + 36);
|
||||
fwrite("WAVE", 1, 4, wav_file);
|
||||
|
||||
int byte_rate = sample_rate * num_channels * bytes_per_sample;
|
||||
int bits_per_sample = 8 * bytes_per_sample;
|
||||
int block_align = num_channels * bits_per_sample;
|
||||
int fmt_length = 16;
|
||||
int fmt_format_tag = 1; // PCM
|
||||
|
||||
/* write fmt chunk */
|
||||
fwrite("fmt ", 1, 4, wav_file);
|
||||
little_endian_fstore_32(wav_file, fmt_length);
|
||||
little_endian_fstore_16(wav_file, fmt_format_tag);
|
||||
little_endian_fstore_16(wav_file, num_channels);
|
||||
little_endian_fstore_32(wav_file, sample_rate);
|
||||
little_endian_fstore_32(wav_file, byte_rate);
|
||||
little_endian_fstore_16(wav_file, block_align);
|
||||
little_endian_fstore_16(wav_file, bits_per_sample);
|
||||
|
||||
/* write data chunk */
|
||||
fwrite("data", 1, 4, wav_file);
|
||||
little_endian_fstore_32(wav_file, data_bytes);
|
||||
}
|
||||
|
||||
void init_wav_writer(const char * cvsd_filename){
|
||||
FILE * wav_file = fopen(cvsd_filename, "wb");
|
||||
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);
|
||||
}
|
||||
|
||||
void close_wav_writer(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);
|
||||
}
|
||||
|
||||
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 i = 0;
|
||||
int8_t zero_byte = 0;
|
||||
for (i=0; i<num_samples;i++){
|
||||
fwrite(&zero_byte, 1, 1, wav_writer_state.wav_file);
|
||||
uint8_t byte_value = (uint8_t)data[i];
|
||||
fwrite(&byte_value, 1, 1, wav_writer_state.wav_file);
|
||||
}
|
||||
|
||||
wav_writer_state.total_num_samples+=num_samples;
|
||||
wav_writer_state.frame_count++;
|
||||
}
|
||||
|
||||
int8_t * next_sine_audio_frame(void){
|
||||
int i;
|
||||
for (i=0;i<CVSD_FRAME_SIZE;i++){
|
||||
audio_frame_in[i] = (int8_t)sine[phase];
|
||||
phase++;
|
||||
if (phase >= 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;i++){
|
||||
// if (i >= buf_size-nr_bad_bytes){
|
||||
// buf[i] = bad_byte_value;
|
||||
// }
|
||||
// }
|
||||
// }
|
18
test/hfp/wav_utils.h
Normal file
18
test/hfp/wav_utils.h
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user