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