Files
itgmania212121/stepmania/src/RandomSample.cpp
T

121 lines
2.7 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
2002-03-10 10:30:45 +00:00
/*
-----------------------------------------------------------------------------
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 "RageSound.h"
2002-03-10 10:30:45 +00:00
#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;
}
2003-04-13 00:44:50 +00:00
2002-03-10 10:30:45 +00:00
RandomSample::~RandomSample()
{
2003-07-09 02:27:05 +00:00
UnloadAll();
2002-03-10 10:30:45 +00:00
}
bool RandomSample::Load( CString sFilePath, int iMaxToLoad )
{
2003-10-29 19:39:02 +00:00
if( GetExtension(sFilePath) == "" )
return LoadSoundDir( sFilePath, iMaxToLoad );
else
return LoadSound( sFilePath );
}
2003-07-09 02:27:05 +00:00
void RandomSample::UnloadAll()
{
for( unsigned i=0; i<m_pSamples.size(); i++ )
delete m_pSamples[i];
m_pSamples.clear();
}
bool RandomSample::LoadSoundDir( CString sDir, int iMaxToLoad )
2002-03-10 10:30:45 +00:00
{
2002-07-23 01:41:40 +00:00
if( sDir == "" )
return true;
2003-01-19 21:24:11 +00:00
#if 0
/* (don't want to do this just yet) */
/* If this is actually a directory, add a backslash to the filename,
* so we'll look for eg. themes\Default\sounds\sDir\*.mp3. Otherwise,
* don't, so we'll look for all of the files starting with sDir,
* eg. themes\Default\sounds\sDir*.mp3. */
2003-12-10 09:26:05 +00:00
if(IsADirectory(sDir) && sDir[sDir.GetLength()-1] != "/" )
sDir += "/";
2003-01-19 21:24:11 +00:00
#else
// make sure there's a slash at the end of this path
2003-12-10 09:26:05 +00:00
if( sDir.Right(1) != "/" )
sDir += "/";
2003-01-19 21:24:11 +00:00
#endif
2002-03-10 10:30:45 +00:00
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 );
2003-05-13 13:35:32 +00:00
random_shuffle( arraySoundFiles.begin(), arraySoundFiles.end() );
arraySoundFiles.resize( min( arraySoundFiles.size(), (unsigned)iMaxToLoad ) );
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 )
{
2003-04-25 00:27:30 +00:00
LOG->Trace( "RandomSample::LoadSound( %s )", sSoundFilePath.c_str() );
2002-03-10 10:30:45 +00:00
2003-01-02 08:13:34 +00:00
RageSound *pSS = new RageSound;
2002-03-10 10:30:45 +00:00
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
if( m_pSamples.empty() )
2002-03-10 10:30:45 +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
{
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();
}