kimhyunwoo commited on
Commit
bcaf481
ยท
verified ยท
1 Parent(s): fe818d2

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +128 -18
index.html CHANGED
@@ -1,19 +1,129 @@
1
- <!doctype html>
2
  <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
  <html>
3
+ <head>
4
+ <title>FFT ๋ถ„์„ ํ›„ ์‚ฌ์ธํŒŒ/์ฝ”์‚ฌ์ธํŒŒ ์žฌ์ƒ ์˜ˆ์ œ</title>
5
+ <style>
6
+ body { font-family: sans-serif; text-align: center; margin-top: 50px; }
7
+ button { padding: 10px 20px; font-size: 16px; cursor: pointer; }
8
+ #status { margin-top: 20px; color: gray; }
9
+ </style>
10
+ </head>
11
+ <body>
12
+
13
+ <h1>FFT ๋ถ„์„ ํ›„ ์‚ฌ์ธํŒŒ/์ฝ”์‚ฌ์ธํŒŒ ์žฌ์ƒ</h1>
14
+ <p>๋งˆ์ดํฌ ์ž…๋ ฅ์„ ๋ฐ›์•„ FFT ๋ถ„์„ ํ›„ ์ฃผํŒŒ์ˆ˜ ์„ฑ๋ถ„์œผ๋กœ ์†Œ๋ฆฌ๋ฅผ ์žฌ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.</p>
15
+
16
+ <button id="startButton">๋งˆ์ดํฌ ์ž…๋ ฅ ์‹œ์ž‘ ๋ฐ ์žฌ์ƒ</button>
17
+ <div id="status"></div>
18
+
19
+ <script>
20
+ let audioContext;
21
+ let analyser;
22
+ let source;
23
+ let oscillatorNode; // ์‚ฌ์ธ/์ฝ”์‚ฌ์ธ ํŒŒํ˜•์„ ์ƒ์„ฑํ•  ์˜ค์‹ค๋ ˆ์ดํ„ฐ ๋…ธ๋“œ
24
+
25
+ // FFT ๋ถ„์„ํ•  ์ฃผํŒŒ์ˆ˜ ์„ฑ๋ถ„์˜ ๊ฐœ์ˆ˜ (2์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ)
26
+ const fftSize = 2048;
27
+ const bufferLength = fftSize / 2; // ์ฃผํŒŒ์ˆ˜ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ
28
+
29
+ // FFT ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
30
+ let dataArray = new Uint8Array(bufferLength); // ์ฃผํŒŒ์ˆ˜ ํฌ๊ธฐ (0-255)
31
+
32
+ // ์žฌ์ƒํ•  ์‚ฌ์ธํŒŒ/์ฝ”์‚ฌ์ธํŒŒ์˜ ๊ฐœ์ˆ˜ (FFT ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ๋ช‡ ๊ฐœ์˜ ํŒŒํ˜•์œผ๋กœ ์žฌ๊ตฌ์„ฑํ• ์ง€)
33
+ const numReconstructedOscillators = 50;
34
+
35
+ document.getElementById('startButton').addEventListener('click', async () => {
36
+ try {
37
+ // ์˜ค๋””์˜ค ์ปจํ…์ŠคํŠธ ์ƒ์„ฑ (๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ์„ ์œ„ํ•ด ์ ‘๋‘์‚ฌ ๊ณ ๋ ค)
38
+ audioContext = new (window.AudioContext || window.webkitAudioContext)();
39
+
40
+ // ๋งˆ์ดํฌ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ ๊ฐ€์ ธ์˜ค๊ธฐ
41
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
42
+
43
+ // ๋ฏธ๋””์–ด ์ŠคํŠธ๋ฆผ ์†Œ์Šค ๋…ธ๋“œ ์ƒ์„ฑ
44
+ source = audioContext.createMediaStreamSource(stream);
45
+
46
+ // ๋ถ„์„๊ธฐ ๋…ธ๋“œ ์ƒ์„ฑ
47
+ analyser = audioContext.createAnalyser();
48
+ analyser.fftSize = fftSize; // FFT ์‚ฌ์ด์ฆˆ ์„ค์ •
49
+
50
+ // ๋งˆ์ดํฌ ์†Œ์Šค๋ฅผ ๋ถ„์„๊ธฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ
51
+ source.connect(analyser);
52
+
53
+ // ------------- FFT ๋ถ„์„ ๋ฐ ํŒŒํ˜• ์žฌ๊ตฌ์„ฑ ๋กœ์ง -------------
54
+
55
+ // ์‹ค์‹œ๊ฐ„์œผ๋กœ FFT ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜
56
+ const processAudio = () => {
57
+ // ์ฃผํŒŒ์ˆ˜ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ (ํฌ๊ธฐ)
58
+ analyser.getByteFrequencyData(dataArray);
59
+
60
+ // ์ด์ „ ์˜ค์‹ค๋ ˆ์ดํ„ฐ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์—ฐ๊ฒฐ ํ•ด์ œ ๋ฐ ์‚ญ์ œ
61
+ if (oscillatorNode) {
62
+ oscillatorNode.disconnect();
63
+ oscillatorNode = null;
64
+ }
65
+
66
+ // ์ƒˆ๋กœ์šด ๊ฒŒ์ธ ๋…ธ๋“œ ์ƒ์„ฑ (๋ณผ๋ฅจ ์กฐ์ ˆ)
67
+ const gainNode = audioContext.createGain();
68
+ gainNode.gain.value = 0.5; // ์žฌ์ƒ ๋ณผ๋ฅจ (์กฐ์ ˆ ๊ฐ€๋Šฅ)
69
+
70
+ // ๊ฒŒ์ธ ๋…ธ๋“œ๋ฅผ ์ตœ์ข… ์ถœ๋ ฅ์— ์—ฐ๊ฒฐ
71
+ gainNode.connect(audioContext.destination);
72
+
73
+ // ์˜ค์‹ค๋ ˆ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด
74
+ const oscillators = [];
75
+
76
+ // FFT ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‚ฌ์ธํŒŒ/์ฝ”์‚ฌ์ธํŒŒ ์ƒ์„ฑ (๋‹จ์ˆœํ™”๋œ ์˜ˆ์ œ)
77
+ for (let i = 0; i < numReconstructedOscillators; i++) {
78
+ const oscillator = audioContext.createOscillator();
79
+
80
+ // ์ฃผํŒŒ์ˆ˜ ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์—์„œ ํ•ด๋‹นํ•˜๋Š” ์ฃผํŒŒ์ˆ˜ ์ธ๋ฑ์Šค ๊ณ„์‚ฐ
81
+ const frequencyIndex = Math.min(Math.floor(i * (bufferLength / numReconstructedOscillators)), bufferLength - 1);
82
+
83
+ // ํ•ด๋‹น ์ฃผํŒŒ์ˆ˜ ์„ฑ๋ถ„์˜ ํฌ๊ธฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฃผํŒŒ์ˆ˜ ์„ค์ • (๋‹จ์ˆœํ™”)
84
+ // ์‹ค์ œ๋กœ๋Š” analyser.getByteFrequencyData๋Š” ํฌ๊ธฐ๋งŒ ์ œ๊ณตํ•˜๋ฉฐ, ์ฃผํŒŒ์ˆ˜๋Š” ์ธ๋ฑ์Šค์™€ ์ƒ˜ํ”Œ ๋ ˆ์ดํŠธ๋กœ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
85
+ // ์—ฌ๊ธฐ์„œ์˜ frequency๋Š” ์˜ˆ์‹œ๋ฅผ ์œ„ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฃผํŒŒ์ˆ˜ ์ธ๋ฑ์Šค์™€ ๋น„๋ก€ํ•˜๊ฒŒ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
86
+ const frequency = audioContext.sampleRate / fftSize * frequencyIndex; // ์‹ค์ œ ์ฃผํŒŒ์ˆ˜ ๊ณ„์‚ฐ
87
+
88
+ // ์ฃผํŒŒ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋‚ฎ๊ฑฐ๋‚˜ ๋†’์œผ๋ฉด ์ œ์™ธ (์กฐ์ ˆ ๊ฐ€๋Šฅ)
89
+ if (frequency > 50 && frequency < 15000) {
90
+ oscillator.type = 'sine'; // ๋˜๋Š” 'cosine'
91
+ oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
92
+
93
+ // ๊ฐ ์˜ค์‹ค๋ ˆ์ดํ„ฐ์˜ ๋ณผ๋ฅจ์„ ํ•ด๋‹น ์ฃผํŒŒ์ˆ˜ ์„ฑ๋ถ„์˜ ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ์„ค์ • (๋‹จ์ˆœํ™”)
94
+ const oscillatorGain = audioContext.createGain();
95
+ oscillatorGain.gain.value = dataArray[frequencyIndex] / 255 * 0.1; // ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ๋ณผ๋ฅจ ์กฐ์ ˆ
96
+
97
+ // ์˜ค์‹ค๋ ˆ์ดํ„ฐ๋ฅผ ๊ฒŒ์ธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐํ•˜๊ณ  ๊ฒŒ์ธ ๋…ธ๋“œ๋ฅผ ๋ฉ”์ธ ๊ฒŒ์ธ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ
98
+ oscillator.connect(oscillatorGain);
99
+ oscillatorGain.connect(gainNode);
100
+
101
+ // ์˜ค์‹ค๋ ˆ์ดํ„ฐ ์‹œ์ž‘
102
+ oscillator.start();
103
+
104
+ // ์˜ค์‹ค๋ ˆ์ดํ„ฐ ๋ฐฐ์—ด์— ์ถ”๊ฐ€
105
+ oscillators.push(oscillator);
106
+ }
107
+ }
108
+
109
+ // ์ƒ์„ฑ๋œ ์˜ค์‹ค๋ ˆ์ดํ„ฐ ๋…ธ๋“œ๋ฅผ ์ €์žฅ
110
+ oscillatorNode = oscillators;
111
+
112
+ // ๋‹ค์Œ ํ”„๋ ˆ์ž„์— ๋‹ค์‹œ ๋ถ„์„ ๋ฐ ์žฌ์ƒ ์š”์ฒญ
113
+ requestAnimationFrame(processAudio);
114
+ };
115
+
116
+ // ๋ถ„์„ ๋ฐ ์žฌ์ƒ ์‹œ์ž‘
117
+ processAudio();
118
+
119
+ document.getElementById('status').textContent = '๋งˆ์ดํฌ ์ž…๋ ฅ ๋ฐ ์žฌ์ƒ ์ค‘...';
120
+ } catch (err) {
121
+ console.error('๋งˆ์ดํฌ ์ ‘๊ทผ ์˜ค๋ฅ˜:', err);
122
+ document.getElementById('status').textContent = `์˜ค๋ฅ˜ ๋ฐœ์ƒ: ${err.message}`;
123
+ }
124
+ });
125
+
126
+ </script>
127
+
128
+ </body>
129
+ </html>