test2.c 11.5 KB
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/*====================================================================
 * test2.c
 *
 * Synopsis:
 *
 * Player for testing of driver functions
 *
 * Copyright 1993, Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
 * Inc.; the contents of this file may not be disclosed to third
 * parties, copied or duplicated in any form, in whole or in part,
 * without the prior written permission of Silicon Graphics, Inc.
 *
 * RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to
 * restrictions as set forth in subdivision (c)(1)(ii) of the Rights
 * in Technical Data and Computer Software clause at DFARS
 * 252.227-7013, and/or in similar or successor clauses in the FAR,
 * DOD or NASA FAR Supplement. Unpublished - rights reserved under the
 * Copyright Laws of the United States.
 *====================================================================*/

#include <libaudio.h>
#include "audio.h"
#include "audiotest.h"

#define MIN_TIME        0
#define MAX_TIME        10000
#define MAX_INT         0x7fffffff
#define MAX_SHORT       0x7fff

#define MAX_VECTORS     8
#define MAX_VOICES      16
#define MAX_WAVES       32

typedef enum {
    START = 0,
    READY = 1,
    PLAY  = 2
} State;

typedef void (*StateAction)(void *, int);

typedef struct {
    State       next;
    StateAction action;
    int         weight;
    int         count;          /* number of times visited */
} StateVector;

typedef struct {
    int         numVectors;
    int         weightTotal;
    StateVector vector[MAX_VECTORS];
} StateMachine;

typedef struct {
    State       state;
    ALVoice     voice;
} VoiceState;

typedef struct MyPlayer_s {
    ALPlayer            node;           /* note: must be first in structure */
    StateMachine        sm[3];
    VoiceState          voices[MAX_VOICES];
    int                 numVoices;
    ALSynth             *drvr;
    int                 numWaveTables;
    ALWaveTable         *waves[MAX_WAVES];
    int                 state;
} MyPlayer;

static void    playerNew(MyPlayer *p, int numVoices);
static void    playerDelete(MyPlayer *p);
static void    playerSetBank(MyPlayer *p, ALBank *bank);
static int     playerPlay(MyPlayer *p);
static int     playerStop(MyPlayer *p);

static int myRandom(int min, int max);

static void nullAction(MyPlayer *p, int voice);
static void allocVoice(MyPlayer *p, int voice);
static void freeVoice(MyPlayer *p, int voice);
static void startVoice(MyPlayer *p, int voice);
static void stopVoice(MyPlayer *p, int voice);
static void setVol(MyPlayer *p, int voice);
static void setPan(MyPlayer *p, int voice);
static void setPitch(MyPlayer *p, int voice);
static void setFxMix(MyPlayer *p, int voice);
static void setPriority(MyPlayer *p, int voice);
static void getPriority(MyPlayer *p, int voice);

static ALMicroTime _voiceHandler(void *node);

static void startTest2();
static void stopTest2();
static void doRetrace();

static MyPlayer *p;

test_cntrl Test2 = {
    &startTest2,
    &stopTest2,
    NULL,
    &doRetrace,
    NULL,            /* void *retraceData */
    1                /* u32  numSubTests */
};

static int frame;

extern u32      gTestDone;
extern ALHeap   gHeapRec;
extern u8       gHeapBuf[AUDIO_HEAP_SIZE];

/***********************************************************************
 * Test interface routines
 ***********************************************************************/
static void startTest2() 
{
    ALSynConfig         c;
    u32                 bankLen;
    ALBankFile          *bankPtr;
    ALBank              *bank;
    amConfig            amc;

    frame = 0;

    alHeapInit(&gHeapRec, gHeapBuf, sizeof(gHeapBuf));    

    /**** Load the bank file from ROM ****/
    bankLen = _midibankSegmentRomEnd - _midibankSegmentRomStart;
    bankPtr = alHeapAlloc(&gHeapRec, 1, bankLen);
    romCopy(_midibankSegmentRomStart, (char *)bankPtr, bankLen);

    alBnkfNew(bankPtr, (u8 *) _miditableSegmentRomStart);
    bank = bankPtr->bankArray[0];

    /**** Create the Audio Manager ****/
    c.maxVVoices = MAX_SYNTH_VVOICES;
    c.maxPVoices = MAX_SYNTH_PVOICES;
    c.maxUpdates = MAX_SYNTH_UPDATES;
    c.dmaproc    = 0;                  /* audio mgr will fill this in */
    c.fxType     = AL_FX_NONE; /*AL_FX_SMALLROOM; */
    c.heap       = &gHeapRec;
    
    amc.outputRate = 44100;
    amc.framesPerField = NUM_FIELDS;
    amc.maxACMDSize = MAX_RSP_CMDS;
    amCreateAudioMgr(&c, AUDIO_PRIORITY, &amc);

    /* create the test player */
    p = (MyPlayer *) alHeapAlloc(&gHeapRec, 1 , sizeof(MyPlayer));
    playerNew(p, 16);

    /* load the bank and assign it to the player */
    playerSetBank(p, bank);

}

