| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY, without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <hydrogen/note.h> |
|---|
| 24 | #include <hydrogen/instrument.h> |
|---|
| 25 | |
|---|
| 26 | #include <cassert> |
|---|
| 27 | #include <cstdlib> |
|---|
| 28 | |
|---|
| 29 | namespace H2Core |
|---|
| 30 | { |
|---|
| 31 | |
|---|
| 32 | Note::Note( |
|---|
| 33 | Instrument *pInstrument, |
|---|
| 34 | unsigned position, |
|---|
| 35 | float velocity, |
|---|
| 36 | float fPan_L, |
|---|
| 37 | float fPan_R, |
|---|
| 38 | int nLength, |
|---|
| 39 | float fPitch, |
|---|
| 40 | NoteKey key |
|---|
| 41 | ) |
|---|
| 42 | : Object( "Note" ) |
|---|
| 43 | , m_fSamplePosition( 0.0 ) |
|---|
| 44 | , m_nHumanizeDelay( 0 ) |
|---|
| 45 | , m_noteKey( key ) |
|---|
| 46 | // , m_pADSR( NULL ) |
|---|
| 47 | , m_fCutoff( 1.0 ) |
|---|
| 48 | , m_fResonance( 0.0 ) |
|---|
| 49 | , m_fBandPassFilterBuffer_L( 0.0 ) |
|---|
| 50 | , m_fBandPassFilterBuffer_R( 0.0 ) |
|---|
| 51 | , m_fLowPassFilterBuffer_L( 0.0 ) |
|---|
| 52 | , m_fLowPassFilterBuffer_R( 0.0 ) |
|---|
| 53 | , __position( position ) |
|---|
| 54 | , __velocity( velocity ) |
|---|
| 55 | , __leadlag( 0.0 ) |
|---|
| 56 | , __noteoff( false) |
|---|
| 57 | { |
|---|
| 58 | set_pan_l( fPan_L ); |
|---|
| 59 | set_pan_r( fPan_R ); |
|---|
| 60 | set_lenght( nLength ); |
|---|
| 61 | |
|---|
| 62 | set_instrument( pInstrument ); |
|---|
| 63 | set_pitch( fPitch ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | Note::Note( const Note* pNote ) |
|---|
| 70 | : Object( "Note" ) |
|---|
| 71 | { |
|---|
| 72 | __position = pNote->get_position(); |
|---|
| 73 | __velocity = pNote->get_velocity(); |
|---|
| 74 | set_pan_l( pNote->get_pan_l() ); |
|---|
| 75 | set_pan_r( pNote->get_pan_r() ); |
|---|
| 76 | set_leadlag( pNote->get_leadlag() ); |
|---|
| 77 | set_lenght( pNote->get_lenght() ); |
|---|
| 78 | set_pitch( pNote->get_pitch() ); |
|---|
| 79 | set_noteoff( pNote->get_noteoff() ); |
|---|
| 80 | m_noteKey = pNote->m_noteKey; |
|---|
| 81 | m_fCutoff = pNote->m_fCutoff; |
|---|
| 82 | m_fResonance = pNote->m_fResonance; |
|---|
| 83 | m_fBandPassFilterBuffer_L = pNote->m_fBandPassFilterBuffer_L; |
|---|
| 84 | m_fBandPassFilterBuffer_R = pNote->m_fBandPassFilterBuffer_R; |
|---|
| 85 | m_fLowPassFilterBuffer_L = pNote->m_fLowPassFilterBuffer_L; |
|---|
| 86 | m_fLowPassFilterBuffer_R = pNote->m_fLowPassFilterBuffer_R; |
|---|
| 87 | m_nHumanizeDelay = pNote->m_nHumanizeDelay; |
|---|
| 88 | m_fSamplePosition = pNote->m_fSamplePosition; |
|---|
| 89 | set_instrument( pNote->__instrument ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | Note::~Note() |
|---|
| 95 | { |
|---|
| 96 | //infoLog("DESTROY"); |
|---|
| 97 | //delete m_pADSR; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | void Note::set_instrument( Instrument* instrument ) |
|---|
| 103 | { |
|---|
| 104 | if ( instrument == NULL ) { |
|---|
| 105 | return; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | __instrument = instrument; |
|---|
| 109 | assert( __instrument->get_adsr() ); |
|---|
| 110 | m_adsr = ADSR( *( __instrument->get_adsr() ) ); |
|---|
| 111 | |
|---|
| 112 | /* |
|---|
| 113 | if ( pInstrument->m_pADSR == NULL ) { |
|---|
| 114 | ERRORLOG( "NULL ADSR? Instrument: " + pInstrument->m_sName ); |
|---|
| 115 | } |
|---|
| 116 | else { |
|---|
| 117 | INFOLOG( "copio l'adsr dallo strumento" ); |
|---|
| 118 | if ( m_pADSR ) { |
|---|
| 119 | WARNINGLOG( "Replacing an existing ADSR" ); |
|---|
| 120 | delete m_pADSR; |
|---|
| 121 | m_pADSR = NULL; |
|---|
| 122 | } |
|---|
| 123 | m_pADSR = new ADSR( *(m_pInstrument->m_pADSR) ); |
|---|
| 124 | } |
|---|
| 125 | */ |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | void Note::dumpInfo() |
|---|
| 131 | { |
|---|
| 132 | INFOLOG( "pos: " + to_string( get_position() ) + "\t humanize offset" + to_string(m_nHumanizeDelay) + "\t instr: " + __instrument->get_name()+ "\t key: " + keyToString( m_noteKey ) + "\t pitch: " + to_string( get_pitch() ) + "\t noteoff: " + to_string( get_noteoff() ) ); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | NoteKey Note::stringToKey( const QString& str ) |
|---|
| 138 | { |
|---|
| 139 | NoteKey key; |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | QString sKey = str.left( str.length() - 1 ); |
|---|
| 143 | QString sOct = str.mid( str.length() - 1, str.length() ); |
|---|
| 144 | |
|---|
| 145 | if ( sKey.endsWith( "-" ) ){ |
|---|
| 146 | sKey.replace("-", ""); |
|---|
| 147 | sOct.insert( 0, "-"); |
|---|
| 148 | } |
|---|
| 149 | int nOctave = sOct.toInt(); |
|---|
| 150 | |
|---|
| 151 | // _INFOLOG( "skey: " + sKey ); |
|---|
| 152 | // _INFOLOG( "sOct: " + sOct ); |
|---|
| 153 | // _INFOLOG( "nOctave: " + to_string( nOctave ) ); |
|---|
| 154 | |
|---|
| 155 | if ( sKey == "C" ) { |
|---|
| 156 | key.m_key = NoteKey::C; |
|---|
| 157 | } else if ( sKey == "Cs" ) { |
|---|
| 158 | key.m_key = NoteKey::Cs; |
|---|
| 159 | } else if ( sKey == "D" ) { |
|---|
| 160 | key.m_key = NoteKey::D; |
|---|
| 161 | } else if ( sKey == "Ef" ) { |
|---|
| 162 | key.m_key = NoteKey::Ef; |
|---|
| 163 | } else if ( sKey == "E" ) { |
|---|
| 164 | key.m_key = NoteKey::E; |
|---|
| 165 | } else if ( sKey == "F" ) { |
|---|
| 166 | key.m_key = NoteKey::F; |
|---|
| 167 | } else if ( sKey == "Fs" ) { |
|---|
| 168 | key.m_key = NoteKey::Fs; |
|---|
| 169 | } else if ( sKey == "G" ) { |
|---|
| 170 | key.m_key = NoteKey::G; |
|---|
| 171 | } else if ( sKey == "Af" ) { |
|---|
| 172 | key.m_key = NoteKey::Af; |
|---|
| 173 | } else if ( sKey == "A" ) { |
|---|
| 174 | key.m_key = NoteKey::A; |
|---|
| 175 | } else if ( sKey == "Bf" ) { |
|---|
| 176 | key.m_key = NoteKey::Bf; |
|---|
| 177 | } else if ( sKey == "B" ) { |
|---|
| 178 | key.m_key = NoteKey::B; |
|---|
| 179 | } else { |
|---|
| 180 | _ERRORLOG( "Unhandled key: " + sKey ); |
|---|
| 181 | } |
|---|
| 182 | key.m_nOctave = nOctave; |
|---|
| 183 | |
|---|
| 184 | return key; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | QString Note::keyToString( NoteKey key ) |
|---|
| 190 | { |
|---|
| 191 | QString sKey; |
|---|
| 192 | |
|---|
| 193 | switch ( key.m_key ) { |
|---|
| 194 | case NoteKey::C: |
|---|
| 195 | sKey = "C"; |
|---|
| 196 | break; |
|---|
| 197 | case NoteKey::Cs: |
|---|
| 198 | sKey = "Cs"; |
|---|
| 199 | break; |
|---|
| 200 | case NoteKey::D: |
|---|
| 201 | sKey = "D"; |
|---|
| 202 | break; |
|---|
| 203 | case NoteKey::Ef: |
|---|
| 204 | sKey = "Ef"; |
|---|
| 205 | break; |
|---|
| 206 | case NoteKey::E: |
|---|
| 207 | sKey = "E"; |
|---|
| 208 | break; |
|---|
| 209 | case NoteKey::F: |
|---|
| 210 | sKey = "F"; |
|---|
| 211 | break; |
|---|
| 212 | case NoteKey::Fs: |
|---|
| 213 | sKey = "Fs"; |
|---|
| 214 | break; |
|---|
| 215 | case NoteKey::G: |
|---|
| 216 | sKey = "G"; |
|---|
| 217 | break; |
|---|
| 218 | case NoteKey::Af: |
|---|
| 219 | sKey = "Af"; |
|---|
| 220 | break; |
|---|
| 221 | case NoteKey::A: |
|---|
| 222 | sKey = "A"; |
|---|
| 223 | break; |
|---|
| 224 | case NoteKey::Bf: |
|---|
| 225 | sKey = "Bf"; |
|---|
| 226 | break; |
|---|
| 227 | case NoteKey::B: |
|---|
| 228 | sKey = "B"; |
|---|
| 229 | break; |
|---|
| 230 | |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | sKey += to_string( key.m_nOctave ); |
|---|
| 234 | |
|---|
| 235 | return sKey; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | Note* Note::copy() |
|---|
| 240 | { |
|---|
| 241 | Note* note = new Note( |
|---|
| 242 | get_instrument(), |
|---|
| 243 | get_position(), |
|---|
| 244 | get_velocity(), |
|---|
| 245 | get_pan_l(), |
|---|
| 246 | get_pan_r(), |
|---|
| 247 | get_lenght(), |
|---|
| 248 | get_pitch(), |
|---|
| 249 | m_noteKey |
|---|
| 250 | ); |
|---|
| 251 | |
|---|
| 252 | note->set_leadlag(get_leadlag()); |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | return note; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | }; |
|---|