From c21a3f1a17a4ef894ca105b8679daac27293096a Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Thu, 22 Jul 2021 08:37:22 +0100 Subject: Audio looks like is filtered --- WasmAudio/main.c | 235 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 204 insertions(+), 31 deletions(-) (limited to 'WasmAudio') diff --git a/WasmAudio/main.c b/WasmAudio/main.c index 6f1766b..38f5e1a 100644 --- a/WasmAudio/main.c +++ b/WasmAudio/main.c @@ -35,12 +35,18 @@ //#include #endif +#include "../FIR/firmath.h" + #define SCREEN_WIDTH 320 #define SCREEN_WIDTH2 (SCREEN_WIDTH/2) #define SCREEN_HEIGHT 320 #define SCREEN_HEIGHT2 (SCREEN_HEIGHT/2) #define BUFSIZE 1536000 +#define BUFSIZE2 (BUFSIZE/2) +#define BUFSIZE4 (BUFSIZE/4) +#define BUFSIZE8 (BUFSIZE/8) +#define BUFSIZE16 (BUFSIZE/16) //Maximum recording time plus padding #define RECORDING_BUFFER_SECONDS (2) @@ -54,34 +60,37 @@ static SDL_Renderer *renderer = NULL; //Audio device IDs static SDL_AudioDeviceID recordingDeviceId = 0; static SDL_AudioDeviceID playbackDeviceId = 0; +int audio_buf_position=0; +//uint8_t *audio_play_buffer=NULL; +int current_state=0; + + int audio_rec_buffer_size=-1; int audio_rec_buffer_max_size=-1; //int audio_play_buffer_size=-1; //int audio_play_buffer_max_size=-1; static uint8_t audio_rec_buffer_[BUFSIZE]; -static double process_buf[BUFSIZE/4]; +const int process_buf_size = BUFSIZE4; +static double process_buf[process_buf_size*4]; +static double process_buf2[process_buf_size*4]; uint8_t *audio_rec_buffer=NULL; -int audio_buf_position=0; -//uint8_t *audio_play_buffer=NULL; -int current_state=0; -void audio_recording_callback(void *data, uint8_t *insamples, int len) -{ - //printf("record len=%d\n",len); +FILE *f_1,*f_2; +fir_t fir_lp; +firw_t firw_lp; + +void audio_recording_callback(void *data, uint8_t *insamples, int len) { if (audio_buf_position> 24)&0xff; + //audio_rec_buffer[i+1] = ((int)i32>> 16)&0xff; + //audio_rec_buffer[i+2] = ((int)i32>> 8)&0xff; + //audio_rec_buffer[i+3] = ((int)i32&0xff); + + audio_rec_buffer[i+3] = ((int)i32>> 24)&0xff; + audio_rec_buffer[i+2] = ((int)i32>> 16)&0xff; + audio_rec_buffer[i+1] = ((int)i32>> 8)&0xff; + audio_rec_buffer[i+0] = ((int)i32&0xff); + + //audio_rec_buffer[i+0] = ((int)f>> 24)&0xff; + //audio_rec_buffer[i+1] = ((int)f>> 16)&0xff; + //audio_rec_buffer[i+2] = ((int)f>> 8)&0xff; + //audio_rec_buffer[i+3] = ((int)f&0xff); + + //memcpy(&f,&audio_rec_buffer[i],4); + } + //printf("\n"); + + for (i=0;i<100;i+=4) { + fprintf(f_2, "%d %02x %02x %02x %02x\n", i/4,audio_rec_buffer[i],audio_rec_buffer[i+1],audio_rec_buffer[i+2],audio_rec_buffer[i+3]); } } + + void convert_f32_to_double() { int i=0; - for (i=0;i ",process_buf[i]); + process_buf[i] = process_buf[i]*1.01; + if (process_buf[i] > 1.0) process_buf[i] = 1.0; + if (process_buf[i] < -1.0) process_buf[i] = -1.0; + //printf("%f\n",process_buf[i]); + } +} + +void save_to_matlab() { + int i; + FILE *f=NULL; + + f = fopen("B.mat","w+"); + fprintf(f,"# Created by me\n"); + fprintf(f,"# name: B\n"); + fprintf(f,"# type: matrix\n"); + fprintf(f,"# rows: 1\n"); + fprintf(f,"# columns: %d\n",BUFSIZE16); + +#if 1 + for (i=0;inum_taps-1], input, ilength*sizeof(double)); + + for (n=0; nfir_coef; + inputp = &input[fir->num_taps - 1 + n]; + acc = 0; + + for (k=0;knum_taps;k++) + { + acc += (*coeffp++)*(*inputp--); + } + + output[n] = acc; + } + + memmove(&input[0], &input[ilength], (fir->num_taps-1)*sizeof(double)); +} + uint32_t tick_start, tick_end,tick_1sec; #if __EMSCRIPTEN__ @@ -127,9 +244,9 @@ void main_tick() { #else int main_tick() { #endif - int i,j; + int i; int update_fractal=0; - uint64_t perf_start, perf_end; + uint64_t perf_start; perf_start = SDL_GetPerformanceCounter(); tick_start = SDL_GetTicks(); @@ -180,6 +297,26 @@ int main_tick() { //process recorderd buffer with low pass filter if (current_state == RECORDED) { convert_f32_to_double(); + save_to_matlab(); + //simple volume raise +#if 0 + convert_dobule_up(); +#endif + //fir filter apply +#if 1 + printf("Apply low pass filter\n"); + + fir_filter(&fir_lp, process_buf, process_buf_size, process_buf2, process_buf_size); + //memcpy(process_buf2,process_buf,process_buf_size*sizeof(double)); + memcpy(process_buf,process_buf2,process_buf_size*sizeof(double)); + //for (i=0;i<100;i++) { + //process_buf[i] = process_buf2[i]; + // printf("[%d]=%f\n",i,process_buf[i]); + //} + + //memmove(process_buf,process_buf2,process_buf_size*sizeof(double)); + +#endif convert_double_to_f32(); } break; @@ -209,6 +346,7 @@ int main_tick() { } else if (current_state == PLAYBACK) { SDL_LockAudioDevice( playbackDeviceId ); //Finished playback + printf("audio_buf_position=%d\n",audio_buf_position); if( audio_buf_position > audio_rec_buffer_size ) { //Stop playing audio @@ -244,9 +382,6 @@ int main_tick() { tick_end = SDL_GetTicks(); if ((tick_end-tick_1sec)>1000) { - //perf_end = SDL_GetPerformanceCounter()-perf_start; - //tick_end = SDL_GetTicks()-tick_start; - //printf("FPS:%f\n",(1.0f/perf_end)); printf("FPS:%f\n",1.0f/((tick_end-tick_start)/1000.0f)); tick_1sec = tick_end; } @@ -260,7 +395,7 @@ int main_tick() { void main_loop() { - int i,count; + tick_1sec = SDL_GetTicks(); #if __EMSCRIPTEN__ emscripten_set_main_loop(main_tick, 25, 1); @@ -271,7 +406,7 @@ void main_loop() #if !__EMSCRIPTEN__ if (current_state == RECORDING) { SDL_LockAudioDevice( recordingDeviceId ); - printf("recording=%d\n",audio_buf_position); + //printf("recording=%d\n",audio_buf_position); if( audio_buf_position > audio_rec_buffer_size ) { //Stop recording audio @@ -284,6 +419,7 @@ void main_loop() } else if (current_state == PLAYBACK) { SDL_LockAudioDevice( playbackDeviceId ); //Finished playback + //printf("audio_buf_position=%d audio_rec_buffer_size=%d\n",audio_buf_position,audio_rec_buffer_size); if( audio_buf_position > audio_rec_buffer_size ) { //Stop playing audio @@ -299,19 +435,46 @@ void main_loop() #endif } + + int main(int argc, const char * argv[]) { int i,count; + + const int N=200; + double h[N],w[N]; + int filter_N=60; + int filter_FS_hz=48000; + int filter_FC_hz=20000; + double filter_FC=1.0*filter_FC_hz/filter_FS_hz; + + + fir_lp.num_taps =filter_N; + fir_lp.fir_coef = h; + firw_lp.num_taps = filter_N; + firw_lp.win_coef = w; + + for (i=0;i %d\n",audio_rec_want.format, audio_rec_have.format); } + printf("F32 is signed (%d)\n",SDL_AUDIO_ISSIGNED(audio_rec_have.format)); + printf("F32 is bigend (%d)\n",SDL_AUDIO_ISBIGENDIAN(audio_rec_have.format)); + printf("F32 is bitsize(%d)\n",SDL_AUDIO_BITSIZE(audio_rec_have.format)); + if (recordingDeviceId == 0) { printf("Failed to open recording device\n"); } else { @@ -414,6 +581,9 @@ int main(int argc, const char * argv[]) { } */ + f_1 = fopen("pre.txt","w+"); + f_2 = fopen("post.txt","w+"); + main_loop(); SDL_DestroyRenderer(renderer); @@ -422,5 +592,8 @@ int main(int argc, const char * argv[]) { SDL_VideoQuit();//? SDL_Quit(); + fclose(f_1); + fclose(f_2); + return 0; } -- cgit v1.2.3