diff audioio/AudioCallbackPlaySource.cpp @ 1:97c69acdcb82

* Adjust use of target sample rate, so as to make the decision about whether to resample or not more robust. * Update copyrights to 2006
author Chris Cannam
date Thu, 12 Jan 2006 13:45:06 +0000
parents db6fcbd4405c
children 75c3ea1c3a32
line wrap: on
line diff
--- a/audioio/AudioCallbackPlaySource.cpp	Tue Jan 10 16:33:16 2006 +0000
+++ b/audioio/AudioCallbackPlaySource.cpp	Thu Jan 12 13:45:06 2006 +0000
@@ -2,7 +2,7 @@
 
 /*
     A waveform viewer and audio annotation editor.
-    Chris Cannam, Queen Mary University of London, 2005
+    Chris Cannam, Queen Mary University of London, 2005-2006
     
     This is experimental software.  Not for distribution.
 */
@@ -118,22 +118,10 @@
     }
 
     if (buffersChanged || srChanged) {
-
 	if (m_converter) {
 	    src_delete(m_converter);
 	    m_converter = 0;
 	}
-
-	if (getSourceSampleRate() != getTargetSampleRate()) {
-
-	    int err = 0;
-	    m_converter = src_new(SRC_SINC_FASTEST, m_bufferCount, &err);
-	    if (!m_converter) {
-		std::cerr
-		    << "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
-		    << src_strerror(err) << std::endl;
-	    }
-	}
     }
 
     m_audioGenerator->addModel(model);
@@ -148,10 +136,9 @@
 #ifdef DEBUG_AUDIO_PLAY_SOURCE
     std::cerr << "AudioCallbackPlaySource::addModel: emitting modelReplaced" << std::endl;
 #endif
-    emit modelReplaced();
 
-    if (srChanged && (getSourceSampleRate() != getTargetSampleRate())) {
-	emit sampleRateMismatch(getSourceSampleRate(), getTargetSampleRate());
+    if (buffersChanged || srChanged) {
+	emit modelReplaced();
     }
 }
 
@@ -323,6 +310,19 @@
 AudioCallbackPlaySource::setTargetSampleRate(size_t sr)
 {
     m_targetSampleRate = sr;
+
+    if (getSourceSampleRate() != getTargetSampleRate()) {
+
+	int err = 0;
+	m_converter = src_new(SRC_SINC_BEST_QUALITY, m_bufferCount, &err);
+	if (!m_converter) {
+	    std::cerr
+		<< "AudioCallbackPlaySource::setModel: ERROR in creating samplerate converter: "
+		<< src_strerror(err) << std::endl;
+	}
+
+	emit sampleRateMismatch(getSourceSampleRate(), getTargetSampleRate());
+    }
 }
 
 size_t
@@ -543,6 +543,11 @@
 #endif
 
     bool resample = (getSourceSampleRate() != getTargetSampleRate());
+
+#ifdef DEBUG_AUDIO_PLAY_SOURCE
+    std::cout << (resample ? "" : "not ") << "resampling (source " << getSourceSampleRate() << ", target " << getTargetSampleRate() << ")" << std::endl;
+#endif
+
     size_t channels = getSourceChannelCount();
     size_t orig = space;
     size_t got = 0;
@@ -558,6 +563,14 @@
 
     size_t generatorBlockSize = m_audioGenerator->getBlockSize();
 
+    if (resample && !m_converter) {
+	static bool warned = false;
+	if (!warned) {
+	    std::cerr << "WARNING: sample rates differ, but no converter available!" << std::endl;
+	    warned = true;
+	}
+    }
+
     if (resample && m_converter) {
 
 	double ratio =