gpt4 book ai didi

javascript - 是否可以从 .wav 文件创建 MediaStream?

转载 作者:行者123 更新时间:2023-11-29 20:41:57 35 4
gpt4 key购买 nike

我目前有一个函数可以从 navigator.getUserMedia() 接收 MediaStream,效果很好。我想提供上传音频文件并通过相同功能模拟它的选项。我想知道是否可以上传一个音频文件并创建一个 Mediastream 对象并通过以下函数传递它?

    startUserMedia(stream) {
this.setState({ audio: stream });
var audioContext = new AudioContext();
var source = audioContext.createMediaStreamSource(stream);
var processor = audioContext.createScriptProcessor(8192, 1, 1);

source.connect(processor);
processor.connect(audioContext.destination);

const that = this;
let audioBuffers = [];
this.setState({ currentDuration: 0 });
processor.onaudioprocess = function(e) {
// Do something with the data, i.e Convert this to WAV
if (that.state.currentDuration < that.state.chunkDuration) {
that.setState({
currentDuration: that.state.currentDuration + e.inputBuffer.duration
});

resampler(e.inputBuffer, 16000, function(event) {
const buffer = event.getAudioBuffer();
if (that.state.voiceActive) {
audioBuffers.push(buffer);
}
});
} else {
if (!that.state.voiceActive) {
that.mergeAndSendAudio(audioBuffers, audioContext);
that.setState({ currentDuration: 0 });
audioBuffers = [];
audioBuffers.push(e.inputBuffer);
} else {
audioBuffers.push(e.inputBuffer);
}
}
};

var options = {
onVoiceStart: function() {
console.log("voice start");
that.setState({ voiceActive: true });
},
onVoiceStop: function() {
console.log("voice stop");
that.setState({ voiceActive: false });
},
onUpdate: function(val) {
// console.log('curr val:', val);
}
};
vad(audioContext, stream, options);
}

最佳答案

找到答案:

  handleselectedFile = event => {
this.setState({
selectedFile: event.target.files[0],
loaded: 0
});

const objectURL = window.URL.createObjectURL(event.target.files[0]);
const audio = new Audio(objectURL);
const stream = audio.captureStream();

audio.play().then(_ => {
this.startUserMedia(stream);
}); // stream now has input
};

关于javascript - 是否可以从 .wav 文件创建 MediaStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55207716/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com