43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
// SonicForge Studio Audio Engine Service
|
|
(function() {
|
|
let audioCtx = null;
|
|
|
|
function getAudioContext() {
|
|
if (!audioCtx) {
|
|
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
}
|
|
if (audioCtx.state === 'suspended') {
|
|
audioCtx.resume();
|
|
}
|
|
return audioCtx;
|
|
}
|
|
|
|
function analyzeAudioBufferChannels(audioBuffer) {
|
|
if (!audioBuffer) return { channels: 1, isStereo: false, label: 'MONO' };
|
|
const numChannels = audioBuffer.numberOfChannels;
|
|
const isStereo = numChannels >= 2;
|
|
return {
|
|
channels: numChannels,
|
|
isStereo: isStereo,
|
|
label: isStereo ? 'STEREO' : 'MONO',
|
|
sampleRate: audioBuffer.sampleRate,
|
|
duration: audioBuffer.duration,
|
|
length: audioBuffer.length
|
|
};
|
|
}
|
|
|
|
async function decodeAudioFile(file) {
|
|
const ctx = getAudioContext();
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
const audioBuffer = await ctx.decodeAudioData(arrayBuffer);
|
|
const channelInfo = analyzeAudioBufferChannels(audioBuffer);
|
|
return { audioBuffer, channelInfo };
|
|
}
|
|
|
|
window.SonicAudio = {
|
|
getAudioContext,
|
|
analyzeAudioBufferChannels,
|
|
decodeAudioFile
|
|
};
|
|
})();
|