test1.c 9.98 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
/*====================================================================
 * test1.c
 *
 * Synopsis:  This file contains a simple test for the audiotest suite
 *
 * 
 * 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 "audiotest.h"
#include "audio.h"
#include <libaudio.h>


#define MAX_VOICES		20
#define PLYR1_SILENT		1
#define PLYR1_RUNNING		2
#define PLYR1_NEEDSTOP		3
#define PLYR1_STOPPING		4
#define PLYR1_STOPPED		5
#define PLYR1_ASCENDING		1
#define PLYR1_DESCENDING	2
#define PLYR1_LEFT		3
#define PLYR1_RIGHT		4
#define PLYR1_LOUDER		5
#define PLYR1_SOFTER		6
#define SWEEP_MAX		2


/**** Share the heap with other test files, so have external ****/
extern ALHeap     gHeapRec;
extern u8         gHeapBuf[AUDIO_HEAP_SIZE];
extern u32        gTestDone;

/**** Prototypes and test control structure ****/
static void InitTest1 (u32 subTestNum);
static void StopTest1 (u32 subTestNum); 
static void retraceProc1 (void *t);

static ALMicroTime __plyr1VoiceHandler(void *node);

test_cntrl Test1 = {
    &InitTest1,      /* void *InitTest; */
    &StopTest1,      /* void *StopTest; */
    NULL,
    &retraceProc1,   /* void *retraceProc */
    NULL,            /* void *retraceData */
    6                /* u32  numSubTests */
};


/**** define a player structure for my test to use. ****/
typedef struct {
    ALPlayer            node;           /* note: must be first in structure */
    ALSynth             *drvr;          /* reference to the client driver   */
    u32                 curTest;
    u8                  state;
} player1;

static player1           *plyr1;


/**** Globals unique to this test ****/
static ALBank            *gMidiBank;
static ALVoice           voice[MAX_VOICES];
static s32               voiceAlloc[MAX_VOICES];
static ALVoiceConfig     config = { 64, 0, 0 }; /* priority, fxBus, unityPitchFlag */
static s16               vol;
static u8                pan;
static u8                fxmix = 80;
static ALMicroTime       deltaTime = 0;
static u32               timer = 0;
static u32               maxNotes;
static f32               pitch;
static u32               direction;
static u32               sweepCt;

static void InitTest1 (u32 subTestNum) 
{
    ALSynConfig         c;
    u32                 bankLen;
    ALBankFile          *bankPtr;
    amConfig            amc;
    u32                 i;


    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);
    gMidiBank = 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);

    /**** Set up the player, and sign in to the driver ****/
    plyr1 = alHeapAlloc(&gHeapRec,1,sizeof(player1));
    plyr1->node.next       = NULL;
    plyr1->node.clientData = plyr1;
    plyr1->drvr = &alGlobals->drvr;
    plyr1->state = PLYR1_SILENT; 
    plyr1->node.handler = __plyr1VoiceHandler;
    plyr1->curTest = subTestNum;

    switch(subTestNum )
    {
	case 0:
	    PRINTF("Test1.0:  Play a single note, sweep low pitch to high.\n");
	    pitch = 0.25f;
	    pan = 64;
	    vol = 0x7000;
	    maxNotes = 1;
	    direction = PLYR1_ASCENDING;
	    break;
	case 1:
	    PRINTF("Test1.1:  Play a single note, sweep left to right.\n");
	    pitch = 1.0f;
	    pan = 0;
	    vol = 0x7000;
	    maxNotes = 1;
	    direction = PLYR1_RIGHT;
	    break;
	case 2:
	    PRINTF("Test1.2:  Play a single note, sweep soft to loud.\n");
	    pitch = 1.0f;
	    pan = 64;
	    vol = 0;
	    maxNotes = 1;
	    direction = PLYR1_LOUDER;
	    break;
	    
	case 3:
	    PRINTF("Test1.3:  Play multiple notes, sweep low pitch to high.\n");
	    pitch = 0.25f;
	    pan = 64;
	    maxNotes = 20;
	    vol = 0x2400;
	    direction = PLYR1_ASCENDING;
	    break;
	case 4:
	    PRINTF("Test1.4:  Play multiple notes, sweep from left to right.\n");
	    pitch = 1.0f;
	    pan = 0;
	    maxNotes = 20;
	    vol = 0x2400;
	    direction = PLYR1_RIGHT;
	    break;
	case 5:
	    PRINTF("Test1.5:  Play multiple notes, sweep from soft to loud.\n");
	    pitch = 1.0f;
	    pan = 64;
	    vol = 0;
	    maxNotes = 20;
	    direction = PLYR1_LOUDER;
	    break;
    }
    
    sweepCt = 0;

    if(maxNotes>MAX_VOICES)
	maxNotes = MAX_VOICES;
    
    for( i = 0; i < MAX_VOICES; i++)
	voiceAlloc[i] = 0;

    alSynAddPlayer(plyr1->drvr, &plyr1->node);

}

static void StopTest1(u32 subTestNum)
{
    alSynRemovePlayer(plyr1->drvr, &plyr1->node);
    amDestroyAM();
}


