AeN64DriverNotify.c++ 8.32 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
//
// AeN64DriverNotify.c++
//
// This file contains the client notify handlers that
// are called by the assetbase in response to actions
// made on assets such as create, update, delete, etc.
//
// Since bank assets and fx assets are treated so
// differently by the n64, i have separated out their
// handling into different routines for clarity.
//

#include <assert.h>

#include "AeN64Driver.h"
#include "AeConfig.h"
#include "AeAsset.h"
#include "AeAssetBase.h"
#include "GList.h"


void
AeN64Driver::CreateAsset (AeAsset * asset, Boolean suspended)
{
    assert (asset);

    if (asset->IsBankComponent ())
	createBankAsset (asset, suspended);

    else if (asset->IsFxComponent ())
	createFxAsset (asset, suspended);
}


void
AeN64Driver::createBankAsset (AeAsset * asset, Boolean suspended)
{
    allocBankMem (asset);

    if (asset->GetType () == kBankType)
	fBank = (AeBankAsset *)asset;

    updateBankAsset (asset, suspended);
}


void
AeN64Driver::createFxAsset (AeAsset * asset, Boolean suspended)
{
    updateFxAsset (asset, suspended);
}


void
AeN64Driver::UpdateAsset (AeAsset * asset, Boolean suspended)
{
    assert (asset);

    if (asset->IsBankComponent ())
	updateBankAsset (asset, suspended);

    // pass non-bank updates through (eg. fx updates)
    else if (asset->IsFxComponent ())
	updateFxAsset (asset, suspended);
}

// if a bank asset changes, make sure the bank
// is still loadable before proceeding with the update.
// if we're suspended, do nothing since resume will handle it.
void
AeN64Driver::updateBankAsset (AeAsset * asset, Boolean suspended)
{
    if (!suspended && checkBank ())
	updateAsset (asset);
}


void
AeN64Driver::updateFxAsset (AeAsset * asset, Boolean suspended)
{
    checkFx ();			// make sure the effect is still okay
    updateAsset (asset);	// update the asset
}


void
AeN64Driver::DeleteAsset (AeAsset * asset, Boolean suspended)
{
    assert (asset);

    if (asset->IsBankComponent ())
	deleteBankAsset (asset, suspended);

    else if (asset->IsFxComponent ())
	deleteFxAsset (asset, suspended);
}


void
AeN64Driver::deleteBankAsset (AeAsset * asset, Boolean suspended)
{
    // if the deleted asset is the current bank, then tell the UI to offline it
    if (asset == fBank)
    {
	fBank->SetOnline (False);
	fAssetBase->NotifyOnline (fBank);
	fBank = NULL;
    }

    // if we're not suspended the check the bank's loadability	
    if (!suspended)
	checkBank ();

    deallocBankMem (asset);
}


void
AeN64Driver::deleteFxAsset (AeAsset * asset, Boolean suspended)
{
    // if the asset being deleted is the current fx, the invalidate the fx
    if (asset == fConfig->GetFxAsset ())
	fConfig->SetFxAsset (NULL);

    // if we're not suspended the check the effect's loadability	
    if (!suspended)
	checkFx ();
}


void
AeN64Driver::OnlineAsset (AeAsset * asset, Boolean suspended)
{
    if (asset->IsBankComponent ())
	onlineBankAsset (asset, suspended);

    else if (asset->IsFxComponent ())
	onlineFxAsset (asset, suspended);
}


void
AeN64Driver::onlineBankAsset (AeAsset * asset, Boolean suspended)
{
    // we currently don't allow the user to put banks on and offline
    if (asset == fBank && isBankLoadable ())
	asset->SetOnline (True);
}


void
AeN64Driver::onlineFxAsset (AeAsset * asset, Boolean suspended)
{
    if (asset->GetType () == kFxType)
    {
	AeFxAsset * fxAsset = fConfig->GetFxAsset ();
	AeErr err;

	// if asset is not loadable then take asset offline
	if (!isFxLoadable ((AeFxAsset *)asset))
	{
	    asset->SetOnline (False);

	    if (fxAsset != asset)
		return;	    
	}
	
	// if asset is to be put online, then take any currently
	// online asset offline
	if (asset->IsOnline ())
	{
	    if (fxAsset)
	    {
		fxAsset->SetOnline (False);
		fAssetBase->NotifyOnline (fxAsset);
	    }
	}

	// if asset is to be put offline, then indicate this
	// by setting the currently configured fx to NULL
	else
	{
	    assert (asset == fxAsset);
	    asset = NULL;
	}

	fConfig->SetFxAsset ((AeFxAsset *)asset);
	
	fNeedsConfigure = True;
	if (!suspended && (err = ReconfigureAudio ()))
	{
	    if (asset)
		asset->SetOnline (False);
	}
    }    
}


