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-04-16 17:31:00 +00:00
|
|
|
#include "ErrorCatcher/ErrorCatcher.h"
|
2002-03-10 10:30:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
RandomSample::RandomSample()
|
|
|
|
|
{
|
|
|
|
|
m_iIndexLastPlayed = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RandomSample::~RandomSample()
|
|
|
|
|
{
|
|
|
|
|
for( int i=0; i<m_pSamples.GetSize(); i++ )
|
|
|
|
|
SAFE_DELETE( m_pSamples[i] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool RandomSample::LoadSoundDir( CString sDir )
|
|
|
|
|
{
|
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
|
|
for( int i=0; i<arraySoundFiles.GetSize(); i++ )
|
|
|
|
|
LoadSound( sDir + arraySoundFiles[i] );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RandomSample::LoadSound( CString sSoundFilePath )
|
|
|
|
|
{
|
2002-05-01 19:14:55 +00:00
|
|
|
LOG->WriteLine( "RandomSample::LoadSound( %s )", sSoundFilePath );
|
2002-03-10 10:30:45 +00:00
|
|
|
|
|
|
|
|
RageSoundSample* pSS = new RageSoundSample;
|
|
|
|
|
pSS->Load( sSoundFilePath );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_pSamples.Add( pSS );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RandomSample::PlayRandom()
|
|
|
|
|
{
|
|
|
|
|
// play one of the samples
|
|
|
|
|
if( m_pSamples.GetSize() == 0 )
|
|
|
|
|
{
|
2002-05-01 19:14:55 +00:00
|
|
|
LOG->WriteLine( "WARNING: Tried to play a RandomSample that has 0 sounds loaded." );
|
2002-03-10 10:30:45 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-04-28 20:42:32 +00:00
|
|
|
int iIndexToPlay;
|
|
|
|
|
for( int i=0; i<5; i++ )
|
|
|
|
|
{
|
|
|
|
|
iIndexToPlay = rand() % m_pSamples.GetSize();
|
|
|
|
|
if( iIndexToPlay != m_iIndexLastPlayed )
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-10 10:30:45 +00:00
|
|
|
m_pSamples[iIndexToPlay]->Play();
|
|
|
|
|
m_iIndexLastPlayed = iIndexToPlay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RandomSample::Pause()
|
|
|
|
|
{
|
|
|
|
|
m_pSamples[m_iIndexLastPlayed]->Pause();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RandomSample::Stop()
|
|
|
|
|
{
|
|
|
|
|
if( m_iIndexLastPlayed == -1 ) // nothing is currently playing
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_pSamples[m_iIndexLastPlayed]->Stop();
|
|
|
|
|
}
|