sndplayUI.c 12.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 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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/*====================================================================
 * sndplayUI.c
 *
 * Copyright 1995, 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 <ultra64.h>
#include <ultralog.h>
#include <os.h>

#include <assert.h>

#include "audio.h"
#include "gfx.h"
#include "sndplayUI.h"
#include "UIpane.h"
#include "sequence.h"
#include "font_ext.h"

#include "minus_sprite.h"
#include "plus_sprite.h"
#include "sfxplay_sprite.h"


#define USE_DETUNE	1	/* 1 = pitch box controls detuning, 0 = pitch box controls pitch */


extern ALSndPlayer  *sndp;
extern ALInstrument *sfxInst;

static void __SndPItemInit (UIItem *pItem, int id);
static void __SndPPlayDrawProc (UIItem *pItem, Gfx **ppGfx);
static void __SndPTitleDrawProc (UIItem *pItem, Gfx **ppGfx);
static void __SndPNBoxDrawProc (UIItem *pItem, Gfx **ppGfx);
static void __SndPPaneDrawProc (UIPane *pPane, Gfx **ppGfx);
static char __SndPHandleEventProc (UIPane *pPane, UIEvent *pEvent);
static char __SndPHandleMouseDown (UIPane *pPane, Point *pPt);
static char __SndPHandleMouseUp (UIPane *pPane, Point *pPt);
static char __SndPHandleButtonDown (UIPane *pPane, int button);
static void __SndPDrawStrings (Gfx **ppGfx, Rect *bounds, char *str);


#if USE_DETUNE
static char labelStr[] = "FX@ Detune Pan  Vol  FXMix";
#else
static char labelStr[] = "FX@  Pitch Pan  Vol  FXMix";
#endif

static sndplayUI        sSndPUI;
static u32              CurValStrDL = 0;

static u8       CurFXNum = 0;
static u8       CurFXPan = 64;
static u8       CurFXVol = 127;
static u8       CurFXMix = 80;
static f32      CurFXPitch = 1.0f;

static char	sSndNeedsPlaying = FALSE;	/* sct 11/28/95 */

#define NUM_SFX_STR_BMS         36

static Bitmap SFXStrBM[NUM_SFX_STR_BMS];

#define NUM_TEXT_STRS           6

static Gfx SFXStrDL[NUM_TEXT_STRS][NUM_DL(NUM_SFX_STR_BMS)];

static Sprite SFXStrSprite = {
        0,0,                 /* Position: x,y */
        0,0,                 /* Sprite size in texels (x,y) */
        1.0,1.0,             /* Sprite Scale: x,y */
        0,0,                 /* Explosion (x,y) */
        SP_TRANSPARENT,      /* Sprite Attributes */
        0x1234,              /* Sprite Depth: Z */
        255,255,255,255,     /* Sprite Coloration: RGBA */
        0,0,NULL,            /* Color LookUp Table: start_index, length, address */
        0,1,                 /* Sprite Bitmap index: start index, step increment */
        NUM_SFX_STR_BMS,     /* Number of bitmaps */
        NUM_DL(NUM_SFX_STR_BMS), /* Number of display list locations allocated */
        15, 128,             /* Sprite Bitmap Height: Used_height, physical height */
        G_IM_FMT_I,          /* Sprite Bitmap Format */
        G_IM_SIZ_4b,         /* Sprite Bitmap Texel Size */
        SFXStrBM,            /* Pointer to bitmaps */
        &SFXStrDL[0][0],            /* Display list memory */
        &SFXStrDL[0][0],            /* HACK: dynamic_dl pointer */

};



