Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
calculationmanager.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 //=============================================================================
21 // Calculation manager
22 //=============================================================================
23 
24 #include "calculationmanager.h"
25 
26 #include <iostream>
27 #include <cmath>
28 
29 #include "../config.h"
30 #include "../system/log.h"
31 #include "../system/timer.h"
32 #include "../messages/messagehandler.h"
33 #include "../messages/messagecaluclationprogress.h"
34 #include "../messages/messagechangetuningcurve.h"
35 #include "algorithmfactory.h"
37 
38 //-----------------------------------------------------------------------------
39 // Constructor
40 //-----------------------------------------------------------------------------
41 
45 
47 {
48 }
49 
51 {
52  stop();
53 }
54 
55 
57  static CalculationManager THE_ONE_AND_ONLY;
58  return THE_ONE_AND_ONLY;
59 }
60 
61 //-----------------------------------------------------------------------------
62 // Start calculation
63 //-----------------------------------------------------------------------------
64 
70 
71 void CalculationManager::start(const std::string &algorithmName, const Piano &piano)
72 {
73  if (mAlgorithms.count(algorithmName) != 1) {
74  EPT_EXCEPT(EptException::ERR_INVALIDPARAMS, "An algorithm with name '" + algorithmName + "' does not exist.");
75  }
76 
77  // stop the old algorithm to be sure
78  stop();
79 
80  // create and start new algorithm
81  mCurrentAlgorithm = std::move(mAlgorithms[algorithmName]->createAlgorithm(piano));
82  mCurrentAlgorithm->start();
83 }
84 
85 
86 //-----------------------------------------------------------------------------
87 // Stop calculation
88 //-----------------------------------------------------------------------------
89 
93 
95 {
96  if (mCurrentAlgorithm) {
97  mCurrentAlgorithm->stop();
98  mCurrentAlgorithm.reset();
99  }
100 }
101 
102 void CalculationManager::registerFactory(const std::string &name, AlgorithmFactoryBase* factory)
103 {
104  if (mAlgorithms.count(name) == 1) {
105  EPT_EXCEPT(EptException::ERR_DUPLICATE_ITEM, "An algorithm with name '" + name + "' already exists.");
106  }
107 
108 #if EPT_EXCLUDE_EXAMPLE_ALGORITHM
109  // exclude example algorithm(s)
110  if (name.find("example") != std::string::npos) {
111  return;
112  }
113 #endif
114 
115  mAlgorithms[name] = factory;
116 }
117 
119 {
120  if (!hasAlgorithm(algorithmName)) {
121  EPT_EXCEPT(EptException::ERR_DUPLICATE_ITEM, "An algorithm with name '" + algorithmName + "' does not exist.");
122  }
123 
124  return mAlgorithms.at(algorithmName)->getDescription();
125 }
126 
127 std::shared_ptr<const AlgorithmInformation> CalculationManager::loadAlgorithmInformation(const std::string &algorithmName) const
128 {
129  // open the xml file for this algorithm and return the information in the current language
131  return parser.parse(algorithmName);
132 }
133 
134 bool CalculationManager::hasAlgorithm(const std::string &id) const {
135  return mAlgorithms.count(id) == 1;
136 }
137 
139  EptAssert(hasAlgorithm("entropyminimizer"), "Default algorithm doesnt exist");
140  return "entropyminimizer";
141 }
static CalculationManager & getSingleton()
Calculation-Manager.
Definition: piano.h:40
The AlgorithmFactoryBase class create the desired Algorithm.
std::map< std::string, AlgorithmFactoryBase * > mAlgorithms
#define EPT_EXCEPT(num, desc)
Definition: eptexception.h:119
std::shared_ptr< const AlgorithmInformation > parse(const std::string &algorithmId) const
void start(const std::string &algorithmName, const Piano &piano)
Start the calculation thread. By calling this function, the current piano is passed by reference and ...
CalculationManager()
Constructor, resets member variables.
bool hasAlgorithm(const std::string &id) const
void stop()
Stop the calculation thread.
void registerFactory(const std::string &name, AlgorithmFactoryBase *factory)
std::shared_ptr< const AlgorithmInformation > loadAlgorithmInformation(const std::string &algorithmName) const
AlgorithmFactoryDescription & getAlgorithmDescription(const std::string &algorithmName) const
std::unique_ptr< Algorithm > mCurrentAlgorithm
#define EptAssert(a, b)
Definition: eptexception.h:47
std::string getDefaultAlgorithmId() const