ggwave-js : simplify example (#21)

Now that ggwave support input resampling, we don't need to resample
in javascript
This commit is contained in:
Georgi Gerganov
2021-02-21 00:43:37 +02:00
committed by GitHub
parent e5c094296f
commit bd9d6b1d81
2 changed files with 23 additions and 14 deletions

View File

@@ -1,3 +1,4 @@
set(TARGET ggwave-js) set(TARGET ggwave-js)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/index-tmpl.html ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/index.html @ONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/index-tmpl.html ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/index.html @ONLY)
configure_file(${CMAKE_SOURCE_DIR}/bindings/javascript/ggwave.js ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET}/ggwave.js COPYONLY)

View File

@@ -67,10 +67,10 @@
// initialize audio context and ggwave // initialize audio context and ggwave
function init() { function init() {
if (!context) { if (!context) {
// todo : query ggwave's base sample rate
context = new AudioContext({sampleRate: 48000}); context = new AudioContext({sampleRate: 48000});
parameters = ggwave.getDefaultParameters(); parameters = ggwave.getDefaultParameters();
parameters.sampleRateInp = context.sampleRate;
parameters.sampleRateOut = context.sampleRate; parameters.sampleRateOut = context.sampleRate;
instance = ggwave.init(parameters); instance = ggwave.init(parameters);
} }
@@ -136,20 +136,27 @@
recorder.onaudioprocess = function (e) { recorder.onaudioprocess = function (e) {
var source = e.inputBuffer; var source = e.inputBuffer;
var offlineCtx = new OfflineAudioContext(source.numberOfChannels, 48000*source.duration, 48000); var res = ggwave.decode(instance, convertTypedArray(new Float32Array(source.getChannelData(0)), Int8Array));
var offlineSource = offlineCtx.createBufferSource(); if (res) {
rxData.value = res;
}
offlineSource.buffer = source; // obsolete javascript resampling
offlineSource.connect(offlineCtx.destination); // since ggwave v0.2.0 the resampling is built-in ggwave
offlineSource.start(); //var offlineCtx = new OfflineAudioContext(source.numberOfChannels, 48000*source.duration, 48000);
offlineCtx.startRendering(); //var offlineSource = offlineCtx.createBufferSource();
offlineCtx.oncomplete = function(e) {
var resampled = e.renderedBuffer.getChannelData(0); //offlineSource.buffer = source;
var res = ggwave.decode(instance, convertTypedArray(new Float32Array(resampled), Int8Array)); //offlineSource.connect(offlineCtx.destination);
if (res) { //offlineSource.start();
rxData.value = res; //offlineCtx.startRendering();
} //offlineCtx.oncomplete = function(e) {
}; // var resampled = e.renderedBuffer.getChannelData(0);
// var res = ggwave.decode(instance, convertTypedArray(new Float32Array(resampled), Int8Array));
// if (res) {
// rxData.value = res;
// }
//};
} }
mediaStream.connect(recorder); mediaStream.connect(recorder);
@@ -167,6 +174,7 @@
if (recorder) { if (recorder) {
recorder.disconnect(context.destination); recorder.disconnect(context.destination);
mediaStream.disconnect(recorder); mediaStream.disconnect(recorder);
recorder = null;
} }
rxData.value = 'Audio capture is paused! Press the "Start capturing" button to analyze audio from the microphone'; rxData.value = 'Audio capture is paused! Press the "Start capturing" button to analyze audio from the microphone';