mirror of
https://github.com/thestk/stk
synced 2026-01-11 20:11:52 +00:00
123 lines
4.0 KiB
Plaintext
123 lines
4.0 KiB
Plaintext
/*! \page polyvoices Voice Management
|
|
|
|
The previous tutorial chapters were concerned only with monophonic ToolKit instrument playback and control. At this point, it should be relatively clear that one can instantiate multiple instruments and perhaps sum together their sounds or even direct their sounds to separate output channels. It is less clear how one might go about controlling a group of instruments. The Voicer class is designed to serve just this purpose.
|
|
|
|
The STK Voicer class is a relatively simple voice manager. The user can dynamically add and delete instruments from its "control", with the option of controlling specific instruments via unique note tags and/or grouping sets of instruments via a "channel" number. All sounding instrument outputs are summed and returned via the <TT>tick()</TT> function. The Voicer class responds to noteOn, noteOff, setFrequency, pitchBend, and controlChange messages, automatically assigning incoming messages to the voices in its control. When all voices are sounding and a new noteOn is encountered, the Voicer interrupts the oldest sounding voice. The user is responsible for creating and deleting all instrument instances.
|
|
|
|
In the following example, we modify the <TT>controlbee.cpp</TT> program to make use of three BeeThree instruments, all controlled using a Voicer.
|
|
|
|
\code
|
|
// threebees.cpp
|
|
|
|
#include "BeeThree.h"
|
|
#include "RtWvOut.h"
|
|
#include "Messager.h"
|
|
#include "Voicer.h"
|
|
#include "SKINI.msg"
|
|
|
|
int main()
|
|
{
|
|
// Set the global sample rate before creating class instances.
|
|
Stk::setSampleRate( 44100.0 );
|
|
|
|
int i;
|
|
RtWvOut *output = 0;
|
|
Messager *messager = 0;
|
|
Voicer *voicer = 0;
|
|
bool done = FALSE;
|
|
Instrmnt *instrument[3];
|
|
for ( i=0; i<3; i++ ) instrument[i] = 0;
|
|
|
|
try {
|
|
// Define and load the BeeThree instruments
|
|
for ( i=0; i<3; i++ )
|
|
instrument[i] = new BeeThree();
|
|
|
|
// Define and open the default realtime output device for one-channel playback
|
|
output = new RtWvOut(1);
|
|
}
|
|
catch (StkError &) {
|
|
goto cleanup;
|
|
}
|
|
|
|
try {
|
|
// Create a Messager instance to read from a redirected SKINI scorefile.
|
|
messager = new Messager();
|
|
}
|
|
catch (StkError &) {
|
|
goto cleanup;
|
|
}
|
|
|
|
// Instantiate the voicer for a maximum of three voices.
|
|
voicer = new Voicer( 3 );
|
|
for ( i=0; i<3; i++ )
|
|
voicer->addInstrument( instrument[i] );
|
|
|
|
// Play the instrument until the end of the scorefile.
|
|
int nTicks, type;
|
|
MY_FLOAT byte2, byte3;
|
|
while (!done) {
|
|
|
|
// Look for new messages and return a delta time (in samples).
|
|
type = messager->nextMessage();
|
|
if (type < 0)
|
|
done = TRUE;
|
|
|
|
nTicks = messager->getDelta();
|
|
try {
|
|
for ( i=0; i<nTicks; i++ )
|
|
output->tick( voicer->tick() );
|
|
}
|
|
catch (StkError &) {
|
|
goto cleanup;
|
|
}
|
|
|
|
if ( type > 0 ) {
|
|
// Process the new control message.
|
|
byte2 = messager->getByteTwo();
|
|
byte3 = messager->getByteThree();
|
|
|
|
switch(type) {
|
|
|
|
case __SK_NoteOn_:
|
|
voicer->noteOn( byte2, byte3 );
|
|
break;
|
|
|
|
case __SK_NoteOff_:
|
|
voicer->noteOff( byte2, byte3 );
|
|
break;
|
|
|
|
case __SK_ControlChange_:
|
|
voicer->controlChange( (int) byte2, byte3 );
|
|
break;
|
|
|
|
case __SK_AfterTouch_:
|
|
voicer->controlChange( 128, byte2 );
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
cleanup:
|
|
for ( i=0; i<3; i++ ) delete instrument[i];
|
|
delete output;
|
|
delete messager;
|
|
delete voicer;
|
|
|
|
return 0;
|
|
}
|
|
\endcode
|
|
|
|
Assuming the program is compiled as <TT>threebees</TT>, the three-voice SKINI scorefile <A HREF="tutorial/bachfugue.ski"><TT>bachfugue.ski</TT></A> (also located in the <tt>scores</tt> directory with the examples) could be redirected to the program as:
|
|
|
|
\code
|
|
threebees < bachfugue.ski
|
|
\endcode
|
|
|
|
For more fun, surf to <A HREF="http://kern.humdrum.net/">Kern Scores</A> for a huge assortment of other scorefiles which can be downloaded in the SKINI format.
|
|
|
|
Another easy extension would be to use the <TT>STK_MIDI</TT> constructor argument to the Messager class and then play the instruments via a MIDI keyboard.
|
|
|
|
[<A HREF="tutorial.html">Main tutorial page</A>]
|
|
*/
|