File size: 7,459 Bytes
a4cc52f
e4cfe31
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
f7a05cc
7d44b25
 
 
 
 
a4cc52f
7d44b25
a4cc52f
7d44b25
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
a4cc52f
7d44b25
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
7d44b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4cc52f
f7a05cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
    <title>Широкополосный FM и ELF Demodulator</title>
    <style>
        body { font-family: Arial, sans-serif; }
        #controls { margin-bottom: 20px; }
    </style>
</head>
<body>
    <div id="controls">
        <label for="frequencySlider">Частота (Hz): </label>
        <input type="range" id="frequencySlider" min="3" max="20000" step="1" value="1000">
        <span id="frequencyValue">1000</span> Hz
        <button id="toggleAudio">Включить звук</button>
        <button id="toggleELF">Включить ELF режим</button>
    </div>
    <canvas id="elfCanvas" width="800" height="150" style="border:1px solid #000000;"></canvas>
    <script>
let audioContext;
let analyser;
let microphone;
let canvas;
let canvasCtx;
let scriptProcessor;
let prevPhase = 0;
let demodulatedSignal = [];
let gainNode;
let isAudioEnabled = false;
let isELFMode = false;
let centerFrequency = 1000; // Hz
let elfBuffer = [];
let elfCanvas;
let elfCtx;

function startAudioAnalysis() {
    audioContext = new (window.AudioContext || window.webkitAudioContext)();
    analyser = audioContext.createAnalyser();
    analyser.fftSize = 2048;
    
    scriptProcessor = audioContext.createScriptProcessor(1024, 1, 1);
    gainNode = audioContext.createGain();
    gainNode.gain.setValueAtTime(0, audioContext.currentTime);
    
    navigator.mediaDevices.getUserMedia({ audio: true, video: false })
        .then(function(stream) {
            microphone = audioContext.createMediaStreamSource(stream);
            microphone.connect(analyser);
            analyser.connect(scriptProcessor);
            scriptProcessor.connect(gainNode);
            gainNode.connect(audioContext.destination);
            
            canvas = document.createElement('canvas');
            canvas.width = 800;
            canvas.height = 300;
            document.body.appendChild(canvas);
            canvasCtx = canvas.getContext('2d');
            
            elfCanvas = document.getElementById('elfCanvas');
            elfCtx = elfCanvas.getContext('2d');
            
            drawSpectrum();
        })
        .catch(function(err) {
            console.error('Ошибка доступа к микрофону:', err);
        });
        
    scriptProcessor.onaudioprocess = processAudio;
}

function drawSpectrum() {
    requestAnimationFrame(drawSpectrum);
    
    const bufferLength = analyser.frequencyBinCount;
    const dataArray = new Uint8Array(bufferLength);
    analyser.getByteFrequencyData(dataArray);
    
    canvasCtx.fillStyle = 'rgb(0, 0, 0)';
    canvasCtx.fillRect(0, 0, canvas.width, canvas.height);
    
    const barWidth = (canvas.width / bufferLength) * 2.5;
    let x = 0;
    
    for(let i = 0; i < bufferLength; i++) {
        const barHeight = dataArray[i] / 2;
        
        canvasCtx.fillStyle = `rgb(${barHeight + 100}, 50, 50)`;
        canvasCtx.fillRect(x, canvas.height - barHeight, barWidth, barHeight);
        
        x += barWidth + 1;
    }
    
    // Рисуем демодулированный сигнал
    canvasCtx.strokeStyle = 'rgb(0, 255, 0)';
    canvasCtx.beginPath();
    for(let i = 0; i < demodulatedSignal.length; i++) {
        const x = (i / demodulatedSignal.length) * canvas.width;
        const y = (0.5 - demodulatedSignal[i] / 2) * canvas.height;
        if(i === 0) {
            canvasCtx.moveTo(x, y);
        } else {
            canvasCtx.lineTo(x, y);
        }
    }
    canvasCtx.stroke();
    
    // Рисуем маркер центральной частоты
    const centerX = (centerFrequency / 20000) * canvas.width;
    canvasCtx.strokeStyle = 'rgb(255, 255, 0)';
    canvasCtx.beginPath();
    canvasCtx.moveTo(centerX, 0);
    canvasCtx.lineTo(centerX, canvas.height);
    canvasCtx.stroke();

    // Рисуем ELF сигнал
    if (isELFMode) {
        elfCtx.fillStyle = 'rgb(0, 0, 0)';
        elfCtx.fillRect(0, 0, elfCanvas.width, elfCanvas.height);
        elfCtx.strokeStyle = 'rgb(0, 255, 0)';
        elfCtx.beginPath();
        for(let i = 0; i < elfBuffer.length; i++) {
            const x = (i / elfBuffer.length) * elfCanvas.width;
            const y = (0.5 - elfBuffer[i] / 2) * elfCanvas.height;
            if(i === 0) {
                elfCtx.moveTo(x, y);
            } else {
                elfCtx.lineTo(x, y);
            }
        }
        elfCtx.stroke();
    }
}

