40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
export function setupPlayButton(
|
|
sources: Map<number, AudioBufferSourceNode>,
|
|
gains: Map<number, GainNode>,
|
|
context: AudioContext
|
|
) {
|
|
const button = document.createElement('button')
|
|
button.textContent = '▶ Play All'
|
|
button.style.fontSize = '1.5rem'
|
|
button.style.padding = '10px'
|
|
button.style.marginTop = '20px'
|
|
|
|
button.onclick = () => {
|
|
const startTime = context.currentTime + 0.1 // 100ms in Zukunft
|
|
const rampTime = 2
|
|
|
|
sources.forEach((source, freq) => {
|
|
const gainNode = gains.get(freq)
|
|
if (!gainNode) return
|
|
|
|
// Ramp up GainNode
|
|
const gain = gainNode.gain
|
|
gain.setValueAtTime(0.001, startTime)
|
|
gain.exponentialRampToValueAtTime(1.0, startTime + rampTime)
|
|
|
|
// Start BufferSource
|
|
try {
|
|
source.start(startTime)
|
|
console.log(`Started ${freq}Hz at ${startTime.toFixed(2)}`)
|
|
} catch (err) {
|
|
console.warn(`Start error for ${freq}Hz`, err)
|
|
}
|
|
})
|
|
|
|
button.disabled = true
|
|
button.textContent = '🔊 Playing...'
|
|
}
|
|
|
|
document.body.appendChild(button)
|
|
}
|
|
|