void
SndP_Init (UIPane **ppPane)
{
    UIItem      *pItem;
    int         i;

    /* Initialize SndPlayer specific fields. */
    sSndPUI.curSnd = 0;

    /* Initialize the SndPlayUI's pane. */
    Pane_Init (&sSndPUI.fPane);
    sSndPUI.fPane.numItems = kSndPNumItems;
    sSndPUI.fPane.itemList = sSndPUI.fItemList;
    sSndPUI.fPane.pData = &sSndPUI;
    sSndPUI.fPane.DrawProc = __SndPPaneDrawProc;
    sSndPUI.fPane.HandleEventProc = __SndPHandleEventProc;
        
    SetRect (&sSndPUI.fPane.bounds, kSndP_l, kSndP_t, kSndP_l + kSndP_w, kSndP_t + kSndP_h);
    
    /* Initialize each of the SndPlayUI's pane's items. */
    for (i=0; i<sSndPUI.fPane.numItems; i++)
    {
        pItem = &sSndPUI.fPane.itemList[i];

        __SndPItemInit (pItem, i);
    }

    *ppPane = &sSndPUI.fPane;
}

static void
__SndPItemInit (UIItem *pItem, int id)
{
    Item_Init (pItem, id);
    
    pItem->pPane = &sSndPUI.fPane; 

    switch (id)
    {
        case kSndPPlayID:
          SetRect (&pItem->bounds, kSndPPlay_l, kSndPNBox_t, kSndPPlay_l + kSndPPlay_w, kSndPNBox_b);
          pItem->DrawProc = __SndPPlayDrawProc;
          pItem->pSpriteIcon = &sfxplay_sprite;
          break;

        case kSndPTitleID:
          SetRect (&pItem->bounds, kSndPTitle_l, kSndPTitle_t, kSndPTitle_r, kSndPTitle_b);
          pItem->DrawProc = __SndPTitleDrawProc;
          break;

        case kSndPNumberNBoxID:
          SetRect (&pItem->bounds, kSndPNumberNBox_l, kSndPNBox_t, kSndPNumberNBox_l + kSndPNBox_w, kSndPNBox_b);
          pItem->DrawProc = __SndPNBoxDrawProc;
          break;
          
        case kSndPPitchNBoxID:
          SetRect (&pItem->bounds, kSndPPitchNBox_l, kSndPNBox_t, kSndPPitchNBox_l + kSndPNBox_w, kSndPNBox_b);
          pItem->DrawProc = __SndPNBoxDrawProc;
          break;
          
        case kSndPPanNBoxID:
          SetRect (&pItem->bounds, kSndPPanNBox_l, kSndPNBox_t, kSndPPanNBox_l + kSndPNBox_w, kSndPNBox_b);
          pItem->DrawProc = __SndPNBoxDrawProc;
          break;
          
        case kSndPVolumeNBoxID:
          SetRect (&pItem->bounds, kSndPVolumeNBox_l, kSndPNBox_t, kSndPVolumeNBox_l + kSndPNBox_w, kSndPNBox_b);
          pItem->DrawProc = __SndPNBoxDrawProc;
          break;
          
        case kSndPFXMixNBoxID:
          SetRect (&pItem->bounds, kSndPFXMixNBox_l, kSndPNBox_t, kSndPFXMixNBox_l + kSndPNBox_w, kSndPNBox_b);
          pItem->DrawProc = __SndPNBoxDrawProc;
          break;          
    }
}




f32 sfxPitch[] = {0.5,0.5,0.5,0.3175,1.0,0.136,0.5,0.25,0.25,0.204};

void
SndP_PollAudioFrame (void)
{
    if (sSndNeedsPlaying)
    {
	ALSound *    sound;
	ALSndId      sfxID;

	sSndNeedsPlaying = FALSE;
	sound = sfxInst->soundArray[CurFXNum];

	if((sfxID = alSndpAllocate(sndp, sound)) != -1)
	{
	    alSndpSetSound(sndp, sfxID);
	    alSndpSetFXMix(sndp, CurFXMix);
	    alSndpSetPan(sndp, (ALPan)CurFXPan);
	    alSndpSetVol(sndp, (s16)CurFXVol << 8);

#if USE_DETUNE
            alSndpSetPitch(sndp, alCents2Ratio(sound->keyMap->detune) * sfxPitch[CurFXNum]);
#else
	    alSndpSetPitch(sndp, CurFXPitch * sfxPitch[CurFXNum]);
#endif

	    alSndpPlay(sndp);
	}
    }
}


