gibber-cc / gibberish

Fast, JavaScript DSP library that creates JIT optimized audio callbacks using code generation techniques
387 stars 35 forks source link

audio buffer #2

Closed metaphorz closed 10 years ago

metaphorz commented 10 years ago

Is there a way to create my own arbitrary sample without using a .WAV file? For example, let's say we are looking at a 44.1Khz sample rate for 2 seconds, yielding an rray of length 88200. I'd like to be able to fill this array with data computed elsewhere and then use it as an "oscillator" or a sample in the Sampler. Thanks - new at this library, and not sure if this is the appropriate place to submit questions.

charlieroberts commented 10 years ago

There's a Wavetable oscillator (most of the raw oscillators used this behind the scenes) that uses a window size of 1024 samples... you can fill it with whatever 1024 samples you want using the setTable() method. Take a look at the Sine class to see how that would look.

You can also set the buffer of the Sampler object using setBuffer(), but you'd then need to also set the bufferLength property as well. I'll try to clean that up tomorrow so that setBuffer does everything needed to use the samples... I'm a little surprised I hadn't already done this.

Maybe I can also make a little wavetable example... I've been meaning to do that for a while. Stay tuned. - Charlie

charlieroberts commented 10 years ago

PS - this is a great place to ask questions :)

metaphorz commented 10 years ago

Thanks- a wavetable example or appearing in Gibberish Reference (as a method) would be useful, and/or the sampler approach. Right now, the documented API doesn't seem to surface these.

charlieroberts commented 10 years ago

OK, I added a wavetable example and the associated reference material. Let me know how it goes.

metaphorz commented 10 years ago

Hmm..I cannot see the reference material (no example in index.html) called "wavetable". I saw the added code for WaveTable, and tried the following without any luck. I doubt if I have the right connection set up. I borrowed the code in Sine() to fill a table.

var pi_2 = Math.PI * 2; table = new Float32Array(1024);
for(var i = 1024; i--;) { table[i] = Math.sin( (i / 1024) * pi_2); } wave_table = new Gibberish.Wavetable(); wave_table.setTable(table); wave_table.connect();

metaphorz commented 10 years ago

I did a modification that appears to work, although is a lot of code it seems to set up a wavetable audio node. Is it all required? I ended up just copying the text from the Sine() oscillator:

var pi_2 = Math.PI * 2; table = new Float32Array(1024);
for(var i = 1024; i--;) { table[i] = Math.sin( (i / 1024) * pi_2); } wave_table = new Gibberish.Wavetable(); wave_table.setTable(table); wave_table.init( arguments ); wave_table.oscillatorInit(); wave_table.processProperties( arguments ); wave_table.connect();

charlieroberts commented 10 years ago

I haven't changed the online version yet, but the repository seems to be updated correctly...

charlieroberts commented 10 years ago

FYI, all I basically did was create a Table class that does all the housekeeping (like you did in your comment above) for you. You could actually just create a Sine object and call setTable on it and it would work the exact same way, as the Sine class has a Wavetable prototype.

metaphorz commented 10 years ago

I suppose my question was prompted by "OK, I added a wavetable example and the associated reference material. Let me know how it goes."

Where is the example and reference material? I may well be looking entirely in the wrong place. All I could find was the raw code track changes.

charlieroberts commented 10 years ago

Are you cloning / pulling the repo? If you look at line 70 here:

https://github.com/charlieroberts/Gibberish/blob/master/index.html

... there is definitely a wavetable example. I haven't posted this stuff to charlie-roberts.com yet. The docs should be in the repo as well, although I can't point to a line number as the documentation is minified.

metaphorz commented 10 years ago

You are right - I was cloning, or pulling, the wrong code. I see it now.