Files
itgmania212121/src/RandomSample.cpp
T

151 lines
4.0 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
#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-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
}
2006-01-22 01:00:06 +00:00
bool RandomSample::Load( RString 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();
}
2006-01-22 01:00:06 +00:00
bool RandomSample::LoadSoundDir( RString 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. */
2005-12-21 07:50:14 +00:00
if(IsADirectory(sDir) && sDir[sDir.size()-1] != "/" )
2003-12-10 09:26:05 +00:00
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
2006-01-22 01:00:06 +00:00
vector<RString> arraySoundFiles;
2002-03-10 10:30:45 +00:00
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;
}
2006-01-22 01:00:06 +00:00
bool RandomSample::LoadSound( RString sSoundFilePath )
2002-03-10 10:30:45 +00:00
{
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;
2005-04-27 01:23:56 +00:00
if( !pSS->Load(sSoundFilePath) )
{
LOG->Trace( "Error loading \"%s\": %s", sSoundFilePath.c_str(), pSS->GetError().c_str() );
delete pSS;
return false;
}
2002-03-10 10:30:45 +00:00
2002-10-31 04:23:39 +00:00
m_pSamples.push_back( pSS );
2002-03-10 10:30:45 +00:00
return true;
}
int RandomSample::GetNextToPlay()
2002-03-10 10:30:45 +00:00
{
// play one of the samples
if( m_pSamples.empty() )
return -1;
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 = RandomInt( 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_iIndexLastPlayed = iIndexToPlay;
return iIndexToPlay;
}
void RandomSample::PlayRandom()
{
int iIndexToPlay = GetNextToPlay();
if( iIndexToPlay == -1 )
return;
m_pSamples[iIndexToPlay]->Play();
}
void RandomSample::PlayCopyOfRandom()
{
int iIndexToPlay = GetNextToPlay();
if( iIndexToPlay == -1 )
return;
2007-01-18 08:45:58 +00:00
m_pSamples[iIndexToPlay]->PlayCopy();
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();
}
2004-05-31 21:35:31 +00:00
/*
* (c) 2001-2004 Chris Danford, Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/