title: Making byte-beats using WebAudio
body: <p><a href="http://canonical.org/~kragen/bytebeat/" rel="nofollow noreferrer">Byte-beats</a> are a fun way of making lo-fi music. I want to make some music myself using the <strong>WebAudio</strong> API. Here's my current code:</p>
  
  <pre><code>const sampleRate = 8000;
  const frameCount = sampleRate * 5;
  
  const audioCtx = new AudioContext({ sampleRate: sampleRate });
  const src = audioCtx.createBufferSource();
  const buf = audioCtx.createBuffer(1, frameCount, sampleRate);
  
  buf.getChannelData(0).set(buf.getChannelData(0).map((_, t) =&gt; {
      return (Math.sin(t / 10 + Math.sin(t * Math.pow(2, t &gt;&gt; 10)))) * 64 + 128;
  }));
  
  src.buffer = buf;
  src.connect(audioCtx.destination);
  src.start(0, 0, 100);
  
  console.log('Reached the end :/');
  </code></pre>
  
  <p>My issue with this solution is that I've to create an huge buffer which has to be kept in memory. I was hoping that there would be a dynamic way of setting the sound's amplitude to save memory.</p>
  
  <p>The byte-beats will be entire music compositions and can be pretty long. So, the frame counts can become pretty huge.</p>
  
  <p>Can anyone please suggest me how to do this? Using other libraries is an option but I would prefer avoiding that.</p>