From 5028657d02f56c82a1df721bbe203e36548d4bfa Mon Sep 17 00:00:00 2001 From: cnlohr Date: Wed, 10 Jul 2024 02:28:53 -0700 Subject: [PATCH] Add shim to add sound --- ch32v/ch32v203-goertzel/adcgoertzel.c | 4 +- ch32v/lib/calculator.html | 2 +- ch32v/lib/webhidcontrol.js | 73 ++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/ch32v/ch32v203-goertzel/adcgoertzel.c b/ch32v/ch32v203-goertzel/adcgoertzel.c index 5c5c862..3e07492 100644 --- a/ch32v/ch32v203-goertzel/adcgoertzel.c +++ b/ch32v/ch32v203-goertzel/adcgoertzel.c @@ -89,7 +89,7 @@ int32_t g_goertzel_coefficient = -1453756170; int32_t g_goertzel_coefficient_s = 1580594514; #endif -#if 0 +#if 1 int g_pwm_period = (30-1); int g_goertzel_buffer = (180); int32_t g_goertzel_omega_per_sample = 5509657063; // 0.816667 of whole per step / 0.880000MHz @@ -105,7 +105,7 @@ const int32_t g_goertzel_coefficient = 32748822; const int32_t g_goertzel_coefficient_s = 2147233926; #endif -#if 1 +#if 0 int g_pwm_period = (30-1); int g_goertzel_buffer = (576); int32_t g_goertzel_omega_per_sample = 1264972285; // 0.187500 of whole per step / 90.300000MHz diff --git a/ch32v/lib/calculator.html b/ch32v/lib/calculator.html index 812025b..948a552 100644 --- a/ch32v/lib/calculator.html +++ b/ch32v/lib/calculator.html @@ -227,7 +227,7 @@ function onLoad() Live Control: -
+
diff --git a/ch32v/lib/webhidcontrol.js b/ch32v/lib/webhidcontrol.js index 799000c..6bf3990 100644 --- a/ch32v/lib/webhidcontrol.js +++ b/ch32v/lib/webhidcontrol.js @@ -58,6 +58,77 @@ async function closeDeviceTool() setStatusError( "Disconnected" ); } + + +async function toggleAudio() +{ +var bypass = '\ +class PlayingAudioProcessor extends AudioWorkletProcessor {\ + static get parameterDescriptors() {\ + return [\ + {\ + name: "ingestRate",\ + defaultValue: 25000,\ + minValue: 1,\ + maxValue: 1000000\ + },\ + ]\ + };\ + constructor() {\ + super();\ + this.port.onmessage = (e) => {\ + console.log(e.data);\ + this.ingestData = e.data;\ + };\ + this.ingestData = new ArrayBuffer(0);\ + }\ + \ + process(inputs, outputs, parameters) {\ + /*console.log( parameters.ingestRate[0] );*/ \ + /*console.log( this.ingestData );*/ \ + let len = outputs[0][0].length; \ + for (let b = 0|0; b < len|0; b++) { \ + outputs[0][0][b] = Math.random()*0.01; \ + } \ + return true; \ + } \ +} \ +\ +registerProcessor("playing-audio-processor", PlayingAudioProcessor);'; + +// The following mechanism does not work on Chrome. +// const dataURI = URL.createObjectURL( new Blob([bypass], { type: 'text/javascript', } ) ); + + + // Extremely tricky trick to side-step local file:// CORS issues. + // https://stackoverflow.com/a/67125196/2926815 + // https://stackoverflow.com/a/72180421/2926815 + let blob = new Blob([bypass], {type: 'application/javascript'}); + let reader = new FileReader(); + await reader.readAsDataURL(blob); + let dataURI = await new Promise((res) => { + reader.onloadend = function () { + res(reader.result); + } + }); + + var audioContext = new AudioContext(); + + await audioContext.audioWorklet.addModule(dataURI); + + PlayingAudioProcessor = new AudioWorkletNode( + audioContext, + "playing-audio-processor" + ); + PlayingAudioProcessor.connect(audioContext.destination); + audioContext.resume(); + + let testParam = PlayingAudioProcessor.parameters.get("ingestRate"); + testParam.setValueAtTime(1000, audioContext.currentTime); + + PlayingAudioProcessor.port.postMessage( new ArrayBuffer(0) ); +} + function onLoadWebHidControl() { liveGraph = document.getElementById( "LiveGraph" ); @@ -174,7 +245,7 @@ async function sendLoop() receiveReport = dev.receiveFeatureReport( 0xAD ).catch( sendLoopError ); if( !receiveReport ) sendLoopError( "error creating receiveReport" ); - frameNo++; + frameNo++ const updateStatsPerfPer = 4; if( frameNo % updateStatsPerfPer == 0 )