title: How to make a function for create oscillatorNodes using Web Audio Api
body: <p>I know this sounds kind of easy or obvious but I can't make this work.</p>
  
  <p>See this snippet:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>ctx = new AudioContext();
  gain = ctx.createGain();
  gain.connect(ctx.destination);
  gain.gain.value = 0.5;
  
  
  osc1 = ctx.createOscillator();
  osc1.connect(gain);
  osc1.frequency.value = 450;
  osc1.start();
  osc1.stop(2);
  
  osc2 = ctx.createOscillator();
  osc2.connect(gain);
  osc2.frequency.value = 500;
  osc2.start();
  osc2.stop(2);
  console.log("playing");</code></pre>
  </div>
  </div>
  </p>
  
  <p>It plays two Oscillators at the time without problems but it repeats code. If I try to put the redundant code in a function it does not work.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>ctx = new AudioContext();
  gain = ctx.createGain();
  createAndPlayOsc(450);
  createAndPlayOsc(500);
  
  
  function createAndPlayOsc(freq){
      console.log("creating osc with freq " + freq);
      var osc = ctx.createOscillator();
      osc.connect(gain);
      osc.frequency.value = freq;
      osc.start();
      osc.stop(2);
      console.log("playing osc with freq " + freq);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Even if I send the AudioContext</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>ctx = new AudioContext();
  gain = ctx.createGain();
  createAndPlayOsc(450, ctx);
  createAndPlayOsc(500, ctx);
  
  
  function createAndPlayOsc(freq, context){
      console.log("creating osc with freq " + freq);
      var osc = context.createOscillator();
      osc.connect(gain);
      osc.frequency.value = freq;
      osc.start();
      osc.stop(2);
      console.log("playing osc with freq " + freq);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>or Both the gainNode and context</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>ctx = new AudioContext();
  gain = ctx.createGain();
  createAndPlayOsc(450, ctx, gain);
  createAndPlayOsc(500, ctx, gain);
  
  
  function createAndPlayOsc(freq, context, gainNode){
      console.log("creating osc with freq " + freq);
      var osc = context.createOscillator();
      osc.connect(gainNode);
      osc.frequency.value = freq;
      osc.start();
      osc.stop(2);
      console.log("playing osc with freq " + freq);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>What I am missing?</p>