ЁЯО╡ Indian Music Instrument (Sa Re Ga Ma)
Sa
Re
Ga
Ma
Pa
Dha
Ni
Sa'
ReтЩн
GaтЩн
Ma#
DhaтЩн
NiтЩн
ЁЯФК Volume:
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
let volumeControl = document.getElementById("volume");
function playSound(freq) {
const oscillator = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
// richer harmonium-like tone
oscillator.type = "triangle";
oscillator.frequency.value = freq;
gainNode.gain.value = volumeControl.value;
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.start();
gainNode.gain.exponentialRampToValueAtTime(
0.0001, audioCtx.currentTime + 1
);
oscillator.stop(audioCtx.currentTime + 1);
recordNote(freq);
}
/* UI Interaction */
const keys = document.querySelectorAll(".key");
keys.forEach(key => {
["click","touchstart"].forEach(evt => {
key.addEventListener(evt, () => {
playSound(key.dataset.note);
key.classList.add("active");
setTimeout(()=>key.classList.remove("active"),150);
});
});
});
/* Keyboard Support */
const keyMap = {
a:261.63, s:293.66, d:329.63, f:349.23,
g:392.00, h:440.00, j:493.88, k:523.25
};
document.addEventListener("keydown", e => {
let freq = keyMap[e.key];
if(freq) playSound(freq);
});
/* Recording Feature */
let recording = [];
let isRecording = false;
let startTime;
function recordNote(freq){
if(isRecording){
recording.push({
freq: freq,
time: Date.now() - startTime
});
}
}
function startRecording(){
recording = [];
isRecording = true;
startTime = Date.now();
alert("Recording started");
}
function stopRecording(){
isRecording = false;
alert("Recording stopped");
}
function playRecording(){
recording.forEach(note => {
setTimeout(()=>{
playSound(note.freq);
}, note.time);
});
}
Discover more from
Subscribe to get the latest posts sent to your email.