diff options
author | ZoRo <dos21h@gmail.com> | 2021-07-28 08:02:21 +0100 |
---|---|---|
committer | ZoRo <dos21h@gmail.com> | 2021-07-28 08:02:21 +0100 |
commit | 76b7852f88b70bfb419bb1c6e9bef457c10e23a4 (patch) | |
tree | 4f1b250d8866d0e31389b83db6ea6fb46c96fd8e | |
parent | 2511c4fa6b2f5c4fae85a7b2e8ff12001318d9cd (diff) | |
download | WasmAudio-76b7852f88b70bfb419bb1c6e9bef457c10e23a4.tar.gz WasmAudio-76b7852f88b70bfb419bb1c6e9bef457c10e23a4.zip |
Preparing usable webgui
-rw-r--r-- | Build/Makefile | 2 | ||||
-rw-r--r-- | Build/index.html | 116 | ||||
-rw-r--r-- | WasmAudio/main.c | 78 |
3 files changed, 150 insertions, 46 deletions
diff --git a/Build/Makefile b/Build/Makefile index 5126c48..22146a4 100644 --- a/Build/Makefile +++ b/Build/Makefile @@ -10,7 +10,7 @@ make: $(CC) main.o firmath.o -o WasmAudio $(LDFLAGS) emcc: - $(EMCC) $(SOURCEDIR)/main.c ../FIR/firmath.c -s WASM=1 -O3 -o index.js $(EM_LDFALGS) -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' + $(EMCC) $(SOURCEDIR)/main.c ../FIR/firmath.c -s WASM=1 -O3 -o index.js $(EM_LDFALGS) -s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' deploy: diff --git a/Build/index.html b/Build/index.html index 02c0d3f..239bfd0 100644 --- a/Build/index.html +++ b/Build/index.html @@ -18,58 +18,110 @@ var script = document.createElement('script'); script.src = "index.js"; script.onload = function() { - console.log("Emscripten boilerplate loaded.") + console.log("Emscripten boilerplate loaded."); + get_cutoff_freq = Module.cwrap("get_cutoff_freq","number",[]); + set_cutoff_plus_1k= Module.cwrap("set_cutoff_plus_1k",[],[]); + set_cutoff_minus_1k = Module.cwrap("set_cutoff_minus_1k",[],[]); + get_tap_number = Module.cwrap("get_tap_number","number",[]); + recalculate_fir = Module.cwrap("recalculate_fir",'number',['float']); + start_loop = Module.cwrap("start_loop",'number',[]); + stop_loop = Module.cwrap("stop_loop",'number',[]); + set_tap_number_plus_10 = Module.cwrap("set_tap_number_plus_10",[],[]); + set_tap_number_minus_10 = Module.cwrap("set_tap_number_minus_10",[],[]); + + var setCutoffFrequency = document.querySelector('.setCutoffFrequency') + var getCutoffFrequency = document.querySelector('.getCutoffFrequency') + var outSetCutoffFrequency = document.querySelector('.outSetCutoffFrequency') + var minusNumberTaps = document.querySelector('.minusNumberTaps') + var plusNumberTaps = document.querySelector('.pplusNumberTaps') + 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(); + + + document.querySelector("#recalculateFir").onclick = function() { + console.log("Oh noe"); + //https://stackoverflow.com/questions/17883799/how-to-handle-passing-returning-array-pointers-to-emscripten-compiled-code + function cArray(size) { + var offset = Module._malloc(size * 8); + Module.HEAPF64.set(new Float64Array(size), offset / 8); + return { + "data": Module.HEAPF64.subarray(offset / 8, offset / 8 + size), + "offset": offset + } + } + myArray = cArray(200); + var result = recalculate_fir(myArray.offset); + var arr = Array(myArray.data.slice(1,result-1)); + arr = arr.map(function(item){ + return item.map(function(num){ + return parseFloat(num.toFixed(5)); + }); + }); + outCoeficients.value = arr; + } + + document.querySelector("#getCutoffFrequency").onclick = function() { + console.log("--1kHz"); + set_cutoff_minus_1k(); + var result=get_cutoff_freq(); + outSetCutoffFrequency.value = result; + } + + document.querySelector("#setCutoffFrequency").onclick = function() { + console.log("+1kHz"); + set_cutoff_plus_1k(); + var result=get_cutoff_freq(); + outSetCutoffFrequency.value = result; + } + + document.querySelector("#plusNumberTaps").onclick = function() { + console.log("Oh noe"); + set_tap_number_plus_10(); + var result = get_tap_number() + outSetNumberTaps.value = result; + } + document.querySelector("#minusNumberTaps").onclick = function() { + console.log("Oh noe"); + set_tap_number_minus_10(); + var result = get_tap_number() + outSetNumberTaps.value = result; + } } document.body.appendChild(script); }); + </script> <div align="center"> <canvas id="canvas" ></canvas> </div> - <button class="setCutoffFrequency" type="button">Set Cutoff(Hz)</button> + <button id="setCutoffFrequency" type="button">Cutoff +1KHz</button> + <button id="getCutoffFrequency" type="button">Cutoff -1Khz</button> <textarea class="outSetCutoffFrequency"></textarea> - <button class="setNumberTaps" type="button">Set Taps</button> + <button id="plusNumberTaps" type="button">Tap N+10</button> + <button id="minusNumberTaps" type="button">Tap N-10</button> <textarea class="outSetNumberTaps"></textarea> - <button class="recalculateFir" id="recalculateFir" type="button">Calc</button> - <textarea class="outCoeficients">1</textarea> - + <button id="recalculateFir" type="button">Calc</button> + <textarea class="outCoeficients"></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; - }) - - document.querySelector("#recalculateFir").onclick = function() { - 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 - Run r, listen with s, apply filtr with q and listen again with s + Workflow(r->s->q) Supported keys: r - record sound buffer s - play recorded buffer @@ -81,7 +133,7 @@ v0.1 - initial release </pre> - <!--<script src="index.js"></script>--> + </body> </html> diff --git a/WasmAudio/main.c b/WasmAudio/main.c index 4987fff..ab17933 100644 --- a/WasmAudio/main.c +++ b/WasmAudio/main.c @@ -84,7 +84,8 @@ uint8_t *audio_rec_buffer=NULL; FILE *f_1,*f_2; fir_t fir_lp; firw_t firw_lp; -const int N=200; +//const int N=200; +#define N (200) double h[N],w[N]; int filter_N=60; int filter_FS_hz=48000; @@ -164,13 +165,11 @@ int KEEPALIVE get_sample_rate() { return filter_FS_hz; } -void set_sample_rate() { - -} - -void set_cutoff_freq(int cutoff) { - if (cutoff < 0) { +void KEEPALIVE set_cutoff_freq(int cutoff) { + printf(" cutoff=%d\n", cutoff); + if (cutoff <= 0) { cutoff = 0; + printf(" cutoff=%d\n", cutoff); filter_FC_hz = cutoff; return; } @@ -178,9 +177,19 @@ void set_cutoff_freq(int cutoff) { if (cutoff > filter_FS_hz/2) { cutoff = filter_FS_hz/2; filter_FC_hz = cutoff; - filter_FC=1.0*cutoff/filter_FS_hz; + printf(" cutoff=%d\n", cutoff); + filter_FC=1.0*filter_FC_hz/filter_FS_hz; return; } + filter_FC_hz = cutoff; +} + +void KEEPALIVE set_cutoff_plus_1k() { + set_cutoff_freq(filter_FC_hz+1000); +} + +void KEEPALIVE set_cutoff_minus_1k() { + set_cutoff_freq(filter_FC_hz-1000); } int KEEPALIVE get_cutoff_freq() { @@ -191,15 +200,31 @@ int KEEPALIVE get_tap_number() { return filter_N; } -void set_tap_number(int tapN) { + + +void KEEPALIVE set_tap_number(int tapN) { if (tapN<10) { - tapN = 10; + filter_N = 10; return; - } + + if (tapN>N) { + filter_N = 100; + return; + } + + filter_N = tapN; +} + +void KEEPALIVE set_tap_number_plus_10() { + set_tap_number(filter_N+10); +} + +void KEEPALIVE set_tap_number_minus_10() { + set_tap_number(filter_N-10); } -int KEEPALIVE recalculate_fir() { +int KEEPALIVE recalculate_fir(double *arr) { int i = 0; filter_FC=1.0*filter_FC_hz/filter_FS_hz; fir_lp.num_taps =filter_N; @@ -220,7 +245,10 @@ int KEEPALIVE recalculate_fir() { //firw_rect(&firw_lp); fir_convolute(&fir_lp, &firw_lp); //fir_print_matlab(&fir_lp); - return 0; + for (i=0;i<fir_lp.num_taps;i++) { + arr[i] = fir_lp.fir_coef[i]; + } + return fir_lp.num_taps; } @@ -236,6 +264,26 @@ void convert_dobule_up() { } } +void cut_up() { + int i=0; + for (i=0;i<BUFSIZE4;i++) { + if (process_buf[i] > 1.0) process_buf[i] = 1.0; + if (process_buf[i] < -1.0) process_buf[i] = -1.0; + } +} + +#ifdef __EMSCRIPTEN__ +int KEEPALIVE stop_loop() { + emscripten_pause_main_loop(); + return 0; +} + +int KEEPALIVE start_loop() { + emscripten_resume_main_loop(); + return 0; +} +#endif + void save_to_matlab(char *name) { int i; FILE *f=NULL; @@ -368,6 +416,7 @@ int main_tick() { fir_filter(&fir_lp, process_buf, PROCESS_BUF_SIZE, process_buf2, PROCESS_BUF_SIZE); memcpy(process_buf,process_buf2,PROCESS_BUF_SIZE*sizeof(double)); + cut_up(); save_to_matlab("B2"); #endif convert_double_to_f32(); @@ -403,7 +452,10 @@ int main_tick() { if( audio_buf_position > audio_rec_buffer_size ) { //Stop playing audio + //SDL_Delay(500); SDL_PauseAudioDevice( playbackDeviceId, SDL_TRUE ); + //printf("%d\n",SDL_GetQueuedAudioSize(playbackDeviceId)); + //SDL_ClearQueuedAudio(playbackDeviceId); current_state = RECORDED; printf("State: RECORDED\n"); } |