2002-03-10 10:30:45 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
File: RandomSample.h
|
|
|
|
|
|
|
|
|
|
Desc: Holds multiple sounds samples and can play a random sound easily.
|
|
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
2002-03-10 10:30:45 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "RandomSample.h"
|
|
|
|
|
#include "RageUtil.h"
|
2002-05-01 19:14:55 +00:00
|
|
|
#include "RageLog.h"
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2002-03-10 10:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
RandomSample::RandomSample()
|
|
|
|
|
{
|
|
|
|
|
m_iIndexLastPlayed = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RandomSample::~RandomSample()
|
|
|
|
|
{
|
2002-10-31 03:16:02 +00:00
|
|
|
for( unsigned i=0; i<m_pSamples.size(); i++ )
|
2002-03-10 10:30:45 +00:00
|
|
|
SAFE_DELETE( m_pSamples[i] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool RandomSample::LoadSoundDir( CString sDir )
|
|
|
|
|
{
|
2002-07-23 01:41:40 +00:00
|
|
|
if( sDir == "" )
|
|
|
|
|
return true;
|
|
|
|
|
|
2002-03-10 10:30:45 +00:00
|
|
|
// make sure there's a backslash at the end of this path
|
|
|
|
|
if( sDir[sDir.GetLength()-1] != '\\' )
|
|
|
|
|
sDir += "\\";
|
|
|
|
|
|
|
|
|
|
CStringArray arraySoundFiles;
|
|
|
|
|
GetDirListing( sDir + "*.mp3", arraySoundFiles );
|
2002-05-19 01:59:48 +00:00
|
|
|
GetDirListing( sDir + "*.ogg", arraySoundFiles );
|
2002-03-10 10:30:45 +00:00
|
|
|
GetDirListing( sDir + "*.wav", arraySoundFiles );
|
|
|
|
|
|
2002-10-31 03:16:02 +00:00
|
|
|
for( unsigned i=0; i<arraySoundFiles.size(); i++ )
|
2002-03-10 10:30:45 +00:00
|
|
|
LoadSound( sDir + arraySoundFiles[i] );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RandomSample::LoadSound( CString sSoundFilePath )
|
|
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
LOG->Trace( "RandomSample::LoadSound( %s )", sSoundFilePath.GetString() );
|
2002-03-10 10:30:45 +00:00
|
|
|
|
|
|
|
|
RageSoundSample* pSS = new RageSoundSample;
|
|
|
|
|
pSS->Load( sSoundFilePath );
|
|
|
|
|
|
|
|
|
|
|
2002-10-31 04:23:39 +00:00
|
|
|
m_pSamples.push_back( pSS );
|
2002-03-10 10:30:45 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RandomSample::PlayRandom()
|
|
|
|
|
{
|
|
|
|
|
// play one of the samples
|
2002-10-31 03:16:02 +00:00
|
|
|
if( m_pSamples.empty() )
|
2002-03-10 10:30:45 +00:00
|
|
|
{
|
2002-11-13 05:17:15 +00:00
|
|
|
// LOG->Trace( "WARNING: Tried to play a RandomSample that has 0 sounds loaded." );
|
2002-09-07 10:43:16 +00:00
|
|
|
return;
|
2002-03-10 10:30:45 +00:00
|
|
|
}
|
2002-09-07 10:43:16 +00:00
|
|
|
|
|
|
|
|
int iIndexToPlay = 0;
|
|
|
|
|
for( int i=0; i<5; i++ )
|
2002-03-10 10:30:45 +00:00
|
|
|
{
|
2002-10-31 03:16:02 +00:00
|
|
|
iIndexToPlay = rand() % m_pSamples.size();
|
2002-09-07 10:43:16 +00:00
|
|
|
if( iIndexToPlay != m_iIndexLastPlayed )
|
|
|
|
|
break;
|
2002-03-10 10:30:45 +00:00
|
|
|
}
|
2002-09-07 10:43:16 +00:00
|
|
|
|
|
|
|
|
m_pSamples[iIndexToPlay]->Play();
|
|
|
|
|
m_iIndexLastPlayed = iIndexToPlay;
|
2002-03-10 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RandomSample::Stop()
|
|
|
|
|
{
|
|
|
|
|
if( m_iIndexLastPlayed == -1 ) // nothing is currently playing
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_pSamples[m_iIndexLastPlayed]->Stop();
|
|
|
|
|
}
|