summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2019-04-15 12:30:52 +0100
committercah <cah@ableton.com>2019-12-03 11:16:07 +0100
commitdbb4f997a37d4056ce1ed04ede07afb449beb13d (patch)
tree55fbf8e96e167b26eb5904093dc315d9b0745139
parent5157202ab48af46323ba3d37de6837ac3e90ae5e (diff)
Fix erroneous getStreamTime() results in some cases.
Without this, if HAVE_GETTIMEOFDAY is defined getStreamTime() can return a wrong value in the following case: - start stream - stop stream - start stream - call getStreamTime() immediately In this case tickStreamTime() will not have been called since the stream was restarted, hence lastTickTimestamp will refer to the previous run of the stream. getStreamTime() will therefore extrapolate wrongly from stream._streamTime. This patch only uses lastTickTimestamp when it is valid.
-rw-r--r--RtAudio.cpp19
-rw-r--r--RtAudio.h3
2 files changed, 21 insertions, 1 deletions
diff --git a/RtAudio.cpp b/RtAudio.cpp
index d80660e..1b27de1 100644
--- a/RtAudio.cpp
+++ b/RtAudio.cpp
@@ -465,7 +465,7 @@ double RtApi :: getStreamTime( void )
struct timeval then;
struct timeval now;
- if ( stream_.state != STREAM_RUNNING || stream_.streamTime == 0.0 )
+ if ( stream_.state != STREAM_RUNNING || (stream_.lastTickTimestamp.tv_sec == 0 && stream_.lastTickTimestamp.tv_usec == 0) )
return stream_.streamTime;
gettimeofday( &now, NULL );
@@ -496,6 +496,14 @@ unsigned int RtApi :: getStreamSampleRate( void )
return stream_.sampleRate;
}
+void RtApi :: startStream( void )
+{
+#if defined( HAVE_GETTIMEOFDAY )
+ stream_.lastTickTimestamp.tv_sec = 0;
+ stream_.lastTickTimestamp.tv_usec = 0;
+#endif
+}
+
// *************************************************** //
//
@@ -1535,6 +1543,7 @@ void RtApiCore :: closeStream( void )
void RtApiCore :: startStream( void )
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiCore::startStream(): the stream is already running!";
error( RtAudioError::WARNING );
@@ -2497,6 +2506,7 @@ void RtApiJack :: closeStream( void )
void RtApiJack :: startStream( void )
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiJack::startStream(): the stream is already running!";
error( RtAudioError::WARNING );
@@ -3380,6 +3390,7 @@ bool stopThreadCalled = false;
void RtApiAsio :: startStream()
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiAsio::startStream(): the stream is already running!";
error( RtAudioError::WARNING );
@@ -4558,6 +4569,7 @@ void RtApiWasapi::closeStream( void )
void RtApiWasapi::startStream( void )
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiWasapi::startStream: The stream is already running.";
@@ -6365,6 +6377,7 @@ void RtApiDs :: closeStream()
void RtApiDs :: startStream()
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiDs::startStream(): the stream is already running!";
error( RtAudioError::WARNING );
@@ -6425,6 +6438,7 @@ void RtApiDs :: startStream()
void RtApiDs :: stopStream()
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiDs::stopStream(): the stream is already stopped!";
error( RtAudioError::WARNING );
@@ -8063,6 +8077,7 @@ void RtApiAlsa :: startStream()
// This method calls snd_pcm_prepare if the device isn't already in that state.
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiAlsa::startStream(): the stream is already running!";
error( RtAudioError::WARNING );
@@ -8631,6 +8646,7 @@ void RtApiPulse::callbackEvent( void )
void RtApiPulse::startStream( void )
{
+ RtApi::startStream();
PulseAudioHandle *pah = static_cast<PulseAudioHandle *>( stream_.apiHandle );
if ( stream_.state == STREAM_CLOSED ) {
@@ -9628,6 +9644,7 @@ void RtApiOss :: closeStream()
void RtApiOss :: startStream()
{
verifyStream();
+ RtApi::startStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiOss::startStream(): the stream is already running!";
error( RtAudioError::WARNING );
diff --git a/RtAudio.h b/RtAudio.h
index 4bb7554..d4f9f38 100644
--- a/RtAudio.h
+++ b/RtAudio.h
@@ -796,6 +796,9 @@ protected:
double streamTime; // Number of elapsed seconds since the stream started.
#if defined(HAVE_GETTIMEOFDAY)
+ // The gettimeofday() when tickStreamTime was last called, or both
+ // fields at 0 if tickStreamTime has not been called since the last
+ // startStream().
struct timeval lastTickTimestamp;
#endif