メインコンテンツへスキップ
見出し画像

教室アプリ19:音量計


    画像

    このアプリは、音量という曖昧な対象を可視化する道具です。操作は簡単。「測定開始」を押し、マイクを子供に向けます。バーが下から上に伸び、色が変わることで声の大きさがわかります。

    <!DOCTYPE html>
    <html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>教室アプリ:音量計</title>
        <style>
            body { 
                font-family: 'Helvetica Neue', Arial, sans-serif; 
                text-align: center; 
                background: #f0f4f8; 
                margin: 0; 
                display: flex; 
                flex-direction: column; 
                align-items: center; 
                justify-content: center; 
                height: 100vh; 
            }
            h2 { margin-bottom: 10px; color: #444; }
            
            #volume-display { font-size: 5rem; font-weight: bold; margin-bottom: 20px; }
    
            /* 縦型メーターのコンテナ */
            #meter-container { 
                width: 100px; 
                height: 400px; 
                background: #e0e0e0; 
                border-radius: 50px; 
                overflow: hidden; 
                border: 6px solid #fff;
                box-shadow: inset 0 4px 10px rgba(0,0,0,0.1);
                position: relative;
                display: flex;
                flex-direction: column-reverse; /* 下から上に伸ばすための設定 */
            }
    
            /* 伸びるバーの部分 */
            #meter-fill { 
                width: 100%; 
                height: 0%; 
                transition: height 0.1s, background-color 0.2s; 
                background-color: #4caf50; 
            }
    
            button { 
                margin-top: 30px; padding: 15px 40px; font-size: 1.2rem; cursor: pointer; 
                border-radius: 30px; border: none; background: #2196f3; color: white;
                box-shadow: 0 4px 10px rgba(0,0,0,0.2); transition: 0.2s;
            }
            button:hover { background: #1976d2; transform: scale(1.05); }
        </style>
    </head>
    <body>
        <h2>声のエネルギー</h2>
        <div id="volume-display">0</div>
        
        <div id="meter-container">
            <div id="meter-fill"></div>
        </div>
    
        <button id="startButton">測定を開始する</button>
    
        <script>
            const startButton = document.getElementById('startButton');
            const volumeDisplay = document.getElementById('volume-display');
            const meterFill = document.getElementById('meter-fill');
    
            startButton.onclick = async () => {
                try {
                    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                    const audioContext = new (window.AudioContext || window.webkitAudioContext)();
                    const analyser = audioContext.createAnalyser();
                    const microphone = audioContext.createMediaStreamSource(stream);
                    
                    analyser.fftSize = 256;
                    microphone.connect(analyser);
    
                    const bufferLength = analyser.frequencyBinCount;
                    const dataArray = new Uint8Array(bufferLength);
    
                    function update() {
                        analyser.getByteFrequencyData(dataArray);
                        let sum = 0;
                        for (let i = 0; i < bufferLength; i++) {
                            sum += dataArray[i];
                        }
                        let average = sum / bufferLength;
                        
                        // 感度調整(1.5を環境に合わせて変更してください)
                        let volume = Math.round(average * 1.5); 
                        if (volume > 100) volume = 100;
    
                        volumeDisplay.innerText = volume;
                        
                        // 縦の「高さ」を制御
                        meterFill.style.height = volume + '%';
    
                        // 色のロジック
                        if (volume <= 30) {
                            meterFill.style.backgroundColor = "#4caf50"; // 緑
                            volumeDisplay.style.color = "#4caf50";
                        } else if (volume <= 80) {
                            meterFill.style.backgroundColor = "#ffeb3b"; // 黄
                            volumeDisplay.style.color = "#fbc02d";
                        } else {
                            meterFill.style.backgroundColor = "#f44336"; // 赤
                            volumeDisplay.style.color = "#f44336";
                        }
                        
                        requestAnimationFrame(update);
                    }
                    update();
                    startButton.style.display = 'none';
                } catch (err) {
                    alert("マイクの使用を許可してください。");
                }
            };
        </script>
    </body>
    </html>
     
     
    小学校教師/教職30年/小学校の教室で使うアプリをAIで作っています

    あなたへのおすすめ