void
SndP_Play (void)
{
    /* sct 11/28/95 - Defer snd allocation to audio thread to avoid conflicts dealloc. */
    /* Actual play command is now done in SndP_PollAudioFrame (). */
    sSndNeedsPlaying = TRUE;	
}

void
SndP_NumberPlus (void)
{
    if(CurFXNum < (sfxInst->soundCount - 1))
	CurFXNum++;
}

void
SndP_NumberMinus (void)
{
    if(CurFXNum > 0)
	CurFXNum--;
}

void
SndP_PitchPlus (void)
{
#if USE_DETUNE
    sfxInst->soundArray[CurFXNum]->keyMap->detune++;
#else
    CurFXPitch += 0.01f;
#endif
}

SndP_PitchMinus (void)
{
#if USE_DETUNE
    sfxInst->soundArray[CurFXNum]->keyMap->detune--;
#else
    if(CurFXPitch > 0.01f)
	CurFXPitch -= 0.01f;
#endif
}

void
SndP_PanPlus (void)
{
    if(CurFXPan < 127)
	CurFXPan++;
}

void
SndP_PanMinus (void)
{
    if(CurFXPan > 0)
	CurFXPan--;
}

void
SndP_VolumePlus (void)
{
    if(CurFXVol < 127)
	CurFXVol++;
}

void
SndP_VolumeMinus (void)
{
    if(CurFXVol > 0)
	CurFXVol--;
}

void
SndP_FXMixPlus (void)
{
    if(CurFXMix < 127)
	CurFXMix++;
}

void
SndP_FXMixMinus (void)
{
    if(CurFXMix > 0)
	CurFXMix--;
}



static void
__SndPPaneDrawProc (UIPane *pPane, Gfx **ppGfx)
{
    UIItem *    pItem;
    int         i;
    
    CurValStrDL = 0;

    DrawBeveledRect (&pPane->bounds, FALSE, FALSE, ppGfx); 

    /* Draw the sndplayer's ui items. */
    for (i=0; i<pPane->numItems; i++) 
    {
        pItem = &pPane->itemList[i];
        pItem->DrawProc (pItem, ppGfx);
    } 
}


static void
__SndPPlayDrawProc (UIItem *pItem, Gfx **ppGfx)
{
    Rect        globalRect = pItem->bounds;
    
    OffsetRect (&globalRect, pItem->pPane->bounds.left, pItem->pPane->bounds.top);
    
    DrawBeveledRect (&globalRect, FALSE, FALSE, ppGfx);
    pItem->pSpriteIcon->rsp_dl_next = pItem->pSpriteIcon->rsp_dl;
    if (pItem->pSpriteIcon != NULL)
        DrawSpriteIcon (&globalRect, pItem->pSpriteIcon, ppGfx);
}

static void
__SndPTitleDrawProc (UIItem *pItem, Gfx **ppGfx)
{
    Rect        globalRect;

    Item_GetScreenBounds (pItem, &globalRect);
    __SndPDrawStrings(ppGfx, &globalRect, labelStr);     

}

static void
__SndPNBoxDrawProc (UIItem *pItem, Gfx **ppGfx)
{
    Rect        globalRect;
    u8          str[16];
    
    Item_GetScreenBounds (pItem, &globalRect);

    switch(pItem->id)
    {
        case  kSndPNumberNBoxID:
            sprintf(str,"%3d",(u32)CurFXNum);
            break;
        case kSndPPitchNBoxID:
#if USE_DETUNE
	    sprintf(str,"%3d", sfxInst->soundArray[CurFXNum]->keyMap->detune);
#else 
            sprintf(str,"%f",CurFXPitch);
	    str[4] = 0;
#endif
            break;
        case kSndPPanNBoxID:
            sprintf(str,"%3d",(u32)CurFXPan);
            break;
        case kSndPVolumeNBoxID:
            sprintf(str,"%3d",(u32)CurFXVol);
            break;
        case kSndPFXMixNBoxID:
            sprintf(str,"%3d",(u32)CurFXMix);
            break;
    }

    /* Assign unique storage for this item's sprite display list. */
    SFXStrSprite.rsp_dl_next = &SFXStrDL[CurValStrDL++][0]; 

    /* Draw the value box. */
    font_set_font (kPinFont);
    DrawValueStrBox (&globalRect, str, Item_IsSelected (pItem), &SFXStrSprite, ppGfx);
}


