Spaces:
Running
Running
File size: 5,626 Bytes
33c3286 |
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 |
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.start();
function playNote(note) {
const noteFrequency = getNoteFrequency(note);
oscillator.frequency.setValueAtTime(noteFrequency, audioContext.currentTime);
gainNode.gain.setValueAtTime(1, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 1);
}
function getNoteFrequency(note) {
const noteFrequencies = {
'C4': 261.63,
'C#4': 277.18,
'D4': 293.66,
'D#4': 311.13,
'E4': 329.63,
'F4': 349.23,
'F#4': 369.99,
'G4': 392.00,
'G#4': 415.30,
'A4': 440.00,
'A#4': 466.16,
'B4': 493.88
};
return noteFrequencies[note];
}
const pianoKeys = document.querySelectorAll('.key');
pianoKeys.forEach(key => {
key.addEventListener('click', () => {
const note = key.getAttribute('data-note');
playNote(note);
});
});
const drumButtons = document.querySelectorAll('.drum-button');
drumButtons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/drums/${sound}.mp3`);
});
});
const pluckButtons = document.querySelectorAll('.pluck-button');
pluckButtons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/pluck/${sound}.mp3`);
});
});
const voiceButtons = document.querySelectorAll('.voice-button');
voiceButtons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/voice/${sound}.mp3`);
});
});
const guitarButtons = document.querySelectorAll('.guitar-button');
guitarButtons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/guitar/${sound}.mp3`);
});
});
const saxophoneButtons = document.querySelectorAll('.saxophone-button');
saxophoneButtons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/saxophone/${sound}.mp3`);
});
});
const bit8Buttons = document.querySelectorAll('.8bit-button');
bit8Buttons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/8bit/${sound}.mp3`);
});
});
const bit16Buttons = document.querySelectorAll('.16bit-button');
bit16Buttons.forEach(button => {
button.addEventListener('click', () => {
const sound = button.getAttribute('data-sound');
playSound(`sounds/16bit/${sound}.mp3`);
});
});
const genreButtons = document.querySelectorAll('.genre-button');
genreButtons.forEach(button => {
button.addEventListener('click', () => {
const genre = button.getAttribute('data-genre');
applyGenre(genre);
});
});
const loopButtons = document.querySelectorAll('.loop-button');
loopButtons.forEach(button => {
button.addEventListener('click', () => {
const loop = button.getAttribute('data-loop');
playLoop(`sounds/loops/${loop}.mp3`);
});
});
const fxButtons = document.querySelectorAll('.fx-button');
fxButtons.forEach(button => {
button.addEventListener('click', () => {
const fx = button.getAttribute('data-fx');
applyFX(`sounds/fx/${fx}.mp3`);
});
});
function playSound(soundFile) {
const audio = new Audio(soundFile);
audio.play();
}
function playLoop(loopFile) {
const audio = new Audio(loopFile);
audio.loop = true;
audio.play();
}
function applyFX(fxFile) {
const audio = new Audio(fxFile);
audio.play();
}
function applyGenre(genre) {
// Apply genre effects (e.g., change tempo, pitch, etc.)
console.log(`Applying ${genre} genre effects...`);
}
let isRecording = false;
let recordedNotes = [];
document.getElementById('record-button').addEventListener('click', () => {
isRecording = true;
recordedNotes = [];
document.getElementById('record-button').disabled = true;
document.getElementById('stop-button').disabled = false;
});
document.getElementById('stop-button').addEventListener('click', () => {
isRecording = false;
document.getElementById('record-button').disabled = false;
document.getElementById('stop-button').disabled = true;
document.getElementById('play-button').disabled = false;
});
document.getElementById('play-button').addEventListener('click', () => {
recordedNotes.forEach((note, index) => {
setTimeout(() => playNote(note), index * 500);
});
});
pianoKeys.forEach(key => {
key.addEventListener('click', () => {
const note = key.getAttribute('data-note');
if (isRecording) {
recordedNotes.push(note);
}
});
});
document.getElementById('audio-file').addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
document.getElementById('play-file-button').disabled = false;
}
});
document.getElementById('play-file-button').addEventListener('click', () => {
const file = document.getElementById('audio-file').files[0];
if (file) {
const audio = new Audio(URL.createObjectURL(file));
audio.play();
}
}); |