test4.c 12.4 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
/*====================================================================
 * test4.c
 *
 * Synopsis:
 *
 * Random sound player tester.
 *
 * 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 TEST_FRAMES        3000
#define ACTIONS_PER_FRAME  24

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

#define MAX_VECTORS     8
#define MAX_SOUNDS      16

#define OK_COMPLETION   0
#define FAIL_COMPLETION 1

#define MAX_INST_SND    14

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

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

typedef struct {
    State       next;
    StateAction action;
    s32         weight;
    s32         OKCt;          /* number of times visited */
    s32         FailCt;
} StateVector;

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

typedef struct {
    State       state;
    ALSndId     sfxID;
    u8          priority;
} SoundState;


static StateMachine        sm4[3];
static ALSndPlayer         *sndp;
static SoundState          snds[MAX_SOUNDS];
static s32                 numActionsFrame;
static s32                 usedSnds;

static void initSM(void);

static s32 myRandom(int min, int max);

static s32 nullAction(ALSndPlayer *p, s32 sndNum);
static s32 allocSound(ALSndPlayer *p, s32 sndNum);
static s32 freeSound(ALSndPlayer *p, s32 sndNum);
static s32 startSound(ALSndPlayer *p, s32 sndNum);
static s32 stopSound(ALSndPlayer *p, s32 sndNum);
static s32 setVol(ALSndPlayer *p, s32 sndNum);
static s32 setPan(ALSndPlayer *p, s32 sndNum);
static s32 setPitch(ALSndPlayer *p, s32 sndNum);
static s32 setFxMix(ALSndPlayer *p, s32 sndNum);
static s32 setPriority(ALSndPlayer *p, s32 sndNum);


static void startTest4();
static void stopTest4();
static void doRetrace();


test_cntrl Test4 = {
    &startTest4,
    &stopTest4,
    NULL,            /* button proc */
    &doRetrace,
    NULL,            /* void *retraceData */
    1                /* u32  numSubTests */
};

static int frame;

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

static ALInstrument    *sfxInst;
    
/***********************************************************************
 * Test interface routines
 ***********************************************************************/
static void startTest4() 
{
    ALSynConfig         c;
    u32                 bankLen;
    ALBankFile          *bankPtr;
    ALBank              *bank;
    amConfig            amc;
    ALSndpConfig        SPConfig;

    PRINTF("Starting test4 init\n");
    numActionsFrame = ACTIONS_PER_FRAME;
    usedSnds = 0;
    alHeapInit(&gHeapRec, gHeapBuf, sizeof(gHeapBuf));    

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

    alBnkfNew(bankPtr, (u8 *) _sfxtableSegmentRomStart);
    bank = bankPtr->bankArray[0];
    sfxInst = bank->instArray[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);

    /*** Initialize the soundplayer  ****/
    sndp = alHeapAlloc(&gHeapRec, 1, sizeof(ALSndPlayer));
    SPConfig.maxSounds = MAX_SNDP_VOICES;
    SPConfig.maxEvents = MAX_SNDP_EVENTS;
    SPConfig.heap  = &gHeapRec;

    alSndpNew(sndp, &SPConfig);

    initSM();
    
}
/***********************************************************************
 * Init the state machine and the snds array.
 ***********************************************************************/