static char
__SndPHandleEventProc (UIPane *pPane, UIEvent *pEvent)
{
    char isHandled = FALSE;

    switch (pEvent->type)
    {
      case kMouseDown:
          isHandled = __SndPHandleMouseDown (pPane, &pEvent->where);
          break;

      case kMouseUp:
          isHandled = __SndPHandleMouseUp (pPane, &pEvent->where);
          break;

      case kButtonDown:
          isHandled = __SndPHandleButtonDown (pPane, pEvent->what);
          break;
    }

    return isHandled;
}


static char
__SndPHandleMouseDown (UIPane *pPane, Point *pPt)
{
    UIItem 	*pItem;
    char	isHandled = FALSE;

    Pane_DeselectAllItems (pPane);

    if (Pane_ItemClicked (pPane, pPt, &pItem))
    {   
	assert(pItem != NULL);

        switch (pItem->id)
        {
          case kSndPPlayID:
              SndP_Play();
              break;
          
          case kSndPNumberNBoxID:
          case kSndPPitchNBoxID:
          case kSndPPanNBoxID:
          case kSndPVolumeNBoxID:
          case kSndPFXMixNBoxID:
	      Item_SetSelected (pItem, TRUE);
              break;
        }

	isHandled = TRUE;
    }
    
    return isHandled;
}


static char
__SndPHandleMouseUp (UIPane *pPane, Point *pPt)
{
    return FALSE;
}


static char
__SndPHandleButtonDown (UIPane *pPane, int button)
{
    UIItem *	pItem;

    if (button & CONT_START)
	SndP_Play();

    if (button & CONT_UP || button & CONT_DOWN)
    {
	if (Pane_GetSelectedItem (pPane, &pItem))
	{
	    switch (pItem->id)
	    {
	      case kSndPNumberNBoxID:
		  if (button & CONT_UP)
		      SndP_NumberPlus ();
		  else
		      SndP_NumberMinus ();
		  break;

	      case kSndPPitchNBoxID:
		  if (button & CONT_UP)
		      SndP_PitchPlus ();
		  else
		      SndP_PitchMinus ();
		  break;

	      case kSndPPanNBoxID:
		  if (button & CONT_UP)
		      SndP_PanPlus ();
		  else
		      SndP_PanMinus ();
		  break;

	      case kSndPVolumeNBoxID:
		  if (button & CONT_UP)
		      SndP_VolumePlus ();
		  else
		      SndP_VolumeMinus ();
		  break;

	      case kSndPFXMixNBoxID:
		  if (button & CONT_UP)
		      SndP_FXMixPlus ();
		  else
		      SndP_FXMixMinus ();
		  break;
	    }
	}
    }

    return TRUE;
}




static void
__SndPDrawStrings (Gfx **ppGfx, Rect *bounds, char *str)
{
    Gfx         *gp;
    u32         len = 0;

    gp = *ppGfx;

    while(str[len])
        len++;

    font_init (&gp);

    SFXStrSprite.rsp_dl_next = &SFXStrDL[CurValStrDL][0]; 

    CurValStrDL++;
/*    font_set_transparent( 1, &SFXStrSprite);  */

    font_set_font (kPinFont);			/* sct 11/28/95 - Setup the font. */
    font_set_pos (bounds->left, bounds->top);
    font_set_color (0, 0, 0, 255);
    font_set_scale (1.0, 1.0);
    font_set_win (len, 1);

    font_show_string (&gp, str, &SFXStrSprite);
    
    font_finish (&gp);

    *ppGfx = gp;
}