1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/************** Test Main Program Individual Voice *********************/
#include "SKINImsg.h"
#include "Instrmnt.h"
#include "JCRev.h"
#include "Drone.h"
#include "Sitar.h"
#include "Tabla.h"
#include "VoicDrum.h"
#include "Messager.h"
#include "RtAudio.h"
#include <signal.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using std::min;
using namespace stk;
StkFloat float_random(StkFloat max) // Return random float between 0.0 and max
{
StkFloat temp = (StkFloat) (max * rand() / (RAND_MAX + 1.0) );
return temp;
}
void usage(void) {
// Error function in case of incorrect command-line argument specifications.
std::cout << "\nuseage: ragamat flags \n";
std::cout << " where flag = -s RATE to specify a sample rate,\n";
std::cout << " flag = -ip for realtime SKINI input by pipe\n";
std::cout << " (won't work under Win95/98),\n";
std::cout << " and flag = -is <port> for realtime SKINI input by socket.\n";
exit(0);
}
bool done;
static void finish(int ignore){ done = true; }
// The TickData structure holds all the class instances and data that
// are shared by the various processing functions.
struct TickData {
JCRev reverbs[2];
Drone drones[3];
Sitar sitar;
VoicDrum voicDrums;
Tabla tabla;
Messager messager;
Skini::Message message;
StkFloat lastSample;
StkFloat t60;
int counter;
bool settling;
bool haveMessage;
StkFloat droneChance, noteChance;
StkFloat drumChance, voiceChance;
int tempo;
int chanceCounter;
int key;
int ragaStep;
int ragaPoint;
int endPhase;
StkFloat rateScaler;
// Default constructor.
TickData()
: t60(4.0), counter(0),
settling( false ), haveMessage( false ), droneChance(0.01), noteChance(0.01),
drumChance(0.0), voiceChance(0.0), tempo(3000), chanceCounter(3000), key(0), ragaPoint(6), endPhase(0) {}
};
// Raga key numbers and drone frequencies.
const int ragaUp[2][13] = {{57, 60, 62, 64, 65, 68, 69, 71, 72, 76, 77, 81},
{52, 54, 55, 57, 59, 60, 63, 64, 66, 67, 71, 72}};
const int ragaDown[2][13] = {{57, 60, 62, 64, 65, 67, 69, 71, 72, 76, 79, 81},
{48, 52, 53, 55, 57, 59, 60, 64, 66, 68, 70, 72}};
StkFloat droneFreqs[3] = { 55.0, 82.5, 220.0 };
#define DELTA_CONTROL_TICKS 64 // default sample frames between control input checks
// The processMessage() function encapsulates the handling of control
// messages. It can be easily relocated within a program structure
// depending on the desired scheduling scheme.
void processMessage( TickData* data )
{
register unsigned int value1 = data->message.intValues[0];
register StkFloat value2 = data->message.floatValues[1];
register StkFloat temp = value2 * ONE_OVER_128;
switch( data->message.type ) {
case __SK_Exit_:
if ( data->settling == false ) goto settle;
if ( data->endPhase < 5 ) return;
done = true;
return;
case __SK_ControlChange_:
switch ( value1 ) {
case 1:
data->droneChance = temp;
break;
case 2:
data->noteChance = temp;
break;
case 4:
data->voiceChance = temp;
break;
case 7:
data->tempo = (int) (11025 - value2 * 70.0 );
break;
case 11:
data->drumChance = temp;
break;
case 64:
if ( value2 == 0.0 ) {
data->key = 1;
droneFreqs[0] = 55.0;
droneFreqs[1] = 82.5;
droneFreqs[2] = 220.0;
}
else {
data->key = 0;
droneFreqs[0] = 82.5;
droneFreqs[1] = 123.5;
droneFreqs[2] = 330.0;
}
break;
default:
break;
}
} // end of type switch
data->haveMessage = false;
return;
settle:
// Exit and program change messages are preceeded with a short settling period.
data->counter = (int) (data->t60 * Stk::sampleRate());
data->drones[1].noteOn( droneFreqs[1], 0.1 );
data->settling = true;
std::cout << "What Need Have I for This?" << std::endl;
}
// The tick() function handles sample computation and scheduling of
// control updates. It will be called automatically by RtAudio when
// the system needs a new buffer of audio samples.
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *dataPointer )
{
TickData *data = (TickData *) dataPointer;
register StkFloat temp, outs[2], *samples = (StkFloat *) outputBuffer;
int i, voiceNote, counter, nTicks = (int) nBufferFrames;
while ( nTicks > 0 && !done ) {
if ( !data->haveMessage ) {
data->messager.popMessage( data->message );
if ( data->message.type > 0 ) {
data->counter = (long) (data->message.time * Stk::sampleRate());
data->haveMessage = true;
}
else
data->counter = DELTA_CONTROL_TICKS;
}
counter = min( nTicks, data->counter );
data->counter -= counter;
for ( i=0; i<counter; i++ ) {
outs[0] = data->reverbs[0].tick( data->drones[0].tick() + data->drones[2].tick()
+ data->sitar.tick() );
outs[1] = data->reverbs[1].tick( 1.5 * data->drones[1].tick() + 0.5 * data->voicDrums.tick()
+ 0.5 * data->tabla.tick() );
// Mix a little left to right and back.
*samples++ = outs[0] + 0.3 * outs[1];
*samples++ = outs[1] + 0.3 * outs[0];
nTicks--;
// Do a bunch of random controls unless settling down to end.
if ( data->settling ) {
if ( data->counter == 0 ) {
data->counter = (int) (data->t60 * Stk::sampleRate());
if ( data->endPhase == 0 ) {
data->drones[2].noteOn( droneFreqs[2], 0.1 );
std::cout << "What Need Have I for This?" << std::endl;
}
else if ( data->endPhase == 1 ) {
data->drones[0].noteOn( droneFreqs[0], 0.1 );
std::cout << "RagaMatic finished ... " << std::endl;
}
else if ( data->endPhase == 2 ) {
std::cout << "All is Bliss ... " << std::endl;
}
else if ( data->endPhase == 3 ) {
std::cout << "All is Bliss ..." << std::endl;
}
data->endPhase++;
}
}
else {
data->chanceCounter--;
if (data->chanceCounter == 0) {
data->chanceCounter = (int) ( data->tempo / data->rateScaler );
if ( float_random(1.0) < data->droneChance )
data->drones[0].noteOn( droneFreqs[0], 0.1 );
if ( float_random(1.0) < data->droneChance )
data->drones[1].noteOn( droneFreqs[1], 0.1 );
if ( float_random(1.0) < data->droneChance )
data->drones[2].noteOn( droneFreqs[2], 0.1 );
if ( float_random(1.0) < data->noteChance ) {
temp = float_random(1.0);
if ( temp < 0.1) data->ragaStep = 0;
else if (temp < 0.5) data->ragaStep = 1;
else data->ragaStep = -1;
data->ragaPoint += data->ragaStep;
if ( data->ragaPoint < 0 )
data->ragaPoint -= ( 2 * data->ragaStep );
if ( data->ragaPoint > 11 ) data->ragaPoint = 11;
if ( data->ragaStep > 0 )
data->sitar.noteOn( Midi2Pitch[ragaUp[data->key][data->ragaPoint]],
0.05 + float_random(0.3) );
else
data->sitar.noteOn( Midi2Pitch[ragaDown[data->key][data->ragaPoint]],
0.05 + float_random(0.3) );
}
if ( float_random(1.0) < data->voiceChance ) {
voiceNote = (int) float_random(11);
data->voicDrums.noteOn( voiceNote, 0.3 + (0.4 * data->drumChance) +
float_random(0.3 * data->voiceChance));
}
if ( float_random(1.0) < data->drumChance ) {
voiceNote = (int) float_random(TABLA_NUMWAVES);
data->tabla.noteOn( voiceNote, 0.2 + (0.2 * data->drumChance) +
float_random(0.6 * data->drumChance));
}
}
}
}
if ( nTicks == 0 ) break;
// Process control messages.
if ( data->haveMessage ) processMessage( data );
}
return 0;
}
int main( int argc, char *argv[] )
{
TickData data;
RtAudio dac;
int i;
if ( argc < 2 || argc > 6 ) usage();
// If you want to change the default sample rate (set in Stk.h), do
// it before instantiating any objects! If the sample rate is
// specified in the command line, it will override this setting.
Stk::setSampleRate( 44100.0 );
// Parse the command-line arguments.
unsigned int port = 2001;
for ( i=1; i<argc; i++ ) {
if ( !strcmp( argv[i], "-is" ) ) {
if ( i+1 < argc && argv[i+1][0] != '-' ) port = atoi(argv[++i]);
data.messager.startSocketInput( port );
}
else if (!strcmp( argv[i], "-ip" ) )
data.messager.startStdInput();
else if ( !strcmp( argv[i], "-s" ) && ( i+1 < argc ) && argv[i+1][0] != '-')
Stk::setSampleRate( atoi(argv[++i]) );
else
usage();
}
// Allocate the dac here.
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 2;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data );
}
catch ( RtAudioError& error ) {
error.printMessage();
goto cleanup;
}
data.reverbs[0].setT60( data.t60 );
data.reverbs[0].setEffectMix( 0.5 );
data.reverbs[1].setT60( 2.0 );
data.reverbs[1].setEffectMix( 0.2 );
data.drones[0].noteOn( droneFreqs[0], 0.1 );
data.drones[1].noteOn( droneFreqs[1], 0.1 );
data.drones[2].noteOn( droneFreqs[2], 0.1 );
data.rateScaler = 22050.0 / Stk::sampleRate();
// Install an interrupt handler function.
(void) signal( SIGINT, finish );
// If realtime output, set our callback function and start the dac.
try {
dac.startStream();
}
catch ( RtAudioError &error ) {
error.printMessage();
goto cleanup;
}
// Setup finished.
while ( !done ) {
// Periodically check "done" status.
Stk::sleep( 50 );
}
// Shut down the output stream.
try {
dac.closeStream();
}
catch ( RtAudioError& error ) {
error.printMessage();
}
cleanup:
return 0;
}
|