root/branches/transport_redesign_2/libs/hydrogen/src/sampler/sampler.cpp @ 1077

Revision 1077, 23.6 KB (checked in by gabriel@…, 4 years ago)

Convert Sampler to new transport.

All compile except Hydrogen and Sampler. Note that there are
several things not implemented (like SeqScriptIterator?).

Sampler would compile except for a reference to Song* for the tracking
outputs.

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