js实现浏览器中多个Oscillator声音可调节音量混音输出示例代码

代码语言:html

所属分类:多媒体

代码描述:js实现浏览器中多个Oscillator声音可调节音量混音输出示例代码

代码标签: js 浏览器 多个 Oscillator 声音 调节 音量 混音 输出 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>多音频混音示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        h1 {
            text-align: center;
        }
        .audio-control {
            margin-bottom: 20px;
        }
        label {
            display: inline-block;
            width: 100px;
        }
        input[type="range"] {
            width: 200px;
        }
    </style>
</head>
<body>
    <h1>多音频混音示例</h1>
    <div class="audio-control">
        <label for="audio1">音频1音量:</label>
        <input type="range" id="audio1" min="0" max="1" step="0.1" value="0.5">
        <span id="audio1Value">0.5</span>
    </div>
    <div class="audio-control">
        <label for="audio2">音频2音量:</label>
        <input type="range" id="audio2" min="0" max="1" step="0.1" value="0.5">
        <span id="audio2Value">0.5</span>
    </div>
    <div class="audio-control">
        <label for="audio3">音频3音量:</label>
        <input type="range" id="audio3" min="0" max="1" step="0.1" value="0.5">
        <span id="audio3Value">0.5</span>
    </div>
    <button id="playBtn">播放</button>
    <button id="stopBtn">停止</button>

    <script>
        // 创建音频上下文
        const audioContext = new (window.AudioContext || window.webkitAudioContext)();
        
        // 创建三个振荡器作为音源
        const oscillator1 = audioContext.createOscillator();
        const oscillator2 = audioContext.createOscillator();
        const oscillator3 = audioContext.createOscillator();
        
        // 设置不同的频率
        oscillator1.frequency.setValueAtTime(261.63, audioContext.currentTime); // C4
        oscillator2.frequency.setValueAtTime(329.63, audioContext.currentTime); // E4
        oscillator3.frequency.setValueAtTime(392.00, audioContext.currentTime); // G4
        
        // 创建增益节点来控制音量
        const gainNode1 = audioContext.createGain();
        const gainNode2 = audioContext.createGain();
        const gainNode3 = audioContext.createGain();
        
        // 连接音源到增益节点,再连接到输出
        oscillator1.connect(gainNode1).connect(audioContext.destination);
        oscillator2.connect(gainNode2).connect(audioContex.........完整代码请登录后点击上方下载按钮下载查看

网友评论0