ddf / Minim

A Java audio library, designed to be used with Processing.
http://code.compartmental.net/tools/minim
GNU Lesser General Public License v3.0
670 stars 137 forks source link

Any reason to change bufferSize of audioOutput dynamically? #107

Closed ffd8 closed 4 years ago

ffd8 commented 4 years ago

Curious if it's possible (or even necessary/useful) to change the bufferSize of an audioOutput stream? Specifically in the workflow of wavetable synthesis:

Workflow:

The audioOutput is initiated with a specific bufferSize to use – which presumably should be the same size of the float[] for updating the wavetable's waveform (though smaller work fine and seem to get interpolated). If one changes the size of that waveform to be half or double the length... what exactly happens? Does it get squished up/down to fit the bufferSize of the audioOutput, or just send double/half the amount of info at a time per sample out?

I surely need to read more about how all of this works on a technical level.. but wondering why one wouldn't want to adjust it after creating it? Secondly, since it's based on the computers own performance... how should one best test this on a laptop vs RaspberryPi, to know whether it should be 512, 1024, 2048 etc..?

ffd8 commented 4 years ago

Sorry - this was dumb.. just realized one can re-initialize the getLineOut() function and overwrite it with a new bufferSize(). However, can this be dynamically done without issues? Or do the oscil/pan()/summer() etc all need to be un-patched then re-patched to the audioOutput?

I suppose the bufferSize + sampleRate are all dependent on the hardware and DAC one is using, so probably hard to give any suggestions. Would be very interested if Minim has a technique for testing this out to find the optimal one for a given setup.

ddf commented 4 years ago

Yeah the Line Out is comparable to the audio output settings in your DAW. Usually you set and forget those and use the highest sample rate and lowest buffer size (ie latency) your particular machine / audio interface can handle. Generally you wouldn't make more than one AudioOutput or change buffer size / sample rate later. As for choosing the best values based on platform, it should be the case the getLineOut() without arguments will do this, but you can always test with arguments on your target platform to find the highest sample rate and smallest buffer size that don't generate clicks and breaks in audio due to not being able to generate fast enough. But that will also depend on how large of synthesis network you have patched to the output.

The AudioOutput buffer size absolutely does not have to be the same size as the wavetable used by Oscils patched to it. Even though the output presents a buffer of samples at a time, it generates that buffer one sample at a time. And as for the Oscil, how quickly it steps through its wavetable depends on the frequency it is oscillating at, so it's basically never the case that there is a 1:1 mapping between samples in the wavetable and samples generated by the AudioOutput.

ffd8 commented 4 years ago

Aha, good to know regarding set-it-and-forget-it.. but technically there shouldn't be an issue (with oscil, etc patched in) when overriding the getLineOut() by calling it a 2nd/3rd time (if changing these settings, say in the setup() of Processing) after it's already been initialized?

Just did a test, changing my output device (both using SoundFlower + MBP Speakers) from 44100 to 96000 samplerate in Audio + Midi Setup, however using default getLineOut() didn't seem to adjust to that hardware sample rate when checking the out.sampleRate(). I saw the defaults are 1024/44100, but technically this should change? Or only when actual hardware DAC is being used? I do feel like I've seen it automatically use 96000 on a MOTU device.

import ddf.minim.*;  
Minim minim;
AudioOutput out;

void setup() { 
  size(500, 500);
  minim = new Minim(this);
  out = minim.getLineOut(Minim.STEREO);
  println(out.sampleRate()); // 44100 (hardware set to 96000)
  println(out.bufferSize()); // 1024
} 
ddf commented 4 years ago

Ah, you're right and I misremembered, calling getLineOut() defaults to 1024/44100. If for some reason that's not available on the default output mixer in Javasound, I think it will hand back something different, which might be why you've seen it automatically use 96000 on a MOTU device.