diff options
Diffstat (limited to 'WasmAudio/main.c')
-rw-r--r-- | WasmAudio/main.c | 106 |
1 files changed, 92 insertions, 14 deletions
diff --git a/WasmAudio/main.c b/WasmAudio/main.c index c8a5191..9ddf5ba 100644 --- a/WasmAudio/main.c +++ b/WasmAudio/main.c @@ -35,6 +35,13 @@ //#include <GLES2/gl2.h> #endif + +#ifdef __EMSCRIPTEN__ +#define KEEPALIVE EMSCRIPTEN_KEEPALIVE +#else +#define KEEPALIVE +#endif + #include "../FIR/firmath.h" #define SCREEN_WIDTH 320 @@ -53,6 +60,7 @@ //Maximum recording time #define MAX_RECORDING_SECONDS (RECORDING_BUFFER_SECONDS+2) +//SDL drawing recording int first_run=1; static int quit=0; static SDL_Window *window = NULL; @@ -63,7 +71,7 @@ static SDL_AudioDeviceID playbackDeviceId = 0; int audio_buf_position=0; int current_state=0; - +//audio buffer vars int audio_rec_buffer_size=-1; int audio_rec_buffer_max_size=-1; static uint8_t audio_rec_buffer_[BUFSIZE]; @@ -72,9 +80,16 @@ static double process_buf[PROCESS_BUF_SIZE+100]; static double process_buf2[PROCESS_BUF_SIZE+100]; uint8_t *audio_rec_buffer=NULL; +//FIR filter stuff FILE *f_1,*f_2; fir_t fir_lp; firw_t firw_lp; +const int N=200; +double h[N],w[N]; +int filter_N=60; +int filter_FS_hz=48000; +int filter_FC_hz=1000; +double filter_FC; void audio_recording_callback(void *data, uint8_t *insamples, int len) { if (audio_buf_position<audio_rec_buffer_size) { @@ -85,7 +100,7 @@ void audio_recording_callback(void *data, uint8_t *insamples, int len) { } void audio_playback_callback(void *data, uint8_t *outsamples, int len) { - printf("call play\n"); + //printf("call play\n"); if (audio_buf_position<audio_rec_buffer_size) { memcpy( outsamples, audio_rec_buffer+audio_buf_position,len); audio_buf_position += len; @@ -116,14 +131,13 @@ void convert_double_to_f32() { float f = d; int32_t *i32_ = &f,i32=*i32_; - 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+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); } //printf("\n"); - } @@ -146,6 +160,69 @@ void convert_f32_to_double() { } +int KEEPALIVE get_sample_rate() { + return filter_FS_hz; +} + +void set_sample_rate() { + +} + +void set_cutoff_freq(int cutoff) { + if (cutoff < 0) { + cutoff = 0; + filter_FC_hz = cutoff; + return; + } + + if (cutoff > filter_FS_hz/2) { + cutoff = filter_FS_hz/2; + filter_FC_hz = cutoff; + filter_FC=1.0*cutoff/filter_FS_hz; + return; + } +} + +int KEEPALIVE get_cutoff_freq() { + return filter_FC_hz; +} + +int KEEPALIVE get_tap_number() { + return filter_N; +} + +void set_tap_number(int tapN) { + if (tapN<10) { + tapN = 10; + return; + + } +} + +int KEEPALIVE recalculate_fir() { + int i = 0; + 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<N;i++) { + h[i] = 0.0; + w[i] = 0.0; + } + + fir_init(&fir_lp, filter_N); + firw_init(&firw_lp, filter_N); + fir_sinc_lp(&fir_lp, filter_FC); + //fir_sinc_hp(&fir_lp, filter_FC); + firw_blackman(&firw_lp); + //firw_rect(&firw_lp); + fir_convolute(&fir_lp, &firw_lp); + //fir_print_matlab(&fir_lp); + return 0; +} + void convert_dobule_up() { int i=0; @@ -176,6 +253,7 @@ void save_to_matlab(char *name) { for (i=0;i<dumpSize;i++) { float f1=2.0f; fprintf(f," %.4f",process_buf[i]); + printf(" %.4f",process_buf[i]); } #endif #if 0 @@ -415,14 +493,11 @@ void main_loop() 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=1000; - double filter_FC=1.0*filter_FC_hz/filter_FS_hz; + char *audio_rec_device_name=NULL; + char *audio_play_device_name=NULL; + 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; @@ -484,7 +559,10 @@ int main(int argc, const char * argv[]) { audio_rec_want.channels = 2; audio_rec_want.samples = 4096; audio_rec_want.callback = audio_recording_callback; - recordingDeviceId = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(1, SDL_TRUE ), SDL_TRUE, &audio_rec_want, &audio_rec_have, SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); + + audio_rec_device_name = SDL_GetAudioDeviceName(0, SDL_TRUE ); + printf("Use for recording device %s\n",audio_rec_device_name); + recordingDeviceId = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0, SDL_TRUE ), SDL_TRUE, &audio_rec_want, &audio_rec_have, SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_FREQUENCY_CHANGE); if (audio_rec_want.freq != audio_rec_have.freq) { printf("Freq changed %d -> %d\n",audio_rec_want.freq,audio_rec_have.freq); @@ -545,5 +623,5 @@ int main(int argc, const char * argv[]) { fclose(f_1); fclose(f_2); - return 0; + //return 0; } |