static void initSM(void)
{
    int i, w;
    
    for (i = 0; i < MAX_SOUNDS; i++)
    {
        snds[i].state = START;
        snds[i].sfxID = -1;  /* minus one indicates no sfxID */
	snds[i].priority = AL_DEFAULT_PRIORITY;
    }
    
    /*
     * initialize the state machine
     */
    /* START state */
    i = 0; 
    w = 0;
    sm4[START].vector[i].action = (StateAction)allocSound;
    sm4[START].vector[i].next = READY;
    sm4[START].vector[i].OKCt = 0;
    sm4[START].vector[i].FailCt = 0;
    w += sm4[START].vector[i++].weight = 1;
    
    sm4[START].vector[i].action = (StateAction)nullAction;
    sm4[START].vector[i].next = START;
    sm4[START].vector[i].OKCt = 0;
    sm4[START].vector[i].FailCt = 0;
    w += sm4[START].vector[i++].weight = 2;

    sm4[START].numVectors = i;
    sm4[START].weightTotal = w;
    
    /* READY state */
    i = 0; w = 0;
    sm4[READY].vector[i].action = (StateAction)freeSound;
    sm4[READY].vector[i].next = START;
    sm4[READY].vector[i].OKCt = 0;
    sm4[READY].vector[i].FailCt = 0;
    w += sm4[READY].vector[i++].weight = 1;

    sm4[READY].vector[i].action = (StateAction)startSound;
    sm4[READY].vector[i].next = PLAY;
    sm4[READY].vector[i].OKCt = 0;
    sm4[READY].vector[i].FailCt = 0;
    w += sm4[READY].vector[i++].weight = 2;
    
    sm4[READY].numVectors = i;
    sm4[READY].weightTotal = w;

    /* PLAY state */
    i = 0; w = 0;
    sm4[PLAY].vector[i].action = (StateAction)stopSound;
    sm4[PLAY].vector[i].next = READY;
    sm4[PLAY].vector[i].OKCt = 0;
    sm4[PLAY].vector[i].FailCt = 0;
    w += sm4[PLAY].vector[i++].weight = 1;

    sm4[PLAY].vector[i].action = (StateAction)setVol;
    sm4[PLAY].vector[i].next = PLAY;
    sm4[PLAY].vector[i].OKCt = 0;
    sm4[PLAY].vector[i].FailCt = 0;
    w += sm4[PLAY].vector[i++].weight = 4;

    sm4[PLAY].vector[i].action = (StateAction)setPitch;
    sm4[PLAY].vector[i].next = PLAY;
    sm4[PLAY].vector[i].OKCt = 0;
    sm4[PLAY].vector[i].FailCt = 0;
    w += sm4[PLAY].vector[i++].weight = 4;

    sm4[PLAY].vector[i].action = (StateAction) setPan;
    sm4[PLAY].vector[i].next = PLAY;
    sm4[PLAY].vector[i].OKCt = 0;
    sm4[PLAY].vector[i].FailCt = 0;
    w += sm4[PLAY].vector[i++].weight = 4;

    sm4[PLAY].vector[i].action = (StateAction)setFxMix;
    sm4[PLAY].vector[i].next = PLAY;
    sm4[PLAY].vector[i].OKCt = 0;
    sm4[PLAY].vector[i].FailCt = 0;
    w += sm4[PLAY].vector[i++].weight = 4;

    sm4[PLAY].vector[i].action = (StateAction) setPriority;
    sm4[PLAY].vector[i].next = PLAY;
    sm4[PLAY].vector[i].OKCt = 0;
    sm4[PLAY].vector[i].FailCt = 0;
    w += sm4[PLAY].vector[i++].weight = 4;

    sm4[PLAY].numVectors = i;
    sm4[PLAY].weightTotal = w;

}

static void stopTest4() 
{
    int i;

    PRINTF("START state:\n");
    for (i = 0; i < sm4[START].numVectors; i++) 
    {
        PRINTF("vector %d: OK   %d times\n", i, sm4[START].vector[i].OKCt);
        PRINTF("vector %d: Fail %d times\n", i, sm4[START].vector[i].FailCt);
    }

    PRINTF("READY state:\n");
    for (i = 0; i < sm4[READY].numVectors; i++) 
    {
        PRINTF("vector %d: OK   %d times\n", i, sm4[READY].vector[i].OKCt);
        PRINTF("vector %d: Fail %d times\n", i, sm4[READY].vector[i].FailCt);
    }

    PRINTF("STOP state:\n");
    for (i = 0; i < sm4[PLAY].numVectors; i++)
    { 
        PRINTF("vector %d: OK   %d times\n", i, sm4[PLAY].vector[i].OKCt);
        PRINTF("vector %d: Fail %d times\n", i, sm4[PLAY].vector[i].FailCt);
    }

    alSndpDelete(sndp);
    amDestroyAM();  
}


