Chris@6: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@6: Chris@6: /* Chris@6: Tony Chris@6: An intonation analysis and annotation tool Chris@6: Centre for Digital Music, Queen Mary, University of London. Chris@6: This file copyright 2006-2012 Chris Cannam and QMUL. Chris@6: Chris@6: This program is free software; you can redistribute it and/or Chris@6: modify it under the terms of the GNU General Public License as Chris@6: published by the Free Software Foundation; either version 2 of the Chris@6: License, or (at your option) any later version. See the file Chris@6: COPYING included with this distribution for more information. Chris@6: */ Chris@6: Chris@6: #ifndef ANALYSER_H Chris@6: #define ANALYSER_H Chris@6: Chris@6: #include Chris@191: #include Chris@341: #include Chris@6: Chris@128: #include Chris@163: #include Chris@163: Chris@163: #include "framework/Document.h" Chris@164: #include "base/Selection.h" Chris@194: #include "base/Clipboard.h" Chris@6: Chris@6: class WaveFileModel; Chris@6: class Pane; Chris@6: class PaneStack; Chris@6: class Layer; Chris@128: class TimeValueLayer; Chris@128: class Layer; Chris@6: Chris@163: class Analyser : public QObject, Chris@163: public Document::LayerCreationHandler Chris@6: { Chris@6: Q_OBJECT Chris@6: Chris@6: public: Chris@6: Analyser(); Chris@6: virtual ~Analyser(); Chris@6: Chris@140: // Process new main model, add derived layers; return "" on success or error string on failure Chris@140: QString newFileLoaded(Document *newDocument, WaveFileModel *model, Chris@140: PaneStack *paneStack, Pane *pane); Chris@226: Chris@325: // Remove any derived layers, process the main model, add derived layers; return "" on success or error string on failure Chris@325: QString analyseExistingFile(); Chris@325: Chris@226: // Discard any layers etc associated with the current document Chris@226: void fileClosed(); gyorgyf@45: gyorgyf@45: void setIntelligentActions(bool); Chris@6: Chris@399: bool getDisplayFrequencyExtents(double &min, double &max); Chris@399: bool setDisplayFrequencyExtents(double min, double max); Chris@227: Chris@314: // Return completion %age for initial analysis -- 100 means it's done Chris@314: int getInitialAnalysisCompletion(); Chris@314: Chris@128: enum Component { Chris@242: Audio = 0, Chris@242: PitchTrack = 1, Chris@242: Notes = 2, Chris@242: Spectrogram = 3, Chris@128: }; Chris@128: Chris@128: bool isVisible(Component c) const; Chris@128: void setVisible(Component c, bool v); Chris@144: void toggleVisible(Component c) { setVisible(c, !isVisible(c)); } Chris@128: Chris@128: bool isAudible(Component c) const; Chris@128: void setAudible(Component c, bool v); Chris@144: void toggleAudible(Component c) { setAudible(c, !isAudible(c)); } Chris@128: Chris@128: void cycleStatus(Component c) { Chris@128: if (isVisible(c)) { Chris@128: if (isAudible(c)) { Chris@128: setVisible(c, false); Chris@128: setAudible(c, false); Chris@128: } else { Chris@128: setAudible(c, true); Chris@128: } Chris@128: } else { Chris@128: setVisible(c, true); Chris@128: setAudible(c, false); Chris@128: } Chris@128: } Chris@128: Chris@260: WaveFileModel *getMainModel() const { Chris@260: return m_fileModel; Chris@260: } Chris@260: Chris@158: float getGain(Component c) const; Chris@158: void setGain(Component c, float gain); Chris@158: Chris@158: float getPan(Component c) const; Chris@158: void setPan(Component c, float pan); Chris@158: Chris@399: void getEnclosingSelectionScope(sv_frame_t f, sv_frame_t &f0, sv_frame_t &f1); Chris@139: Chris@192: struct FrequencyRange { Chris@192: FrequencyRange() : min(0), max(0) { } Chris@399: FrequencyRange(double min_, double max_) : min(min_), max(max_) { } Chris@192: bool isConstrained() const { return min != max; } Chris@399: double min; Chris@399: double max; Chris@396: bool operator==(const FrequencyRange &r) { Chris@396: return min == r.min && max == r.max; Chris@396: } Chris@192: }; Chris@192: Chris@164: /** Chris@165: * Analyse the selection and schedule asynchronous adds of Chris@165: * candidate layers for the region it contains. Returns "" on Chris@192: * success or a user-readable error string on failure. If the Chris@192: * frequency range isConstrained(), analysis will be constrained Chris@192: * to that range. Chris@164: */ Chris@192: QString reAnalyseSelection(Selection sel, FrequencyRange range); Chris@164: Chris@167: /** Chris@184: * Return true if the analysed pitch candidates are currently Chris@199: * visible (they are hidden from the call to reAnalyseSelection Chris@199: * until they are requested through showPitchCandidates()). Note Chris@199: * that this may return true even when no pitch candidate layers Chris@199: * actually exist yet, because they are constructed Chris@199: * asynchronously. If that is the case, then the layers will Chris@199: * appear when they are created (otherwise they will remain hidden Chris@199: * after creation). Chris@184: */ Chris@184: bool arePitchCandidatesShown() const; Chris@184: Chris@184: /** Chris@199: * Show or hide the analysed pitch candidate layers. This is reset Chris@199: * (to "hide") with each new call to reAnalyseSelection. Because Chris@199: * the layers are created asynchronously, setting this to true Chris@199: * does not guarantee that they appear immediately, only that they Chris@199: * will appear once they have been created. Chris@184: */ Chris@184: void showPitchCandidates(bool shown); Chris@184: Chris@184: /** Chris@167: * If a re-analysis has been activated, switch the selected area Chris@167: * of the main pitch track to a different candidate from the Chris@167: * analysis results. Chris@167: */ Chris@167: void switchPitchCandidate(Selection sel, bool up); Chris@167: Chris@167: /** Chris@199: * Return true if it is possible to switch up to another pitch Chris@199: * candidate. This may mean that the currently selected pitch Chris@199: * candidate is not the highest, or it may mean that no alternate Chris@199: * pitch candidate has been selected at all yet (but some are Chris@199: * available). Chris@199: */ Chris@199: bool haveHigherPitchCandidate() const; Chris@199: Chris@199: /** Chris@199: * Return true if it is possible to switch down to another pitch Chris@199: * candidate. This may mean that the currently selected pitch Chris@199: * candidate is not the lowest, or it may mean that no alternate Chris@199: * pitch candidate has been selected at all yet (but some are Chris@199: * available). Chris@199: */ Chris@199: bool haveLowerPitchCandidate() const; Chris@199: Chris@199: /** Chris@184: * Delete the pitch estimates from the selected area of the main Chris@168: * pitch track. Chris@168: */ Chris@184: void deletePitches(Selection sel); Chris@168: Chris@168: /** Chris@168: * Move the main pitch track and any active analysis candidate Chris@168: * tracks up or down an octave in the selected area. Chris@168: */ Chris@168: void shiftOctave(Selection sel, bool up); Chris@168: Chris@168: /** Chris@199: * Remove any re-analysis layers and also reset the pitch track in Chris@194: * the given selection to its state prior to the last re-analysis, Chris@199: * abandoning any changes made since then. No re-analysis layers Chris@199: * will be available until after the next call to Chris@199: * reAnalyseSelection. Chris@167: */ Chris@199: void abandonReAnalysis(Selection sel); Chris@167: Chris@174: /** Chris@269: * Remove any re-analysis layers, without any expectation of Chris@269: * adding them later, unlike showPitchCandidates(false), and Chris@269: * without changing the current pitch track, unlike Chris@269: * abandonReAnalysis(). Chris@269: */ Chris@269: void clearReAnalysis(); Chris@269: Chris@269: /** Chris@174: * Import the pitch track from the given layer into our Chris@174: * pitch-track layer. Chris@174: */ Chris@174: void takePitchTrackFrom(Layer *layer); Chris@174: Chris@174: Pane *getPane() { Chris@174: return m_pane; Chris@174: } Chris@174: Chris@174: Layer *getLayer(Component type) { Chris@174: return m_layers[type]; Chris@174: } Chris@174: Chris@128: signals: Chris@128: void layersChanged(); Chris@314: void initialAnalysisCompleted(); Chris@128: Chris@242: protected slots: Chris@242: void layerAboutToBeDeleted(Layer *); Chris@314: void layerCompletionChanged(); Chris@403: void reAnalyseRegion(sv_frame_t, sv_frame_t, float, float); Chris@398: void materialiseReAnalysis(); Chris@242: Chris@6: protected: Chris@6: Document *m_document; Chris@6: WaveFileModel *m_fileModel; Chris@133: PaneStack *m_paneStack; Chris@6: Pane *m_pane; Chris@165: Chris@128: mutable std::map m_layers; Chris@132: Chris@194: Clipboard m_preAnalysis; Chris@165: Selection m_reAnalysingSelection; Chris@396: FrequencyRange m_reAnalysingRange; Chris@165: std::vector m_reAnalysisCandidates; Chris@167: int m_currentCandidate; Chris@184: bool m_candidatesVisible; Chris@341: Document::LayerCreationAsyncHandle m_currentAsyncHandle; Chris@341: QMutex m_asyncMutex; Chris@165: Chris@326: QString doAllAnalyses(bool withPitchTrack); Chris@325: Chris@161: QString addVisualisations(); Chris@161: QString addWaveform(); Chris@161: QString addAnalyses(); Chris@161: Chris@199: void discardPitchCandidates(); Chris@260: Chris@260: void stackLayers(); Chris@199: Chris@163: // Document::LayerCreationHandler method Chris@341: void layersCreated(Document::LayerCreationAsyncHandle, Chris@341: std::vector, std::vector); Chris@163: Chris@132: void saveState(Component c) const; Chris@132: void loadState(Component c); Chris@6: }; Chris@6: Chris@6: #endif