+16
-64
| // AudioConverter.js | ||
| class BufferPool { | ||
| constructor() { | ||
| this.buffers = new Map(); | ||
| this.maxSize = 1024 * 1024; // 1MB | ||
| this.currentSize = 0; | ||
| } | ||
| getOrCreate(channels, length, sampleRate) { | ||
| const key = `${channels}-${length}-${sampleRate}`; | ||
| if (this.buffers.has(key)) { | ||
| const buffer = this.buffers.get(key); | ||
| this.buffers.delete(key); | ||
| this.currentSize -= buffer.length * channels * 2; | ||
| this.buffers.set(key, buffer); | ||
| this.currentSize += buffer.length * channels * 2; | ||
| return buffer; | ||
| } | ||
| const buffer = new this.audioContext.createBuffer( | ||
| channels, | ||
| length, | ||
| sampleRate | ||
| ); | ||
| if (this.currentSize + length * channels * 2 > this.maxSize) { | ||
| this.cleanup(); | ||
| } | ||
| this.buffers.set(key, buffer); | ||
| this.currentSize += buffer.length * channels * 2; | ||
| return buffer; | ||
| } | ||
| cleanup() { | ||
| const keys = Array.from(this.buffers.keys()); | ||
| while (this.currentSize > this.maxSize / 2 && keys.length > 0) { | ||
| const key = keys.pop(); | ||
| const buffer = this.buffers.get(key); | ||
| this.currentSize -= buffer.length * buffer.numberOfChannels * 2; | ||
| this.buffers.delete(key); | ||
| } | ||
| } | ||
| } | ||
| class AudioConverter { | ||
| constructor() { | ||
| this.audioContext = new (window.AudioContext || | ||
| window.webkitAudioContext)(); | ||
| this.audioContext = new (window.AudioContext || window.webkitAudioContext)(); | ||
| this.bufferPool = new BufferPool(); | ||
@@ -58,5 +12,5 @@ } | ||
| const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer); | ||
| const auduData = { | ||
| format: "audu", | ||
| format: 'audu', | ||
| version: 1, | ||
@@ -66,9 +20,9 @@ sampleRate: this.audioContext.sampleRate, | ||
| length: audioBuffer.length, | ||
| data: this.bufferToDataUrl(audioBuffer), | ||
| data: this.bufferToDataUrl(audioBuffer) | ||
| }; | ||
| return auduData; | ||
| } catch (error) { | ||
| throw new AudioConverterError(401, "変換エラーが発生しました", { | ||
| originalError: error, | ||
| throw new AudioConverterError(401, '変換エラーが発生しました', { | ||
| originalError: error | ||
| }); | ||
@@ -97,16 +51,14 @@ } | ||
| try { | ||
| const blob = new Blob([JSON.stringify(auduData)], { | ||
| type: "application/audu", | ||
| }); | ||
| const blob = new Blob([JSON.stringify(auduData)], { type: 'application/audu' }); | ||
| const url = URL.createObjectURL(blob); | ||
| const a = document.createElement("a"); | ||
| const a = document.createElement('a'); | ||
| a.href = url; | ||
| a.download = "output.audu"; | ||
| a.download = 'output.audu'; | ||
| a.click(); | ||
| return blob; | ||
| } catch (error) { | ||
| throw new AudioConverterError(401, "保存エラーが発生しました", { | ||
| originalError: error, | ||
| throw new AudioConverterError(401, '保存エラーが発生しました', { | ||
| originalError: error | ||
| }); | ||
@@ -122,6 +74,6 @@ } | ||
| this.details = details; | ||
| this.name = "AudioConverterError"; | ||
| this.name = 'AudioConverterError'; | ||
| } | ||
| } | ||
| export const AudioConverter = new AudioConverter(); | ||
| export { AudioConverter }; |
+1
-1
| { | ||
| "name": "audu-utils", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "AUDU format utility to combine AudioBuffer generated from multiple audio into one file", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
2402
-33.18%64
-39.62%