static void doRetrace() 
{
    s32     randSnd,
            randAct,
	    result,
            w, i, n;

    State   sndState;
    
    static s32 retraces = 0;

    
    for (n = 0; n < numActionsFrame; n++ ) 
    {
        randSnd = myRandom(0, MAX_SOUNDS-1); /* pick a snd, any snd */
        sndState = snds[randSnd].state;
	
        randAct = myRandom(0, sm4[sndState].weightTotal);
        w = 0;

        for (i = 0; i < sm4[sndState].numVectors; i++)
        {
            w += sm4[sndState].vector[i].weight;
            if (w >= randAct)
                break;
        }
        
        result = (*sm4[sndState].vector[i].action)(sndp, randSnd);
	
        if(result == OK_COMPLETION)
        {   
            sm4[sndState].vector[i].OKCt++;
            snds[randSnd].state = sm4[sndState].vector[i].next;
        }
        else
            sm4[sndState].vector[i].FailCt++;
        
    }

    retraces++;

    if (retraces > TEST_FRAMES)
	gTestDone = 1;

}

static s32 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 (s32)(frand + 0.5);
}

static s32 nullAction(ALSndPlayer *p, s32 sndNum)
{
    return OK_COMPLETION;
}

static s32 allocSound(ALSndPlayer *p, s32 sndNum)
{
    s32  instSndNum;

    instSndNum = myRandom(0,MAX_INST_SND);
    snds[sndNum].sfxID = alSndpAllocate(p,sfxInst->soundArray[instSndNum]); /* randomly grab a sfx */
    snds[sndNum].priority = AL_DEFAULT_PRIORITY;
    
    if(snds[sndNum].sfxID != -1)
    {
	usedSnds++;
	return OK_COMPLETION;
    }
    else
	return FAIL_COMPLETION;

}

static s32 freeSound(ALSndPlayer *p, s32 sndNum)
{
    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	if(alSndpGetState(p) == AL_STOPPED)
	{
	    alSndpDeallocate(p,snds[sndNum].sfxID);
	    snds[sndNum].sfxID = -1;
	    usedSnds--;
	    return OK_COMPLETION;
	}
    }
    return FAIL_COMPLETION;
}

static s32 startSound(ALSndPlayer *p, s32 sndNum)
{
    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	if(alSndpGetState(p) == AL_STOPPED)
	{
	    alSndpPlay(p);
	    return OK_COMPLETION;
	}
    }
    return FAIL_COMPLETION;
}

static s32 stopSound(ALSndPlayer *p, s32 sndNum)
{
    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	alSndpStop(p);
        return OK_COMPLETION;
    }    
    PRINTF("Failure in stopSound vector, sndNum %d\n", sndNum);
    return FAIL_COMPLETION;
}

static s32 setVol(ALSndPlayer *p, s32 sndNum)
{
    short vol = myRandom(0, MAX_SHORT);

    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	alSndpSetVol(p,vol);
        return OK_COMPLETION;
    }    
    PRINTF("Failure in setVol vector\n");
    return FAIL_COMPLETION;
}

static s32 setPitch(ALSndPlayer *p, s32 sndNum)
{
    float ratio = (float)myRandom(1, MAX_INT)/(float)MAX_INT;
    
    ratio *= 2.0f;

    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	alSndpSetPitch(p,ratio);
        return OK_COMPLETION;
    }    
    PRINTF("Failure in setPitch vector\n");
    return FAIL_COMPLETION;
}

static s32 setPan(ALSndPlayer *p, s32 sndNum)
{
    ALPan pan = myRandom(0, 127);

    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	alSndpSetPan(p,pan);
        return OK_COMPLETION;
    }    
    PRINTF("Failure in setPan vector\n");
    return FAIL_COMPLETION;
}

static s32 setFxMix(ALSndPlayer *p, s32 sndNum)
{
    char fxmix = myRandom(0, 127);

    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetSound(p,snds[sndNum].sfxID);
	alSndpSetFXMix(p,fxmix);
        return OK_COMPLETION;
    }    
    PRINTF("Failure in setFXMix vector\n");
    return FAIL_COMPLETION;
}

static s32 setPriority(ALSndPlayer *p, s32 sndNum) 
{
    snds[sndNum].priority =  myRandom(0, AL_MAX_PRIORITY);
    
    if(snds[sndNum].sfxID != -1)
    {
	alSndpSetPriority(p,snds[sndNum].sfxID,snds[sndNum].priority);
	return OK_COMPLETION;
    }
    PRINTF("Failure in setPriority vector\n");
    return FAIL_COMPLETION;
}