root/branches/new_fx_rack_and_sample_fun/gui/src/InstrumentEditor/InstrumentEditor.cpp @ 599

Revision 599, 19.7 KB (checked in by wolke, 5 years ago)

add changes from trunk to branche

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
24#include <QtGui>
25
26#include <math.h>
27#include <assert.h>
28
29#include <hydrogen/Song.h>
30#include <hydrogen/hydrogen.h>
31#include <hydrogen/globals.h>
32#include <hydrogen/adsr.h>
33#include <hydrogen/sample.h>
34#include <hydrogen/instrument.h>
35#include <hydrogen/audio_engine.h>
36#include <hydrogen/event_queue.h>
37using namespace H2Core;
38
39#include "../HydrogenApp.h"
40#include "../Skin.h"
41#include "../widgets/Rotary.h"
42#include "../widgets/ClickableLabel.h"
43#include "../widgets/Button.h"
44#include "../widgets/LCD.h"
45#include "../widgets/Fader.h"
46#include "InstrumentEditor.h"
47#include "WaveDisplay.h"
48#include "LayerPreview.h"
49#include "AudioFileBrowser/AudioFileBrowser.h"
50
51InstrumentEditor::InstrumentEditor( QWidget* pParent )
52 : QWidget( pParent )
53 , Object( "InstrumentEditor" )
54 , m_pInstrument( NULL )
55 , m_nSelectedLayer( 0 )
56{
57        setFixedWidth( 290 );
58
59// Instrument properties top
60        m_pInstrumentPropTop = new PixmapWidget( this );
61        m_pInstrumentPropTop->setPixmap( "/instrumentEditor/instrumentTab_top.png" );
62
63        m_pShowInstrumentBtn = new ToggleButton(
64                        m_pInstrumentPropTop,
65                        "/skin_btn_on.png",
66                        "/skin_btn_off.png",
67                        "/skin_btn_over.png",
68                        QSize( 100, 17 ),
69                        true
70        );
71        m_pShowInstrumentBtn->setText(trUtf8("General"));
72        m_pShowInstrumentBtn->setToolTip( trUtf8( "Show instrument properties" ) );
73        connect( m_pShowInstrumentBtn, SIGNAL( clicked(Button*) ), this, SLOT( buttonClicked(Button*) ) );
74        m_pShowInstrumentBtn->move( 40, 7 );
75        m_pShowInstrumentBtn->setPressed( true );
76
77
78        m_pShowLayersBtn = new ToggleButton(
79                        m_pInstrumentPropTop,
80                        "/skin_btn_on.png",
81                        "/skin_btn_off.png",
82                        "/skin_btn_over.png",
83                        QSize( 100, 17 ),
84                        true
85        );
86        m_pShowLayersBtn->setText( trUtf8("Layers") );
87        m_pShowLayersBtn->setToolTip( trUtf8( "Show layers properties" ) );
88        connect( m_pShowLayersBtn, SIGNAL( clicked(Button*) ), this, SLOT( buttonClicked(Button*) ) );
89        m_pShowLayersBtn->move( 144, 7 );
90
91
92// Instrument properties
93        m_pInstrumentProp = new PixmapWidget( this );
94        m_pInstrumentProp->move(0, 31);
95        m_pInstrumentProp->setPixmap( "/instrumentEditor/instrumentTab.png" );
96
97        m_pNameLbl = new ClickableLabel( m_pInstrumentProp );
98        m_pNameLbl->setGeometry( 8, 5, 275, 28 );
99
100        QFont boldFont;
101        boldFont.setBold(true);
102        m_pNameLbl->setFont( boldFont );
103        connect( m_pNameLbl, SIGNAL( labelClicked(ClickableLabel*) ), this, SLOT( labelClicked(ClickableLabel*) ) );
104
105        m_pRandomPitchRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Random pitch factor" ), false, true );
106        m_pRandomPitchRotary->move( 117, 192 );
107        connect( m_pRandomPitchRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
108
109        // Filter
110        m_pFilterBypassBtn = new ToggleButton(
111                        m_pInstrumentProp,
112                        "/instrumentEditor/bypass_on.png",
113                        "/instrumentEditor/bypass_off.png",
114                        "/instrumentEditor/bypass_over.png",
115                        QSize( 30, 13 )
116        );
117        connect( m_pFilterBypassBtn, SIGNAL( clicked(Button*) ), this, SLOT( filterActiveBtnClicked(Button*) ) );
118
119        m_pCutoffRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Filter Cutoff" ), false, true );
120        connect( m_pCutoffRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
121
122        m_pResonanceRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Filter resonance" ), false, true );
123        connect( m_pResonanceRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
124
125        m_pFilterBypassBtn->move( 70, 152 );
126        m_pCutoffRotary->move( 117, 146 );
127        m_pResonanceRotary->move( 170, 146 );
128        //~ Filter
129
130        // ADSR
131        m_pAttackRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Attack" ), false, true );
132        m_pDecayRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Decay" ), false, true );
133        m_pSustainRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Sustain" ), false, true );
134        m_pReleaseRotary = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Release" ), false, true );
135        connect( m_pAttackRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
136        connect( m_pDecayRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
137        connect( m_pSustainRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
138        connect( m_pReleaseRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
139        m_pAttackRotary->move( 53, 52 );
140        m_pDecayRotary->move( 105, 52 );
141        m_pSustainRotary->move( 157, 52 );
142        m_pReleaseRotary->move( 209, 52 );
143        //~ ADSR
144
145        // instrument gain
146        m_pInstrumentGainLCD = new LCDDisplay( m_pInstrumentProp, LCDDigit::SMALL_BLUE, 4 );
147        m_pInstrumentGain = new Rotary( m_pInstrumentProp, Rotary::TYPE_NORMAL, trUtf8( "Instrument gain" ), false, false );
148        connect( m_pInstrumentGain, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
149        m_pInstrumentGainLCD->move( 67, 105 );
150        m_pInstrumentGain->move( 117, 100 );
151
152
153        m_pMuteGroupLCD = new LCDDisplay( m_pInstrumentProp, LCDDigit::SMALL_BLUE, 4 );
154        m_pMuteGroupLCD->move( 160, 105 );
155
156        m_pAddMuteGroupBtn = new Button(
157                        m_pInstrumentProp,
158                        "/lcd/LCDSpinBox_up_on.png",
159                        "/lcd/LCDSpinBox_up_off.png",
160                        "/lcd/LCDSpinBox_up_over.png",
161                        QSize( 16, 8 )
162        );
163
164        m_pAddMuteGroupBtn->move( 202, 104 );
165        connect( m_pAddMuteGroupBtn, SIGNAL( clicked(Button*) ), this, SLOT( muteGroupBtnClicked(Button*) ) );
166
167
168        m_pDelMuteGroupBtn = new Button(
169                        m_pInstrumentProp,
170                        "/lcd/LCDSpinBox_down_on.png",
171                        "/lcd/LCDSpinBox_down_off.png",
172                        "/lcd/LCDSpinBox_down_over.png",
173                        QSize(16,8)
174        );
175        m_pDelMuteGroupBtn->move( 202, 113 );
176        connect( m_pDelMuteGroupBtn, SIGNAL( clicked(Button*) ), this, SLOT( muteGroupBtnClicked(Button*) ) );
177
178//~ Instrument properties
179
180
181
182
183
184// LAYER properties
185        m_pLayerProp = new PixmapWidget( this );
186        m_pLayerProp->move( 0, 31 );
187        m_pLayerProp->hide();
188        m_pLayerProp->setPixmap( "/instrumentEditor/layerTab.png" );
189
190
191
192        // Layer preview
193        m_pLayerPreview = new LayerPreview( m_pLayerProp );
194        m_pLayerPreview->move( 6, 4 );
195
196
197        // Waveform display
198        m_pWaveDisplay = new WaveDisplay( m_pLayerProp );
199        m_pWaveDisplay->updateDisplay( NULL );
200        m_pWaveDisplay->move( 5, 201 );
201
202        m_pLoadLayerBtn = new Button(
203                        m_pLayerProp,
204                        "/instrumentEditor/loadLayer_on.png",
205                        "/instrumentEditor/loadLayer_off.png",
206                        "/instrumentEditor/loadLayer_over.png",
207                        QSize( 94, 13 )
208        );
209
210        m_pRemoveLayerBtn = new Button(
211                        m_pLayerProp,
212                        "/instrumentEditor/deleteLayer_on.png",
213                        "/instrumentEditor/deleteLayer_off.png",
214                        "/instrumentEditor/deleteLayer_over.png",
215                        QSize( 94, 13 )
216        );
217
218        m_pLoadLayerBtn->move( 48, 267 );
219        m_pRemoveLayerBtn->move( 145, 267 );
220
221        connect( m_pLoadLayerBtn, SIGNAL( clicked(Button*) ), this, SLOT( buttonClicked(Button*) ) );
222        connect( m_pRemoveLayerBtn, SIGNAL( clicked(Button*) ), this, SLOT( buttonClicked(Button*) ) );
223
224        // Layer gain
225        m_pLayerGainLCD = new LCDDisplay( m_pLayerProp, LCDDigit::SMALL_BLUE, 4 );
226        m_pLayerGainRotary = new Rotary( m_pLayerProp,  Rotary::TYPE_NORMAL, trUtf8( "Layer gain" ), false, false );
227        connect( m_pLayerGainRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
228
229        m_pLayerPitchCoarseLCD = new LCDDisplay( m_pLayerProp, LCDDigit::SMALL_BLUE, 4 );
230        m_pLayerPitchFineLCD = new LCDDisplay( m_pLayerProp, LCDDigit::SMALL_BLUE, 4 );
231
232        m_pLayerPitchCoarseRotary = new Rotary( m_pLayerProp, Rotary::TYPE_CENTER, trUtf8( "Layer pitch (Coarse)" ), true, false );
233        m_pLayerPitchCoarseRotary->setMin( -24.0 );
234        m_pLayerPitchCoarseRotary->setMax( 24.0 );
235        connect( m_pLayerPitchCoarseRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
236
237        m_pLayerPitchFineRotary = new Rotary( m_pLayerProp, Rotary::TYPE_CENTER, trUtf8( "Layer pitch (Fine)" ), false, false );
238        m_pLayerPitchFineRotary->setMin( -50.0 );
239        m_pLayerPitchFineRotary->setMax( 50.0 );
240        connect( m_pLayerPitchFineRotary, SIGNAL( valueChanged(Rotary*) ), this, SLOT( rotaryChanged(Rotary*) ) );
241
242        m_pLayerGainLCD->move( 54, 301 + 3 );
243        m_pLayerGainRotary->move( 102, 301 );
244
245        m_pLayerPitchCoarseLCD->move( 54, 360 + 3 );
246        m_pLayerPitchCoarseRotary->move( 102, 360 );
247
248        m_pLayerPitchFineLCD->move(  151, 360 + 3 );
249        m_pLayerPitchFineRotary->move( 199, 360 );
250//~ Layer properties
251
252
253
254
255
256
257
258
259        selectLayer( m_nSelectedLayer );
260
261        HydrogenApp::getInstance()->addEventListener(this);
262
263        selectedInstrumentChangedEvent();       // force an update
264}
265
266
267
268InstrumentEditor::~InstrumentEditor()
269{
270        //INFOLOG( "DESTROY" );
271}
272
273
274
275void InstrumentEditor::selectedInstrumentChangedEvent()
276{
277        AudioEngine::get_instance()->lock( "InstrumentEditor::selectedInstrumentChanged" );
278
279        Song *pSong = Hydrogen::get_instance()->getSong();
280        if (pSong != NULL) {
281                InstrumentList *pInstrList = pSong->get_instrument_list();
282                int nInstr = Hydrogen::get_instance()->getSelectedInstrumentNumber();
283                if ( nInstr >= (int)pInstrList->get_size() ) {
284                        nInstr = -1;
285                }
286
287                if (nInstr == -1) {
288                        m_pInstrument = NULL;
289                }
290                else {
291                        m_pInstrument = pInstrList->get( nInstr );
292                        //INFOLOG( "new instr: " + m_pInstrument->m_sName );
293                }
294        }
295        else {
296                m_pInstrument = NULL;
297        }
298        AudioEngine::get_instance()->unlock();
299
300        // update layer list
301        if (m_pInstrument) {
302                m_pNameLbl->setText( m_pInstrument->get_name() );
303
304                // ADSR
305                m_pAttackRotary->setValue( m_pInstrument->get_adsr()->__attack / 10000.0 );
306                m_pDecayRotary->setValue( m_pInstrument->get_adsr()->__decay / 10000.0 );
307                m_pSustainRotary->setValue( m_pInstrument->get_adsr()->__sustain );
308                m_pReleaseRotary->setValue( m_pInstrument->get_adsr()->__release / 10000.0 );
309                //~ ADSR
310
311                // filter
312                m_pFilterBypassBtn->setPressed( !m_pInstrument->is_filter_active());
313                m_pCutoffRotary->setValue( m_pInstrument->get_filter_cutoff());
314                m_pResonanceRotary->setValue( m_pInstrument->get_filter_resonance());
315                //~ filter
316
317                // random pitch
318                m_pRandomPitchRotary->setValue( m_pInstrument->get_random_pitch_factor());
319
320                // instr gain
321                char tmp[20];
322                sprintf( tmp, "%#.2f", m_pInstrument->get_gain());
323                m_pInstrumentGainLCD->setText( tmp );
324                m_pInstrumentGain->setValue( m_pInstrument->get_gain()/ 5.0 );
325
326                // instr mute group
327                QString sMuteGroup = to_string( m_pInstrument->get_mute_group());
328                if (m_pInstrument->get_mute_group() == -1 ) {
329                        sMuteGroup = "Off";
330                }
331                m_pMuteGroupLCD->setText( sMuteGroup );
332
333                // select the last valid layer
334                for (int i = MAX_LAYERS - 1; i >= 0; i-- ) {
335                        if ( m_pInstrument->get_layer( i ) ) {
336                                m_nSelectedLayer = i;
337                                break;
338                        }
339                }
340                m_pWaveDisplay->updateDisplay( m_pInstrument->get_layer( m_nSelectedLayer ) );
341        }
342        else {
343                m_pNameLbl->setText( QString( "NULL Instrument..." ) );
344                m_pWaveDisplay->updateDisplay( NULL );
345                m_nSelectedLayer = 0;
346        }
347        selectLayer( m_nSelectedLayer );
348
349}
350
351
352
353void InstrumentEditor::rotaryChanged(Rotary *ref)
354{
355        float fVal = ref->getValue();
356
357        if ( m_pInstrument ) {
358                if ( ref == m_pRandomPitchRotary ){
359                        m_pInstrument->set_random_pitch_factor( fVal );
360                }
361                else if ( ref == m_pCutoffRotary ) {
362                        m_pInstrument->set_filter_cutoff( fVal );
363                }
364                else if ( ref == m_pResonanceRotary ) {
365                        if ( fVal > 0.95f ) {
366                                fVal = 0.95f;
367                        }
368                        m_pInstrument->set_filter_resonance( fVal );
369                }
370                else if ( ref == m_pAttackRotary ) {
371                        m_pInstrument->get_adsr()->__attack = fVal * 10000;
372                }
373                else if ( ref == m_pDecayRotary ) {
374                        m_pInstrument->get_adsr()->__decay = fVal * 10000;
375                }
376                else if ( ref == m_pSustainRotary ) {
377                        m_pInstrument->get_adsr()->__sustain = fVal;
378                }
379                else if ( ref == m_pReleaseRotary ) {
380                        m_pInstrument->get_adsr()->__release = fVal * 10000;
381                }
382                else if ( ref == m_pLayerGainRotary ) {
383                        fVal = fVal * 5.0;
384                        char tmp[20];
385                        sprintf( tmp, "%#.2f", fVal );
386                        m_pLayerGainLCD->setText( tmp );
387
388                        H2Core::InstrumentLayer *pLayer = m_pInstrument->get_layer( m_nSelectedLayer );
389                        if ( pLayer ) {
390                                pLayer->set_gain( fVal );
391                                m_pWaveDisplay->updateDisplay( pLayer );
392                        }
393                }
394                else if ( ref == m_pLayerPitchCoarseRotary ) {
395                        //fVal = fVal * 24.0 - 12.0;
396                        m_pLayerPitchCoarseLCD->setText( QString( "%1" ).arg( (int)fVal ) );
397                        H2Core::InstrumentLayer *pLayer = m_pInstrument->get_layer( m_nSelectedLayer );
398                        if ( pLayer ) {
399                                int nCoarse = (int)m_pLayerPitchCoarseRotary->getValue();
400                                float fFine = m_pLayerPitchFineRotary->getValue() / 100.0;
401                                pLayer->set_pitch( nCoarse + fFine );
402                                INFOLOG( "pitch: " + to_string( pLayer->get_pitch() ) );
403                        }
404                }
405                else if ( ref == m_pLayerPitchFineRotary ) {
406                        m_pLayerPitchFineLCD->setText( QString( "%1" ).arg( fVal ) );
407                        H2Core::InstrumentLayer *pLayer = m_pInstrument->get_layer( m_nSelectedLayer );
408                        if ( pLayer ) {
409                                int nCoarse = (int)m_pLayerPitchCoarseRotary->getValue();
410                                float fFine = m_pLayerPitchFineRotary->getValue() / 100.0;
411                                pLayer->set_pitch( nCoarse + fFine );
412                                INFOLOG( "pitch: " + to_string( pLayer->get_pitch()) );
413                        }
414
415                }
416                else if ( ref == m_pInstrumentGain ) {
417                        fVal = fVal * 5.0;
418                        char tmp[20];
419                        sprintf( tmp, "%#.2f", fVal );
420                        m_pInstrumentGainLCD->setText( tmp );
421
422                        m_pInstrument->set_gain( fVal );
423                }
424                else {
425                        ERRORLOG( "[rotaryChanged] unhandled rotary" );
426                }
427        }
428}
429
430
431void InstrumentEditor::filterActiveBtnClicked(Button *ref)
432{
433        if ( m_pInstrument ) {
434                m_pInstrument->set_filter_active( !ref->isPressed() );
435        }
436}
437
438
439void InstrumentEditor::buttonClicked( Button* pButton )
440{
441
442        if ( pButton == m_pShowInstrumentBtn ) {
443                m_pShowInstrumentBtn->setPressed( true );
444                m_pShowLayersBtn->setPressed( false );
445                m_pInstrumentProp->show();
446                m_pLayerProp->hide();
447
448                m_pShowLayersBtn->show();
449                m_pShowInstrumentBtn->show();
450        }
451        else if ( pButton == m_pShowLayersBtn ) {
452                m_pShowLayersBtn->setPressed( true );
453                m_pShowInstrumentBtn->setPressed( false );
454                m_pLayerProp->show();
455                m_pInstrumentProp->hide();
456
457                m_pShowLayersBtn->show();
458                m_pShowInstrumentBtn->show();
459        }
460        else if ( pButton == m_pLoadLayerBtn ) {
461                loadLayer();
462        }
463        else if ( pButton == m_pRemoveLayerBtn ) {
464                //Hydrogen *pEngine = Hydrogen::get_instance();
465                AudioEngine::get_instance()->lock( "InstrumentPropertiesDialog::deleteBtnClicked" );
466
467                if ( m_pInstrument ) {
468                        H2Core::InstrumentLayer *pLayer = m_pInstrument->get_layer( m_nSelectedLayer );
469                        if ( pLayer ) {
470                                m_pInstrument->set_layer( NULL, m_nSelectedLayer );
471                                delete pLayer;
472                        }
473                }
474                AudioEngine::get_instance()->unlock();
475                selectedInstrumentChangedEvent();    // update all
476                m_pLayerPreview->updateAll();
477        }
478        else {
479                ERRORLOG( "[buttonClicked] unhandled button" );
480        }
481}
482
483
484
485void InstrumentEditor::loadLayer()
486{
487        static QString lastUsedDir = QDir::homePath();
488
489        Hydrogen *engine = Hydrogen::get_instance();
490
491        AudioFileBrowser *fb = new AudioFileBrowser( NULL );
492        QStringList filename;
493        filename << "false" << "false" << "";
494
495        if (fb->exec() == QDialog::Accepted) {
496                filename = fb->selectedFile();
497        }
498
499        delete fb;
500
501        if ( filename[2] == "") return;
502
503        bool fnc = false;       
504        if ( filename[0] ==  "true" ){
505                fnc = true;
506        }
507
508        //use auto velocity if we want to work with multiple filenames
509        if ( filename.size() > 3) filename[1] = "true";
510
511        int selectedLayer =  m_nSelectedLayer;
512       
513       
514
515        if (filename.size() > 2) {
516               
517                for(int i=2;i < filename.size();++i)
518                {
519                        if( i-2 >= MAX_LAYERS ) break;
520
521                        Sample *newSample = Sample::load( filename[i] );
522       
523                        H2Core::Instrument *pInstr = NULL;
524       
525                        AudioEngine::get_instance()->lock( "InstrumentPropertiesDialog::browseBtnClicked" );
526                        Song *song = engine->getSong();
527                        InstrumentList *instrList = song->get_instrument_list();
528                        pInstr = instrList->get( engine->getSelectedInstrumentNumber() );
529       
530                        /*
531                                if we're using multiple layers, we start inserting the first layer
532                                at m_nSelectedLayer and the next layer at m_nSelectedLayer+1
533                        */
534                       
535                        selectedLayer = m_nSelectedLayer + i - 2;
536
537                       
538                        H2Core::InstrumentLayer *pLayer = pInstr->get_layer( selectedLayer );
539                        if (pLayer != NULL) {
540                                // delete old sample
541                                Sample *oldSample = pLayer->get_sample();
542                                delete oldSample;
543       
544                                // insert new sample from newInstrument
545                                pLayer->set_sample( newSample );
546                        }
547                        else {
548                                pLayer = new H2Core::InstrumentLayer(newSample);
549                                pInstr->set_layer( pLayer, selectedLayer );
550                        }
551       
552                        if ( fnc ){
553                                QString newfilename = filename[i].section( '/', -1 );
554                                        newfilename.replace( "." + newfilename.section( '.', -1 ), "");
555                                m_pInstrument->set_name( newfilename );
556                        }
557       
558                        //set automatic velocity
559                        if ( filename[1] ==  "true" ){
560                                setAutoVelocity();
561                        }
562       
563                        pInstr->set_drumkit_name( "" );   // external sample, no drumkit info
564       
565                        AudioEngine::get_instance()->unlock();
566
567                }
568        }
569
570        selectedInstrumentChangedEvent();    // update all
571        m_pLayerPreview->updateAll();
572}
573
574
575void InstrumentEditor::setAutoVelocity()
576{
577        int layerinuse[ MAX_LAYERS ] = {0};
578        int layers = 0;
579        for ( int i = 0; i < MAX_LAYERS ; i++ ) {
580                InstrumentLayer *pLayers = m_pInstrument->get_layer( i );
581                if ( pLayers ) {
582                        layers++;
583                        layerinuse[i] = i;
584                }
585        }
586
587        float velocityrange = 1.0 / layers;
588
589        for ( int i = 0; i < MAX_LAYERS ; i++ ) {
590                if ( layerinuse[i] == i ){
591                        layers--;
592                        InstrumentLayer *pLayer = m_pInstrument->get_layer( i );
593                        if ( pLayer ) {
594                                pLayer->set_start_velocity( layers * velocityrange);
595                                pLayer->set_end_velocity( layers * velocityrange + velocityrange );
596                        }
597                }
598        }
599}
600
601
602void InstrumentEditor::labelClicked( ClickableLabel* pRef )
603{
604        UNUSED( pRef );
605
606        if (m_pInstrument) {
607                QString sOldName = m_pInstrument->get_name();
608                bool bIsOkPressed;
609                QString sNewName = QInputDialog::getText( this, "Hydrogen", trUtf8( "New instrument name" ), QLineEdit::Normal, sOldName, &bIsOkPressed );
610                if ( bIsOkPressed  ) {
611                        m_pInstrument->set_name( sNewName );
612                        selectedInstrumentChangedEvent();
613
614                        // this will force an update...
615                        EventQueue::get_instance()->push_event( EVENT_SELECTED_INSTRUMENT_CHANGED, -1 );
616
617                }
618                else {
619                        // user entered nothing or pressed Cancel
620                }
621        }
622}
623
624
625void InstrumentEditor::selectLayer( int nLayer )
626{
627        if (!m_pInstrument) {
628                return;
629        }
630
631        m_nSelectedLayer = nLayer;
632
633        H2Core::InstrumentLayer *pLayer = m_pInstrument->get_layer( nLayer );
634        m_pWaveDisplay->updateDisplay( pLayer );
635        if (pLayer) {
636                char tmp[20];
637
638                // Layer GAIN
639                m_pLayerGainRotary->setValue( pLayer->get_gain() / 5.0 );
640                sprintf( tmp, "%#.2f", pLayer->get_gain() );
641                m_pLayerGainLCD->setText( tmp );
642
643                // Layer PITCH
644                //int nCoarsePitch = pLayer->m_fPitch / 24 + 0.5;
645                int nCoarsePitch = (int)pLayer->get_pitch();
646                float fFinePitch = pLayer->get_pitch() - nCoarsePitch;
647                //INFOLOG( "fine pitch: " + to_string( fFinePitch ) );
648                m_pLayerPitchCoarseRotary->setValue( nCoarsePitch );
649                m_pLayerPitchFineRotary->setValue( fFinePitch );
650
651                m_pLayerPitchCoarseLCD->setText( QString( "%1" ).arg( nCoarsePitch ) );
652                m_pLayerPitchFineLCD->setText( QString( "%1" ).arg( fFinePitch ) );
653        }
654        else {
655                // Layer GAIN
656                m_pLayerGainRotary->setValue( 1.0 );
657                m_pLayerGainLCD->setText( "" );
658
659                // Layer PITCH
660                m_pLayerPitchCoarseRotary->setValue( 0.0 );
661                m_pLayerPitchFineRotary->setValue( 0.0 );
662
663                m_pLayerPitchCoarseLCD->setText( "" );
664                m_pLayerPitchFineLCD->setText( "" );
665        }
666}
667
668
669
670void InstrumentEditor::muteGroupBtnClicked(Button *pRef)
671{
672        assert( m_pInstrument );
673
674        if (pRef == m_pAddMuteGroupBtn ) {
675                m_pInstrument->set_mute_group( m_pInstrument->get_mute_group() + 1);
676        }
677        else if (pRef == m_pDelMuteGroupBtn ) {
678                m_pInstrument->set_mute_group( m_pInstrument->get_mute_group() - 1);
679        }
680
681        selectedInstrumentChangedEvent();       // force an update
682}
683
Note: See TracBrowser for help on using the browser.