Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've written a simple ADSR synth, allowing the user to enter the parameters for an instrument that will then be synthesized, making it's data available for visualization or playback. I also used 44100 hz.</p> <p>The key to getting a good looking image, is to realize that you don't want to plot just once for each horizontal pixel, but rather once for each sample. In the case of say a 0.2s sample and a 256px wide canvas, you'll need to fit 8,820 samples into 256 pixels - ~34 samples per pixel. It seems like overkill, but it's the only way to get a visualization that doesn't miss data. It's also the way to get an image that resembles those found in sound-editing programs, eg Audacity, Milkytracker etc, etc.</p> <p>Here's a hi-hat drum, Fmax > 10,000 hz <img src="https://i.stack.imgur.com/wxkgA.png" alt="Visualization of a hi-hat drum - Fmax = &gt; 10,000 hz"></p> <p><strong>EDIT:</strong> image added to show that calling closePath does not add a line from the end-point back to the starting point.</p> <p><img src="https://i.stack.imgur.com/G6DJ9.png" alt="enter image description here"></p> <p>Here's the visualization code I use:</p> <pre><code>function drawFloatArray(samples, canvas) { var i, n = samples.length; var dur = (n / 44100 * 1000)&gt;&gt;0; canvas.title = 'Duration: ' + dur / 1000.0 + 's'; var width=canvas.width,height=canvas.height; var ctx = canvas.getContext('2d'); ctx.strokeStyle = 'yellow'; ctx.fillStyle = '#303030'; ctx.fillRect(0,0,width,height); ctx.beginPath(); ctx.moveTo(0,height/2); for (i=0; i&lt;n; i++) { x = ( (i*width) / n); y = ((samples[i]*height/2)+height/2); ctx.lineTo(x, y); } ctx.stroke(); ctx.closePath(); canvas.mBuffer = samples; canvas.onclick = function() { playSound(this.mBuffer, 44100, 50); }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload