| 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 | float velocity, |
|---|
| 35 | float fPan_L, |
|---|
| 36 | float fPan_R, |
|---|
| 37 | int nLength, |
|---|
| 38 | float fPitch, |
|---|
| 39 | NoteKey key |
|---|
| 40 | ) |
|---|
| 41 | : Object( "Note" ) |
|---|
| 42 | , m_nSilenceOffset( 0 ) |
|---|
| 43 | , m_nReleaseOffset( 0 ) |
|---|
| 44 | , m_fSamplePosition( 0.0 ) |
|---|
| 45 | , m_noteKey( key ) |
|---|
| 46 | , m_fCutoff( 1.0 ) |
|---|
| 47 | , m_fResonance( 0.0 ) |
|---|
| 48 | , m_fBandPassFilterBuffer_L( 0.0 ) |
|---|
| 49 | , m_fBandPassFilterBuffer_R( 0.0 ) |
|---|
| 50 | , m_fLowPassFilterBuffer_L( 0.0 ) |
|---|
| 51 | , m_fLowPassFilterBuffer_R( 0.0 ) |
|---|
| 52 | , m_nHumanizeDelay( 0 ) |
|---|
| 53 | , __velocity( velocity ) |
|---|
| 54 | , __leadlag( 0.0 ) |
|---|
| 55 | { |
|---|
| 56 | set_pan_l( fPan_L ); |
|---|
| 57 | set_pan_r( fPan_R ); |
|---|
| 58 | set_length( nLength ); |
|---|
| 59 | |
|---|
| 60 | set_instrument( pInstrument ); |
|---|
| 61 | set_pitch( fPitch ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | Note::Note( const Note* pNote ) |
|---|
| 68 | : Object( "Note" ) |
|---|
| 69 | { |
|---|
| 70 | m_nSilenceOffset = pNote->m_nSilenceOffset; |
|---|
| 71 | m_nReleaseOffset = pNote->m_nReleaseOffset; |
|---|
| 72 | m_fSamplePosition = pNote->m_fSamplePosition; |
|---|
| 73 | m_noteKey = pNote->m_noteKey; |
|---|
| 74 | // m_adsr copied in set_instrument() |
|---|
| 75 | m_fCutoff = pNote->m_fCutoff; |
|---|
| 76 | m_fResonance = pNote->m_fResonance; |
|---|
| 77 | m_fBandPassFilterBuffer_L = pNote->m_fBandPassFilterBuffer_L; |
|---|
| 78 | m_fBandPassFilterBuffer_R = pNote->m_fBandPassFilterBuffer_R; |
|---|
| 79 | m_fLowPassFilterBuffer_L = pNote->m_fLowPassFilterBuffer_L; |
|---|
| 80 | m_fLowPassFilterBuffer_R = pNote->m_fLowPassFilterBuffer_R; |
|---|
| 81 | m_nHumanizeDelay = pNote->m_nHumanizeDelay; |
|---|
| 82 | set_instrument( pNote->__instrument ); |
|---|
| 83 | __velocity = pNote->get_velocity(); |
|---|
| 84 | set_pan_l( pNote->get_pan_l() ); |
|---|
| 85 | set_pan_r( pNote->get_pan_r() ); |
|---|
| 86 | set_leadlag( pNote->get_leadlag() ); |
|---|
| 87 | set_length( pNote->get_length() ); |
|---|
| 88 | set_pitch( pNote->get_pitch() ); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | Note::~Note() |
|---|
| 94 | { |
|---|
| 95 | //infoLog("DESTROY"); |
|---|
| 96 | //delete m_pADSR; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | void Note::set_instrument( Instrument* instrument ) |
|---|
| 102 | { |
|---|
| 103 | if ( instrument == NULL ) { |
|---|
| 104 | return; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | __instrument = instrument; |
|---|
| 108 | assert( __instrument->get_adsr() ); |
|---|
| 109 | m_adsr = ADSR( *( __instrument->get_adsr() ) ); |
|---|
| 110 | |
|---|
| 111 | /* |
|---|
| 112 | if ( pInstrument->m_pADSR == NULL ) { |
|---|
| 113 | ERRORLOG( "NULL ADSR? Instrument: " + pInstrument->m_sName ); |
|---|
| 114 | } |
|---|
| 115 | else { |
|---|
| 116 | INFOLOG( "copio l'adsr dallo strumento" ); |
|---|
| 117 | if ( m_pADSR ) { |
|---|
| 118 | WARNINGLOG( "Replacing an existing ADSR" ); |
|---|
| 119 | delete m_pADSR; |
|---|
| 120 | m_pADSR = NULL; |
|---|
| 121 | } |
|---|
| 122 | m_pADSR = new ADSR( *(m_pInstrument->m_pADSR) ); |
|---|
| 123 | } |
|---|
| 124 | */ |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | void Note::dumpInfo() const |
|---|
| 130 | { |
|---|
| 131 | INFOLOG( "humanize offset" + to_string(m_nHumanizeDelay) + "\t instr: " + __instrument->get_name()+ "\t key: " + keyToString( m_noteKey ) + "\t pitch: " + to_string( get_pitch() ) ); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | NoteKey Note::stringToKey( const QString& str ) |
|---|
| 137 | { |
|---|
| 138 | NoteKey key; |
|---|
| 139 | |
|---|
| 140 | QString sKey = str.left( str.length() - 1 ); |
|---|
| 141 | QString sOct = str.mid( str.length() - 1, str.length() ); |
|---|
| 142 | int nOctave = sOct.toInt(); |
|---|
| 143 | |
|---|
| 144 | // _INFOLOG( "skey: " + sKey ); |
|---|
| 145 | // _INFOLOG( "sOct: " + sOct ); |
|---|
| 146 | // _INFOLOG( "nOctave: " + to_string( nOctave ) ); |
|---|
| 147 | |
|---|
| 148 | if ( sKey == "C" ) { |
|---|
| 149 | key.m_key = NoteKey::C; |
|---|
| 150 | } else if ( sKey == "Cs" ) { |
|---|
| 151 | key.m_key = NoteKey::Cs; |
|---|
| 152 | } else if ( sKey == "D" ) { |
|---|
| 153 | key.m_key = NoteKey::D; |
|---|
| 154 | } else if ( sKey == "Ef" ) { |
|---|
| 155 | key.m_key = NoteKey::Ef; |
|---|
| 156 | } else if ( sKey == "E" ) { |
|---|
| 157 | key.m_key = NoteKey::E; |
|---|
| 158 | } else if ( sKey == "F" ) { |
|---|
| 159 | key.m_key = NoteKey::F; |
|---|
| 160 | } else if ( sKey == "Fs" ) { |
|---|
| 161 | key.m_key = NoteKey::Fs; |
|---|
| 162 | } else if ( sKey == "G" ) { |
|---|
| 163 | key.m_key = NoteKey::G; |
|---|
| 164 | } else if ( sKey == "Af" ) { |
|---|
| 165 | key.m_key = NoteKey::Af; |
|---|
| 166 | } else if ( sKey == "A" ) { |
|---|
| 167 | key.m_key = NoteKey::A; |
|---|
| 168 | } else if ( sKey == "Bf" ) { |
|---|
| 169 | key.m_key = NoteKey::Bf; |
|---|
| 170 | } else if ( sKey == "B" ) { |
|---|
| 171 | key.m_key = NoteKey::B; |
|---|
| 172 | } else { |
|---|
| 173 | _ERRORLOG( "Unhandled key: " + sKey ); |
|---|
| 174 | } |
|---|
| 175 | key.m_nOctave = nOctave; |
|---|
| 176 | |
|---|
| 177 | return key; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | QString Note::keyToString( NoteKey key ) |
|---|
| 183 | { |
|---|
| 184 | QString sKey; |
|---|
| 185 | |
|---|
| 186 | switch ( key.m_key ) { |
|---|
| 187 | case NoteKey::C: |
|---|
| 188 | sKey = "C"; |
|---|
| 189 | break; |
|---|
| 190 | case NoteKey::Cs: |
|---|
| 191 | sKey = "Cs"; |
|---|
| 192 | break; |
|---|
| 193 | case NoteKey::D: |
|---|
| 194 | sKey = "D"; |
|---|
| 195 | break; |
|---|
| 196 | case NoteKey::Ef: |
|---|
| 197 | sKey = "Ef"; |
|---|
| 198 | break; |
|---|
| 199 | case NoteKey::E: |
|---|
| 200 | sKey = "E"; |
|---|
| 201 | break; |
|---|
| 202 | case NoteKey::F: |
|---|
| 203 | sKey = "F"; |
|---|
| 204 | break; |
|---|
| 205 | case NoteKey::Fs: |
|---|
| 206 | sKey = "Fs"; |
|---|
| 207 | break; |
|---|
| 208 | case NoteKey::G: |
|---|
| 209 | sKey = "G"; |
|---|
| 210 | break; |
|---|
| 211 | case NoteKey::Af: |
|---|
| 212 | sKey = "Af"; |
|---|
| 213 | break; |
|---|
| 214 | case NoteKey::A: |
|---|
| 215 | sKey = "A"; |
|---|
| 216 | break; |
|---|
| 217 | case NoteKey::Bf: |
|---|
| 218 | sKey = "Bf"; |
|---|
| 219 | break; |
|---|
| 220 | case NoteKey::B: |
|---|
| 221 | sKey = "B"; |
|---|
| 222 | break; |
|---|
| 223 | |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | sKey += to_string( key.m_nOctave ); |
|---|
| 227 | |
|---|
| 228 | return sKey; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | Note* Note::copy() |
|---|
| 233 | { |
|---|
| 234 | Note* note = new Note( |
|---|
| 235 | get_instrument(), |
|---|
| 236 | get_velocity(), |
|---|
| 237 | get_pan_l(), |
|---|
| 238 | get_pan_r(), |
|---|
| 239 | get_length(), |
|---|
| 240 | get_pitch(), |
|---|
| 241 | m_noteKey |
|---|
| 242 | ); |
|---|
| 243 | |
|---|
| 244 | note->set_leadlag(get_leadlag()); |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | return note; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | }; |
|---|