1 /*
2  * MidiEvent.h - MidiEvent class
3  *
4  * Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5  *
6  * This file is part of LMMS - https://lmms.io
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program (see COPYING); if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  *
23  */
24 
25 #ifndef MIDI_EVENT_H
26 #define MIDI_EVENT_H
27 
28 #include <cstdlib>
29 #include "Midi.h"
30 #include "panning_constants.h"
31 #include "volume.h"
32 
33 class MidiEvent
34 {
35 public:
36 	MidiEvent( MidiEventTypes type = MidiActiveSensing,
37 				int8_t channel = 0,
38 				int16_t param1 = 0,
39 				int16_t param2 = 0,
40 				const void* sourcePort = NULL ) :
m_type(type)41 		m_type( type ),
42 		m_metaEvent( MidiMetaInvalid ),
43 		m_channel( channel ),
44 		m_sysExData( NULL ),
45 		m_sourcePort( sourcePort )
46 	{
47 		m_data.m_param[0] = param1;
48 		m_data.m_param[1] = param2;
49 	}
50 
MidiEvent(MidiEventTypes type,const char * sysExData,int dataLen)51 	MidiEvent( MidiEventTypes type, const char* sysExData, int dataLen ) :
52 		m_type( type ),
53 		m_metaEvent( MidiMetaInvalid ),
54 		m_channel( 0 ),
55 		m_sysExData( sysExData ),
56 		m_sourcePort( NULL )
57 	{
58 		m_data.m_sysExDataLen = dataLen;
59 	}
60 
MidiEvent(const MidiEvent & other)61 	MidiEvent( const MidiEvent& other ) :
62 		m_type( other.m_type ),
63 		m_metaEvent( other.m_metaEvent ),
64 		m_channel( other.m_channel ),
65 		m_data( other.m_data ),
66 		m_sysExData( other.m_sysExData ),
67 		m_sourcePort( other.m_sourcePort )
68 	{
69 	}
70 
type()71 	MidiEventTypes type() const
72 	{
73 		return m_type;
74 	}
75 
setType(MidiEventTypes type)76 	void setType( MidiEventTypes type )
77 	{
78 		m_type = type;
79 	}
80 
setMetaEvent(MidiMetaEventType metaEvent)81 	void setMetaEvent( MidiMetaEventType metaEvent )
82 	{
83 		m_metaEvent = metaEvent;
84 	}
85 
metaEvent()86 	MidiMetaEventType metaEvent() const
87 	{
88 		return m_metaEvent;
89 	}
90 
channel()91 	int8_t channel() const
92 	{
93 		return m_channel;
94 	}
95 
setChannel(int8_t channel)96 	void setChannel( int8_t channel )
97 	{
98 		m_channel = channel;
99 	}
100 
param(int i)101 	int16_t param( int i ) const
102 	{
103 		return m_data.m_param[i];
104 	}
105 
setParam(int i,uint16_t value)106 	void setParam( int i, uint16_t value )
107 	{
108 		m_data.m_param[i] = value;
109 	}
110 
key()111 	int16_t key() const
112 	{
113 		return param( 0 );
114 	}
115 
setKey(int16_t key)116 	void setKey( int16_t key )
117 	{
118 		m_data.m_param[0] = key;
119 	}
120 
velocity()121 	uint8_t velocity() const
122 	{
123 		return m_data.m_param[1] & 0x7F;
124 	}
125 
setVelocity(int16_t velocity)126 	void setVelocity( int16_t velocity )
127 	{
128 		m_data.m_param[1] = velocity;
129 	}
130 
panning()131 	panning_t panning() const
132 	{
133 		return (panning_t) ( PanningLeft +
134 			( (float)( midiPanning() - MidiMinPanning ) ) /
135 			( (float)( MidiMaxPanning - MidiMinPanning ) ) *
136 			( (float)( PanningRight - PanningLeft ) ) );
137 	}
midiPanning()138 	int16_t midiPanning() const
139 	{
140 		return m_data.m_param[1];
141 	}
142 
volume(int midiBaseVelocity)143 	volume_t volume( int midiBaseVelocity ) const
144 	{
145 		return (volume_t)( velocity() * DefaultVolume / midiBaseVelocity );
146 	}
147 
sourcePort()148 	const void* sourcePort() const
149 	{
150 		return m_sourcePort;
151 	}
152 
controllerNumber()153 	uint8_t controllerNumber() const
154 	{
155 		return param( 0 ) & 0x7F;
156 	}
157 
setControllerNumber(uint8_t num)158 	void setControllerNumber( uint8_t num )
159 	{
160 		setParam( 0, num );
161 	}
162 
controllerValue()163 	uint8_t controllerValue() const
164 	{
165 		return param( 1 );
166 	}
167 
setControllerValue(uint8_t value)168 	void setControllerValue( uint8_t value )
169 	{
170 		setParam( 1, value );
171 	}
172 
program()173 	uint8_t program() const
174 	{
175 		return param( 0 );
176 	}
177 
channelPressure()178 	uint8_t channelPressure() const
179 	{
180 		return param( 0 );
181 	}
182 
pitchBend()183 	int16_t pitchBend() const
184 	{
185 		return param( 0 );
186 	}
187 
setPitchBend(uint16_t pitchBend)188 	void setPitchBend( uint16_t pitchBend )
189 	{
190 		setParam( 0, pitchBend );
191 	}
192 
193 
194 private:
195 	MidiEventTypes m_type;		// MIDI event type
196 	MidiMetaEventType m_metaEvent;	// Meta event (mostly unused)
197 	int8_t m_channel;		// MIDI channel
198 	union
199 	{
200 		int16_t m_param[2];	// first/second parameter (key/velocity)
201 		uint8_t m_bytes[4];		// raw bytes
202 		int32_t m_sysExDataLen;	// len of m_sysExData
203 	} m_data;
204 
205 	const char* m_sysExData;
206 	const void* m_sourcePort;
207 
208 } ;
209 
210 #endif
211