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
recordingmanager.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 // Recording manager
22 //=============================================================================
23 
24 #include "recordingmanager.h"
25 
26 #include "../../messages/messageprojectfile.h"
27 #include "../../messages/messagekeyselectionchanged.h"
28 #include "../../messages/messagemodechanged.h"
29 
31  : mAudioRecorder (audioRecorder),
32  mPiano(nullptr),
33  mOperationMode(MODE_IDLE),
34  mSelectedKey(nullptr),
35  mKeyNumberOfA4(88),
36  mNumberOfSelectedKey(-1)
37 {
39 }
40 
41 
42 //-----------------------------------------------------------------------------
43 // Message handler (for standby and stroboscope)
44 //-----------------------------------------------------------------------------
45 
50 
52 {
53  switch (m->getType())
54  {
56  {
57  // In case that e.g. a new file was opened:
58  auto mpf(std::static_pointer_cast<MessageProjectFile>(m));
59  mPiano = &mpf->getPiano();
61  mSelectedKey = nullptr;
64  break;
65  }
67  {
68  // Tell the audio recorder to wait after completed analysis
69  // in order to avoid self-recording of the echo sound
71  break;
72  }
74  {
75  // Change of the operation mode:
76  auto mmc(std::static_pointer_cast<MessageModeChanged>(m));
77  mOperationMode = mmc->getMode();
78  switch (mOperationMode)
79  {
80  case MODE_TUNING:
82  mAudioRecorder->setStandby(false);
83  break;
84  case MODE_RECORDING:
86  mAudioRecorder->setStandby(false);
87  break;
88  default:
91  break;
92  }
93  break;
94  }
96  {
97  // If a key is selected update the stroboscopic frequencies
99  {
100  auto message(std::static_pointer_cast<MessageKeySelectionChanged>(m));
101  mSelectedKey = message->getKey();
102  mNumberOfSelectedKey = message->getKeyNumber();
104  }
105  break;
106  }
108  {
111  break;
112  }
114  {
117  break;
118  }
119  default:
120  break;
121  }
122 }
123 
124 
125 //-----------------------------------------------------------------------------
126 // Update the stroboscopic frequencies
127 //-----------------------------------------------------------------------------
128 
136 
138 {
139  std::vector<double> ftab;
140  if (mSelectedKey)
141  {
142  const double fc = mSelectedKey->getComputedFrequency();
143  if (fc > 0)
144  {
145  const Key::PeakListType peaks = mSelectedKey->getPeaks();
146  // This is the formula determining the number of partials shown in the stroboscope:
147  const int numberOfStroboscopicPartials = std::max(1,1+(mKeyNumberOfA4+6+24-mNumberOfSelectedKey)/6);
148 
149  // if peaks are available
150  if (peaks.size()>0)
151  {
152  const double f1 = peaks.begin()->first;
153  int N = 0;
154 
155  if (f1>0) for (auto &e : peaks)
156  {
157  if (++N > numberOfStroboscopicPartials) break;
158  // Push partials adjusted by concert pitch
159  ftab.push_back(e.first/f1*mPiano->getConcertPitch()/440.0*fc);
160  }
161  }
162  else // if no peaks are available
163  {
164  // Push partials with the expected harmonic spectrum
165  const double B = mPiano->getExpectedInharmonicity (fc);
166  for (int n=1; n<=3; ++n) ftab.push_back(n*fc*sqrt((1+B*n*n)/1+B));
167  }
168  }
169  }
171 }
void setFramesPerSecond(double fps)
Stroboscope::setFramesPerSecond.
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
Stroboscope * getStroboscope()
Abstract adapter class for recording audio signals.
void setWaitingFlag(bool flag)
virtual void handleMessage(MessagePtr m)
Listen to messages and take action accordingly.
const double FPS_SLOW
Stroboscopic fps during non-recording.
const PeakListType & getPeaks() const
Get a read-only reference to mPeaks.
Definition: key.cpp:259
const Key * mSelectedKey
Currently selected key.
Message that a change was made with the current project file.
Definition: message.h:68
void updateStroboscopicFrequencies()
Update the stroboscopic frequencies.
const Piano * mPiano
Poitner to the actual piano.
Message that the operation mode has changed.
Definition: message.h:65
Mode for recording the piano keys.
Definition: prerequisites.h:69
void stop()
Stop the stroboscope.
Definition: stroboscope.h:82
Message that a key has been selected.
Definition: message.h:63
const Keyboard & getKeyboard() const
Definition: piano.h:83
double getComputedFrequency() const
Get computed frequency.
Definition: key.cpp:174
void start()
Start the stroboscope.
Definition: stroboscope.h:81
int mNumberOfSelectedKey
Number of actually selected key.
void setFrequencies(const std::vector< double > &frequencies)
Stroboscope::setFrequencies.
const double FPS_FAST
Stroboscopic fps during recording.
RecordingManager(AudioRecorderAdapter *audioRecorder)
Mode for manually tuning the piano.
Definition: prerequisites.h:71
int mKeyNumberOfA4
Total number of keys.
int getKeyNumberOfA4() const
Definition: keyboard.h:73
const double & getConcertPitch() const
Definition: piano.h:80
recording ended
Definition: message.h:52
OperationMode mOperationMode
Current operation mode.
double getExpectedInharmonicity(double f) const
Compute expected approximative inharmonicity.
Definition: piano.cpp:102
Do nothing.
Definition: prerequisites.h:68
analysis of the signal ended
Definition: message.h:54
std::map< double, double > PeakListType
Type for a peak map.
Definition: key.h:55
void setStandby(bool flag)
keystroke recognized and recording started
Definition: message.h:51
AudioRecorderAdapter * mAudioRecorder
Pointer to the audio device.