2004-04-11 04:51:03 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "RageSoundPosMap.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
#include "RageUtil.h"
|
2004-07-14 21:06:03 +00:00
|
|
|
#include "RageTimer.h"
|
2006-11-30 01:52:12 +00:00
|
|
|
#include "Foreach.h"
|
2004-04-11 04:51:03 +00:00
|
|
|
|
2006-11-30 02:05:36 +00:00
|
|
|
#include <list>
|
2006-11-30 01:39:58 +00:00
|
|
|
|
2004-04-11 04:51:03 +00:00
|
|
|
/* The number of frames we should keep pos_map data for. This being too high
|
|
|
|
|
* is mostly harmless; the data is small. */
|
|
|
|
|
const int pos_map_backlog_frames = 100000;
|
|
|
|
|
|
2006-11-30 01:39:58 +00:00
|
|
|
struct pos_map_t
|
|
|
|
|
{
|
2006-11-30 01:46:20 +00:00
|
|
|
int64_t m_iSourceFrame;
|
|
|
|
|
int64_t m_iDestFrame;
|
2006-11-30 01:39:58 +00:00
|
|
|
int64_t m_iFrames;
|
|
|
|
|
|
2006-11-30 01:46:20 +00:00
|
|
|
pos_map_t() { m_iSourceFrame = 0; m_iDestFrame = 0; m_iFrames = 0; }
|
2006-11-30 01:39:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pos_map_impl
|
|
|
|
|
{
|
2006-11-30 02:05:36 +00:00
|
|
|
list<pos_map_t> m_Queue;
|
2006-11-30 01:39:58 +00:00
|
|
|
void Cleanup();
|
|
|
|
|
};
|
|
|
|
|
|
2004-04-16 22:28:57 +00:00
|
|
|
pos_map_queue::pos_map_queue()
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 01:39:58 +00:00
|
|
|
m_pImpl = new pos_map_impl;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-30 01:39:58 +00:00
|
|
|
pos_map_queue::~pos_map_queue()
|
|
|
|
|
{
|
|
|
|
|
delete m_pImpl;
|
|
|
|
|
}
|
2004-04-11 04:51:03 +00:00
|
|
|
|
2004-04-16 22:28:57 +00:00
|
|
|
pos_map_queue::pos_map_queue( const pos_map_queue &cpy )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
|
|
|
|
*this = cpy;
|
2006-11-30 01:39:58 +00:00
|
|
|
m_pImpl = new pos_map_impl( *cpy.m_pImpl );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pos_map_queue &pos_map_queue::operator=( const pos_map_queue &rhs )
|
|
|
|
|
{
|
|
|
|
|
delete m_pImpl;
|
|
|
|
|
m_pImpl = new pos_map_impl( *rhs.m_pImpl );
|
|
|
|
|
return *this;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-30 02:11:08 +00:00
|
|
|
void pos_map_queue::Insert( int64_t iSourceFrame, int iFrames, int iDestFrame )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 01:39:58 +00:00
|
|
|
if( m_pImpl->m_Queue.size() )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
|
|
|
|
/* Optimization: If the last entry lines up with this new entry, just merge them. */
|
2006-11-30 01:39:58 +00:00
|
|
|
pos_map_t &last = m_pImpl->m_Queue.back();
|
2006-11-30 01:46:20 +00:00
|
|
|
if( last.m_iSourceFrame+last.m_iFrames == iSourceFrame &&
|
|
|
|
|
last.m_iDestFrame+last.m_iFrames == iDestFrame )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 01:46:20 +00:00
|
|
|
last.m_iFrames += iFrames;
|
2004-04-11 04:51:03 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-30 01:55:52 +00:00
|
|
|
m_pImpl->m_Queue.push_back( pos_map_t() );
|
|
|
|
|
pos_map_t &m = m_pImpl->m_Queue.back();
|
|
|
|
|
m.m_iSourceFrame = iSourceFrame;
|
|
|
|
|
m.m_iDestFrame = iDestFrame;
|
|
|
|
|
m.m_iFrames = iFrames;
|
2004-04-11 04:51:03 +00:00
|
|
|
|
2006-11-30 01:39:58 +00:00
|
|
|
m_pImpl->Cleanup();
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-30 01:39:58 +00:00
|
|
|
void pos_map_impl::Cleanup()
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 02:05:36 +00:00
|
|
|
/* Scan backwards until we have at least pos_map_backlog_frames. */
|
|
|
|
|
list<pos_map_t>::iterator it = m_Queue.end();
|
|
|
|
|
int iTotalFrames = 0;
|
|
|
|
|
while( iTotalFrames < pos_map_backlog_frames )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 02:05:36 +00:00
|
|
|
if( it == m_Queue.begin() )
|
|
|
|
|
break;
|
|
|
|
|
--it;
|
|
|
|
|
iTotalFrames += it->m_iFrames;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
2006-11-30 02:05:36 +00:00
|
|
|
|
|
|
|
|
m_Queue.erase( m_Queue.begin(), it );
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-30 01:46:20 +00:00
|
|
|
int64_t pos_map_queue::Search( int64_t iSourceFrame, bool *bApproximate ) const
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
|
|
|
|
if( IsEmpty() )
|
|
|
|
|
{
|
2006-11-26 17:56:29 +00:00
|
|
|
if( bApproximate )
|
|
|
|
|
*bApproximate = true;
|
2004-04-11 04:51:03 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-30 01:46:20 +00:00
|
|
|
/* iSourceFrame is probably in pos_map. Search to figure out what position
|
2004-04-11 04:51:03 +00:00
|
|
|
* it maps to. */
|
2006-11-26 17:56:29 +00:00
|
|
|
int64_t iClosestPosition = 0, iClosestPositionDist = INT_MAX;
|
2006-11-30 01:52:12 +00:00
|
|
|
const pos_map_t *pClosestBlock = &*m_pImpl->m_Queue.begin(); /* print only */
|
2006-11-30 02:05:36 +00:00
|
|
|
FOREACHL_CONST( pos_map_t, m_pImpl->m_Queue, it )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 01:52:12 +00:00
|
|
|
const pos_map_t &pm = *it;
|
|
|
|
|
|
|
|
|
|
if( iSourceFrame >= pm.m_iSourceFrame &&
|
|
|
|
|
iSourceFrame < pm.m_iSourceFrame+pm.m_iFrames )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-30 01:46:20 +00:00
|
|
|
/* iSourceFrame lies in this block; it's an exact match. Figure
|
2004-04-11 04:51:03 +00:00
|
|
|
* out the exact position. */
|
2006-11-30 01:52:12 +00:00
|
|
|
int64_t diff = pm.m_iDestFrame - pm.m_iSourceFrame;
|
2006-11-30 01:46:20 +00:00
|
|
|
return iSourceFrame + diff;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* See if the current position is close to the beginning of this block. */
|
2006-11-30 01:52:12 +00:00
|
|
|
int64_t dist = llabs( pm.m_iSourceFrame - iSourceFrame );
|
2006-11-26 17:56:29 +00:00
|
|
|
if( dist < iClosestPositionDist )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-26 17:56:29 +00:00
|
|
|
iClosestPositionDist = dist;
|
2006-11-30 01:52:12 +00:00
|
|
|
pClosestBlock = ±
|
|
|
|
|
iClosestPosition = pm.m_iDestFrame;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* See if the current position is close to the end of this block. */
|
2006-11-30 01:52:12 +00:00
|
|
|
dist = llabs( pm.m_iSourceFrame + pm.m_iFrames - iSourceFrame );
|
2006-11-26 17:56:29 +00:00
|
|
|
if( dist < iClosestPositionDist )
|
2004-04-11 04:51:03 +00:00
|
|
|
{
|
2006-11-26 17:56:29 +00:00
|
|
|
iClosestPositionDist = dist;
|
2006-11-30 01:52:12 +00:00
|
|
|
pClosestBlock = ±
|
|
|
|
|
iClosestPosition = pm.m_iDestFrame + pm.m_iFrames;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-26 17:56:29 +00:00
|
|
|
/*
|
|
|
|
|
* The frame is out of the range of data we've actually sent.
|
2004-04-11 04:51:03 +00:00
|
|
|
* 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 frame number than we know about.
|
|
|
|
|
*/
|
2004-04-21 23:35:07 +00:00
|
|
|
#if defined(WIN32)
|
|
|
|
|
#define LI "%I64i"
|
|
|
|
|
#else
|
|
|
|
|
#define LI "%lli"
|
|
|
|
|
#endif
|
2004-07-14 21:06:03 +00:00
|
|
|
static RageTimer last;
|
|
|
|
|
if( last.PeekDeltaTime() >= 1.0f )
|
|
|
|
|
{
|
|
|
|
|
last.GetDeltaTime();
|
2006-11-30 01:39:58 +00:00
|
|
|
LOG->Trace( "Approximate sound time: driver frame " LI ", m_pImpl->m_Queue frame " LI ".." LI " (dist " LI "), closest position is " LI,
|
2006-11-30 01:52:12 +00:00
|
|
|
iSourceFrame, pClosestBlock->m_iSourceFrame, pClosestBlock->m_iSourceFrame+pClosestBlock->m_iFrames,
|
2006-11-26 17:56:29 +00:00
|
|
|
iClosestPositionDist, iClosestPosition );
|
2004-07-14 21:06:03 +00:00
|
|
|
}
|
2004-04-11 04:51:03 +00:00
|
|
|
|
2006-11-26 17:56:29 +00:00
|
|
|
if( bApproximate )
|
|
|
|
|
*bApproximate = true;
|
|
|
|
|
return iClosestPosition;
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pos_map_queue::Clear()
|
|
|
|
|
{
|
2006-11-30 01:39:58 +00:00
|
|
|
m_pImpl->m_Queue.clear();
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool pos_map_queue::IsEmpty() const
|
|
|
|
|
{
|
2006-11-30 01:39:58 +00:00
|
|
|
return m_pImpl->m_Queue.empty();
|
2004-04-11 04:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2004-05-06 02:40:33 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002-2004 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.
|
|
|
|
|
*/
|