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
keyboard.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 #include "keyboard.h"
21 #include "piano.h"
22 
23 
24 //-----------------------------------------------------------------------------
25 // Keyboard Constructor
26 //-----------------------------------------------------------------------------
27 
32 
33 Keyboard::Keyboard(size_t initialSize) :
34  mKeys(initialSize),
35  mKeyNumberOfA4(Piano::DEFAULT_KEY_NUMBER_OF_A),
36  mNumberOfBassKeys(Piano::DEFAULT_KEYS_ON_BASS_BRIDGE)
37 {
38 }
39 
40 
41 //-----------------------------------------------------------------------------
42 // Clearing data
43 //-----------------------------------------------------------------------------
44 
46 { for (auto &key : mKeys) key.clear(); }
47 
49 { for (auto &key : mKeys) key.setComputedFrequency(0); }
50 
52 { for (auto &key : mKeys) key.setTunedFrequency(0); }
53 
55 { for (auto &key : mKeys) key.setOverpull(0); }
56 
57 
58 
59 //-----------------------------------------------------------------------------
60 // Change keyboard configuration
61 //-----------------------------------------------------------------------------
62 
72 
73 void Keyboard::changeKeyboardConfiguration (int numberOfKeys, int keyNumberOfA)
74 {
75  Keys keyCopy(std::move(mKeys));
76  mKeys.clear();
77  mKeys.resize(numberOfKeys);
78 
79  // if keys already exist they are shifted to their new locations:
80  for (int toIndex = 0; toIndex < static_cast<int>(mKeys.size()); ++toIndex)
81  {
82  int fromIndex = toIndex + mKeyNumberOfA4 - keyNumberOfA;
83  if (fromIndex >= 0 && fromIndex < static_cast<int>(keyCopy.size()))
84  {
85  mKeys[toIndex] = keyCopy[fromIndex];
86  }
87  }
88  mKeyNumberOfA4 = keyNumberOfA;
89 }
90 
91 
92 
93 //-----------------------------------------------------------------------------
94 // Get pointer to a key
95 //-----------------------------------------------------------------------------
96 
103 
104 const Key * Keyboard::getKeyPtr (int i) const
105 {
106  if (i < 0 || i >= static_cast<int>(mKeys.size())) {return nullptr;}
107  return &mKeys[i];
108 }
109 
116 
118 {
119  if (i < 0 || i >= static_cast<int>(mKeys.size())) {return nullptr;}
120  return &mKeys[i];
121 }
122 
123 
124 //-----------------------------------------------------------------------------
125 // Get the name of the key
126 //-----------------------------------------------------------------------------
127 
134 
135 std::string Keyboard::getNoteName (int keynumber) const
136 {
137  const int K = size();
138  const int Akey = mKeyNumberOfA4;
139 
140  if (keynumber < 0 || keynumber >+ K) return "--";
141  int Ckey = Akey - 9;
142  int octave = (keynumber - Ckey + 120) / 12 - 6;
143  int index = (keynumber - Ckey + 120) % 12;
144  const std::string names[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
145  char octchar = (char)('0' + octave);
146  return names[index] + octchar;
147 }
148 
149 
150 //-----------------------------------------------------------------------------
151 // Get the color of the key
152 //-----------------------------------------------------------------------------
153 
159 
161 {
162  const piano::KeyColor White = piano::KC_WHITE;
163  const piano::KeyColor Black = piano::KC_BLACK;
164  const piano::KeyColor scheme[12] =
165  {White, Black, White, Black, White, White, Black, White, Black, White, Black, White};
166  return scheme[(keynumber + 9 + (12 * 100 - getKeyNumberOfA4())) % 12];
167 }
Keys mKeys
Vector holding the keys.
Definition: keyboard.h:85
void clearOverpulls()
Set all overpull markers to zero.
Definition: keyboard.cpp:54
void changeKeyboardConfiguration(int numberOfKeys, int keyNumberOfA)
Change keyboard configuration.
Definition: keyboard.cpp:73
Class describing a single piano key.
Definition: key.h:45
Definition: piano.h:40
const Key * getKeyPtr(int i) const
Get pointer to a key with a given number, returning nullptr if the number is out of range (read-only ...
Definition: keyboard.cpp:104
KeyColor
The KeyColor enum.
Definition: pianodefines.h:51
void clearComputedPitches()
Set all computed pitches to zero.
Definition: keyboard.cpp:48
void clearTunedPitches()
Set all tuned pitches to zero.
Definition: keyboard.cpp:51
std::string getNoteName(int keynumber) const
Get the name of the key as a string.
Definition: keyboard.cpp:135
size_t size() const
Definition: keyboard.h:48
void clearAllKeys()
Clear all keys completely.
Definition: keyboard.cpp:45
White key.
Definition: pianodefines.h:53
Black key.
Definition: pianodefines.h:52
Keyboard(size_t initialSize)
Keyboard constructor.
Definition: keyboard.cpp:33
int getKeyNumberOfA4() const
Definition: keyboard.h:73
std::vector< Key > Keys
Definition: keyboard.h:42
piano::KeyColor getKeyColor(int keynumber) const
Get the color of the key (black / white)
Definition: keyboard.cpp:160
int mKeyNumberOfA4
Index of the key A4 (440Hz)
Definition: keyboard.h:86