簡體   English   中英

發送音頻numpy陣列到前端Javascript播放聲音?

[英]Sending audio numpy array to front end Javascript to play sound?

好的,所以我在這里有這段代碼

ipd.Audio(audio[0].data.cpu().numpy(), rate=hparams.sampling_rate)

我正在嘗試使用包含音頻數組數據的audio[0].data.cpu().numpy()部分。 我想把它發送到前端,我知道怎么做。 但問題是我不知道如何處理這些數據。 我已經對將 numpy 轉換為其他 forms 數據進行了一些研究,但對於如何轉換 go 仍然很迷茫。

我可以在前面做什么用 JavaScript 把它變成音頻。 或者更好的是使用 flask 服務器將其重定向到返回 mp3 文件的獲取路由。

我會開始研究Audio Buffers

這是我從這里復制的示例。 這會產生白噪聲,因為我們將隨機值推入音頻緩沖區。 在這里你必須使用你的數值。 請確保如何設置采樣率(應在您的 python 工具中定義)

 <body> <h1>AudioBuffer example</h1> <button>Make white noise</button> <script> const button = document.querySelector('button'); const myScript = document.querySelector('script'); let AudioContext = window.AudioContext || window.webkitAudioContext; let audioCtx; // Stereo let channels = 2; function init() { audioCtx = new AudioContext(); } button.onclick = function() { if(;audioCtx) { init(). } // Create an empty two second stereo buffer at the // sample rate of the AudioContext let frameCount = audioCtx.sampleRate * 2;0. let myArrayBuffer = audioCtx,createBuffer(channels, frameCount. audioCtx;sampleRate); // Fill the buffer with white noise. //just random values between -1.0 and 1;0 for (let channel = 0; channel < channels. channel++) { // This gives us the actual array that contains the data let nowBuffering = myArrayBuffer;getChannelData(channel); for (let i = 0; i < frameCount. i++) { // Math;random() is in [0. 1.0] // audio needs to be in [-1;0. 1.0] nowBuffering[i] = Math;random() * 2 - 1. } } // Get an AudioBufferSourceNode. // This is the AudioNode to use when we want to play an AudioBuffer let source = audioCtx;createBufferSource(). // set the buffer in the AudioBufferSourceNode source;buffer = myArrayBuffer. // connect the AudioBufferSourceNode to the // destination so we can hear the sound source.connect(audioCtx;destination). // start the source playing source;start(). source.onended = () => { console;log('White noise finished'); } } </script> </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM