| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586 |
- /*
- ** Command & Conquer Generals(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /*****************************************************************************
- ** **
- ** Westwood Studios Pacific. **
- ** **
- ** Confidential Information **
- ** Copyright (C) 2000 - All Rights Reserved **
- ** **
- ******************************************************************************
- ** **
- ** Project: Dune Emperor **
- ** **
- ** Module: <module> (<prefix>_) **
- ** **
- ** Version: $ID$ **
- ** **
- ** File name: audcache.cpp **
- ** **
- ** Created by: 04/30/99 TR **
- ** **
- ** Description: <description> **
- ** **
- *****************************************************************************/
- /*****************************************************************************
- ** Includes **
- *****************************************************************************/
- #include <string.h>
- #include <memory.h>
- #include <wpaudio/altypes.h> // always include this header first
- #include <wpaudio/memory.h>
- #include <wpaudio/list.h>
- #include <wpaudio/source.h>
- #include <wpaudio/cache.h>
- #include <wpaudio/profiler.h>
- #include <wsys/File.h>
- // 'assignment within condition expression'.
- #pragma warning(disable : 4706)
- DBG_DECLARE_TYPE ( AudioCache );
- DBG_DECLARE_TYPE ( AudioCacheItem );
- /*****************************************************************************
- ** Externals **
- *****************************************************************************/
- /*****************************************************************************
- ** Defines **
- *****************************************************************************/
- #define DEBUG_CACHE 0
- /*****************************************************************************
- ** Private Types **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Data **
- *****************************************************************************/
- /*****************************************************************************
- ** Public Data **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Prototypes **
- *****************************************************************************/
- /*****************************************************************************
- ** Private Functions **
- *****************************************************************************/
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- static void audioCacheAssetClose ( AudioCache *cache )
- {
- if ( cache->assetFile )
- {
- cache->assetFile->close();
- cache->assetFile = NULL;
- }
- cache->assetBytesLeft = 0;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- static int audioCacheAssetOpen ( AudioCache *cache, const char *name )
- {
- audioCacheAssetClose ( cache );
- if ( name == NULL || cache->openAssetCB == NULL )
- {
- return FALSE;
- }
- cache->assetFile = cache->openAssetCB( name );
- if ( !cache->assetFile )
- {
- return FALSE;
- }
- if ( !AudioFormatReadWaveFile ( cache->assetFile, &cache->assetFormat, &cache->assetBytesLeft ))
- {
- audioCacheAssetClose ( cache );
- return FALSE;
- }
- return TRUE;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- int audioCacheAssetRead ( AudioCache *cache, void *data, int bytes )
- {
- if ( bytes > cache->assetBytesLeft )
- {
- bytes = cache->assetBytesLeft;
- }
- return cache->assetFile ? cache->assetFile->read ( data, bytes ) : 0 ;
- }
- /*****************************************************************************
- ** Public Functions **
- *****************************************************************************/
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- AudioCache* AudioCacheCreate ( int cacheSize, int maxItems, int frameSize )
- {
- AudioCache *cache;
- int frameBytes;
- int pages;
- ALLOC_STRUCT ( cache, AudioCache );
- DBG_SET_TYPE ( cache, AudioCache );
- cache->frameSize = frameSize;
- frameBytes = sizeof ( AudioFrame ) + frameSize;
- pages = cacheSize/frameBytes;
- cache->framePool = MemoryPoolCreate ( pages, frameBytes );
- cache->itemPool = MemoryPoolCreate ( maxItems, sizeof ( AudioCacheItem ) );
- ListInit ( &cache->items );
- cache->assetFile = NULL;
- AudioFormatInit ( &cache->assetFormat );
- ProfCacheInit ( &cache->profile, pages, frameSize );
- ProfCacheUpdateInterval ( &cache->profile, 10 ); // every ten milliseconds
-
- if ( !cache->framePool || !cache->itemPool )
- {
- AudioCacheDestroy ( cache );
- cache = NULL;
- }
- return cache;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void AudioCacheDestroy ( AudioCache *cache )
- {
- AudioCacheItem *item;
-
- DBG_ASSERT_TYPE ( cache, AudioCache );
- while ( ( item = (AudioCacheItem *) ListNodeNext ( &cache->items )) )
- {
- AudioCacheItemFree ( item );
- }
- if ( cache->framePool )
- {
- MemoryPoolDestroy ( cache->framePool );
- }
- if ( cache->itemPool )
- {
- MemoryPoolDestroy ( cache->itemPool );
- }
- DBG_INVALIDATE_TYPE ( cache );
- AudioMemFree ( cache );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- AudioCacheItem* AudioCacheGetItem ( AudioCache *cache, const char *name )
- {
- AudioCacheItem *item, *head;
-
- DBG_ASSERT_TYPE ( cache, AudioCache );
- item = head = (AudioCacheItem *) &cache->items ;
- while ( (item = (AudioCacheItem *) item->nd.next ) != head )
- {
- if ( item->valid && item->name == name )
- {
- return item;
- }
- }
- return NULL;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void AudioCacheInvalidate ( AudioCache *cache )
- {
- AudioCacheItem *item, *head;
-
- DBG_ASSERT_TYPE ( cache, AudioCache );
- item = head = (AudioCacheItem *) &cache->items ;
- while ( (item = (AudioCacheItem *) item->nd.next ) != head )
- {
- item->valid = FALSE;
- }
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- AudioCacheOpenCB* AudioCacheSetOpenCB ( AudioCache *cache, AudioCacheOpenCB *cb )
- {
- DBG_ASSERT_TYPE ( cache, AudioCache );
- AudioCacheOpenCB *old = cache->openAssetCB;
- cache->openAssetCB = cb;
- return old;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- AudioCacheItem* AudioCacheLoadItem ( AudioCache *cache, const char *name )
- {
- AudioCacheItem *item;
- int error;
-
- DBG_ASSERT_TYPE ( cache, AudioCache );
- if ( (item = AudioCacheGetItem ( cache, name )))
- {
- #if DEBUG_CACHE
- DBGPRINTF (("ACACHE: %10s - Cached\n", item->name ));
- #endif
- ListNodeRemove ( &item->nd );
- ListNodeAppend ( &cache->items, &item->nd );
- ProfCacheHit ( &cache->profile );
- return item;
- }
- ProfCacheMiss ( &cache->profile );
- // item is not in the cache so load it
- // see first if the sample exists
- #if DEBUG_CACHE
- DBGPRINTF (("ACACHE: %10s - Loading\n", name ));
- #endif
- ProfCacheLoadStart ( &cache->profile, 0 );
- if ( !audioCacheAssetOpen( cache, name ))
- {
- #if DEBUG_CACHE
- DBGPRINTF (("does not exist\n"));
- #endif
- goto none;
- }
- item = (AudioCacheItem *) MemoryPoolGetItem ( cache->itemPool );
- if ( !item )
- {
- // free the oldest item so that we can use it's item struct
- AudioCacheFreeOldestItem ( cache );
- item = (AudioCacheItem *) MemoryPoolGetItem ( cache->itemPool );
- if ( !item )
- {
- // the oldest item could not be freed because it was still playing
- DBGPRINTF (("Audio cache overflow\n"));
- audioCacheAssetClose ( cache );
- goto none;
- }
- }
- // prepare item for use
- item->name = name;
- item->cache = cache;
- ListNodeInit ( &item->nd );
- LockInit ( &item->lock );
- AudioSampleInit ( &item->sample );
- AudioSampleSetName ( &item->sample, name );
- AudioFormatInit ( &item->format );
- item->sample.Format = &item->format;
- DBG_SET_TYPE ( item, AudioCacheItem );
- error = FALSE;
- // ok load sample data in to cache
- {
- int bytesToTransfer;
- int bytes;
- int bytesTransfered;
- bytesToTransfer = cache->assetBytesLeft;
- while ( bytesToTransfer )
- {
- AudioFrame *frame;
- void *data;
- if ( (bytes = cache->frameSize ) > bytesToTransfer )
- {
- bytes = bytesToTransfer;
- }
- while ( ! (frame = (AudioFrame *) MemoryPoolGetItem ( cache->framePool )))
- {
- if ( !AudioCacheFreeOldestItem ( cache ) )
- {
- break;
- }
- }
- if ( !frame )
- {
- error = TRUE;
- break;
- }
- data = (void *) ( (uint) frame + sizeof ( AudioFrame ));
- AudioFrameInit ( frame, data, bytes );
- AudioSampleAddFrame ( &item->sample, frame );
- bytesTransfered = audioCacheAssetRead ( cache, data, bytes );
- ProfCacheAddLoadBytes ( &cache->profile, bytesTransfered );
- ProfCacheAddPage ( &cache->profile );
- ProfCacheFill ( &cache->profile, bytesTransfered );
- if ( bytesTransfered != bytes )
- {
- error = TRUE;
- break;
- }
- bytesToTransfer -= bytesTransfered;
- }
- }
- if ( error )
- {
- #if DEBUG_CACHE
- DBGPRINTF (("FAILED\n"));
- #endif
- AudioCacheItemFree ( item );
- goto none;
- }
- #if DEBUG_CACHE
- DBGPRINTF (("done\n"));
- #endif
- // update the format structure
- memcpy ( &item->format, &cache->assetFormat, sizeof ( AudioFormat) );
- ListNodeAppend ( &cache->items, &item->nd );
- item->valid = TRUE;
- audioCacheAssetClose ( cache );
- ProfCacheLoadEnd ( &cache->profile );
- return item;
- none:
- ProfCacheLoadEnd ( &cache->profile );
- return NULL;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- // AudioCacheFreeOldestItem()
- // Free the oldest UNUSED item in the list. These should tend to be
- // towards the end of the list, as old events should expire and unlock.
- // But not always (e.g. loops), so don't count on it.
- int AudioCacheFreeOldestItem( AudioCache *cache )
- {
- AudioCacheItem *item;
- DBG_ASSERT_TYPE( cache, AudioCache );
- item = (AudioCacheItem*) ListNodePrev( &cache->items );
- while ( item )
- {
- if ( !AudioCacheItemInUse( item ) )
- {
- AudioCacheItemFree( item );
- return TRUE;
- }
- item = (AudioCacheItem*) ListNodePrev( (ListNode*) item );
- }
- return FALSE;
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void AudioCacheItemLock ( AudioCacheItem *item )
- {
-
- DBG_ASSERT_TYPE ( item, AudioCacheItem );
- LockAcquire ( &item->lock );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void AudioCacheItemUnlock ( AudioCacheItem *item )
- {
-
- DBG_ASSERT_TYPE ( item, AudioCacheItem );
- LockRelease ( &item->lock );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- int AudioCacheItemInUse ( AudioCacheItem *item )
- {
-
- DBG_ASSERT_TYPE ( item, AudioCacheItem );
- return Locked ( &item->lock );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- void AudioCacheItemFree ( AudioCacheItem *item )
- {
-
- DBG_ASSERT_TYPE ( item, AudioCacheItem );
- if ( ListNodeInList ( &item->nd ))
- {
- ListNodeRemove ( &item->nd );
- }
- DBG_MSGASSERT ( !AudioCacheItemInUse ( item ), ("cache item is still in use"));
- #if DEBUG_CACHE
- DBGPRINTF (("ACACHE: %10s - Freeing\n", AudioBagGetItemName ( item->cache->bag, item->id )));
- #endif
- // return frames to frame pool
- {
- AudioFrame *frame;
- while ( (frame = (AudioFrame *) ListNodeNext ( &item->sample.Frames )) )
- {
- ListNodeRemove ( &frame->nd );
- ProfCacheRemove ( &item->cache->profile, frame->Bytes );
- AudioFrameDeinit ( frame );
- ProfCacheRemovePage ( &item->cache->profile );
- MemoryPoolReturnItem ( item->cache->framePool, frame );
- }
- }
- AudioSampleDeinit ( &item->sample );
- DBG_INVALIDATE_TYPE ( item );
- MemoryPoolReturnItem ( item->cache->itemPool, item );
- }
- /******************************************************************/
- /* */
- /* */
- /******************************************************************/
- AudioSample* AudioCacheItemSample ( AudioCacheItem *item )
- {
- DBG_ASSERT_TYPE ( item, AudioCacheItem );
- return &item->sample;
- }
|