1. Re: Hosting playback module for samples (Ross Bencina)
I'm not really sure that I understand the problem. Can you be more
specific about the problems that you're facing?
Specific: (Forgive me if you know all of this): My code is C#, but
there is no simple way to bridge the gap between the managed realm
and a module that can play orchestral samples.
Most sample banks these days seem to be in NKI format (Native
Instruments). They have the ability to map ranges of a keyboard into
different samples so the timbres don't become munchkin-ized or
Vader-ized. IOW, natural sound within each register.
A playback engine is typically something like Native Instruments'
Kontakt, which is 'hosted' by the main program (my composition
software, for ex). then NI Kontakt can load up NKI files and
deliver sound when it receives events.
The whole process of linking events, etc is what usually what
stymies programmers who are new to VST-based programming. And
even many who are familiar.
Personally I would avoid managed code for anything real-time (ducks).
Actually, C# can be faster than pre-compiled code! The first time a given
section of code runs, the JIT (just in time) compiler replaces the 'IL'
code with highly optimized machine code. Since the JIT module can
test the CPU that it running on, it can optimize for that particular
CPU and chip set. Early on, one of the popular book authors (Richard
Grimes) coded FFT algorithms in both C++ and C#, and C# was faster.
You're need to build a simple audio engine (consider PortAudio or the
ASIO SDK). And write some VSTi hosting code using the VST SDK. It's this
last bit that will require some work. But if you limit yourself to a
small number of supported plugins to begin with it should not be too
hard. MIDI scheduling in a VSTi is not particularly challenging -- the
plugins do the sub-buffer scheduling, you just need to put together a
frame of MIDI events for each audio frame.
That's inspiring. I'm not sure that this is done in the same way as a
regular plugin though. And I believe it's pretty difficult to host a
VSIi in managed code. That is pretty much the crux of the problem right
there. I've heard of a lot of people who started the project but were
never aboe to get it off te ground.
If there's any kind of "synchronisation with the outside world" things
will get trickier, but if you can clock the MIDI time off the
accumulated sample position it's not hard.
I could do without sync to external for now.
... I guess the main
approaches would be to either (A) schedule MIDI events ahead of time
from your C# code and use a priority queue (Knuth Heap is easy and
relatively safe for real-time) in the audio thread to work out when to
schedule them; or (B) maintain the whole MIDI sequence in a vector and
just play through it from the audio thread. Then you need a mechanism to
update the sequence when it changes (just swap in a new one?).
The internals of a VSTi host are beyond me at present. I was hoping
for some simple thing that could be accessed by sending MIDI-like events
to a single queue.
Thanks to both who responded. I've signed up for the PortAudio mailing
list. Maybe they have something.