function processAudio(audioProcessingEvent) {
    const inputBuffer = audioProcessingEvent.inputBuffer;
    const outputBuffer = audioProcessingEvent.outputBuffer;
    const inputData = inputBuffer.getChannelData(0);
    const outputData = outputBuffer.getChannelData(0);
    
    demodulatedSignal = [];
    
    for(let i = 0; i < inputData.length; i++) {
        const phase = Math.atan2(inputData[i], prevPhase);
        const instantFreq = (phase - prevPhase + Math.PI) % (2 * Math.PI) - Math.PI;
        let demodulated;
        if (isELFMode) {
            demodulated = lowPassFilter(instantFreq);
        } else {
            demodulated = instantFreq * (centerFrequency / 1000);
        }
        demodulatedSignal.push(demodulated);
        outputData[i] = demodulated;
        prevPhase = inputData[i];
    }

    if (isELFMode) {
        elfBuffer = elfBuffer.concat(demodulatedSignal);
        if (elfBuffer.length > 1000) {
            elfBuffer = elfBuffer.slice(-1000);
        }
    }
}

function lowPassFilter(input) {
    // Простой фильтр нижних частот
    const alpha = 0.1;
    this.filtered = (this.filtered || 0) * (1 - alpha) + input * alpha;
    return this.filtered;
}

function toggleAudio() {
    if (isAudioEnabled) {
        gainNode.gain.setValueAtTime(0, audioContext.currentTime);
        document.getElementById('toggleAudio').textContent = 'Включить звук';
    } else {
        gainNode.gain.setValueAtTime(1, audioContext.currentTime);
        document.getElementById('toggleAudio').textContent = 'Выключить звук';
    }
    isAudioEnabled = !isAudioEnabled;
}

function toggleELF() {
    isELFMode = !isELFMode;
    if (isELFMode) {
        document.getElementById('toggleELF').textContent = 'Выключить ELF режим';
        document.getElementById('frequencySlider').min = 3;
        document.getElementById('frequencySlider').max = 30;
        centerFrequency = 15;
        document.getElementById('frequencySlider').value = centerFrequency;
        updateFrequency();
    } else {
        document.getElementById('toggleELF').textContent = 'Включить ELF режим';
        document.getElementById('frequencySlider').min = 3;
        document.getElementById('frequencySlider').max = 20000;
        centerFrequency = 1000;
        document.getElementById('frequencySlider').value = centerFrequency;
        updateFrequency();
    }
    elfBuffer = [];
}

function updateFrequency() {
    centerFrequency = parseFloat(document.getElementById('frequencySlider').value);
    document.getElementById('frequencyValue').textContent = centerFrequency.toFixed(0);
}

window.onload = function() {
    startAudioAnalysis();
    document.getElementById('toggleAudio').addEventListener('click', toggleAudio);
    document.getElementById('toggleELF').addEventListener('click', toggleELF);
    document.getElementById('frequencySlider').addEventListener('input', updateFrequency);
};
    </script>
</body>
</html>