void
AeN64Driver::UpdateResume (void)
{
    if (fNeedsConfigure)
    {
	ReconfigureAudio ();
    }
    else
    {
	// if the fx don't cause the bank to be reloaded
	// then reload the bank
	if (!checkFx ())
	{
	    if (isBankLoadable ())
		loadBank ();

	    else if (fBankOnlineHW)
		unloadBank ();
	}
    }
}



//
// call this routine to correctly place the bank
// online or offline depending on the loadability
// of the current bank.
//
Boolean
AeN64Driver::checkBank (void)
{
    if (isBankLoadable ())
    {
	if (!fBankOnlineHW)
	    loadBank ();
    }
    else
    {
	if (fBankOnlineHW)
	    unloadBank ();
    }

    return fBankOnlineHW;
}


Boolean
AeN64Driver::isBankOnline (void)
{
    return fBank && fBank->IsOnline ();
}


//
// Returns true if the bank is complete enough
// to safely download to the n64.  Otherwise returns false.
//
// The bank asset must exist
// The bank asset must have at least one instrument
//	(else initFromBank will loop forever)
// The bank's instruments must have at least one sound
//	(this is just for consistency, i don't think it would crash the n64)
// The bank's sounds must all have keymaps
//	(else midiNoteOn, lookupSoundQuick will dereference a missing keymap)
// The bank's sounds must all have wavetables
//	(else midiNoteOn will dereference a missing wavetable)
// The bank's sounds must all have envelopes
//	(else midiNoteOn will dereference a missing envelope)
//
Boolean
AeN64Driver::isBankLoadable (void)
{
    GList<AeAsset *> *	childList;
    AeAsset *		child;
    int			i;
    AeInstAsset *	percussion;

    if (!fBank)
	return False;

    childList = fBank->GetChildList ();
    assert (childList);

    if (childList->GetNum () == 0)
	return False;

    childList->Start (i);
    while (childList->Next (i, child))
	if (!isInstLoadable ((AeInstAsset *)child))
	    return False;

    if (percussion = fBank->GetPercussion ())
	if (!isInstLoadable (percussion))
	    return False;

    return True;
}


Boolean
AeN64Driver::isInstLoadable (AeInstAsset * asset)
{
    GList<AeAsset *> *	childList;
    AeAsset *		child;
    int			i;

    assert (asset);

    childList = asset->GetChildList ();
    assert (childList);

    if (childList->GetNum () == 0)
	return False;

    childList->Start (i);
    while (childList->Next (i, child))
	if (!isSoundLoadable ((AeSoundAsset *)child))
	    return False;

    return True;
}


Boolean
AeN64Driver::isSoundLoadable (AeSoundAsset * asset)
{
    assert (asset);

    if (asset->GetKeymap () == NULL)
	return False;

    if (asset->GetEnvelope () == NULL)
	return False;

    if (asset->GetWave () == NULL)
	return False;

    return True;
}


//
// Returns True if the effect is complete enough
// to be downloaded to the n64 safely (ie so it
// won't cause the n64 to crash).
//
// The only criterion is that the effect contains
// at least one section.
//
Boolean
AeN64Driver::isFxLoadable (AeFxAsset * asset)
{
    GList<AeAsset *> *	childList;

    if (asset)
    {
	childList = asset->GetChildList ();
	assert (childList);

	if (childList->GetNum () > 0)
	    return True;
    }

    return False;
}


//
// call this routine to check if the fx should
// no longer be online and take it offline if
// it is not loadable.  since fx are manually put
// online, there is no need to automatically load it.
// this is important if the user removes all the
// children of an effect.
//
Boolean
AeN64Driver::checkFx (void)
{
    AeFxAsset * fxAsset;

    if (fxAsset = fConfig->GetFxAsset ())
    {
	if (!isFxLoadable (fxAsset) && isFxOnline (fxAsset))
	{
	    fxAsset->SetOnline (False);
	    fAssetBase->NotifyOnline ((AeAsset *)fxAsset);
	    return True;
	}	    
    }

    return False;
}


Boolean
AeN64Driver::isFxOnline (AeAsset * asset)
{
    if (!asset)
	return False;

    if (asset->GetType () == kFxSxnType)
	return isFxSxnOnline ((AeFxSxnAsset *)asset);

    return asset->IsOnline ();
}


Boolean
AeN64Driver::isFxSxnOnline (AeFxSxnAsset * asset)
{
    GList<AeAsset *> *	parentList;
    AeAsset *		parent;
    Boolean		loaded = False;

    assert (asset->GetType () == kFxSxnType);

    parentList = asset->GetParentList ();

    if (parentList->GetNum () > 0)
    {
        // only 1 parent allowed per fx section
	assert (parentList->GetNum () == 1);	

	parent = (*parentList)[0];
	loaded =  parent->IsOnline ();
    }

    return loaded;
}