hydrogen 1.2.3
Instrument.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef H2C_INSTRUMENT_H
24#define H2C_INSTRUMENT_H
25
26#include <cassert>
27#include <memory>
28
29#include <core/Object.h>
30#include <core/Basics/Adsr.h>
32#include <core/License.h>
33
34#define EMPTY_INSTR_ID -1
36#define METRONOME_INSTR_ID -2
37#define PLAYBACK_INSTR_ID -3
38
39namespace H2Core
40{
41
42class XMLNode;
43class ADSR;
44class Drumkit;
45class DrumkitComponent;
46class InstrumentLayer;
47class InstrumentComponent;
48
49
54class Instrument : public H2Core::Object<Instrument>
55{
57 public:
63
70 Instrument( const int id=EMPTY_INSTR_ID, const QString& name="Empty Instrument", std::shared_ptr<ADSR> adsr=nullptr );
72 Instrument( std::shared_ptr<Instrument> other );
75
82 static std::shared_ptr<Instrument> load_instrument( const QString& drumkit_path, const QString& instrument_name );
83
90 void load_from( const QString& drumkit_path, const QString& instrument_name );
91
99 void load_from( std::shared_ptr<Drumkit> drumkit, std::shared_ptr<Instrument> instrument );
100
106 void load_samples( float fBpm = 120 );
112 void unload_samples();
113
127 void save_to( XMLNode* node, int component_id, bool bRecentVersion = true, bool bFull = false );
143 static std::shared_ptr<Instrument> load_from( XMLNode* pNode,
144 const QString& sDrumkitPath = "",
145 const QString& sDrumkitName = "",
146 const License& license = License(),
147 bool bSilent = false );
148
150 void set_name( const QString& name );
152 const QString& get_name() const;
153
155 void set_id( const int id );
157 int get_id() const;
158
160 void set_adsr( std::shared_ptr<ADSR> adsr );
162 std::shared_ptr<ADSR> get_adsr() const;
164 std::shared_ptr<ADSR> copy_adsr() const;
165
167 void set_mute_group( int group );
169 int get_mute_group() const;
170
172 void set_midi_out_channel( int channel );
174 int get_midi_out_channel() const;
175
177 void set_midi_out_note( int note );
179 int get_midi_out_note() const;
180
182 void set_muted( bool muted );
184 bool is_muted() const;
185
187 void setPan( float val );
189 void setPanWithRangeFrom0To1( float fVal ) {
190 this->setPan( -1.f + 2.f * fVal ); // scale and translate into [-1;1]
191 };
193 float getPan() const;
196 return 0.5f * ( 1.f + m_fPan );
197 }
198
199
201 void set_gain( float gain );
203 float get_gain() const;
205 void set_volume( float volume );
207 float get_volume() const;
208
210 void set_filter_active( bool active );
212 bool is_filter_active() const;
213
215 void set_filter_resonance( float val );
217 float get_filter_resonance() const;
218
220 void set_filter_cutoff( float val );
222 float get_filter_cutoff() const;
223
225 void set_peak_l( float val );
227 float get_peak_l() const;
229 void set_peak_r( float val );
231 float get_peak_r() const;
232
234 void set_fx_level( float level, int index );
236 float get_fx_level( int index ) const;
237
239 void set_random_pitch_factor( float val );
241 float get_random_pitch_factor() const;
242
244 void set_pitch_offset( float val );
246 float get_pitch_offset() const;
247
249 void set_active( bool active );
251 bool is_active() const;
252
254 void set_soloed( bool soloed );
256 bool is_soloed() const;
257
259 void enqueue();
261 void dequeue();
263 bool is_queued() const;
264
266 void set_stop_notes( bool stopnotes );
268 bool is_stop_notes() const;
269
272
273 void set_hihat_grp( int hihat_grp );
274 int get_hihat_grp() const;
275
276 void set_lower_cc( int message );
277 int get_lower_cc() const;
278
279 void set_higher_cc( int message );
280 int get_higher_cc() const;
281
283 void set_drumkit_path( const QString& sPath );
285 QString get_drumkit_path() const;
287 void set_drumkit_name( const QString& sName );
289 const QString& get_drumkit_name() const;
290
292 void set_is_preview_instrument(bool isPreview);
293 bool is_preview_instrument() const;
294
296 void set_is_metronome_instrument(bool isMetronome);
297 bool is_metronome_instrument() const;
298
299 std::vector<std::shared_ptr<InstrumentComponent>>* get_components();
300 std::shared_ptr<InstrumentComponent> get_component( int DrumkitComponentID );
301
302 void set_apply_velocity( bool apply_velocity );
303 bool get_apply_velocity() const;
304
305 bool is_currently_exported() const;
306 void set_currently_exported( bool isCurrentlyExported );
307
309 void set_missing_samples( bool bHasMissingSamples ) { m_bHasMissingSamples = bHasMissingSamples; }
310
313 bool hasSamples() const;
322 QString toQString( const QString& sPrefix = "", bool bShort = true ) const override;
323
324 private:
328 int __id;
331 QString __name;
348 float __gain;
349 float __volume;
350 float m_fPan;
351 float __peak_l;
352 float __peak_r;
353 std::shared_ptr<ADSR> __adsr;
369 bool __active;
370 bool __soloed;
371 bool __muted;
380 std::vector<std::shared_ptr<InstrumentComponent>>* __components;
384};
385
386// DEFINITIONS
389inline void Instrument::set_name( const QString& name )
390{
391 __name = name;
392}
395inline const QString& Instrument::get_name() const
396{
397 return __name;
398}
401inline void Instrument::set_id( const int id )
402{
403 __id = id;
404}
407inline int Instrument::get_id() const
408{
409 return __id;
410}
411
412inline std::shared_ptr<ADSR> Instrument::get_adsr() const
413{
414 return __adsr;
415}
416
417inline std::shared_ptr<ADSR> Instrument::copy_adsr() const
418{
419 return std::make_shared<ADSR>( __adsr );
420}
421
422inline void Instrument::set_mute_group( int group )
423{
424 __mute_group = ( group<-1 ? -1 : group );
425}
426
428{
429 return __mute_group;
430}
431
433{
434 return __midi_out_channel;
435}
436
437inline void Instrument::set_midi_out_channel( int nChannel )
438{
439 if ( ( nChannel >= MIDI_OUT_CHANNEL_MIN ) &&
440 ( nChannel <= MIDI_OUT_CHANNEL_MAX ) ) {
441 __midi_out_channel = nChannel;
442 } else {
443 ERRORLOG( QString( "midi out channel [%1] out of bounds [%2,%3]" )
444 .arg( nChannel )
446 .arg( MIDI_OUT_CHANNEL_MAX ) );
447 }
448}
449
451{
452 return __midi_out_note;
453}
454
455inline void Instrument::set_midi_out_note( int note )
456{
457 if ( ( note >= MIDI_OUT_NOTE_MIN ) && ( note <= MIDI_OUT_NOTE_MAX ) ) {
458 __midi_out_note = note;
459 } else {
460 ERRORLOG( QString( "midi out note %1 out of bounds" ).arg( note ) );
461 }
462}
463
464inline void Instrument::set_muted( bool muted )
465{
466 __muted = muted;
467}
468
469inline bool Instrument::is_muted() const
470{
471 return __muted;
472}
473
474inline void Instrument::setPan( float val ) //TODO check boundary factorize function?
475{
476 if ( val > 1.0 ) {
477 m_fPan = 1.0;
478 } else if ( val < -1.0 ) {
479 m_fPan = -1.0;
480 } else {
481 m_fPan = val;
482 }
483}
484
485inline float Instrument::getPan() const
486{
487 return m_fPan;
488}
489
490inline void Instrument::set_gain( float gain )
491{
492 __gain = gain;
493}
494
495inline float Instrument::get_gain() const
496{
497 return __gain;
498}
499
500inline void Instrument::set_volume( float volume )
501{
502 __volume = volume;
503}
504
505inline float Instrument::get_volume() const
506{
507 return __volume;
508}
509
510inline void Instrument::set_filter_active( bool active )
511{
512 __filter_active = active;
513}
514
516{
517 return __filter_active;
518}
519
520inline void Instrument::set_filter_resonance( float val )
521{
522 __filter_resonance = val;
523}
524
526{
527 return __filter_resonance;
528}
529
530inline void Instrument::set_filter_cutoff( float val )
531{
532 __filter_cutoff = val;
533}
534
536{
537 return __filter_cutoff;
538}
539
540inline void Instrument::set_peak_l( float val )
541{
542 __peak_l = val;
543}
544
545inline float Instrument::get_peak_l() const
546{
547 return __peak_l;
548}
549
550inline void Instrument::set_peak_r( float val )
551{
552 __peak_r = val;
553}
554
555inline float Instrument::get_peak_r() const
556{
557 return __peak_r;
558}
559
560inline void Instrument::set_fx_level( float level, int index )
561{
562 __fx_level[index] = level;
563}
564
565inline float Instrument::get_fx_level( int index ) const
566{
567 return __fx_level[index];
568}
569
571{
573}
574
575inline void Instrument::set_pitch_offset( float val )
576{
577 __pitch_offset = val;
578}
579
581{
583}
584
586{
587 return __pitch_offset;
588}
589
590inline void Instrument::set_active( bool active )
591{
592 __active = active;
593}
594
595inline bool Instrument::is_active() const
596{
597 return __active;
598}
599
600inline void Instrument::set_soloed( bool soloed )
601{
602 __soloed = soloed;
603}
604
605inline bool Instrument::is_soloed() const
606{
607 return __soloed;
608}
609
611{
612 __queued++;
613}
614
616{
617 assert( __queued > 0 );
618 __queued--;
619}
620
621inline bool Instrument::is_queued() const
622{
623 return ( __queued > 0 );
624}
625
626inline void Instrument::set_stop_notes( bool stopnotes )
627{
628 __stop_notes = stopnotes;
629}
630
631inline bool Instrument::is_stop_notes() const
632{
633 return __stop_notes;
634}
635
637{
638 __sample_selection_alg = selected_algo;
639}
640
645
646inline void Instrument::set_hihat_grp( int hihat_grp )
647{
648 __hihat_grp = hihat_grp;
649}
650
652{
653 return __hihat_grp;
654}
655
656inline void Instrument::set_lower_cc( int message )
657{
658 __lower_cc = message;
659}
660
661inline int Instrument::get_lower_cc() const
662{
663 return __lower_cc;
664}
665
666inline void Instrument::set_higher_cc( int message )
667{
668 __higher_cc = message;
669}
670
672{
673 return __higher_cc;
674}
675
676inline void Instrument::set_drumkit_path( const QString& sPath )
677{
678 __drumkit_path = sPath;
679}
680
681inline void Instrument::set_drumkit_name( const QString& sName )
682{
683 __drumkit_name = sName;
684}
685
686inline const QString& Instrument::get_drumkit_name() const
687{
688 return __drumkit_name;
689}
690
692{
694}
695
696inline void Instrument::set_is_preview_instrument(bool isPreview)
697{
698 __is_preview_instrument = isPreview;
699}
700
702{
704}
705
706inline void Instrument::set_is_metronome_instrument(bool isMetronome)
707{
708 __is_metronome_instrument = isMetronome;
709}
710
711inline std::vector<std::shared_ptr<InstrumentComponent>>* Instrument::get_components()
712{
713 return __components;
714}
715
716inline void Instrument::set_apply_velocity( bool apply_velocity )
717{
718 __apply_velocity = apply_velocity;
719}
720
722{
723 return __apply_velocity;
724}
725
727{
729}
730
731inline void Instrument::set_currently_exported( bool isCurrentlyExported )
732{
733 __current_instr_for_export = isCurrentlyExported;
734}
735
736};
737
738
739
740#endif // H2C_INSTRUMENT_H
741
742/* vim: set softtabstop=4 noexpandtab: */
#define EMPTY_INSTR_ID
Definition Instrument.h:34
#define H2_OBJECT(name)
Definition Object.h:224
#define ERRORLOG(x)
Definition Object.h:239
Instrument class.
Definition Instrument.h:55
bool has_missing_samples() const
Definition Instrument.h:308
QString __name
Name of the Instrument.
Definition Instrument.h:331
float __peak_r
right current peak value
Definition Instrument.h:352
float get_filter_resonance() const
get the filter resonance of the instrument
Definition Instrument.h:525
void set_is_metronome_instrument(bool isMetronome)
Mark the instrument as metronome instrument.
Definition Instrument.h:706
float __pitch_offset
instrument main pitch offset
Definition Instrument.h:364
float get_filter_cutoff() const
get the filter cutoff of the instrument
Definition Instrument.h:535
int get_mute_group() const
get the mute group of the instrument
Definition Instrument.h:427
void set_name(const QString &name)
get the name of the instrument
Definition Instrument.h:389
int __hihat_grp
the instrument is part of a hihat
Definition Instrument.h:375
float get_peak_r() const
get the right peak of the instrument
Definition Instrument.h:555
void set_pitch_offset(float val)
set the pitch offset of the instrument
Definition Instrument.h:575
float getPanWithRangeFrom0To1() const
get pan of the instrument scaling and translating the range from [-1;1] to [0;1]
Definition Instrument.h:195
bool __active
is the instrument active?
Definition Instrument.h:369
const QString & get_name() const
set the id of the instrument
Definition Instrument.h:395
float get_gain() const
get gain of the instrument
Definition Instrument.h:495
void set_drumkit_name(const QString &sName)
get the name of the related drumkits
Definition Instrument.h:681
bool __is_preview_instrument
is the instrument an hydrogen preview instrument?
Definition Instrument.h:378
float get_pitch_offset() const
get the pitch offset of the instrument
Definition Instrument.h:585
bool get_apply_velocity() const
Definition Instrument.h:721
void set_sample_selection_alg(SampleSelectionAlgo selected_algo)
Definition Instrument.h:636
void enqueue()
enqueue the instrument
Definition Instrument.h:610
bool is_stop_notes() const
get the stop notes of the instrument
Definition Instrument.h:631
~Instrument()
destructor
bool __is_metronome_instrument
is the instrument an metronome instrument?
Definition Instrument.h:379
static std::shared_ptr< Instrument > load_instrument(const QString &drumkit_path, const QString &instrument_name)
creates a new Instrument, loads samples from a given instrument within a given drumkit
void set_fx_level(float level, int index)
set the fx level of the instrument
Definition Instrument.h:560
bool __stop_notes
will the note automatically generate a note off after being on
Definition Instrument.h:367
void set_filter_resonance(float val)
set the filter resonance of the instrument
Definition Instrument.h:520
void set_is_preview_instrument(bool isPreview)
Mark the instrument as hydrogen's preview instrument.
Definition Instrument.h:696
float m_fPan
pan of the instrument, [-1;1] from left to right, as requested by Sampler PanLaws
Definition Instrument.h:350
bool __soloed
is the instrument in solo mode?
Definition Instrument.h:370
int __midi_out_channel
midi out channel
Definition Instrument.h:366
float __random_pitch_factor
Factor to scale the random contribution when humanizing pitch between 0 and AudioEngine::fHumanizePit...
Definition Instrument.h:363
void set_drumkit_path(const QString &sPath)
get the path of the related drumkits
Definition Instrument.h:676
int __mute_group
mute group of the instrument
Definition Instrument.h:372
int __id
Identifier of an instrument, which should be unique.
Definition Instrument.h:328
void set_soloed(bool soloed)
set the soloed status of the instrument
Definition Instrument.h:600
void set_adsr(std::shared_ptr< ADSR > adsr)
set the ADSR of the instrument
int get_higher_cc() const
set the path of the related drumkit
Definition Instrument.h:671
void set_volume(float volume)
set the volume of the instrument
Definition Instrument.h:500
float __filter_resonance
filter resonant frequency (0..1)
Definition Instrument.h:356
float __filter_cutoff
filter cutoff (0..1)
Definition Instrument.h:355
void set_random_pitch_factor(float val)
set the random pitch factor of the instrument
Definition Instrument.h:570
QString __drumkit_name
Name of the #Drumkit found at __drumkit_path.
Definition Instrument.h:347
float __fx_level[MAX_FX]
Ladspa FX level array.
Definition Instrument.h:374
void set_filter_active(bool active)
activate the filter of the instrument
Definition Instrument.h:510
SampleSelectionAlgo __sample_selection_alg
how Hydrogen will chose the sample to use
Definition Instrument.h:368
void load_samples(float fBpm=120)
Calls the InstrumentLayer::load_sample() member function of all layers of each component of the Instr...
bool __muted
is the instrument muted?
Definition Instrument.h:371
bool is_currently_exported() const
Definition Instrument.h:726
int get_hihat_grp() const
Definition Instrument.h:651
bool is_muted() const
get muted status of the instrument
Definition Instrument.h:469
std::shared_ptr< ADSR > copy_adsr() const
get a copy of the ADSR of the instrument
Definition Instrument.h:417
bool __filter_active
is filter active?
Definition Instrument.h:354
int get_lower_cc() const
Definition Instrument.h:661
void dequeue()
dequeue the instrument
Definition Instrument.h:615
bool is_queued() const
get the queued status of the instrument
Definition Instrument.h:621
float __gain
gain of the instrument
Definition Instrument.h:348
QString __drumkit_path
Path of the #Drumkit this Instrument belongs to.
Definition Instrument.h:339
std::shared_ptr< InstrumentComponent > get_component(int DrumkitComponentID)
float get_fx_level(int index) const
get the fx level of the instrument
Definition Instrument.h:565
void set_midi_out_channel(int channel)
set the midi out channel of the instrument
Definition Instrument.h:437
void set_apply_velocity(bool apply_velocity)
Definition Instrument.h:716
bool m_bHasMissingSamples
does the instrument have missing sample files?
Definition Instrument.h:383
bool is_soloed() const
get the soloed status of the instrument
Definition Instrument.h:605
int get_midi_out_note() const
get the midi out note of the instrument
Definition Instrument.h:450
void set_stop_notes(bool stopnotes)
set the stop notes status of the instrument
Definition Instrument.h:626
void set_gain(float gain)
set gain of the instrument
Definition Instrument.h:490
const QString & get_drumkit_name() const
Definition Instrument.h:686
void set_midi_out_note(int note)
set the midi out note of the instrument
Definition Instrument.h:455
bool is_preview_instrument() const
Definition Instrument.h:691
void set_peak_r(float val)
set the right peak of the instrument
Definition Instrument.h:550
int __higher_cc
higher cc level
Definition Instrument.h:377
void set_hihat_grp(int hihat_grp)
Definition Instrument.h:646
void set_id(const int id)
get the id of the instrument
Definition Instrument.h:401
float get_peak_l() const
get the left peak of the instrument
Definition Instrument.h:545
void setPanWithRangeFrom0To1(float fVal)
set pan of the instrument, assuming the input range in [0;1]
Definition Instrument.h:189
float __peak_l
left current peak value
Definition Instrument.h:351
float get_random_pitch_factor() const
get the random pitch factor of the instrument
Definition Instrument.h:580
void setPan(float val)
set pan of the instrument
Definition Instrument.h:474
void set_mute_group(int group)
set the mute group of the instrument
Definition Instrument.h:422
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
std::shared_ptr< ADSR > get_adsr() const
get the ADSR of the instrument
Definition Instrument.h:412
bool __current_instr_for_export
is the instrument currently being exported?
Definition Instrument.h:382
void set_active(bool active)
set the active status of the instrument
Definition Instrument.h:590
int __lower_cc
lower cc level
Definition Instrument.h:376
std::vector< std::shared_ptr< InstrumentComponent > > * __components
InstrumentLayer array.
Definition Instrument.h:380
std::shared_ptr< ADSR > __adsr
attack delay sustain release instance
Definition Instrument.h:353
void set_currently_exported(bool isCurrentlyExported)
Definition Instrument.h:731
void set_filter_cutoff(float val)
set the filter cutoff of the instrument
Definition Instrument.h:530
void unload_samples()
Calls the InstrumentLayer::unload_sample() member function of all layers of each component of the Ins...
float __volume
volume of the instrument
Definition Instrument.h:349
Instrument(const int id=EMPTY_INSTR_ID, const QString &name="Empty Instrument", std::shared_ptr< ADSR > adsr=nullptr)
constructor
void load_from(const QString &drumkit_path, const QString &instrument_name)
loads instrument from a given instrument within a given drumkit into a live Instrument object.
void set_peak_l(float val)
set the left peak of the instrument
Definition Instrument.h:540
bool is_filter_active() const
get the status of the filter of the instrument
Definition Instrument.h:515
int __queued
count the number of notes queued within Sampler::__playing_notes_queue or std::priority_queue m_songN...
Definition Instrument.h:373
void set_missing_samples(bool bHasMissingSamples)
Definition Instrument.h:309
void set_higher_cc(int message)
Definition Instrument.h:666
void save_to(XMLNode *node, int component_id, bool bRecentVersion=true, bool bFull=false)
save the instrument within the given XMLNode
bool __apply_velocity
change the sample gain based on velocity
Definition Instrument.h:381
SampleSelectionAlgo sample_selection_alg() const
Definition Instrument.h:641
void set_lower_cc(int message)
Definition Instrument.h:656
std::vector< std::shared_ptr< InstrumentComponent > > * get_components()
Definition Instrument.h:711
bool is_metronome_instrument() const
Definition Instrument.h:701
int get_id() const
Returns __id.
Definition Instrument.h:407
bool is_active() const
get the active status of the instrument
Definition Instrument.h:595
float getPan() const
get pan of the instrument
Definition Instrument.h:485
QString get_drumkit_path() const
set the name of the related drumkit
int get_midi_out_channel() const
get the midi out channel of the instrument
Definition Instrument.h:432
void set_muted(bool muted)
set muted status of the instrument
Definition Instrument.h:464
int __midi_out_note
midi out note
Definition Instrument.h:365
float get_volume() const
get the volume of the instrument
Definition Instrument.h:505
bool hasSamples() const
Whether the instrument contains at least one non-missing sample.
Wrapper class to help Hydrogen deal with the license information specified in a drumkit.
Definition License.h:48
XMLNode is a subclass of QDomNode with read and write values methods.
Definition Xml.h:39
#define MAX_FX
Maximum number of effects.
Definition config.dox:83
#define MIDI_OUT_NOTE_MIN
Definition Globals.h:30
#define MIDI_OUT_CHANNEL_MAX
Definition Globals.h:33
#define MIDI_OUT_CHANNEL_MIN
Definition Globals.h:32
#define MIDI_OUT_NOTE_MAX
Definition Globals.h:31