Repository navigation
Expand file tree
/
Copy pathbinary-spectogram.html
More file actions
115 lines (97 loc) · 3.62 KB
/
Copy pathbinary-spectogram.html
File metadata and controls
115 lines (97 loc) · 3.62 KB
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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>binary spectogram</title>
<style>
td {
border: 1px solid black;
min-width: 150px;
min-height: 250px;
}
</style>
</head>
<body>
<table>
<tbody>
<tr><td>Open the Console and type "convert(" binary (8-bit) ")"</td></tr>
</tbody>
</table>
<script>
const tbody = document.querySelector('tbody');
let bit = 8;
let audioContext = null;
let audioReadyPromise = null;
function getAudioContext() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
}
return audioContext;
}
function ensureAudioReady() {
const ctx = getAudioContext();
if (ctx.state === 'suspended') {
if (!audioReadyPromise) {
audioReadyPromise = ctx.resume().catch(() => {});
}
return audioReadyPromise;
}
return Promise.resolve();
}
function scheduleBit(bitValue, startTime) {
const ctx = getAudioContext();
const oscillator = ctx.createOscillator();
const gainNode = ctx.createGain();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(440, startTime);
gainNode.gain.setValueAtTime(bitValue === 1 ? 0.7 : 0.0, startTime);
gainNode.gain.exponentialRampToValueAtTime(0.0001, startTime + 0.12);
oscillator.connect(gainNode);
gainNode.connect(ctx.destination);
oscillator.start(startTime);
oscillator.stop(startTime + 0.12);
}
function convert(text) {
tbody.innerHTML = '';
const data = text.match(new RegExp(`.{1,${bit}}`, 'g')) || [];
const rowDelayMs = 700;
const bitDelayMs = 0.12;
data.forEach((chunk, rowIndex) => {
let row = `<tr id="row_${rowIndex + 1}">`;
const bitValues = [];
chunk.split('').forEach((el) => {
let color = 'black';
let bgColor = 'white';
bitValues.push(el);
if (el === '1') {
color = 'white';
bgColor = 'black';
}
row += `<td style="color: ${color}; background-color: ${bgColor}">${el}</td>`;
});
row += '</tr>';
setTimeout(() => {
tbody.insertAdjacentHTML('beforeend', row);
const lastRow = tbody.querySelector('tr:last-child');
if (lastRow) {
lastRow.scrollIntoView({ behavior: 'smooth', block: 'end' });
}
ensureAudioReady().then(() => {
const startTime = getAudioContext().currentTime + 0.05;
bitValues.forEach((bin, index) => {
scheduleBit(Number(bin), startTime + index * bitDelayMs);
});
});
}, rowDelayMs * (rowIndex + 1));
});
}
window.addEventListener('pointerdown', () => {
ensureAudioReady();
}, { once: true });
// Example:
// convert('101010101010');
// Idea, convert funtion: TaMinoDev, Sound things: Gemini
</script>
</body>
</html>