| 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 <cassert> |
|---|
| 24 | #include <cmath> |
|---|
| 25 | |
|---|
| 26 | #include <hydrogen/IO/AudioOutput.h> |
|---|
| 27 | #include <hydrogen/IO/JackOutput.h> |
|---|
| 28 | |
|---|
| 29 | #include <hydrogen/adsr.h> |
|---|
| 30 | #include <hydrogen/audio_engine.h> |
|---|
| 31 | #include <hydrogen/data_path.h> |
|---|
| 32 | #include <hydrogen/globals.h> |
|---|
| 33 | #include <hydrogen/hydrogen.h> |
|---|
| 34 | #include <hydrogen/instrument.h> |
|---|
| 35 | #include <hydrogen/note.h> |
|---|
| 36 | #include <hydrogen/Preferences.h> |
|---|
| 37 | #include <hydrogen/sample.h> |
|---|
| 38 | #include <hydrogen/Song.h> |
|---|
| 39 | |
|---|
| 40 | #include <hydrogen/fx/Effects.h> |
|---|
| 41 | #include <hydrogen/sampler/Sampler.h> |
|---|
| 42 | |
|---|
| 43 | namespace H2Core |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | inline static float linear_interpolation( float fVal_A, float fVal_B, float fVal ) |
|---|
| 47 | { |
|---|
| 48 | return fVal_A * ( 1 - fVal ) + fVal_B * fVal; |
|---|
| 49 | // return fVal_A + fVal * ( fVal_B - fVal_A ); |
|---|
| 50 | // return fVal_A + ((fVal_B - fVal_A) * fVal); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | Sampler::Sampler() |
|---|
| 56 | : Object( "Sampler" ) |
|---|
| 57 | , __main_out_L( NULL ) |
|---|
| 58 | , __main_out_R( NULL ) |
|---|
| 59 | , __audio_output( NULL ) |
|---|
| 60 | , __preview_instrument( NULL ) |
|---|
| 61 | { |
|---|
| 62 | INFOLOG( "INIT" ); |
|---|
| 63 | |
|---|
| 64 | __main_out_L = new float[ MAX_BUFFER_SIZE ]; |
|---|
| 65 | __main_out_R = new float[ MAX_BUFFER_SIZE ]; |
|---|
| 66 | |
|---|
| 67 | // instrument used in file preview |
|---|
| 68 | QString sEmptySampleFilename = DataPath::get_data_path() + "/emptySample.wav"; |
|---|
| 69 | __preview_instrument = new Instrument( sEmptySampleFilename, "preview", new ADSR() ); |
|---|
| 70 | __preview_instrument->set_volume( 0.8 ); |
|---|
| 71 | __preview_instrument->set_layer( new InstrumentLayer( Sample::load( sEmptySampleFilename ) ), 0 ); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | Sampler::~Sampler() |
|---|
| 77 | { |
|---|
| 78 | INFOLOG( "DESTROY" ); |
|---|
| 79 | |
|---|
| 80 | delete[] __main_out_L; |
|---|
| 81 | delete[] __main_out_R; |
|---|
| 82 | |
|---|
| 83 | delete __preview_instrument; |
|---|
| 84 | __preview_instrument = NULL; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // perche' viene passata anche la canzone? E' davvero necessaria? |
|---|
| 88 | void Sampler::process( uint32_t nFrames, Song* pSong ) |
|---|
| 89 | { |
|---|
| 90 | //infoLog( "[process]" ); |
|---|
| 91 | assert( __audio_output ); |
|---|
| 92 | |
|---|
| 93 | memset( __main_out_L, 0, nFrames * sizeof( float ) ); |
|---|
| 94 | memset( __main_out_R, 0, nFrames * sizeof( float ) ); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | #ifdef JACK_SUPPORT |
|---|
| 98 | JackOutput* jao; |
|---|
| 99 | jao = dynamic_cast<JackOutput*>(__audio_output); |
|---|
| 100 | if (jao) { |
|---|
| 101 | int numtracks = jao->getNumTracks(); |
|---|
| 102 | |
|---|
| 103 | if ( jao->has_track_outs() ) { |
|---|
| 104 | for(int nTrack = 0; nTrack < numtracks; nTrack++) { |
|---|
| 105 | memset( __track_out_L[nTrack], |
|---|
| 106 | 0, |
|---|
| 107 | jao->getBufferSize( ) * sizeof( float ) ); |
|---|
| 108 | memset( __track_out_R[nTrack], |
|---|
| 109 | 0, |
|---|
| 110 | jao->getBufferSize( ) * sizeof( float ) ); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | #endif // JACK_SUPPORT |
|---|
| 115 | |
|---|
| 116 | // Max notes limit |
|---|
| 117 | int m_nMaxNotes = Preferences::getInstance()->m_nMaxNotes; |
|---|
| 118 | while ( ( int )__playing_notes_queue.size() > m_nMaxNotes ) { |
|---|
| 119 | Note *oldNote = __playing_notes_queue[ 0 ]; |
|---|
| 120 | __playing_notes_queue.erase( __playing_notes_queue.begin() ); |
|---|
| 121 | oldNote->get_instrument()->dequeue(); |
|---|
| 122 | delete oldNote; // FIXME: send note-off instead of removing the note from the list? |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | // eseguo tutte le note nella lista di note in esecuzione |
|---|
| 127 | unsigned i = 0; |
|---|
| 128 | Note* pNote; |
|---|
| 129 | while ( i < __playing_notes_queue.size() ) { |
|---|
| 130 | pNote = __playing_notes_queue[ i ]; // recupero una nuova nota |
|---|
| 131 | unsigned res = __render_note( pNote, nFrames, pSong ); |
|---|
| 132 | if ( res == 1 ) { // la nota e' finita |
|---|
| 133 | __playing_notes_queue.erase( __playing_notes_queue.begin() + i ); |
|---|
| 134 | pNote->get_instrument()->dequeue(); |
|---|
| 135 | delete pNote; |
|---|
| 136 | pNote = NULL; |
|---|
| 137 | } else { |
|---|
| 138 | ++i; // carico la prox nota |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | void Sampler::note_on( Note *note ) |
|---|
| 146 | { |
|---|
| 147 | //infoLog( "[noteOn]" ); |
|---|
| 148 | assert( note ); |
|---|
| 149 | |
|---|
| 150 | // mute groups |
|---|
| 151 | Instrument *pInstr = note->get_instrument(); |
|---|
| 152 | if ( pInstr->get_mute_group() != -1 ) { |
|---|
| 153 | // remove all notes using the same mute group |
|---|
| 154 | for ( unsigned j = 0; j < __playing_notes_queue.size(); j++ ) { // delete older note |
|---|
| 155 | Note *pNote = __playing_notes_queue[ j ]; |
|---|
| 156 | |
|---|
| 157 | if ( ( pNote->get_instrument() != pInstr ) && ( pNote->get_instrument()->get_mute_group() == pInstr->get_mute_group() ) ) { |
|---|
| 158 | //warningLog("release"); |
|---|
| 159 | pNote->m_adsr.release(); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | pInstr->enqueue(); |
|---|
| 165 | __playing_notes_queue.push_back( note ); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | void Sampler::note_off( Note* note ) |
|---|
| 171 | { |
|---|
| 172 | UNUSED( note ); |
|---|
| 173 | ERRORLOG( "not implemented yet" ); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | /// Render a note |
|---|
| 179 | /// Return 0: the note is not ended |
|---|
| 180 | /// Return 1: the note is ended |
|---|
| 181 | unsigned Sampler::__render_note( Note* pNote, unsigned nBufferSize, Song* pSong ) |
|---|
| 182 | { |
|---|
| 183 | //infoLog( "[renderNote] instr: " + pNote->getInstrument()->m_sName ); |
|---|
| 184 | assert( pSong ); |
|---|
| 185 | |
|---|
| 186 | unsigned int nFramepos; |
|---|
| 187 | Hydrogen* pEngine = Hydrogen::get_instance(); |
|---|
| 188 | if ( pEngine->getState() == STATE_PLAYING ) { |
|---|
| 189 | nFramepos = __audio_output->m_transport.m_nFrames; |
|---|
| 190 | } else { |
|---|
| 191 | // use this to support realtime events when not playing |
|---|
| 192 | nFramepos = pEngine->getRealtimeFrames(); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | Instrument *pInstr = pNote->get_instrument(); |
|---|
| 197 | if ( !pInstr ) { |
|---|
| 198 | ERRORLOG( "NULL instrument" ); |
|---|
| 199 | return 1; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | float fLayerGain = 1.0; |
|---|
| 203 | float fLayerPitch = 0.0; |
|---|
| 204 | |
|---|
| 205 | // scelgo il sample da usare in base alla velocity |
|---|
| 206 | Sample *pSample = NULL; |
|---|
| 207 | for ( unsigned nLayer = 0; nLayer < MAX_LAYERS; ++nLayer ) { |
|---|
| 208 | InstrumentLayer *pLayer = pInstr->get_layer( nLayer ); |
|---|
| 209 | if ( pLayer == NULL ) continue; |
|---|
| 210 | |
|---|
| 211 | if ( ( pNote->get_velocity() >= pLayer->get_start_velocity() ) && ( pNote->get_velocity() <= pLayer->get_end_velocity() ) ) { |
|---|
| 212 | pSample = pLayer->get_sample(); |
|---|
| 213 | fLayerGain = pLayer->get_gain(); |
|---|
| 214 | fLayerPitch = pLayer->get_pitch(); |
|---|
| 215 | break; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | if ( !pSample ) { |
|---|
| 219 | QString dummy = QString( "NULL sample for instrument %1. Note velocity: %2" ).arg( pInstr->get_name() ).arg( pNote->get_velocity() ); |
|---|
| 220 | WARNINGLOG( dummy ); |
|---|
| 221 | return 1; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | if ( pNote->m_fSamplePosition >= pSample->get_n_frames() ) { |
|---|
| 225 | WARNINGLOG( "sample position out of bounds. The layer has been resized during note play?" ); |
|---|
| 226 | return 1; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | int noteStartInFrames = ( int ) ( pNote->get_position() * __audio_output->m_transport.m_nTickSize ) + pNote->m_nHumanizeDelay; |
|---|
| 230 | |
|---|
| 231 | int nInitialSilence = 0; |
|---|
| 232 | if ( noteStartInFrames > ( int ) nFramepos ) { // scrivo silenzio prima dell'inizio della nota |
|---|
| 233 | nInitialSilence = noteStartInFrames - nFramepos; |
|---|
| 234 | int nFrames = nBufferSize - nInitialSilence; |
|---|
| 235 | if ( nFrames < 0 ) { |
|---|
| 236 | int noteStartInFramesNoHumanize = ( int )pNote->get_position() * __audio_output->m_transport.m_nTickSize; |
|---|
| 237 | if ( noteStartInFramesNoHumanize > ( int )( nFramepos + nBufferSize ) ) { |
|---|
| 238 | // this note is not valid. it's in the future...let's skip it.... |
|---|
| 239 | ERRORLOG( QString( "Note pos in the future?? Current frames: %1, note frame pos: %2" ).arg( nFramepos ).arg(noteStartInFramesNoHumanize ) ); |
|---|
| 240 | //pNote->dumpInfo(); |
|---|
| 241 | return 1; |
|---|
| 242 | } |
|---|
| 243 | // delay note execution |
|---|
| 244 | //INFOLOG( "Delaying note execution. noteStartInFrames: " + to_string( noteStartInFrames ) + ", nFramePos: " + to_string( nFramepos ) ); |
|---|
| 245 | return 0; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | float cost_L = 1.0f; |
|---|
| 250 | float cost_R = 1.0f; |
|---|
| 251 | float cost_track_L = 1.0f; |
|---|
| 252 | float cost_track_R = 1.0f; |
|---|
| 253 | float fSendFXLevel_L = 1.0f; |
|---|
| 254 | float fSendFXLevel_R = 1.0f; |
|---|
| 255 | |
|---|
| 256 | if ( pInstr->is_muted() || pSong->__is_muted ) { // is instrument muted? |
|---|
| 257 | cost_L = 0.0; |
|---|
| 258 | cost_R = 0.0; |
|---|
| 259 | if ( Preferences::getInstance()->m_nJackTrackOutputMode == 0 ) { |
|---|
| 260 | // Post-Fader |
|---|
| 261 | cost_track_L = 0.0; |
|---|
| 262 | cost_track_R = 0.0; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | fSendFXLevel_L = 0.0f; |
|---|
| 266 | fSendFXLevel_R = 0.0f; |
|---|
| 267 | } else { // Precompute some values... |
|---|
| 268 | cost_L = cost_L * pNote->get_velocity(); // note velocity |
|---|
| 269 | cost_L = cost_L * pNote->get_pan_l(); // note pan |
|---|
| 270 | cost_L = cost_L * fLayerGain; // layer gain |
|---|
| 271 | cost_L = cost_L * pInstr->get_pan_l(); // instrument pan |
|---|
| 272 | cost_L = cost_L * pInstr->get_gain(); // instrument gain |
|---|
| 273 | fSendFXLevel_L = cost_L; |
|---|
| 274 | |
|---|
| 275 | cost_L = cost_L * pInstr->get_volume(); // instrument volume |
|---|
| 276 | if ( Preferences::getInstance()->m_nJackTrackOutputMode == 0 ) { |
|---|
| 277 | // Post-Fader |
|---|
| 278 | cost_track_L = cost_L * 2; |
|---|
| 279 | } |
|---|
| 280 | cost_L = cost_L * pSong->get_volume(); // song volume |
|---|
| 281 | cost_L = cost_L * 2; // max pan is 0.5 |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | cost_R = cost_R * pNote->get_velocity(); // note velocity |
|---|
| 285 | cost_R = cost_R * pNote->get_pan_r(); // note pan |
|---|
| 286 | cost_R = cost_R * fLayerGain; // layer gain |
|---|
| 287 | cost_R = cost_R * pInstr->get_pan_r(); // instrument pan |
|---|
| 288 | cost_R = cost_R * pInstr->get_gain(); // instrument gain |
|---|
| 289 | fSendFXLevel_R = cost_R; |
|---|
| 290 | |
|---|
| 291 | cost_R = cost_R * pInstr->get_volume(); // instrument volume |
|---|
| 292 | if ( Preferences::getInstance()->m_nJackTrackOutputMode == 0 ) { |
|---|
| 293 | // Post-Fader |
|---|
| 294 | cost_track_R = cost_R * 2; |
|---|
| 295 | } |
|---|
| 296 | cost_R = cost_R * pSong->get_volume(); // song pan |
|---|
| 297 | cost_R = cost_R * 2; // max pan is 0.5 |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | // direct track outputs only use velocity |
|---|
| 301 | if ( Preferences::getInstance()->m_nJackTrackOutputMode == 1 ) { |
|---|
| 302 | cost_track_L = cost_track_L * pNote->get_velocity(); |
|---|
| 303 | cost_track_L = cost_track_L * fLayerGain; |
|---|
| 304 | cost_track_R = cost_track_L; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | // Se non devo fare resample (drumkit) posso evitare di utilizzare i float e gestire il tutto in |
|---|
| 308 | // maniera ottimizzata |
|---|
| 309 | // constant^12 = 2, so constant = 2^(1/12) = 1.059463. |
|---|
| 310 | // float nStep = 1.0;1.0594630943593 |
|---|
| 311 | |
|---|
| 312 | float fTotalPitch = pNote->m_noteKey.m_nOctave * 12 + pNote->m_noteKey.m_key; |
|---|
| 313 | fTotalPitch += pNote->get_pitch(); |
|---|
| 314 | fTotalPitch += fLayerPitch; |
|---|
| 315 | |
|---|
| 316 | //_INFOLOG( "total pitch: " + to_string( fTotalPitch ) ); |
|---|
| 317 | |
|---|
| 318 | if ( fTotalPitch == 0.0 && pSample->get_sample_rate() == __audio_output->getSampleRate() ) { // NO RESAMPLE |
|---|
| 319 | return __render_note_no_resample( pSample, pNote, nBufferSize, nInitialSilence, cost_L, cost_R, cost_track_L, cost_track_R, fSendFXLevel_L, fSendFXLevel_R, pSong ); |
|---|
| 320 | } else { // RESAMPLE |
|---|
| 321 | return __render_note_resample( pSample, pNote, nBufferSize, nInitialSilence, cost_L, cost_R, cost_track_L, cost_track_R, fLayerPitch, fSendFXLevel_L, fSendFXLevel_R, pSong ); |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | int Sampler::__render_note_no_resample( |
|---|
| 329 | Sample *pSample, |
|---|
| 330 | Note *pNote, |
|---|
| 331 | int nBufferSize, |
|---|
| 332 | int nInitialSilence, |
|---|
| 333 | float cost_L, |
|---|
| 334 | float cost_R, |
|---|
| 335 | float cost_track_L, |
|---|
| 336 | float cost_track_R, |
|---|
| 337 | float fSendFXLevel_L, |
|---|
| 338 | float fSendFXLevel_R, |
|---|
| 339 | Song* pSong |
|---|
| 340 | ) |
|---|
| 341 | { |
|---|
| 342 | int retValue = 1; // the note is ended |
|---|
| 343 | |
|---|
| 344 | int nNoteLength = -1; |
|---|
| 345 | if ( pNote->get_lenght() != -1 ) { |
|---|
| 346 | nNoteLength = ( int )( pNote->get_lenght() * __audio_output->m_transport.m_nTickSize ); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | int nAvail_bytes = pSample->get_n_frames() - ( int )pNote->m_fSamplePosition; // verifico il numero di frame disponibili ancora da eseguire |
|---|
| 350 | |
|---|
| 351 | if ( nAvail_bytes > nBufferSize - nInitialSilence ) { // il sample e' piu' grande del buffersize |
|---|
| 352 | // imposto il numero dei bytes disponibili uguale al buffersize |
|---|
| 353 | nAvail_bytes = nBufferSize - nInitialSilence; |
|---|
| 354 | retValue = 0; // the note is not ended yet |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | //ADSR *pADSR = pNote->m_pADSR; |
|---|
| 358 | |
|---|
| 359 | int nInitialBufferPos = nInitialSilence; |
|---|
| 360 | int nInitialSamplePos = ( int )pNote->m_fSamplePosition; |
|---|
| 361 | int nSamplePos = nInitialSamplePos; |
|---|
| 362 | int nTimes = nInitialBufferPos + nAvail_bytes; |
|---|
| 363 | int nInstrument = pSong->get_instrument_list()->get_pos( pNote->get_instrument() ); |
|---|
| 364 | |
|---|
| 365 | // filter |
|---|
| 366 | bool bUseLPF = pNote->get_instrument()->is_filter_active(); |
|---|
| 367 | float fResonance = pNote->get_instrument()->get_filter_resonance(); |
|---|
| 368 | float fCutoff = pNote->get_instrument()->get_filter_cutoff(); |
|---|
| 369 | |
|---|
| 370 | float *pSample_data_L = pSample->get_data_l(); |
|---|
| 371 | float *pSample_data_R = pSample->get_data_r(); |
|---|
| 372 | |
|---|
| 373 | float fInstrPeak_L = pNote->get_instrument()->get_peak_l(); // this value will be reset to 0 by the mixer.. |
|---|
| 374 | float fInstrPeak_R = pNote->get_instrument()->get_peak_r(); // this value will be reset to 0 by the mixer.. |
|---|
| 375 | |
|---|
| 376 | float fADSRValue; |
|---|
| 377 | float fVal_L; |
|---|
| 378 | float fVal_R; |
|---|
| 379 | |
|---|
| 380 | /* |
|---|
| 381 | * nInstrument could be -1 if the instrument is not found in the current drumset. |
|---|
| 382 | * This happens when someone is using the prelistening function of the soundlibrary. |
|---|
| 383 | */ |
|---|
| 384 | |
|---|
| 385 | if( nInstrument < 0 ) { |
|---|
| 386 | nInstrument = 0; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | for ( int nBufferPos = nInitialBufferPos; nBufferPos < nTimes; ++nBufferPos ) { |
|---|
| 391 | if ( ( nNoteLength != -1 ) && ( nNoteLength <= pNote->m_fSamplePosition ) ) { |
|---|
| 392 | if ( pNote->m_adsr.release() == 0 ) { |
|---|
| 393 | retValue = 1; // the note is ended |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | fADSRValue = pNote->m_adsr.get_value( 1 ); |
|---|
| 398 | fVal_L = pSample_data_L[ nSamplePos ] * fADSRValue; |
|---|
| 399 | fVal_R = pSample_data_R[ nSamplePos ] * fADSRValue; |
|---|
| 400 | |
|---|
| 401 | // Low pass resonant filter |
|---|
| 402 | if ( bUseLPF ) { |
|---|
| 403 | pNote->m_fBandPassFilterBuffer_L = fResonance * pNote->m_fBandPassFilterBuffer_L + fCutoff * ( fVal_L - pNote->m_fLowPassFilterBuffer_L ); |
|---|
| 404 | pNote->m_fLowPassFilterBuffer_L += fCutoff * pNote->m_fBandPassFilterBuffer_L; |
|---|
| 405 | fVal_L = pNote->m_fLowPassFilterBuffer_L; |
|---|
| 406 | |
|---|
| 407 | pNote->m_fBandPassFilterBuffer_R = fResonance * pNote->m_fBandPassFilterBuffer_R + fCutoff * ( fVal_R - pNote->m_fLowPassFilterBuffer_R ); |
|---|
| 408 | pNote->m_fLowPassFilterBuffer_R += fCutoff * pNote->m_fBandPassFilterBuffer_R; |
|---|
| 409 | fVal_R = pNote->m_fLowPassFilterBuffer_R; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | #ifdef JACK_SUPPORT |
|---|
| 413 | if ( __audio_output->has_track_outs() |
|---|
| 414 | && dynamic_cast<JackOutput*>(__audio_output) ) { |
|---|
| 415 | assert( __track_out_L[ nInstrument ] ); |
|---|
| 416 | assert( __track_out_R[ nInstrument ] ); |
|---|
| 417 | __track_out_L[ nInstrument ][nBufferPos] += fVal_L * cost_track_L; |
|---|
| 418 | __track_out_R[ nInstrument ][nBufferPos] += fVal_R * cost_track_R; |
|---|
| 419 | } |
|---|
| 420 | #endif |
|---|
| 421 | |
|---|
| 422 | fVal_L = fVal_L * cost_L; |
|---|
| 423 | fVal_R = fVal_R * cost_R; |
|---|
| 424 | |
|---|
| 425 | // update instr peak |
|---|
| 426 | if ( fVal_L > fInstrPeak_L ) { |
|---|
| 427 | fInstrPeak_L = fVal_L; |
|---|
| 428 | } |
|---|
| 429 | if ( fVal_R > fInstrPeak_R ) { |
|---|
| 430 | fInstrPeak_R = fVal_R; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | // to main mix |
|---|
| 434 | __main_out_L[nBufferPos] += fVal_L; |
|---|
| 435 | __main_out_R[nBufferPos] += fVal_R; |
|---|
| 436 | |
|---|
| 437 | ++nSamplePos; |
|---|
| 438 | } |
|---|
| 439 | pNote->m_fSamplePosition += nAvail_bytes; |
|---|
| 440 | pNote->get_instrument()->set_peak_l( fInstrPeak_L ); |
|---|
| 441 | pNote->get_instrument()->set_peak_r( fInstrPeak_R ); |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | #ifdef LADSPA_SUPPORT |
|---|
| 445 | // LADSPA |
|---|
| 446 | for ( unsigned nFX = 0; nFX < MAX_FX; ++nFX ) { |
|---|
| 447 | LadspaFX *pFX = Effects::getInstance()->getLadspaFX( nFX ); |
|---|
| 448 | |
|---|
| 449 | float fLevel = pNote->get_instrument()->get_fx_level( nFX ); |
|---|
| 450 | |
|---|
| 451 | if ( ( pFX ) && ( fLevel != 0.0 ) ) { |
|---|
| 452 | fLevel = fLevel * pFX->getVolume(); |
|---|
| 453 | float *pBuf_L = pFX->m_pBuffer_L; |
|---|
| 454 | float *pBuf_R = pFX->m_pBuffer_R; |
|---|
| 455 | |
|---|
| 456 | // float fFXCost_L = cost_L * fLevel; |
|---|
| 457 | // float fFXCost_R = cost_R * fLevel; |
|---|
| 458 | float fFXCost_L = fLevel * fSendFXLevel_L; |
|---|
| 459 | float fFXCost_R = fLevel * fSendFXLevel_R; |
|---|
| 460 | |
|---|
| 461 | int nBufferPos = nInitialBufferPos; |
|---|
| 462 | int nSamplePos = nInitialSamplePos; |
|---|
| 463 | for ( int i = 0; i < nAvail_bytes; ++i ) { |
|---|
| 464 | pBuf_L[ nBufferPos ] += pSample_data_L[ nSamplePos ] * fFXCost_L; |
|---|
| 465 | pBuf_R[ nBufferPos ] += pSample_data_R[ nSamplePos ] * fFXCost_R; |
|---|
| 466 | ++nSamplePos; |
|---|
| 467 | ++nBufferPos; |
|---|
| 468 | } |
|---|
| 469 | } |
|---|
| 470 | } |
|---|
| 471 | // ~LADSPA |
|---|
| 472 | #endif |
|---|
| 473 | |
|---|
| 474 | return retValue; |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | int Sampler::__render_note_resample( |
|---|
| 480 | Sample *pSample, |
|---|
| 481 | Note *pNote, |
|---|
| 482 | int nBufferSize, |
|---|
| 483 | int nInitialSilence, |
|---|
| 484 | float cost_L, |
|---|
| 485 | float cost_R, |
|---|
| 486 | float cost_track_L, |
|---|
| 487 | float cost_track_R, |
|---|
| 488 | float fLayerPitch, |
|---|
| 489 | float fSendFXLevel_L, |
|---|
| 490 | float fSendFXLevel_R, |
|---|
| 491 | Song* pSong |
|---|
| 492 | ) |
|---|
| 493 | { |
|---|
| 494 | int nNoteLength = -1; |
|---|
| 495 | if ( pNote->get_lenght() != -1 ) { |
|---|
| 496 | nNoteLength = ( int )( pNote->get_lenght() * __audio_output->m_transport.m_nTickSize ); |
|---|
| 497 | } |
|---|
| 498 | float fNotePitch = pNote->get_pitch() + fLayerPitch; |
|---|
| 499 | fNotePitch += pNote->m_noteKey.m_nOctave * 12 + pNote->m_noteKey.m_key; |
|---|
| 500 | |
|---|
| 501 | //_INFOLOG( "pitch: " + to_string( fNotePitch ) ); |
|---|
| 502 | |
|---|
| 503 | float fStep = pow( 1.0594630943593, ( double )fNotePitch ); |
|---|
| 504 | fStep *= ( float )pSample->get_sample_rate() / __audio_output->getSampleRate(); // Adjust for audio driver sample rate |
|---|
| 505 | |
|---|
| 506 | int nAvail_bytes = ( int )( ( float )( pSample->get_n_frames() - pNote->m_fSamplePosition ) / fStep ); // verifico il numero di frame disponibili ancora da eseguire |
|---|
| 507 | |
|---|
| 508 | int retValue = 1; // the note is ended |
|---|
| 509 | if ( nAvail_bytes > nBufferSize - nInitialSilence ) { // il sample e' piu' grande del buffersize |
|---|
| 510 | // imposto il numero dei bytes disponibili uguale al buffersize |
|---|
| 511 | nAvail_bytes = nBufferSize - nInitialSilence; |
|---|
| 512 | retValue = 0; // the note is not ended yet |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | // ADSR *pADSR = pNote->m_pADSR; |
|---|
| 516 | |
|---|
| 517 | int nInitialBufferPos = nInitialSilence; |
|---|
| 518 | float fInitialSamplePos = pNote->m_fSamplePosition; |
|---|
| 519 | float fSamplePos = pNote->m_fSamplePosition; |
|---|
| 520 | int nTimes = nInitialBufferPos + nAvail_bytes; |
|---|
| 521 | int nInstrument = pSong->get_instrument_list()->get_pos( pNote->get_instrument() ); |
|---|
| 522 | |
|---|
| 523 | // filter |
|---|
| 524 | bool bUseLPF = pNote->get_instrument()->is_filter_active(); |
|---|
| 525 | float fResonance = pNote->get_instrument()->get_filter_resonance(); |
|---|
| 526 | float fCutoff = pNote->get_instrument()->get_filter_cutoff(); |
|---|
| 527 | |
|---|
| 528 | float *pSample_data_L = pSample->get_data_l(); |
|---|
| 529 | float *pSample_data_R = pSample->get_data_r(); |
|---|
| 530 | |
|---|
| 531 | float fInstrPeak_L = pNote->get_instrument()->get_peak_l(); // this value will be reset to 0 by the mixer.. |
|---|
| 532 | float fInstrPeak_R = pNote->get_instrument()->get_peak_r(); // this value will be reset to 0 by the mixer.. |
|---|
| 533 | |
|---|
| 534 | float fADSRValue = 1.0; |
|---|
| 535 | float fVal_L; |
|---|
| 536 | float fVal_R; |
|---|
| 537 | int nSampleFrames = pSample->get_n_frames(); |
|---|
| 538 | |
|---|
| 539 | /* |
|---|
| 540 | * nInstrument could be -1 if the instrument is not found in the current drumset. |
|---|
| 541 | * This happens when someone is using the prelistening function of the soundlibrary. |
|---|
| 542 | */ |
|---|
| 543 | |
|---|
| 544 | if( nInstrument < 0 ) { |
|---|
| 545 | nInstrument = 0; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | for ( int nBufferPos = nInitialBufferPos; nBufferPos < nTimes; ++nBufferPos ) { |
|---|
| 550 | if ( ( nNoteLength != -1 ) && ( nNoteLength <= pNote->m_fSamplePosition ) ) { |
|---|
| 551 | if ( pNote->m_adsr.release() == 0 ) { |
|---|
| 552 | retValue = 1; // the note is ended |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | int nSamplePos = ( int )fSamplePos; |
|---|
| 557 | float fDiff = fSamplePos - nSamplePos; |
|---|
| 558 | if ( ( nSamplePos + 1 ) >= nSampleFrames ) { |
|---|
| 559 | fVal_L = linear_interpolation( pSample_data_L[ nSampleFrames ], 0, fDiff ); |
|---|
| 560 | fVal_R = linear_interpolation( pSample_data_R[ nSampleFrames ], 0, fDiff ); |
|---|
| 561 | } else { |
|---|
| 562 | fVal_L = linear_interpolation( pSample_data_L[nSamplePos], pSample_data_L[nSamplePos + 1], fDiff ); |
|---|
| 563 | fVal_R = linear_interpolation( pSample_data_R[nSamplePos], pSample_data_R[nSamplePos + 1], fDiff ); |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | // ADSR envelope |
|---|
| 567 | fADSRValue = pNote->m_adsr.get_value( fStep ); |
|---|
| 568 | fVal_L = fVal_L * fADSRValue; |
|---|
| 569 | fVal_R = fVal_R * fADSRValue; |
|---|
| 570 | |
|---|
| 571 | // Low pass resonant filter |
|---|
| 572 | if ( bUseLPF ) { |
|---|
| 573 | pNote->m_fBandPassFilterBuffer_L = fResonance * pNote->m_fBandPassFilterBuffer_L + fCutoff * ( fVal_L - pNote->m_fLowPassFilterBuffer_L ); |
|---|
| 574 | pNote->m_fLowPassFilterBuffer_L += fCutoff * pNote->m_fBandPassFilterBuffer_L; |
|---|
| 575 | fVal_L = pNote->m_fLowPassFilterBuffer_L; |
|---|
| 576 | |
|---|
| 577 | pNote->m_fBandPassFilterBuffer_R = fResonance * pNote->m_fBandPassFilterBuffer_R + fCutoff * ( fVal_R - pNote->m_fLowPassFilterBuffer_R ); |
|---|
| 578 | pNote->m_fLowPassFilterBuffer_R += fCutoff * pNote->m_fBandPassFilterBuffer_R; |
|---|
| 579 | fVal_R = pNote->m_fLowPassFilterBuffer_R; |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | #ifdef JACK_SUPPORT |
|---|
| 584 | if ( __audio_output->has_track_outs() |
|---|
| 585 | && dynamic_cast<JackOutput*>(__audio_output) ) { |
|---|
| 586 | assert( __track_out_L[ nInstrument ] ); |
|---|
| 587 | assert( __track_out_R[ nInstrument ] ); |
|---|
| 588 | __track_out_L[ nInstrument ][nBufferPos] += (fVal_L * cost_track_L); |
|---|
| 589 | __track_out_R[ nInstrument ][nBufferPos] += (fVal_R * cost_track_R); |
|---|
| 590 | } |
|---|
| 591 | #endif |
|---|
| 592 | |
|---|
| 593 | fVal_L = fVal_L * cost_L; |
|---|
| 594 | fVal_R = fVal_R * cost_R; |
|---|
| 595 | |
|---|
| 596 | // update instr peak |
|---|
| 597 | if ( fVal_L > fInstrPeak_L ) { |
|---|
| 598 | fInstrPeak_L = fVal_L; |
|---|
| 599 | } |
|---|
| 600 | if ( fVal_R > fInstrPeak_R ) { |
|---|
| 601 | fInstrPeak_R = fVal_R; |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | // to main mix |
|---|
| 605 | __main_out_L[nBufferPos] += fVal_L; |
|---|
| 606 | __main_out_R[nBufferPos] += fVal_R; |
|---|
| 607 | |
|---|
| 608 | fSamplePos += fStep; |
|---|
| 609 | } |
|---|
| 610 | pNote->m_fSamplePosition += nAvail_bytes * fStep; |
|---|
| 611 | pNote->get_instrument()->set_peak_l( fInstrPeak_L ); |
|---|
| 612 | pNote->get_instrument()->set_peak_r( fInstrPeak_R ); |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | |
|---|
| 616 | #ifdef LADSPA_SUPPORT |
|---|
| 617 | // LADSPA |
|---|
| 618 | for ( unsigned nFX = 0; nFX < MAX_FX; ++nFX ) { |
|---|
| 619 | LadspaFX *pFX = Effects::getInstance()->getLadspaFX( nFX ); |
|---|
| 620 | float fLevel = pNote->get_instrument()->get_fx_level( nFX ); |
|---|
| 621 | if ( ( pFX ) && ( fLevel != 0.0 ) ) { |
|---|
| 622 | fLevel = fLevel * pFX->getVolume(); |
|---|
| 623 | |
|---|
| 624 | float *pBuf_L = pFX->m_pBuffer_L; |
|---|
| 625 | float *pBuf_R = pFX->m_pBuffer_R; |
|---|
| 626 | |
|---|
| 627 | // float fFXCost_L = cost_L * fLevel; |
|---|
| 628 | // float fFXCost_R = cost_R * fLevel; |
|---|
| 629 | float fFXCost_L = fLevel * fSendFXLevel_L; |
|---|
| 630 | float fFXCost_R = fLevel * fSendFXLevel_R; |
|---|
| 631 | |
|---|
| 632 | int nBufferPos = nInitialBufferPos; |
|---|
| 633 | float fSamplePos = fInitialSamplePos; |
|---|
| 634 | for ( int i = 0; i < nAvail_bytes; ++i ) { |
|---|
| 635 | int nSamplePos = ( int )fSamplePos; |
|---|
| 636 | float fDiff = fSamplePos - nSamplePos; |
|---|
| 637 | |
|---|
| 638 | if ( ( nSamplePos + 1 ) >= nSampleFrames ) { |
|---|
| 639 | fVal_L = linear_interpolation( pSample_data_L[nSamplePos], 0, fDiff ); |
|---|
| 640 | fVal_R = linear_interpolation( pSample_data_R[nSamplePos], 0, fDiff ); |
|---|
| 641 | } else { |
|---|
| 642 | fVal_L = linear_interpolation( pSample_data_L[nSamplePos], pSample_data_L[nSamplePos + 1], fDiff ); |
|---|
| 643 | fVal_R = linear_interpolation( pSample_data_R[nSamplePos], pSample_data_R[nSamplePos + 1], fDiff ); |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | pBuf_L[ nBufferPos ] += fVal_L * fFXCost_L; |
|---|
| 647 | pBuf_R[ nBufferPos ] += fVal_R * fFXCost_R; |
|---|
| 648 | fSamplePos += fStep; |
|---|
| 649 | ++nBufferPos; |
|---|
| 650 | } |
|---|
| 651 | } |
|---|
| 652 | } |
|---|
| 653 | #endif |
|---|
| 654 | |
|---|
| 655 | return retValue; |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | void Sampler::stop_playing_notes( Instrument* instrument ) |
|---|
| 660 | { |
|---|
| 661 | /* |
|---|
| 662 | // send a note-off event to all notes present in the playing note queue |
|---|
| 663 | for ( int i = 0; i < __playing_notes_queue.size(); ++i ) { |
|---|
| 664 | Note *pNote = __playing_notes_queue[ i ]; |
|---|
| 665 | pNote->m_pADSR->release(); |
|---|
| 666 | } |
|---|
| 667 | */ |
|---|
| 668 | |
|---|
| 669 | if ( instrument ) { // stop all notes using this instrument |
|---|
| 670 | for ( unsigned i = 0; i < __playing_notes_queue.size(); ) { |
|---|
| 671 | Note *pNote = __playing_notes_queue[ i ]; |
|---|
| 672 | assert( pNote ); |
|---|
| 673 | if ( pNote->get_instrument() == instrument ) { |
|---|
| 674 | delete pNote; |
|---|
| 675 | instrument->dequeue(); |
|---|
| 676 | __playing_notes_queue.erase( __playing_notes_queue.begin() + i ); |
|---|
| 677 | } |
|---|
| 678 | ++i; |
|---|
| 679 | } |
|---|
| 680 | } else { // stop all notes |
|---|
| 681 | // delete all copied notes in the playing notes queue |
|---|
| 682 | for ( unsigned i = 0; i < __playing_notes_queue.size(); ++i ) { |
|---|
| 683 | Note *pNote = __playing_notes_queue[i]; |
|---|
| 684 | pNote->get_instrument()->dequeue(); |
|---|
| 685 | delete pNote; |
|---|
| 686 | } |
|---|
| 687 | __playing_notes_queue.clear(); |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | |
|---|
| 693 | /// Preview, uses only the first layer |
|---|
| 694 | void Sampler::preview_sample( Sample* sample, int length ) |
|---|
| 695 | { |
|---|
| 696 | AudioEngine::get_instance()->lock( "Sampler::previewSample" ); |
|---|
| 697 | |
|---|
| 698 | InstrumentLayer *pLayer = __preview_instrument->get_layer( 0 ); |
|---|
| 699 | |
|---|
| 700 | Sample *pOldSample = pLayer->get_sample(); |
|---|
| 701 | pLayer->set_sample( sample ); |
|---|
| 702 | |
|---|
| 703 | Note *previewNote = new Note( __preview_instrument, 0, 1.0, 0.5, 0.5, length, 0 ); |
|---|
| 704 | |
|---|
| 705 | stop_playing_notes( __preview_instrument ); |
|---|
| 706 | note_on( previewNote ); |
|---|
| 707 | delete pOldSample; |
|---|
| 708 | |
|---|
| 709 | AudioEngine::get_instance()->unlock(); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | |
|---|
| 713 | |
|---|
| 714 | void Sampler::preview_instrument( Instrument* instr ) |
|---|
| 715 | { |
|---|
| 716 | Instrument * old_preview; |
|---|
| 717 | AudioEngine::get_instance()->lock( "Sampler::previewInstrument" ); |
|---|
| 718 | |
|---|
| 719 | stop_playing_notes( __preview_instrument ); |
|---|
| 720 | |
|---|
| 721 | old_preview = __preview_instrument; |
|---|
| 722 | __preview_instrument = instr; |
|---|
| 723 | |
|---|
| 724 | Note *previewNote = new Note( __preview_instrument, 0, 1.0, 0.5, 0.5, MAX_NOTES, 0 ); |
|---|
| 725 | |
|---|
| 726 | note_on( previewNote ); // exclusive note |
|---|
| 727 | AudioEngine::get_instance()->unlock(); |
|---|
| 728 | delete old_preview; |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | |
|---|
| 733 | void Sampler::set_audio_output( AudioOutput* audio_output ) |
|---|
| 734 | { |
|---|
| 735 | __audio_output = audio_output; |
|---|
| 736 | } |
|---|
| 737 | |
|---|
| 738 | void Sampler::makeTrackOutputQueues( ) |
|---|
| 739 | { |
|---|
| 740 | INFOLOG( "Making Output Queues" ); |
|---|
| 741 | |
|---|
| 742 | #ifdef JACK_SUPPORT |
|---|
| 743 | JackOutput* jao = 0; |
|---|
| 744 | if (__audio_output && __audio_output->has_track_outs() ) { |
|---|
| 745 | jao = dynamic_cast<JackOutput*>(__audio_output); |
|---|
| 746 | } |
|---|
| 747 | if ( jao ) { |
|---|
| 748 | for (int nTrack = 0; nTrack < jao->getNumTracks( ); nTrack++) { |
|---|
| 749 | __track_out_L[nTrack] = jao->getTrackOut_L( nTrack ); |
|---|
| 750 | assert( __track_out_L[ nTrack ] ); |
|---|
| 751 | __track_out_R[nTrack] = jao->getTrackOut_R( nTrack ); |
|---|
| 752 | assert( __track_out_R[ nTrack ] ); |
|---|
| 753 | } |
|---|
| 754 | } |
|---|
| 755 | #endif // JACK_SUPPORT |
|---|
| 756 | |
|---|
| 757 | } |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | |
|---|
| 761 | }; |
|---|
| 762 | |
|---|