js浏览器中web audio api实时变声录制下载效果代码

代码语言:html

所属分类:多媒体

代码描述:js浏览器中web audio api实时变声录制下载效果代码

代码标签: js 浏览器 web audio api 实时 变声 录制 下载

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

<!DOCTYPE html>
<html lang="en">
<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;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f0f0f0;
        }
        .container {
            text-align: center;
        }
        button {
            font-size: 18px;
            padding: 10px 20px;
            margin: 10px;
            cursor: pointer;
        }
        #downloadButton {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>实时变声效果与下载</h1>
        <button id="startButton">开始录音</button>
        <button id="stopButton" disabled>停止录音</button>
        <button id="downloadButton">下载音频</button>
        <div id="status"></div>
    </div>

    <script>
        let audioContext;
        let stream;
        let source;
        let processor;
        let pitchShift;
        let mediaRecorder;
        let audioChunks = [];

        const startButton = document.getElementById('startButton');
        const stopButton = document.getElementById('stopButton');
        const downloadButton = document.getElementById('downloadButton');
        const status = document.getElementById('status');

        startButton.addEventListener('click', startRecording);
        stopButton.addEventListener('click', stopRecording);
        downloadButton.addEventListener('click', downloadAudio);

        async function startRecording() {
            try {
                audioContext = new (window.AudioContext || window.webkitAudioContext)();
                stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                source = audioContext.createMediaStreamSource(stream);
                
                processor = audioContext.createScriptProcessor(4096, 1, 1);
                
                pitchShift = audioContext.createBiquadFilter();
                pitchShift.type = "allpass";
                pitchShift.frequency.value = 1000;

                source.connect(pitchShift);
                pitchShift.connect(processor);
                processor.connect(audioContext.destination);

                const destinationStr.........完整代码请登录后点击上方下载按钮下载查看

网友评论0