Spaces:
Running
Running
Doubleupai
commited on
Commit
•
33c3286
1
Parent(s):
457772d
Create script.js
Browse files
script.js
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
2 |
+
const oscillator = audioContext.createOscillator();
|
3 |
+
const gainNode = audioContext.createGain();
|
4 |
+
|
5 |
+
oscillator.connect(gainNode);
|
6 |
+
gainNode.connect(audioContext.destination);
|
7 |
+
oscillator.start();
|
8 |
+
|
9 |
+
function playNote(note) {
|
10 |
+
const noteFrequency = getNoteFrequency(note);
|
11 |
+
oscillator.frequency.setValueAtTime(noteFrequency, audioContext.currentTime);
|
12 |
+
gainNode.gain.setValueAtTime(1, audioContext.currentTime);
|
13 |
+
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 1);
|
14 |
+
}
|
15 |
+
|
16 |
+
function getNoteFrequency(note) {
|
17 |
+
const noteFrequencies = {
|
18 |
+
'C4': 261.63,
|
19 |
+
'C#4': 277.18,
|
20 |
+
'D4': 293.66,
|
21 |
+
'D#4': 311.13,
|
22 |
+
'E4': 329.63,
|
23 |
+
'F4': 349.23,
|
24 |
+
'F#4': 369.99,
|
25 |
+
'G4': 392.00,
|
26 |
+
'G#4': 415.30,
|
27 |
+
'A4': 440.00,
|
28 |
+
'A#4': 466.16,
|
29 |
+
'B4': 493.88
|
30 |
+
};
|
31 |
+
return noteFrequencies[note];
|
32 |
+
}
|
33 |
+
|
34 |
+
const pianoKeys = document.querySelectorAll('.key');
|
35 |
+
pianoKeys.forEach(key => {
|
36 |
+
key.addEventListener('click', () => {
|
37 |
+
const note = key.getAttribute('data-note');
|
38 |
+
playNote(note);
|
39 |
+
});
|
40 |
+
});
|
41 |
+
|
42 |
+
const drumButtons = document.querySelectorAll('.drum-button');
|
43 |
+
drumButtons.forEach(button => {
|
44 |
+
button.addEventListener('click', () => {
|
45 |
+
const sound = button.getAttribute('data-sound');
|
46 |
+
playSound(`sounds/drums/${sound}.mp3`);
|
47 |
+
});
|
48 |
+
});
|
49 |
+
|
50 |
+
const pluckButtons = document.querySelectorAll('.pluck-button');
|
51 |
+
pluckButtons.forEach(button => {
|
52 |
+
button.addEventListener('click', () => {
|
53 |
+
const sound = button.getAttribute('data-sound');
|
54 |
+
playSound(`sounds/pluck/${sound}.mp3`);
|
55 |
+
});
|
56 |
+
});
|
57 |
+
|
58 |
+
const voiceButtons = document.querySelectorAll('.voice-button');
|
59 |
+
voiceButtons.forEach(button => {
|
60 |
+
button.addEventListener('click', () => {
|
61 |
+
const sound = button.getAttribute('data-sound');
|
62 |
+
playSound(`sounds/voice/${sound}.mp3`);
|
63 |
+
});
|
64 |
+
});
|
65 |
+
|
66 |
+
const guitarButtons = document.querySelectorAll('.guitar-button');
|
67 |
+
guitarButtons.forEach(button => {
|
68 |
+
button.addEventListener('click', () => {
|
69 |
+
const sound = button.getAttribute('data-sound');
|
70 |
+
playSound(`sounds/guitar/${sound}.mp3`);
|
71 |
+
});
|
72 |
+
});
|
73 |
+
|
74 |
+
const saxophoneButtons = document.querySelectorAll('.saxophone-button');
|
75 |
+
saxophoneButtons.forEach(button => {
|
76 |
+
button.addEventListener('click', () => {
|
77 |
+
const sound = button.getAttribute('data-sound');
|
78 |
+
playSound(`sounds/saxophone/${sound}.mp3`);
|
79 |
+
});
|
80 |
+
});
|
81 |
+
|
82 |
+
const bit8Buttons = document.querySelectorAll('.8bit-button');
|
83 |
+
bit8Buttons.forEach(button => {
|
84 |
+
button.addEventListener('click', () => {
|
85 |
+
const sound = button.getAttribute('data-sound');
|
86 |
+
playSound(`sounds/8bit/${sound}.mp3`);
|
87 |
+
});
|
88 |
+
});
|
89 |
+
|
90 |
+
const bit16Buttons = document.querySelectorAll('.16bit-button');
|
91 |
+
bit16Buttons.forEach(button => {
|
92 |
+
button.addEventListener('click', () => {
|
93 |
+
const sound = button.getAttribute('data-sound');
|
94 |
+
playSound(`sounds/16bit/${sound}.mp3`);
|
95 |
+
});
|
96 |
+
});
|
97 |
+
|
98 |
+
const genreButtons = document.querySelectorAll('.genre-button');
|
99 |
+
genreButtons.forEach(button => {
|
100 |
+
button.addEventListener('click', () => {
|
101 |
+
const genre = button.getAttribute('data-genre');
|
102 |
+
applyGenre(genre);
|
103 |
+
});
|
104 |
+
});
|
105 |
+
|
106 |
+
const loopButtons = document.querySelectorAll('.loop-button');
|
107 |
+
loopButtons.forEach(button => {
|
108 |
+
button.addEventListener('click', () => {
|
109 |
+
const loop = button.getAttribute('data-loop');
|
110 |
+
playLoop(`sounds/loops/${loop}.mp3`);
|
111 |
+
});
|
112 |
+
});
|
113 |
+
|
114 |
+
const fxButtons = document.querySelectorAll('.fx-button');
|
115 |
+
fxButtons.forEach(button => {
|
116 |
+
button.addEventListener('click', () => {
|
117 |
+
const fx = button.getAttribute('data-fx');
|
118 |
+
applyFX(`sounds/fx/${fx}.mp3`);
|
119 |
+
});
|
120 |
+
});
|
121 |
+
|
122 |
+
function playSound(soundFile) {
|
123 |
+
const audio = new Audio(soundFile);
|
124 |
+
audio.play();
|
125 |
+
}
|
126 |
+
|
127 |
+
function playLoop(loopFile) {
|
128 |
+
const audio = new Audio(loopFile);
|
129 |
+
audio.loop = true;
|
130 |
+
audio.play();
|
131 |
+
}
|
132 |
+
|
133 |
+
function applyFX(fxFile) {
|
134 |
+
const audio = new Audio(fxFile);
|
135 |
+
audio.play();
|
136 |
+
}
|
137 |
+
|
138 |
+
function applyGenre(genre) {
|
139 |
+
// Apply genre effects (e.g., change tempo, pitch, etc.)
|
140 |
+
console.log(`Applying ${genre} genre effects...`);
|
141 |
+
}
|
142 |
+
|
143 |
+
let isRecording = false;
|
144 |
+
let recordedNotes = [];
|
145 |
+
|
146 |
+
document.getElementById('record-button').addEventListener('click', () => {
|
147 |
+
isRecording = true;
|
148 |
+
recordedNotes = [];
|
149 |
+
document.getElementById('record-button').disabled = true;
|
150 |
+
document.getElementById('stop-button').disabled = false;
|
151 |
+
});
|
152 |
+
|
153 |
+
document.getElementById('stop-button').addEventListener('click', () => {
|
154 |
+
isRecording = false;
|
155 |
+
document.getElementById('record-button').disabled = false;
|
156 |
+
document.getElementById('stop-button').disabled = true;
|
157 |
+
document.getElementById('play-button').disabled = false;
|
158 |
+
});
|
159 |
+
|
160 |
+
document.getElementById('play-button').addEventListener('click', () => {
|
161 |
+
recordedNotes.forEach((note, index) => {
|
162 |
+
setTimeout(() => playNote(note), index * 500);
|
163 |
+
});
|
164 |
+
});
|
165 |
+
|
166 |
+
pianoKeys.forEach(key => {
|
167 |
+
key.addEventListener('click', () => {
|
168 |
+
const note = key.getAttribute('data-note');
|
169 |
+
if (isRecording) {
|
170 |
+
recordedNotes.push(note);
|
171 |
+
}
|
172 |
+
});
|
173 |
+
});
|
174 |
+
|
175 |
+
document.getElementById('audio-file').addEventListener('change', (event) => {
|
176 |
+
const file = event.target.files[0];
|
177 |
+
if (file) {
|
178 |
+
document.getElementById('play-file-button').disabled = false;
|
179 |
+
}
|
180 |
+
});
|
181 |
+
|
182 |
+
document.getElementById('play-file-button').addEventListener('click', () => {
|
183 |
+
const file = document.getElementById('audio-file').files[0];
|
184 |
+
if (file) {
|
185 |
+
const audio = new Audio(URL.createObjectURL(file));
|
186 |
+
audio.play();
|
187 |
+
}
|
188 |
+
});
|