static void stopTest2() 
{
    OSIntMask mask;

/*    mask = osSetIntMask(OS_IM_NONE);   */
    playerDelete(p);
/*    osSetIntMask(mask);  */

    amDestroyAM();

}

static void doRetrace() 
{
    if (frame++ > 1000)
        gTestDone = TRUE;
}

/***********************************************************************
 * Player for this test
 ***********************************************************************/
static void playerNew(MyPlayer *p, int numVoices) 
{
    int i, w;

    p->numVoices = numVoices;
    p->drvr = &alGlobals->drvr;
    
    for (i = 0; i < p->numVoices; i++) {
        p->voices[i].state = START;
    }
    
    /*
     * init wavetables array
     */
    p->numWaveTables = 0;
    
    /*
     * initialize the state machine
     */
    /* START state */
    i = 0; w = 0;
    p->sm[START].vector[i].action = (StateAction)allocVoice;
    p->sm[START].vector[i].next = READY;
    p->sm[START].vector[i].count = 0;
    w += p->sm[START].vector[i++].weight = 1;
    
    p->sm[START].vector[i].action = (StateAction)nullAction;
    p->sm[START].vector[i].next = START;
    p->sm[START].vector[i].count = 0;
    w += p->sm[START].vector[i++].weight = 1;

    p->sm[START].numVectors = i;
    p->sm[START].weightTotal = w;
    
    /* READY state */
    i = 0; w = 0;
    p->sm[READY].vector[i].action = (StateAction)freeVoice;
    p->sm[READY].vector[i].next = START;
    p->sm[READY].vector[i].count = 0;
    w += p->sm[READY].vector[i++].weight = 1;

    p->sm[READY].vector[i].action = (StateAction)startVoice;
    p->sm[READY].vector[i].next = PLAY;
    p->sm[READY].vector[i].count = 0;
    w += p->sm[READY].vector[i++].weight = 3;
    
    p->sm[READY].numVectors = i;
    p->sm[READY].weightTotal = w;

    /* PLAY state */
    i = 0; w = 0;
    p->sm[PLAY].vector[i].action = (StateAction)stopVoice;
    p->sm[PLAY].vector[i].next = READY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 1;

    p->sm[PLAY].vector[i].action = (StateAction)setVol;
    p->sm[PLAY].vector[i].next = PLAY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 2;

    p->sm[PLAY].vector[i].action = (StateAction)setPitch;
    p->sm[PLAY].vector[i].next = PLAY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 2;

    p->sm[PLAY].vector[i].action = (StateAction) setPan;
    p->sm[PLAY].vector[i].next = PLAY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 2;

    p->sm[PLAY].vector[i].action = (StateAction)setFxMix;
    p->sm[PLAY].vector[i].next = PLAY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 2;

    p->sm[PLAY].vector[i].action = (StateAction) setPriority;
    p->sm[PLAY].vector[i].next = PLAY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 2;

    p->sm[PLAY].vector[i].action = (StateAction) getPriority;
    p->sm[PLAY].vector[i].next = PLAY;
    p->sm[PLAY].vector[i].count = 0;
    w += p->sm[PLAY].vector[i++].weight = 2;

    p->sm[PLAY].numVectors = i;
    p->sm[PLAY].weightTotal = w;

    /*
     * add ourselves to the driver
     */
    p->node.next       = NULL;
    p->node.handler    = _voiceHandler;
    p->node.clientData = p;
    alSynAddPlayer(&alGlobals->drvr, &p->node);
}

void playerDelete(MyPlayer *p) 
{
    int i;

    PRINTF("START state\n");
    for (i = 0; i < p->sm[START].numVectors; i++) {
        PRINTF("vector %d: %d times\n", i, p->sm[PLAY].vector[i].count);
    }
    
    PRINTF("READY state\n");
    for (i = 0; i < p->sm[READY].numVectors; i++) {
        PRINTF("vector %d: %d times\n", i, p->sm[READY].vector[i].count);
    }
    
    PRINTF("PLAY state\n");
    for (i = 0; i < p->sm[PLAY].numVectors; i++) {
        PRINTF("vector %d: %d times\n", i, p->sm[PLAY].vector[i].count);
    }
    alSynRemovePlayer(&alGlobals->drvr, &p->node);
}

