title: Web Audio API - Cloning an audioBuffer
body: <p>I have a <code>deepClone</code> function that I use throughout my program that clones all the properties and values of one object and returns the cloned object (<em>see code below</em>). I am now wanting to include an <code>audioBuffer</code> in the object and successfully copy it to the returning object. Is there a way to modify function to include this functionality? </p>
  
  <p><strong>TO NOTE: Not all objects passed to the <code>deepClone</code> function will include an <code>audioBuffer</code>, therefore it should be able to cope and successfully clone objects with and without them.</strong></p>
  
  <p><strong>Edit: Can someone think of a way to combine <code>JSON.parse(JSON.stringify(object))</code> with the solution provided as one of the answers? I.e. check properties of the object as the cloning is happening, <code>if (property == 'trackBuffer')</code> clone it differently to the rest?</strong></p>
  
  <p><code>deepClone</code> function:</p>
  
  <pre><code>function deepClone (object) {
     return JSON.parse(JSON.stringify(object));
  };
  </code></pre>
  
  <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>/**
   * Utility function for deep object cloning
   *                                                                                                                                                                               
   * @param   {object} obj  Object to be cloned
   * @returns {object}      The deep-cloned object
   */
  function deepClone (object) {
      return JSON.parse(JSON.stringify(object));
  };
  
  // Create audio context
  var context = new AudioContext(); 
  
  // Create empty audio buffer
  var audioBuffer = context.createBuffer(2, 22050, 44100); 
  
  // Create object to be cloned
  var track = {
    prop1: "val1",
    prop2: "val2",
    trackBuffer: audioBuffer
  };
  
  // Log before clone
  console.log("before clone, track:  \n", track);
  
  // Clone track
  var clonedTrack = deepClone(track);
  
  // Log after clone
  console.log("after clone, clonedTrack: \n", clonedTrack);</code></pre>
  </div>
  </div>
  </p>
  
  <p>As you can see from the code snippet, the <code>clonedTrack</code>'s <code>trackBuffer</code> is converted to an empty object which is to be expected from <code>JSON.parse(JSON.stringify(object));</code>. </p>
  
  <p>What is the best way to modify <code>deepClone</code> to successfully clone the <code>audioBuffer</code> but keep existing functionality?</p>
  
  <p>Any help would be greatly appreciated, thanks!</p>