jussi-kalliokoski/audiolib.js

Help! Somethings trouncing me buffers!

Closed this issue · 7 comments

Hi there.

Been using your lib and in particular your Pluck plugin. Everything was going swimmingly. Then I started making a few mods to Pluck and all was well for a bit. Suddenly I'm getting nowt but pops! (distortion, clipping, no semblance of a 'note') . So I thought, oh dear I've made a booboo somewhere. Tried loads of stuff. To no avail. I went back to original untouched Pluck. Same!

Something is trouncing the buffer after the 'note' function finishes (it'll be all full of 8.1253612 or similar i.e NOT audio)

Now here's the funny thing. If I put a breakpoint (in Chrome) right at the end of the 'note' function. I can hear my note! (after the breakpoint is hit, that is, well at least to my perceptions). So with this breakpoint in place, the buffer survives. Without it: scary speaker pops.

Any ideas?

I'd be glad to help, but I need to see some code. Not knowing what you've modified in the Pluck makes it hard to tell what's the problem, I'm not a psychic (yet) after all. :D

I'm using the original Pluck. Sorry for confusion. I thought it was something to do with my modifications, so I reverted back to the original. It's something going on elsewhere in my app that's causing the issue. I never attempted to post code as I've no idea where the issue is! So, just to be clear: Pluck is unmodified.

My internet has actually exploded so I'm in the library, but my under-development site can be found at http://fretweb.co.uk

click either the "black dog" or "bendtest" button to load a track, then press one of the play buttons under the fretboard. Until recently this would play notes. Currently just getting a pop at audio start and finish. All javascript should be uncompressed, with the action starting in fretboard.js which sends events through audio_audiolib.js which calls Pluck.

I've a feeling it's something to do with my javascript naivety (so, sorry for dodgy noob JS!)

As I say, I'm in the bleedin library, not at my machine. Hopefully you may not mind pointing your debugger at my site, if not I'll get back to you when my interknocker is fixed.

Cheers!

Right, got me some Mobile Broadband...

So, as I said, I'm using the unmodified Pluck. My app is a rudimentary sequenced guitar synth Here is the calling code. I use 6 Plucks: 1 for each string on the guitar. This was working fine. PlayNote gets called whenever needed. Now something I do not understand is stomping on the buffer used inside Pluck. I just looked and the buffer is full of -1.2832028954190643e-19 and very similar. But it's hard to debug as the audio thread/worker does not stop.

And as I mentioned in my first post, if I put a breakpoint right at the end of the 'note' function, I can hear the first note, but then it just goes to the odd pop again.

I am baffled.

var channels, sample;

var AudioLibApp = function () {
    this.channels = new Array(6);
    channels = this.channels;
};


var audioProcess = function (buffer, channelCount) {
    sample = 0;
    for (var i = 0; i < buffer.length; i += channelCount) {
        for (var j = 0; j < 6; ++j) {
            channels[j].generate();
            sample += channels[j].getMix();
        }
        sample /= 6.0;
        for (var n = 0; n < channelCount; n++) {
            buffer[i + n] = sample;
        }
    }
};

AudioLibApp.prototype.stop = function () {
    this.dev.kill();
    this.dev = null;
};

AudioLibApp.prototype.start = function () {
    this.dev = audioLib.AudioDevice(audioProcess, 2);
    for (var i = 0; i < 6; ++i) {
        this.channels[i] = audioLib.Pluck(this.dev.sampleRate, 0.3, 1.0, "brown");
    }
};

AudioLibApp.prototype.playNote = function( frequency, string, velocity ) {
    this.channels[string].note(frequency[0]);
    this.channels[string].amp = velocity;
};

Alright then, the problem is that you're setting Pluck's amp value to undefined which causes it to output NaN.

A quick fix would be to change (in playNote())

this.channels[string].amp = velocity;

to:

this.channels[string].amp = typeof velocity === 'undefined' ? 1.0 : velocity;

But another way to fix it is to always pass a valid velocity to the function. :)

Doh! How on earth did I miss that?! Well I suspected my Javascript naivety and I was right. I'm not used to being able to call functions without all parameters filled in.

Cheers and sorry for bothering you with my stupidity!

Heh, no problem, reaching out is always good! And that wasn't stupidity, I make that same mistake (in different forms) all the time, and that's why it was so easy for me to spot it in your code. ^^

Take care!

All good. Project back on track...

I can get back to my modified Pluck now.

I was wanting to ask you, how you would go about approximating a guitar bend? I say approximating because I'm not writing an actual guitar synth performance tool. It's a teaching tool so it only needs to get the general idea across. The source data (Guitar Pro files) provides "bend points". What I'm doing for now is providing my modified Pluck with an array of frequencies representing said bend points. It creates a buffer for each bend point/frequency. The generate function updates all these buffers but the output value is only taken from the "current" buffer/bendpoint/frequency. This current buffer-to-use is updated in real time. So the frequency jumps rather than bends, which is OK for now.

Just curious how you'd maybe go about it?