3

I have created a oscillator (as shown below), like MDN said:

// from : https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
oscillator.type = 'sine'; // sine wave — other values are 'square', 'sawtooth', 'triangle' and 'custom'
oscillator.frequency.value = 2500; // value in hertz
oscillator.start();

Is there a method to change the volume, like I changed the frequency value?

Stardust
  • 999
  • 3
  • 12
  • 24
  • 1
    Take a look at [this line](https://github.com/mdn/violent-theremin/blob/gh-pages/scripts/app.js#L35) from the source of the example app on that page. You you'd just do something like: `gainNode.gain.value = [volume]`. – Hunan Rostomyan Dec 20 '15 at 18:42
  • @HunanRostomyan, Thank you! That was exactly what I needed. – Stardust Dec 20 '15 at 18:45
  • Great. Np. Might also wanna check this reference article out (it's got nice pictures!): [MDN:GainNode](https://developer.mozilla.org/en-US/docs/Web/API/GainNode). – Hunan Rostomyan Dec 20 '15 at 18:49

2 Answers2

1

You can get a variable frequency control with a slider that controls the output frequency:

document.getElementById('slider99').addEventListener('input', slider99change, false);
function slider99change(e) {
    var x = document.getElementById("slider99");
    var s = x.value.toString();
    oz.frequency.value = s;
}

Your oscillator is defined globally elsewhere:

var ozcontext = new (window.AudioContext || window.webkitAudioContext)();
var oz = ozcontext.createOscillator();
var gainNode = ozcontext.createGain();
oz.connect(gainNode);
gainNode.connect(ozcontext.destination);
gainNode.gain.value = 0;
oz.type = 'sine';
oz.frequency.value = 440;
oz.start();
pollaris
  • 1,281
  • 17
  • 20
0

You need to look at modifying the audioContext and gainNode for volume changes. Following link might help:

https://github.com/mdn/voice-change-o-matic/blob/gh-pages/scripts/app.js

srinivash
  • 1
  • 1