root/trunk/libs/hydrogen/src/sampler/sampler.cpp @ 260

Revision 260, 22.7 KB (checked in by wolke, 5 years ago)

decrease in number of function call divided by jack track outs

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