Renamed all automation related files and classes to match new coding style
[lmms.git] / src / core / note.cpp
blob2088e1188f0a5421f99d8a93b34049e149d35076
1 /*
2 * note.cpp - implementation of class note
4 * Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5 *
6 * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
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.
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.
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.
26 #include <QtXml/QDomElement>
28 #include <math.h>
30 #include "note.h"
31 #include "DetuningHelper.h"
32 #include "templates.h"
38 note::note( const midiTime & _length, const midiTime & _pos,
39 int _key, volume_t _volume, panning_t _panning,
40 DetuningHelper * _detuning ) :
41 m_selected( false ),
42 m_oldKey( tLimit( _key, 0, NumKeys ) ),
43 m_oldPos( _pos ),
44 m_oldLength( _length ),
45 m_isPlaying( false ),
46 m_key( tLimit( _key, 0, NumKeys ) ),
47 m_volume( tLimit( _volume, MinVolume, MaxVolume ) ),
48 m_panning( tLimit( _panning, PanningLeft, PanningRight ) ),
49 m_length( _length ),
50 m_pos( _pos ),
51 m_detuning( NULL )
53 //saveJournallingState( false );
54 // setJournalling( false );
55 if( _detuning )
57 m_detuning = sharedObject::ref( _detuning );
59 else
61 createDetuning();
63 //restoreJournallingState();
69 note::note( const note & _note ) :
70 SerializingObject( _note ),
71 m_selected( _note.m_selected ),
72 m_oldKey( _note.m_oldKey ),
73 m_oldPos( _note.m_oldPos ),
74 m_oldLength( _note.m_oldLength ),
75 m_isPlaying( _note.m_isPlaying ),
76 m_key( _note.m_key),
77 m_volume( _note.m_volume ),
78 m_panning( _note.m_panning ),
79 m_length( _note.m_length ),
80 m_pos( _note.m_pos ),
81 m_detuning( NULL )
83 if( _note.m_detuning )
85 m_detuning = sharedObject::ref( _note.m_detuning );
87 else
89 createDetuning();
96 note::~note()
98 if( m_detuning )
100 sharedObject::unref( m_detuning );
107 void note::setLength( const midiTime & _length )
109 // addJournalEntry( journalEntry( ChangeLength, m_length - _length ) );
110 m_length = _length;
116 void note::setPos( const midiTime & _pos )
118 // addJournalEntry( journalEntry( ChangePosition, m_pos - _pos ) );
119 m_pos = _pos;
125 void note::setKey( const int _key )
127 const int k = tLimit( _key, 0, NumKeys );
128 // addJournalEntry( journalEntry( ChangeKey, m_key - k ) );
129 m_key = k;
135 void note::setVolume( const volume_t _volume )
137 const volume_t v = tLimit( _volume, MinVolume, MaxVolume );
138 // addJournalEntry( journalEntry( ChangeVolume, (int) m_volume - v ) );
139 m_volume = v;
145 void note::setPanning( const panning_t _panning )
147 const panning_t p = tLimit( _panning, PanningLeft, PanningRight );
148 // addJournalEntry( journalEntry( ChangePanning, (int) m_panning - p ) );
149 m_panning = p;
155 midiTime note::quantized( const midiTime & _m, const int _q_grid )
157 float p = ( (float) _m / _q_grid );
158 if( p - floorf( p ) < 0.5f )
160 return( static_cast<int>( p ) * _q_grid );
162 return( static_cast<int>( p + 1 ) * _q_grid );
168 void note::quantizeLength( const int _q_grid )
170 setLength( quantized( length(), _q_grid ) );
171 if( length() == 0 )
173 setLength( _q_grid );
180 void note::quantizePos( const int _q_grid )
182 setPos( quantized( pos(), _q_grid ) );
188 void note::saveSettings( QDomDocument & _doc, QDomElement & _this )
190 _this.setAttribute( "key", m_key );
191 _this.setAttribute( "vol", m_volume );
192 _this.setAttribute( "pan", m_panning );
193 _this.setAttribute( "len", m_length );
194 _this.setAttribute( "pos", m_pos );
195 if( m_length > 0 )
197 m_detuning->saveSettings( _doc, _this );
204 void note::loadSettings( const QDomElement & _this )
206 const int old_key = _this.attribute( "tone" ).toInt() +
207 _this.attribute( "oct" ).toInt() * KeysPerOctave;
208 m_key = qMax( old_key, _this.attribute( "key" ).toInt() );
209 m_volume = _this.attribute( "vol" ).toInt();
210 m_panning = _this.attribute( "pan" ).toInt();
211 m_length = _this.attribute( "len" ).toInt();
212 m_pos = _this.attribute( "pos" ).toInt();
213 if( _this.hasChildNodes() )
215 m_detuning->loadSettings( _this );
222 /*void note::undoStep( journalEntry & _je )
224 saveJournallingState( false );
225 switch( static_cast<Actions>( _je.actionID() ) )
227 case ChangeKey:
228 setKey( key() - _je.data().toInt() );
229 break;
231 case ChangeVolume:
232 setVolume( getVolume() - _je.data().toInt() );
233 break;
235 case ChangePanning:
236 setPanning( getPanning() - _je.data().toInt() );
237 break;
239 case ChangeLength:
240 setLength( length() - _je.data().toInt() );
241 break;
243 case ChangePosition:
244 setPos( pos() - _je.data().toInt() );
245 break;
247 restoreJournallingState();
253 void note::redoStep( journalEntry & _je )
255 journalEntry je( _je.actionID(), -_je.data().toInt() );
256 undoStep( je );
262 void note::editDetuningPattern()
264 m_detuning->automationPattern()->openInAutomationEditor();
270 void note::createDetuning()
272 m_detuning = new DetuningHelper;
273 (void) m_detuning->automationPattern();
274 m_detuning->setRange( -MaxDetuning, MaxDetuning, 0.1f );
280 bool note::hasDetuningInfo() const
282 return m_detuning && m_detuning->hasAutomation();