Skip to content

Instantly share code, notes, and snippets.

View vitaliy-bobrov's full-sized avatar

Vitalii Bobrov vitaliy-bobrov

View GitHub Profile
<input
class="gain-control"
type="range"
min="0"
max="1"
step="0.01"
value="0.5">
// Setting target value (1st argument) starting from
// the current time in 0.01 second period
gainNode.gain.setTargetAtTime(2, context.currentTime, 0.01);
// Doing the same but exponentially.
gainNode.gain.exponentialRampToValueAtTime(gain, context.currentTime + 0.01);
// Create a gain node and set the initial value to 0.5
// that means that volume will be haft of the original.
const gainNode = new GainNode(context, {gain: 0.5});
// Disconnect source before constructing a new graph.
lineInSource.disconnect();
// Connect nodes
lineInSource.connect(gainNode).connect(context.destination);
lineInSource.disconnect();
const context = new AudioContext();
if (context.state === 'suspended') {
await context.resume();
}
const stream = await navigator.mediaDevices
.getUserMedia({
audio: {
echoCancellation: false,
const lineInSource = context.createMediaStreamSource(stream);
lineInSource.connect(context.destination);
// Getting permission status.
const micStatus = await navigator.permissions.query({name: 'microphone'});
console.log(micStatus); // state: "prompt"
// Reset permission to initial state.
await navigator.permissions.revoke({name: 'microphone'});
const stream = await navigator.mediaDevices
.getUserMedia({
audio: {
echoCancellation: false,
autoGainControl: false,
noiseSuppression: false,
latency: 0
}
});
const stream = await navigator.mediaDevices
.getUserMedia({audio: true});
if (context.state === 'suspended') {
await context.resume();
}