OpenBarnyard
 
Loading...
Searching...
No Matches
ASound.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "ASound.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
13 : m_iFlags( 0 ), m_iId( 0 ), m_uiCategoryIndex( 0 ), m_ui8Priority( 255 ), m_fMinDist( 1.0f ), m_vecSamples( AMemory::GetAllocator( AMemory::POOL_Sound ), 1, 1 ), m_vecTracks( AMemory::GetAllocator( AMemory::POOL_Sound ), 1, 1 )
14{
15}
16
18{
19 TINT iFirstWaveIndex = 0;
20 for ( TINT iTrack = 0; iTrack < a_iTrack; iTrack++ )
21 iFirstWaveIndex += m_vecTracks[ iTrack ];
22
23 return iFirstWaveIndex;
24}
25
26TINT ASound::GetRandomSample( TINT a_iWaveIndexMin, TINT a_iWaveIndexMax )
27{
28 return a_iWaveIndexMin + ARandom::GetSingleton()->m_oRandom.GetInt( a_iWaveIndexMax - a_iWaveIndexMin );
29}
30
31TINT ASound::GetRandomSampleWeighted( TINT a_iTrackIndex, TINT a_iWaveIndexMin, TINT a_iWaveIndexMax )
32{
33 TASSERT( a_iTrackIndex != -1 );
34 TASSERT( a_iWaveIndexMax - a_iWaveIndexMin > 0 );
35
36 TINT iNumWaves = a_iWaveIndexMax - a_iWaveIndexMin;
37 TINT iTrackNumWaves = m_vecTracks[ a_iTrackIndex ];
38 TINT* pWeights = new ( AMemory::GetMemBlock( AMemory::POOL_Sound ) ) TINT[ iTrackNumWaves ];
39
40 TINT iTotalWeight = 0;
41 for ( TINT i = 0; i < a_iWaveIndexMax - a_iWaveIndexMin; i++ )
42 {
43 pWeights[ i ] = m_vecSamples[ i + a_iWaveIndexMin ].iWeight;
44 iTotalWeight += pWeights[ i ];
45 }
46
47 TINT* pWeightedSamples = new ( AMemory::GetMemBlock( AMemory::POOL_Sound ) ) TINT[ iTotalWeight ];
48 for ( TINT i = 0, j = 0; i < a_iWaveIndexMax - a_iWaveIndexMin; i++ )
49 {
50 // Copy sample index N (weight) times to the array
51 for ( TINT k = 0; k < pWeights[ i ]; k++ )
52 {
53 TASSERT( j < iTotalWeight );
54 pWeightedSamples[ j++ ] = a_iWaveIndexMin + i;
55 }
56 }
57
58 TINT iSelectedSample = ARandom::GetSingleton()->m_oRandom.GetInt( iTotalWeight );
59
60 delete[] pWeights;
61 delete[] pWeightedSamples;
62
63 return iSelectedSample;
64}
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
int TINT
Definition Typedefs.h:7
@ POOL_Sound
Definition AMemory.h:37
static Toshi::TMemory::MemBlock * GetMemBlock(POOL a_ePool)
Definition AMemory.cpp:38
TINT GetRandomSample(TINT a_iWaveIndexMin, TINT a_iWaveIndexMax)
Definition ASound.cpp:26
ASound()
Definition ASound.cpp:12
TINT GetFirstWaveForTrack(TINT a_iTrack) const
Definition ASound.cpp:17
TINT GetRandomSampleWeighted(TINT a_iTrackIndex, TINT a_iWaveIndexMin, TINT a_iWaveIndexMax)
Definition ASound.cpp:31