This commit is contained in:
yumoqing 2024-08-09 16:44:35 +08:00
parent 8896949cae
commit 7f71284408

View File

@ -9,8 +9,12 @@ bricks.VadText = class extends bricks.VBox {
label:'start',
icon:bricks_resource('imgs/speak.png')
});
this.audio = AudioPlayer({});
hbox = new bricks.HBox({height:'auto'});
hbox.add_widget(this.button);
hbox.add_widget(this.audio)
this.add_widget(hbox);
this.filler = new bricks.Filler({});
this.add_widget(this.button);
this.add_widget(this.filler);
this.text_w = new bricks.Text({text:' ', wrap:true});
this.filler.add_widget(this.text_w);
@ -57,13 +61,16 @@ bricks.VadText = class extends bricks.VBox {
}
async handle_audio(audio){
console.log('handle_audil() called', audio);
var b64audio = btoa(audio);
var wavBuffer = this.floatArrayToWAV(audio, sampleRate=16000);
var url =
var b64audio = this.arrayBufferToBase64(wavBuffer);
this.audio.set_url('data:audio/wav;base64,' + b64audio);
var hj = new bricks.HttpJson();
var d={
method:'POST',
params:{
model:this.model,
audio:audio
audio:b64audio
}
}
var rj = await hj.httpcall(this.url, d);
@ -78,6 +85,25 @@ bricks.VadText = class extends bricks.VBox {
w.open();
}
}
function arrayBufferToBase64(wavBuffer) {
let binary = '';
const bytes = new Uint8Array(wavBuffer);
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function floatArrayToWAV(floatArray, sampleRate = 22000) {
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const buffer = audioCtx.createBuffer(1, floatArray.length, sampleRate);
buffer.getChannelData(0).set(floatArray);
const audioData = buffer;
const wavEncoder = new WavEncoder();
const wavBlob = wavEncoder.encode(audioData);
return wavBlob;
}
getValue(){
var d = {}
d[this.name] = this.text;