void playerSetBank(MyPlayer *p, ALBank *bank)
{
    int i, k;
    ALInstrument *inst;
    ALSound      *sound;
    
    for (i = 0; i < bank->instCount; i++) {
        inst = bank->instArray[i];
        for (k = 0; k < inst->soundCount; k++) {
            sound = inst->soundArray[k];
            p->waves[p->numWaveTables++] = sound->wavetable;
            if (p->numWaveTables >= MAX_WAVES)
                return;
        }    
    }
}

ALMicroTime _voiceHandler(void *node)
{
    MyPlayer *p = (MyPlayer *)node;
    int i, v, rv, w;
    State voiceState;

    static int frames = 0;
    
    if (frames++ > 10)
    {
	v = myRandom(0, p->numVoices-1); /* pick a voice, any voice */
	voiceState = p->voices[v].state;
	
	rv = myRandom(0, p->sm[voiceState].weightTotal);
	w = 0;

	for (i = 0; i < p->sm[voiceState].numVectors; i++) {
	    w += p->sm[voiceState].vector[i].weight;
	    if (w >= rv)
		break;
	}
        
	(*p->sm[voiceState].vector[i].action)(p, v);
	
	p->sm[voiceState].vector[i].count++;
	
	p->voices[v].state = p->sm[voiceState].vector[i].next;

	frames = 0;

	return myRandom(MIN_TIME, MAX_TIME);
    }
    else
/*	return myRandom(MIN_TIME, MAX_TIME);  */
	return 100;
}

int myRandom(int min, int max) 
{
    float frand, fmin, fmax;
    static long seed = 1;

    seed = seed * 1103515245 + 12345;
    
    frand = (float)(seed & MAX_INT)/(float)MAX_INT; /* assume int = long */
    fmin = (float)min;
    fmax = (float)max;
    frand = frand * (max - min) + min;
    
    return (int)(frand + 0.5);
}

void nullAction(MyPlayer *p, int voice)
{
}

void allocVoice(MyPlayer *p, int voice)
{
    ALVoiceConfig vc;

    vc.priority = myRandom(0, AL_MAX_PRIORITY);   /* voice priority       */
    vc.fxBus = 0;                               /* bus assignment       */
    vc.unityPitch = 0;
    
    alSynAllocVoice(p->drvr, &p->voices[voice].voice, &vc);
}

void freeVoice(MyPlayer *p, int voice)
{
    alSynFreeVoice(p->drvr, &p->voices[voice].voice);
}

void startVoice(MyPlayer *p, int v)
{
    int i;
    
    if (!p->numWaveTables)
        return;
    
    i = myRandom(0, p->numWaveTables-1);

    alSynStartVoice(p->drvr, &p->voices[v].voice, p->waves[i]);
}

void stopVoice(MyPlayer *p, int voice)
{
    alSynStopVoice(p->drvr, &p->voices[voice].voice);
}

void setVol(MyPlayer *p, int voice)
{
    short vol = myRandom(0, MAX_SHORT);
    ALMicroTime t = myRandom(0, MAX_SHORT);
    
    alSynSetVol(p->drvr, &p->voices[voice].voice, vol, t);
}

void setPitch(MyPlayer *p, int voice)
{
    float ratio = (float)myRandom(0, MAX_INT)/(float)MAX_INT;

    alSynSetPitch(p->drvr, &p->voices[voice].voice, ratio);
}

void setPan(MyPlayer *p, int voice)
{
    ALPan pan = myRandom(0, 127);
    alSynSetPan(p->drvr, &p->voices[voice].voice, pan);
}

void setFxMix(MyPlayer *p, int voice)
{
    char fxmix = myRandom(0, 127);
    alSynSetFXMix(p->drvr, &p->voices[voice].voice, fxmix);
}

void setPriority(MyPlayer *p, int voice) 
{
    short priority = myRandom(0, AL_MAX_PRIORITY);
    alSynSetPriority(p->drvr, &p->voices[voice].voice, priority);
}

void getPriority(MyPlayer *p, int voice)
{
    alSynGetPriority(p->drvr, &p->voices[voice].voice);
}