diff options
author | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2021-07-26 09:50:19 +0100 |
---|---|---|
committer | Arturs Artamonovs <arturs.artamonovs@protonmail.com> | 2021-07-26 09:50:19 +0100 |
commit | ef53f705cf5708875a43fc314d741964ffa71637 (patch) | |
tree | 0313a10e696bc905b435360b2a3ad5415eef433a | |
parent | 15540b20041cbeb6e804549445c02cc12aead7ed (diff) | |
download | WasmAudio-ef53f705cf5708875a43fc314d741964ffa71637.tar.gz WasmAudio-ef53f705cf5708875a43fc314d741964ffa71637.zip |
Update to be compatible with web
-rw-r--r-- | Build/index.html | 39 | ||||
-rw-r--r-- | WasmAudio/main.c | 106 |
2 files changed, 130 insertions, 15 deletions
diff --git a/Build/index.html b/Build/index.html index 24e84d6..4c371ec 100644 --- a/Build/index.html +++ b/Build/index.html @@ -5,7 +5,7 @@ </head> <body> <pre> - Koh fractal. Version: 0.3 + Wasm audio FIR. Version: 0.1 </pre> <script type='text/javascript'> var Module = {}; @@ -26,6 +26,41 @@ <div align="center"> <canvas id="canvas" ></canvas> </div> + + <button class="setCutoffFrequency" type="button">Set Cutoff(Hz)</button> + <textarea class="outSetCutoffFrequency"></textarea> + + <button class="setNumberTaps" type="button">Set Taps</button> + <textarea class="outSetNumberTaps"></textarea> + + <button class="recalculateFir" type="button">Calc</button> + <textarea class="outCoeficients">1</textarea> + + + <script> + get_cutoff_freq = Module.cwrap("get_cutoff_freq","number",[]); + get_tap_number = Module.cwrap("get_tap_number","number",[]); + recalculate_fir = Module.cwrap("recalculate_fir",'number',[]); + + var setCutoffFrequency = document.querySelector('.setCutoffFrequency') + var outSetCutoffFrequency = document.querySelector('.outSetCutoffFrequency') + var setNumberTaps = document.querySelector('.setNumberTaps') + var outSetNumberTaps = document.querySelector('.outSetNumberTaps') + var recalculateFir = document.querySelector('.recalculateFir') + var outCoeficients = document.querySelector('.outCoeficients') + + //outSetCutoffFrequency.value = get_cutoff_freq(); + //outSetCutoffFrequency.value = "Hello"; + //outSetNumberTaps.value = get_tap_number(); + + recalculateFir.addEventListener('click', function(){ + recalculate_fir(); + console.log("Oh noe"); + outCoeficients.value = 2; + }) + + </script> + <pre> If no recording, check developer console(Ctrl+Shift+I) or add domain (http://wasm.main.lv) to chrome://flags/#unsafely-treat-insecure-origin-as-secure @@ -40,6 +75,8 @@ Changelog: v0.1 - initial release </pre> + + <!--<script src="index.js"></script>--> </body> </html> 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; } |