Chris@0: /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: A waveform viewer and audio annotation editor. Chris@2: Chris Cannam, Queen Mary University of London, 2005-2006 Chris@0: Chris@0: This is experimental software. Not for distribution. Chris@0: */ Chris@0: Chris@0: #include "ViewManager.h" Chris@0: #include "AudioPlaySource.h" Chris@0: #include "PlayParameters.h" Chris@0: #include "Model.h" Chris@0: Chris@0: #include Chris@0: Chris@0: //#define DEBUG_VIEW_MANAGER 1 Chris@0: Chris@0: ViewManager::ViewManager() : Chris@0: m_playSource(0), Chris@0: m_globalCentreFrame(0), Chris@0: m_globalZoom(1024), Chris@0: m_lastLeft(0), Chris@0: m_lastRight(0) Chris@0: { Chris@0: connect(this, Chris@0: SIGNAL(centreFrameChanged(void *, unsigned long, bool)), Chris@0: SLOT(considerSeek(void *, unsigned long, bool))); Chris@0: Chris@0: connect(this, Chris@0: SIGNAL(zoomLevelChanged(void *, unsigned long, bool)), Chris@0: SLOT(considerZoomChange(void *, unsigned long, bool))); Chris@0: } Chris@0: Chris@0: unsigned long Chris@0: ViewManager::getGlobalCentreFrame() const Chris@0: { Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: std::cout << "ViewManager::getGlobalCentreFrame: returning " << m_globalCentreFrame << std::endl; Chris@0: #endif Chris@0: return m_globalCentreFrame; Chris@0: } Chris@0: Chris@0: unsigned long Chris@0: ViewManager::getGlobalZoom() const Chris@0: { Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: std::cout << "ViewManager::getGlobalZoom: returning " << m_globalZoom << std::endl; Chris@0: #endif Chris@0: return m_globalZoom; Chris@0: } Chris@0: Chris@0: void Chris@0: ViewManager::setAudioPlaySource(AudioPlaySource *source) Chris@0: { Chris@0: if (!m_playSource) { Chris@0: QTimer::singleShot(100, this, SLOT(checkPlayStatus())); Chris@0: } Chris@0: m_playSource = source; Chris@0: } Chris@0: Chris@0: PlayParameters * Chris@0: ViewManager::getPlayParameters(const Model *model) Chris@0: { Chris@0: if (m_playParameters.find(model) == m_playParameters.end()) { Chris@0: // Give all models the same type of play parameters for the moment Chris@0: m_playParameters[model] = new PlayParameters; Chris@0: } Chris@0: Chris@0: return m_playParameters[model]; Chris@0: } Chris@0: Chris@0: void Chris@0: ViewManager::clearPlayParameters() Chris@0: { Chris@0: while (!m_playParameters.empty()) { Chris@0: delete m_playParameters.begin()->second; Chris@0: m_playParameters.erase(m_playParameters.begin()); Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: ViewManager::checkPlayStatus() Chris@0: { Chris@0: if (m_playSource && m_playSource->isPlaying()) { Chris@0: Chris@0: float left = 0, right = 0; Chris@0: if (m_playSource->getOutputLevels(left, right)) { Chris@0: if (left != m_lastLeft || right != m_lastRight) { Chris@0: emit outputLevelsChanged(left, right); Chris@0: m_lastLeft = left; Chris@0: m_lastRight = right; Chris@0: } Chris@0: } Chris@0: Chris@0: m_globalCentreFrame = m_playSource->getCurrentPlayingFrame(); Chris@0: Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_globalCentreFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl; Chris@0: #endif Chris@0: Chris@0: emit playbackFrameChanged(m_globalCentreFrame); Chris@0: Chris@0: QTimer::singleShot(20, this, SLOT(checkPlayStatus())); Chris@0: Chris@0: } else { Chris@0: Chris@0: QTimer::singleShot(100, this, SLOT(checkPlayStatus())); Chris@0: Chris@0: if (m_lastLeft != 0.0 || m_lastRight != 0.0) { Chris@0: emit outputLevelsChanged(0.0, 0.0); Chris@0: m_lastLeft = 0.0; Chris@0: m_lastRight = 0.0; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: // std::cout << "ViewManager::checkPlayStatus: Not playing" << std::endl; Chris@0: #endif Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: ViewManager::considerSeek(void *p, unsigned long f, bool locked) Chris@0: { Chris@0: if (locked) { Chris@0: m_globalCentreFrame = f; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: std::cout << "ViewManager::considerSeek(" << p << ", " << f << ", " << locked << ")" << std::endl; Chris@0: #endif Chris@0: Chris@0: if (p == this || !locked) return; Chris@0: Chris@0: if (m_playSource && m_playSource->isPlaying()) { Chris@0: unsigned long playFrame = m_playSource->getCurrentPlayingFrame(); Chris@0: unsigned long diff = std::max(f, playFrame) - std::min(f, playFrame); Chris@0: if (diff > 20000) { Chris@0: m_playSource->play(f); Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: std::cout << "ViewManager::considerSeek: reseeking from " << playFrame << " to " << f << std::endl; Chris@0: #endif Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: void Chris@0: ViewManager::considerZoomChange(void *p, unsigned long z, bool locked) Chris@0: { Chris@0: if (locked) { Chris@0: m_globalZoom = z; Chris@0: } Chris@0: Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@0: std::cout << "ViewManager::considerZoomChange(" << p << ", " << z << ", " << locked << ")" << std::endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: #ifdef INCLUDE_MOCFILES Chris@0: #include "ViewManager.moc.cpp" Chris@0: #endif Chris@0: