From 7676ccf397189ad6655a19d5a10479798d916e16 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 18 Jan 2004 02:07:58 +0000 Subject: [PATCH] split RageSound::SearchPosMap --- stepmania/src/RageSound.cpp | 95 +++++++++++++++++++------------------ stepmania/src/RageSound.h | 3 +- 2 files changed, 52 insertions(+), 46 deletions(-) diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index 378058a0dc..51ee0db147 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -571,6 +571,55 @@ float RageSound::GetLengthSeconds() return len / 1000.f; /* ms -> secs */ } +int RageSound::SearchPosMap( const deque &pos_map, int cur_sample, bool *approximate ) +{ + /* sampleno is probably in pos_map. Search to figure out what position + * this sampleno maps to. */ + int closest_position = 0, closest_position_dist = INT_MAX; + for( unsigned i = 0; i < pos_map.size(); ++i ) + { + if( cur_sample >= pos_map[i].sampleno && + cur_sample < pos_map[i].sampleno+pos_map[i].samples ) + { + /* cur_sample lies in this block; it's an exact match. Figure + * out the exact position. */ + int diff = pos_map[i].position - pos_map[i].sampleno; + return cur_sample + diff; + } + + /* See if the current position is close to the beginning of this block. */ + int dist = abs( pos_map[i].sampleno - cur_sample ); + if( dist < closest_position_dist ) + { + closest_position_dist = dist; + closest_position = pos_map[i].position - dist; + } + + /* See if the current position is close to the end of this block. */ + dist = abs( pos_map[i].sampleno + pos_map[i].samples - cur_sample ); + if( dist < closest_position_dist ) + { + closest_position_dist = dist; + closest_position = pos_map[i].position + pos_map[i].samples + dist; + } + } + + /* The sample is out of the range of data we've actually sent. + * Return the closest position. + * + * There are three cases when this happens: + * 1. After the first GetPCM call, but before it actually gets heard. + * 2. After GetPCM returns EOF and the sound has flushed, but before + * SoundStopped has been called. + * 3. Underflow; we'll be given a larger sample number than we know about. + */ + LOG->Trace( "Approximate sound time: sample %i, dist %i, closest %i", cur_sample, closest_position_dist, closest_position ); + + if( approximate ) + *approximate = true; + return closest_position; +} + /* Get the position in frames (ignoring GetOffsetFix). approximate is set to true * if the returned time is approximated because of underrun, the sound not having started * (after Play()) or finished (after EOF) yet. */ @@ -602,51 +651,7 @@ int RageSound::GetPositionSecondsInternal( bool *approximate ) const /* Get our current hardware position. */ int cur_sample = SOUNDMAN->GetPosition(this); - /* sampleno is probably in pos_maps. Search to figure out what position - * this sampleno maps to. */ - - int closest_position = 0, closest_position_dist = INT_MAX; - for(unsigned i = 0; i < pos_map.size(); ++i) { - if(cur_sample >= pos_map[i].sampleno && - cur_sample < pos_map[i].sampleno+pos_map[i].samples) - { - /* cur_sample lies in this block; it's an exact match. Figure - * out the exact position. */ - int diff = pos_map[i].position - pos_map[i].sampleno; - return cur_sample + diff; - } - - /* See if the current position is close to the beginning of this block. */ - int dist = abs(pos_map[i].sampleno - cur_sample); - if(dist < closest_position_dist) - { - closest_position_dist = dist; - closest_position = pos_map[i].position - dist; - } - - /* See if the current position is close to the end of this block. */ - dist = abs(pos_map[i].sampleno + pos_map[i].samples - cur_sample); - if(dist < closest_position_dist) - { - closest_position_dist = dist; - closest_position = pos_map[i].position + pos_map[i].samples + dist; - } - } - - /* The sample is out of the range of data we've actually sent. - * Return the closest position. - * - * There are three cases when this happens: - * 1. After the first GetPCM call, but before it actually gets heard. - * 2. After GetPCM returns EOF and the sound has flushed, but before - * SoundStopped has been called. - * 3. Underflow; we'll be given a larger sample number than we know about. - */ - LOG->Trace("Approximate sound time: sample %i, dist %i, closest %i", cur_sample, closest_position_dist, closest_position); - - if( approximate ) - *approximate = true; - return closest_position; + return SearchPosMap( pos_map, cur_sample, approximate ); } float RageSound::GetPositionSeconds( bool *approximate ) const diff --git a/stepmania/src/RageSound.h b/stepmania/src/RageSound.h index 17dffc5439..49fe0564dd 100644 --- a/stepmania/src/RageSound.h +++ b/stepmania/src/RageSound.h @@ -105,7 +105,8 @@ private: pos_map_t(int samp, int pos, int cnt) { sampleno=samp, position=pos; samples=cnt; } }; deque pos_map; - + static int SearchPosMap( const deque &pos_map, int cur_sample, bool *approximate ); + CString m_sFilePath; /* The amount of data to play (or loop): */