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 "Model.h" Chris@0: Chris@0: #include Chris@0: Chris@18: //#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@24: m_playbackFrame(0), Chris@0: m_lastLeft(0), Chris@8: m_lastRight(0), Chris@8: m_inProgressExclusive(true), Chris@9: m_toolMode(NavigateMode), Chris@9: m_playLoopMode(false), Chris@20: m_playSelectionMode(false) 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@24: unsigned long Chris@24: ViewManager::getPlaybackFrame() const Chris@24: { Chris@24: if (m_playSource && m_playSource->isPlaying()) { Chris@24: m_playbackFrame = m_playSource->getCurrentPlayingFrame(); Chris@24: } Chris@24: return m_playbackFrame; Chris@24: } Chris@24: Chris@24: void Chris@24: ViewManager::setPlaybackFrame(unsigned long f) Chris@24: { Chris@24: if (m_playbackFrame != f) { Chris@24: m_playbackFrame = f; Chris@24: if (m_playSource && m_playSource->isPlaying()) { Chris@24: m_playSource->play(f); Chris@24: } Chris@24: emit playbackFrameChanged(f); Chris@24: } Chris@24: } Chris@24: Chris@8: bool Chris@8: ViewManager::haveInProgressSelection() const Chris@8: { Chris@8: return !m_inProgressSelection.isEmpty(); Chris@8: } Chris@8: Chris@8: const Selection & Chris@8: ViewManager::getInProgressSelection(bool &exclusive) const Chris@8: { Chris@8: exclusive = m_inProgressExclusive; Chris@8: return m_inProgressSelection; Chris@8: } Chris@8: Chris@8: void Chris@8: ViewManager::setInProgressSelection(const Selection &selection, bool exclusive) Chris@8: { Chris@8: m_inProgressExclusive = exclusive; Chris@8: m_inProgressSelection = selection; Chris@8: if (exclusive) clearSelections(); Chris@9: emit inProgressSelectionChanged(); Chris@8: } Chris@8: Chris@8: void Chris@8: ViewManager::clearInProgressSelection() Chris@8: { Chris@8: m_inProgressSelection = Selection(); Chris@9: emit inProgressSelectionChanged(); Chris@8: } Chris@8: Chris@34: const MultiSelection & Chris@34: ViewManager::getSelection() const Chris@34: { Chris@34: return m_selections; Chris@34: } Chris@34: Chris@24: const MultiSelection::SelectionList & Chris@8: ViewManager::getSelections() const Chris@8: { Chris@24: return m_selections.getSelections(); Chris@8: } Chris@8: Chris@8: void Chris@8: ViewManager::setSelection(const Selection &selection) Chris@8: { Chris@24: m_selections.setSelection(selection); Chris@24: emit selectionChanged(); Chris@8: } Chris@8: Chris@8: void Chris@8: ViewManager::addSelection(const Selection &selection) Chris@8: { Chris@24: m_selections.addSelection(selection); Chris@8: emit selectionChanged(); Chris@8: } Chris@8: Chris@8: void Chris@8: ViewManager::removeSelection(const Selection &selection) Chris@8: { Chris@24: m_selections.removeSelection(selection); Chris@24: emit selectionChanged(); Chris@8: } Chris@8: Chris@8: void Chris@8: ViewManager::clearSelections() Chris@8: { Chris@24: m_selections.clearSelections(); Chris@24: emit selectionChanged(); Chris@8: } Chris@8: Chris@9: Selection Chris@36: ViewManager::getContainingSelection(size_t frame, bool defaultToFollowing) const Chris@9: { Chris@24: return m_selections.getContainingSelection(frame, defaultToFollowing); Chris@9: } Chris@9: Chris@8: void Chris@8: ViewManager::setToolMode(ToolMode mode) Chris@8: { Chris@8: m_toolMode = mode; Chris@8: Chris@8: emit toolModeChanged(); Chris@8: } Chris@8: Chris@0: void Chris@9: ViewManager::setPlayLoopMode(bool mode) Chris@9: { Chris@9: m_playLoopMode = mode; Chris@9: Chris@9: emit playLoopModeChanged(); Chris@9: } Chris@9: Chris@9: void Chris@9: ViewManager::setPlaySelectionMode(bool mode) Chris@9: { Chris@9: m_playSelectionMode = mode; Chris@9: Chris@9: emit playSelectionModeChanged(); Chris@9: } Chris@9: Chris@40: size_t Chris@40: ViewManager::getPlaybackSampleRate() const Chris@40: { Chris@40: if (m_playSource) { Chris@40: return m_playSource->getTargetSampleRate(); Chris@40: } Chris@40: return 0; Chris@40: } Chris@40: Chris@9: 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: void Chris@10: ViewManager::playStatusChanged(bool playing) Chris@10: { Chris@10: checkPlayStatus(); Chris@10: } Chris@10: Chris@10: 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@24: m_playbackFrame = m_playSource->getCurrentPlayingFrame(); Chris@0: Chris@0: #ifdef DEBUG_VIEW_MANAGER Chris@24: std::cout << "ViewManager::checkPlayStatus: Playing, frame " << m_playbackFrame << ", levels " << m_lastLeft << "," << m_lastRight << std::endl; Chris@0: #endif Chris@0: Chris@24: emit playbackFrameChanged(m_playbackFrame); 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@8: bool Chris@8: ViewManager::isPlaying() const Chris@8: { Chris@8: return m_playSource && m_playSource->isPlaying(); Chris@8: } Chris@8: 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@24: m_playbackFrame = f; 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@24: } else { Chris@24: m_playbackFrame = f; //!!! only if that view is in scroll mode? 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: