{"id":362,"date":"2024-12-31T03:46:16","date_gmt":"2024-12-31T03:46:16","guid":{"rendered":"https:\/\/lectenna.com\/?page_id=362"},"modified":"2024-12-31T04:15:37","modified_gmt":"2024-12-31T04:15:37","slug":"audio-over-aux","status":"publish","type":"page","link":"https:\/\/lectenna.com\/index.php\/audio-over-aux\/","title":{"rendered":"Audio over AUX"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>LEctenna Full Melody with Sound<\/title>\n    <style>\n        body {\n            display: flex;\n            flex-direction: column;\n            align-items: center;\n            height: 100vh;\n            margin: 0;\n            background-color: black;\n            color: white;\n            font-family: Arial, sans-serif;\n        }\n\n        h1, p {\n            text-align: center;\n        }\n\n        #statusMessage {\n            margin-top: 20px;\n            text-align: center;\n            font-size: 18px;\n            color: #00ff00;\n        }\n\n        #playButton {\n            margin-top: 20px;\n            padding: 10px 20px;\n            background-color: #008CBA;\n            color: white;\n            border: none;\n            border-radius: 5px;\n            cursor: pointer;\n            font-size: 16px;\n        }\n\n        #playButton:hover {\n            background-color: #005f6a;\n        }\n    <\/style>\n<\/head>\n<body>\n    <div>\n        <h1>LEctenna Full Melody with Sound<\/h1>\n        <p>Click the button to play the melody through the speaker and via LEctenna.<\/p>\n        <p id=\"statusMessage\">Status: Preparing&#8230;<\/p>\n        <button id=\"playButton\">Play Melody<\/button>\n    <\/div>\n\n    <script>\n        const TARGET_URL = \"https:\/\/httpbin.org\/post\"; \/\/ Dummy endpoint for testing\n        const statusMessage = document.getElementById(\"statusMessage\");\n        const playButton = document.getElementById(\"playButton\");\n\n        \/\/ Define the melody (frequency in Hz and duration in milliseconds)\n        const melody = [\n            { frequency: 392, duration: 800 }, \/\/ G4\n            { frequency: 440, duration: 800 }, \/\/ A4\n            { frequency: 494, duration: 800 }, \/\/ B4\n            { frequency: 523, duration: 800 }, \/\/ C5\n            { frequency: 494, duration: 800 }, \/\/ B4\n            { frequency: 440, duration: 800 }, \/\/ A4\n            { frequency: 392, duration: 800 }, \/\/ G4\n            { frequency: 349, duration: 1200 }, \/\/ F4\n        ];\n\n        \/\/ Generate a sinusoidal data block for a specific frequency and size\n        function generateSineWaveData(frequency, sizeInBytes) {\n            const data = new Uint8Array(sizeInBytes);\n            const sampleRate = 44100; \/\/ Standard audio sample rate (44.1 kHz)\n            const samplesPerCycle = Math.floor(sampleRate \/ frequency);\n\n            for (let i = 0; i < sizeInBytes; i++) {\n                \/\/ Generate a smooth sinusoidal wave\n                const angle = (2 * Math.PI * (i % samplesPerCycle)) \/ samplesPerCycle;\n                data[i] = Math.round(128 + 127 * Math.sin(angle)); \/\/ Map sine value to 0-255\n            }\n\n            return data;\n        }\n\n        \/\/ Send sinusoidal data continuously for a given note via LEctenna\n        async function playNoteViaLEctenna(frequency, duration) {\n            const dataBlockSize = 256 * 1024; \/\/ Smaller block size for continuous transmission\n            const dataBlock = generateSineWaveData(frequency, dataBlockSize);\n            const endTime = performance.now() + duration;\n\n            while (performance.now() < endTime) {\n                try {\n                    await fetch(TARGET_URL, {\n                        method: \"POST\",\n                        body: dataBlock,\n                        headers: {\n                            \"Content-Type\": \"application\/octet-stream\",\n                        },\n                    });\n                } catch (error) {\n                    console.error(\"Error during transmission:\", error);\n                    statusMessage.textContent = \"Status: Error. Retrying...\";\n                }\n            }\n        }\n\n        \/\/ Play the melody through the Web Audio API\n        function playMelodyViaSound() {\n            const audioContext = new (window.AudioContext || window.webkitAudioContext)();\n            let currentTime = audioContext.currentTime;\n\n            melody.forEach(note => {\n                const oscillator = audioContext.createOscillator();\n                oscillator.type = \"sine\"; \/\/ Sine wave for a smooth sound\n                oscillator.frequency.setValueAtTime(note.frequency, currentTime);\n                oscillator.connect(audioContext.destination);\n                oscillator.start(currentTime);\n                oscillator.stop(currentTime + note.duration \/ 1000);\n\n                currentTime += note.duration \/ 1000; \/\/ Update current time for next note\n            });\n        }\n\n        \/\/ Function to play the melody via both Web Audio API and LEctenna\n        async function playFullMelody() {\n            \/\/ Start playing via speakers (Web Audio API)\n            playMelodyViaSound();\n\n            \/\/ Send the melody to LEctenna for data transmission\n            for (const note of melody) {\n                statusMessage.textContent = `Playing ${note.frequency} Hz for ${note.duration} ms via LEctenna`;\n                await playNoteViaLEctenna(note.frequency, note.duration);\n            }\n\n            statusMessage.textContent = \"Melody finished. Restarting...\";\n        }\n\n        \/\/ Play the melody when the button is clicked\n        playButton.addEventListener(\"click\", () => {\n            playFullMelody();\n        });\n    <\/script>\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>LEctenna Full Melody with Sound LEctenna Full Melody with Sound Click the button to play the melody through the speaker and via LEctenna. Status: Preparing&#8230; Play Melody<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-362","page","type-page","status-publish","hentry","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/pages\/362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":6,"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/pages\/362\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/pages\/362\/revisions\/370"}],"wp:attachment":[{"href":"https:\/\/lectenna.com\/index.php\/wp-json\/wp\/v2\/media?parent=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}