1 /*
2  * Piano.cpp - implementation of piano-widget used in instrument-track-window
3  *             for testing + according model class
4  *
5  * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6  *
7  * This file is part of LMMS - https://lmms.io
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this program (see COPYING); if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301 USA.
23  *
24  */
25 
26 /** \file Piano.cpp
27  *  \brief A piano keyboard to play notes on in the instrument plugin window.
28  */
29 
30 /*
31  * \mainpage Instrument plugin keyboard display classes
32  *
33  * \section introduction Introduction
34  *
35  * \todo fill this out
36  * \todo write isWhite inline function and replace throughout
37  */
38 
39 #include "Piano.h"
40 
41 #include "InstrumentTrack.h"
42 
43 
44 /*! The black / white order of keys as they appear on the keyboard.
45  */
46 static const Piano::KeyTypes KEY_ORDER[] =
47 {
48 //	C                CIS              D                DIS
49 	Piano::WhiteKey, Piano::BlackKey, Piano::WhiteKey, Piano::BlackKey,
50 //	E                F                FIS              G
51 	Piano::WhiteKey, Piano::WhiteKey, Piano::BlackKey, Piano::WhiteKey,
52 //	GIS              A                AIS              B
53 	Piano::BlackKey, Piano::WhiteKey, Piano::BlackKey, Piano::WhiteKey
54 } ;
55 
56 
57 /*! \brief Create a new keyboard display
58  *
59  *  \param _it the InstrumentTrack window to attach to
60  */
Piano(InstrumentTrack * track)61 Piano::Piano( InstrumentTrack* track ) :
62 	Model( NULL ),              /*!< base class ctor */
63 	m_instrumentTrack( track ),
64 	m_midiEvProc( track )        /*!< the InstrumentTrack Model */
65 {
66 	for( int i = 0; i < NumKeys; ++i )
67 	{
68 		m_pressedKeys[i] = false;
69 	}
70 
71 }
72 
73 
74 
75 
76 /*! \brief Destroy this new keyboard display
77  *
78  */
~Piano()79 Piano::~Piano()
80 {
81 }
82 
83 
84 
85 
86 /*! \brief Turn a key on or off
87  *
88  *  \param key the key number to change
89  *  \param state the state to set the key to
90  */
setKeyState(int key,bool state)91 void Piano::setKeyState( int key, bool state )
92 {
93 	if( isValidKey( key ) )
94 	{
95 		m_pressedKeys[key] = state;
96 
97 		emit dataChanged();
98 	}
99 }
100 
101 
102 
103 
104 /*! \brief Handle a note being pressed on our keyboard display
105  *
106  *  \param key the key being pressed
107  */
handleKeyPress(int key,int midiVelocity)108 void Piano::handleKeyPress( int key, int midiVelocity )
109 {
110 	if( midiVelocity == -1 )
111 	{
112 		midiVelocity = m_instrumentTrack->midiPort()->baseVelocity();
113 	}
114 	if( isValidKey( key ) )
115 	{
116 		m_midiEvProc->processInEvent( MidiEvent( MidiNoteOn, -1, key, midiVelocity ) );
117 		m_pressedKeys[key] = true;
118 	}
119 }
120 
121 
122 
123 
124 
125 /*! \brief Handle a note being released on our keyboard display
126  *
127  *  \param key the key being releassed
128  */
handleKeyRelease(int key)129 void Piano::handleKeyRelease( int key )
130 {
131 	if( isValidKey( key ) )
132 	{
133 		m_midiEvProc->processInEvent( MidiEvent( MidiNoteOff, -1, key, 0 ) );
134 		m_pressedKeys[key] = false;
135 	}
136 }
137 
138 
139 
isBlackKey(int key)140 bool Piano::isBlackKey( int key )
141 {
142 	int keyCode = key % KeysPerOctave;
143 
144 	return KEY_ORDER[keyCode] == Piano::BlackKey;
145 }
146 
147 
isWhiteKey(int key)148 bool Piano::isWhiteKey( int key )
149 {
150 	return !isBlackKey( key );
151 }
152 
153