static void retraceProc1(void *t)
{

}



static ALMicroTime __plyr1VoiceHandler(void *node)
{
    ALInstrument        *instrument;
    ALSound             *sound;
    u32                 i;

    instrument = gMidiBank->instArray[0];
    sound = instrument->soundArray[0];

    switch(plyr1->state)
    {
	case PLYR1_SILENT:
	    for( i = 0 ; i < maxNotes ; i++)
	    {
		voiceAlloc[i] = alSynAllocVoice(plyr1->drvr, &voice[i],
                                                &config);
		if(voiceAlloc[i])
		    alSynStartVoiceParams(plyr1->drvr, &voice[i],
                                          sound->wavetable, pitch, vol, pan,
                                          fxmix, deltaTime);
		else
		    PRINTF("Failed to allocate voice\n");
	    }
	    plyr1->state = PLYR1_RUNNING;
	    break;

	case PLYR1_RUNNING:
	    {
		switch(plyr1->curTest)
		{
		    case 0:
		    case 3:
			if(direction == PLYR1_ASCENDING)
			{
			    pitch += 0.01;
			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetPitch(plyr1->drvr,&voice[i],
                                                  pitch); 
			    if(pitch >= 2.0f)
				direction = PLYR1_DESCENDING;
			}
			else /* must be descending */
			{
			    pitch -= 0.01;
			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i] && pitch > 0.0f)
				    alSynSetPitch(plyr1->drvr, &voice[i],
                                                  pitch); 
			    if(pitch <= 0.0f)
			    {
				pitch = 0.0f;
				direction = PLYR1_ASCENDING;
				sweepCt++;
				if(sweepCt >= SWEEP_MAX) 
				    plyr1->state = PLYR1_NEEDSTOP;
			    }
			}
			break;
		    case 1:
		    case 4:
			if(direction == PLYR1_RIGHT)
			{
			    pan +=1;
			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetPan(plyr1->drvr, &voice[i],pan);
			    if(pan >= 127)
				direction = PLYR1_LEFT;
			}
			else /* must be moving left  */
			{
			    pan -= 1;
			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetPan(plyr1->drvr, &voice[i],pan);
			    if(pan <= 0)
			    {
				pan = 0;
				direction = PLYR1_RIGHT;
				sweepCt++;
				if(sweepCt >= SWEEP_MAX)
				    plyr1->state = PLYR1_NEEDSTOP;
			    }
			}
			break;
		    case 2:
			if(direction == PLYR1_LOUDER)
			{
			    vol +=1000;
			    if((u16)vol >= 0x7FFF)
			    {
				vol = 0x7FFF;
				direction = PLYR1_SOFTER;
			    }
			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetVol(plyr1->drvr, &voice[i],vol,
                                                deltaTime);
			}
			else /* must be getting softer  */
			{
			    vol -= 1000;
			    if(vol <= 0)
			    {
				vol = 0;
				direction = PLYR1_LOUDER;
				sweepCt++;
				if(sweepCt >= SWEEP_MAX)
				    plyr1->state = PLYR1_NEEDSTOP;
			    }

			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetVol(plyr1->drvr, &voice[i],vol,
                                                deltaTime);
			}
			break;
		    case 5:
			if(direction == PLYR1_LOUDER)
			{
			    vol +=400;
			    if((u16)vol >= 0x2400)
			    {
				vol = 0x2400;
				direction = PLYR1_SOFTER;
			    }
			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetVol(plyr1->drvr, &voice[i],vol,
                                                deltaTime);
			}
			else /* must be getting softer  */
			{
			    vol -= 400;
			    if(vol <= 0)
			    {
				vol = 0;
				direction = PLYR1_LOUDER;
				sweepCt++;
				if(sweepCt >= SWEEP_MAX)
				    plyr1->state = PLYR1_NEEDSTOP;
			    }

			    for( i = 0; i < maxNotes ; i++)
				if(voiceAlloc[i])
				    alSynSetVol(plyr1->drvr, &voice[i],vol,
                                                deltaTime);
			}
			break;
			
		}
	    }
	    break;
	    
	case PLYR1_NEEDSTOP:
	    for( i = 0; i < maxNotes ; i++)
	    {
		if(voiceAlloc[i])
		    alSynSetVol(plyr1->drvr, &voice[i],(s16)0,(ALMicroTime)15999); 
	    } 
	    timer = 0;
	    plyr1->state = PLYR1_STOPPING; 
	    break;
	
	case PLYR1_STOPPING:
	    timer++;
	    if (timer > 2) /* fade voices before stopping them */
	    {
		for( i = 0; i < maxNotes ; i++)
		{
		    if(voiceAlloc[i])
		    {
			alSynStopVoice(plyr1->drvr, &voice[i]);
			alSynFreeVoice(plyr1->drvr, &voice[i]);
		    }
		}
		plyr1->state = PLYR1_STOPPED;
		timer = 0;
	    }
	    break;
	
	case PLYR1_STOPPED:
	    gTestDone = TRUE;
	    break;
    
    }

    return 16000;
}