オーディオをダウンサンプリングしたく以下の記事を参考にコードを作成しました。
https://stackoverflow.com/questions/27598270/resample-audio-buffer-from-44100-to-16000
実行してみるとOfflineAudioContextの部分で以下のエラーが発生しました。
Uncaught DOMException: Failed to construct 'OfflineAudioContext': The number of frames provided (0) is less than the minimum bound (1). at fileReader.onload (webpack-internal:///./node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./pages/index.vue?vue&type=script&lang=js&:29:28)
調べてみるとsource.numberOfChannels
がundefinedになっており、そもそもsourceがちゃんと取得できていないようです。
色々と調べてみたのですが解決に至らず...原因がわかる方がおられましたらご教授いただけると幸いです。
javascript
1 const file = 'audio.wav' // 読み込むファイルのURL 2 fetch(file) 3 .then((res) => res.blob()) 4 .then((blob) => { 5 // FileReaderを作成 6 const fileReader = new FileReader() 7 8 // 読み込み完了時のイベント 9 fileReader.onload = function () { 10 const source = fileReader.result // ArrayBuffer 11 const TARGET_SAMPLE_RATE = 16000 12 13 const offlineCtx = new OfflineAudioContext( // ここでエラー 14 source.numberOfChannels, 15 source.duration * TARGET_SAMPLE_RATE, 16 TARGET_SAMPLE_RATE 17 ) 18 19 // Play it from the beginning. 20 const offlineSource = offlineCtx.createBufferSource() 21 offlineSource.buffer = source 22 offlineSource.connect(offlineCtx.destination) 23 offlineSource.start() 24 offlineCtx.startRendering().then((resampled) => { 25 // `resampled` contains an AudioBuffer resampled at 16000Hz. 26 // use resampled.getChannelData(x) to get an Float32Array for channel x. 27 console.log(resampled) 28 }) 29 } 30 31 // 読み込みを実行 32 fileReader.readAsArrayBuffer(blob) 33 }) 34 },
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。