hydrogen 1.2.6
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-2025 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 );
146 static std::shared_ptr<Instrument> load_from( XMLNode* pNode,
147 const QString& sDrumkitPath = "",
148 const QString& sDrumkitName = "",
149 const License& license = License(),
150 bool* pLegacyFormatEncountered = nullptr,
151 bool bSilent = false );
152
154 void set_name( const QString& name );
156 const QString& get_name() const;
157
159 void set_id( const int id );
161 int get_id() const;
162
164 void set_adsr( std::shared_ptr<ADSR> adsr );
166 std::shared_ptr<ADSR> get_adsr() const;
168 std::shared_ptr<ADSR> copy_adsr() const;
169
171 void set_mute_group( int group );
173 int get_mute_group() const;
174
176 void set_midi_out_channel( int channel );
178 int get_midi_out_channel() const;
179
181 void set_midi_out_note( int note );
183 int get_midi_out_note() const;
184
186 void set_muted( bool muted );
188 bool is_muted() const;
189
191 void setPan( float val );
193 void setPanWithRangeFrom0To1( float fVal ) {
194 this->setPan( -1.f + 2.f * fVal ); // scale and translate into [-1;1]
195 };
196
197 float getPan() const;
200 return 0.5f * ( 1.f + m_fPan );
201 }
202
203
205 void set_gain( float gain );
207 float get_gain() const;
209 void set_volume( float volume );
211 float get_volume() const;
212
214 void set_filter_active( bool active );
216 bool is_filter_active() const;
217
219 void set_filter_resonance( float val );
221 float get_filter_resonance() const;
222
224 void set_filter_cutoff( float val );
226 float get_filter_cutoff() const;
227
229 void set_peak_l( float val );
231 float get_peak_l() const;
233 void set_peak_r( float val );
235 float get_peak_r() const;
236
238 void set_fx_level( float level, int index );
240 float get_fx_level( int index ) const;
241
243 void set_random_pitch_factor( float val );
245 float get_random_pitch_factor() const;
246
248 void set_pitch_offset( float val );
250 float get_pitch_offset() const;
251
253 void set_active( bool active );
255 bool is_active() const;
256
258 void set_soloed( bool soloed );
260 bool is_soloed() const;
261
263 void enqueue();
265 void dequeue();
267 bool is_queued() const;
268
270 void set_stop_notes( bool stopnotes );
272 bool is_stop_notes() const;
273
276
277 void set_hihat_grp( int hihat_grp );
278 int get_hihat_grp() const;
279
280 void set_lower_cc( int message );
281 int get_lower_cc() const;
282
283 void set_higher_cc( int message );
284 int get_higher_cc() const;
285
287 void set_drumkit_path( const QString& sPath );
289 QString get_drumkit_path() const;
291 void set_drumkit_name( const QString& sName );
293 const QString& get_drumkit_name() const;
294
296 void set_is_preview_instrument(bool isPreview);
297 bool is_preview_instrument() const;
298
300 void set_is_metronome_instrument(bool isMetronome);
301 bool is_metronome_instrument() const;
302
303 std::vector<std::shared_ptr<InstrumentComponent>>* get_components();
304 std::shared_ptr<InstrumentComponent> get_component( int DrumkitComponentID );
305
306 void set_apply_velocity( bool apply_velocity );
307 bool get_apply_velocity() const;
308
309 bool is_currently_exported() const;
310 void set_currently_exported( bool isCurrentlyExported );
311
313 void set_missing_samples( bool bHasMissingSamples ) { m_bHasMissingSamples = bHasMissingSamples; }
314
317 bool hasSamples() const;
318
320 static constexpr float fPitchMax = 24.5;
322 static constexpr float fPitchMin = -24.5;
323
332 QString toQString( const QString& sPrefix = "", bool bShort = true ) const override;
333
334 private:
338 int __id;
341 QString __name;
358 float __gain;
359 float __volume;
360 float m_fPan;
361 float __peak_l;
362 float __peak_r;
363 std::shared_ptr<ADSR> __adsr;
379 bool __active;
380 bool __soloed;
381 bool __muted;
390 std::vector<std::shared_ptr<InstrumentComponent>>* __components;
394};
395
396// DEFINITIONS
399inline void Instrument::set_name( const QString& name )
400{
401 __name = name;
402}
403
405inline const QString& Instrument::get_name() const
406{
407 return __name;
408}
409
411inline void Instrument::set_id( const int id )
412{
413 __id = id;
414}
415
417inline int Instrument::get_id() const
418{
419 return __id;
420}
421
422inline std::shared_ptr<ADSR> Instrument::get_adsr() const
423{
424 return __adsr;
425}
426
427inline std::shared_ptr<ADSR> Instrument::copy_adsr() const
428{
429 return std::make_shared<ADSR>( __adsr );
430}
431
432inline void Instrument::set_mute_group( int group )
433{
434 __mute_group = ( group<-1 ? -1 : group );
435}
436
438{
439 return __mute_group;
440}
441
443{
444 return __midi_out_channel;
445}
446
447inline void Instrument::set_midi_out_channel( int nChannel )
448{
449 if ( ( nChannel >= MIDI_OUT_CHANNEL_MIN ) &&
450 ( nChannel <= MIDI_OUT_CHANNEL_MAX ) ) {
451 __midi_out_channel = nChannel;
452 } else {
453 ERRORLOG( QString( "midi out channel [%1] out of bounds [%2,%3]" )
454 .arg( nChannel )
456 .arg( MIDI_OUT_CHANNEL_MAX ) );
457 }
458}
459
461{
462 return __midi_out_note;
463}
464
465inline void Instrument::set_midi_out_note( int note )
466{
467 if ( ( note >= MIDI_OUT_NOTE_MIN ) && ( note <= MIDI_OUT_NOTE_MAX ) ) {
468 __midi_out_note = note;
469 } else {
470 ERRORLOG( QString( "midi out note %1 out of bounds" ).arg( note ) );
471 }
472}
473
474inline void Instrument::set_muted( bool muted )
475{
476 __muted = muted;
477}
478
479inline bool Instrument::is_muted() const
480{
481 return __muted;
482}
483
484inline void Instrument::setPan( float val ) //TODO check boundary factorize function?
485{
486 if ( val > 1.0 ) {
487 m_fPan = 1.0;
488 } else if ( val < -1.0 ) {
489 m_fPan = -1.0;
490 } else {
491 m_fPan = val;
492 }
493}
494
495inline float Instrument::getPan() const
496{
497 return m_fPan;
498}
499
500inline void Instrument::set_gain( float gain )
501{
502 __gain = gain;
503}
504
505inline float Instrument::get_gain() const
506{
507 return __gain;
508}
509
510inline void Instrument::set_volume( float volume )
511{
512 __volume = volume;
513}
514
515inline float Instrument::get_volume() const
516{
517 return __volume;
518}
519
520inline void Instrument::set_filter_active( bool active )
521{
522 __filter_active = active;
523}
524
526{
527 return __filter_active;
528}
529
530inline void Instrument::set_filter_resonance( float val )
531{
532 __filter_resonance = val;
533}
534
536{
537 return __filter_resonance;
538}
539
540inline void Instrument::set_filter_cutoff( float val )
541{
542 __filter_cutoff = val;
543}
544
546{
547 return __filter_cutoff;
548}
549
550inline void Instrument::set_peak_l( float val )
551{
552 __peak_l = val;
553}
554
555inline float Instrument::get_peak_l() const
556{
557 return __peak_l;
558}
559
560inline void Instrument::set_peak_r( float val )
561{
562 __peak_r = val;
563}
564
565inline float Instrument::get_peak_r() const
566{
567 return __peak_r;
568}
569
570inline void Instrument::set_fx_level( float level, int index )
571{
572 __fx_level[index] = level;
573}
574
575inline float Instrument::get_fx_level( int index ) const
576{
577 return __fx_level[index];
578}
579
581{
583}
584
586{
588}
589
591{
592 return __pitch_offset;
593}
594
595inline void Instrument::set_active( bool active )
596{
597 __active = active;
598}
599
600inline bool Instrument::is_active() const
601{
602 return __active;
603}
604
605inline void Instrument::set_soloed( bool soloed )
606{
607 __soloed = soloed;
608}
609
610inline bool Instrument::is_soloed() const
611{
612 return __soloed;
613}
614
616{
617 __queued++;
618}
619
621{
622 assert( __queued > 0 );
623 __queued--;
624}
625
626inline bool Instrument::is_queued() const
627{
628 return ( __queued > 0 );
629}
630
631inline void Instrument::set_stop_notes( bool stopnotes )
632{
633 __stop_notes = stopnotes;
634}
635
636inline bool Instrument::is_stop_notes() const
637{
638 return __stop_notes;
639}
640
642{
643 __sample_selection_alg = selected_algo;
644}
645
650
651inline void Instrument::set_hihat_grp( int hihat_grp )
652{
653 __hihat_grp = hihat_grp;
654}
655
657{
658 return __hihat_grp;
659}
660
661inline void Instrument::set_lower_cc( int message )
662{
663 __lower_cc = message;
664}
665
666inline int Instrument::get_lower_cc() const
667{
668 return __lower_cc;
669}
670
671inline void Instrument::set_higher_cc( int message )
672{
673 __higher_cc = message;
674}
675
677{
678 return __higher_cc;
679}
680
681inline void Instrument::set_drumkit_path( const QString& sPath )
682{
683 __drumkit_path = sPath;
684}
685
686inline void Instrument::set_drumkit_name( const QString& sName )
687{
688 __drumkit_name = sName;
689}
690
691inline const QString& Instrument::get_drumkit_name() const
692{
693 return __drumkit_name;
694}
695
697{
699}
700
701inline void Instrument::set_is_preview_instrument(bool isPreview)
702{
703 __is_preview_instrument = isPreview;
704}
705
707{
709}
710
711inline void Instrument::set_is_metronome_instrument(bool isMetronome)
712{
713 __is_metronome_instrument = isMetronome;
714}
715
716inline std::vector<std::shared_ptr<InstrumentComponent>>* Instrument::get_components()
717{
718 return __components;
719}
720
721inline void Instrument::set_apply_velocity( bool apply_velocity )
722{
723 __apply_velocity = apply_velocity;
724}
725
727{
728 return __apply_velocity;
729}
730
732{
734}
735
736inline void Instrument::set_currently_exported( bool isCurrentlyExported )
737{
738 __current_instr_for_export = isCurrentlyExported;
739}
740
741};
742
743
744
745#endif // H2C_INSTRUMENT_H
746
747/* vim: set softtabstop=4 noexpandtab: */
#define EMPTY_INSTR_ID
Definition Instrument.h:34
#define H2_OBJECT(name)
Definition Object.h:227
#define ERRORLOG(x)
Definition Object.h:242
bool has_missing_samples() const
Definition Instrument.h:312
QString __name
Name of the Instrument.
Definition Instrument.h:341
float __peak_r
right current peak value
Definition Instrument.h:362
float get_filter_resonance() const
get the filter resonance of the instrument
Definition Instrument.h:535
void set_is_metronome_instrument(bool isMetronome)
Mark the instrument as metronome instrument.
Definition Instrument.h:711
float __pitch_offset
instrument main pitch offset
Definition Instrument.h:374
float get_filter_cutoff() const
get the filter cutoff of the instrument
Definition Instrument.h:545
int get_mute_group() const
get the mute group of the instrument
Definition Instrument.h:437
void set_name(const QString &name)
get the name of the instrument
Definition Instrument.h:399
int __hihat_grp
the instrument is part of a hihat
Definition Instrument.h:385
float get_peak_r() const
get the right peak of the instrument
Definition Instrument.h:565
void set_pitch_offset(float val)
set the pitch offset of the instrument
float getPanWithRangeFrom0To1() const
get pan of the instrument scaling and translating the range from [-1;1] to [0;1]
Definition Instrument.h:199
bool __active
is the instrument active?
Definition Instrument.h:379
const QString & get_name() const
set the id of the instrument
Definition Instrument.h:405
float get_gain() const
get gain of the instrument
Definition Instrument.h:505
void set_drumkit_name(const QString &sName)
get the name of the related drumkits
Definition Instrument.h:686
bool __is_preview_instrument
is the instrument an hydrogen preview instrument?
Definition Instrument.h:388
float get_pitch_offset() const
get the pitch offset of the instrument
Definition Instrument.h:590
bool get_apply_velocity() const
Definition Instrument.h:726
void set_sample_selection_alg(SampleSelectionAlgo selected_algo)
Definition Instrument.h:641
void enqueue()
enqueue the instrument
Definition Instrument.h:615
bool is_stop_notes() const
get the stop notes of the instrument
Definition Instrument.h:636
static constexpr float fPitchMin
Minimum support pitch value.
Definition Instrument.h:322
~Instrument()
destructor
bool __is_metronome_instrument
is the instrument an metronome instrument?
Definition Instrument.h:389
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:570
bool __stop_notes
will the note automatically generate a note off after being on
Definition Instrument.h:377
void set_filter_resonance(float val)
set the filter resonance of the instrument
Definition Instrument.h:530
void set_is_preview_instrument(bool isPreview)
Mark the instrument as hydrogen's preview instrument.
Definition Instrument.h:701
float m_fPan
pan of the instrument, [-1;1] from left to right, as requested by Sampler PanLaws
Definition Instrument.h:360
bool __soloed
is the instrument in solo mode?
Definition Instrument.h:380
int __midi_out_channel
midi out channel
Definition Instrument.h:376
float __random_pitch_factor
Factor to scale the random contribution when humanizing pitch between 0 and AudioEngine::fHumanizePit...
Definition Instrument.h:373
void set_drumkit_path(const QString &sPath)
get the path of the related drumkits
Definition Instrument.h:681
int __mute_group
mute group of the instrument
Definition Instrument.h:382
int __id
Identifier of an instrument, which should be unique.
Definition Instrument.h:338
void set_soloed(bool soloed)
set the soloed status of the instrument
Definition Instrument.h:605
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:676
void set_volume(float volume)
set the volume of the instrument
Definition Instrument.h:510
float __filter_resonance
filter resonant frequency (0..1)
Definition Instrument.h:366
float __filter_cutoff
filter cutoff (0..1)
Definition Instrument.h:365
void set_random_pitch_factor(float val)
set the random pitch factor of the instrument
Definition Instrument.h:580
QString __drumkit_name
Name of the Drumkit found at __drumkit_path.
Definition Instrument.h:357
float __fx_level[MAX_FX]
Ladspa FX level array.
Definition Instrument.h:384
void set_filter_active(bool active)
activate the filter of the instrument
Definition Instrument.h:520
SampleSelectionAlgo __sample_selection_alg
how Hydrogen will chose the sample to use
Definition Instrument.h:378
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:381
bool is_currently_exported() const
Definition Instrument.h:731
int get_hihat_grp() const
Definition Instrument.h:656
bool is_muted() const
get muted status of the instrument
Definition Instrument.h:479
std::shared_ptr< ADSR > copy_adsr() const
get a copy of the ADSR of the instrument
Definition Instrument.h:427
bool __filter_active
is filter active?
Definition Instrument.h:364
int get_lower_cc() const
Definition Instrument.h:666
void dequeue()
dequeue the instrument
Definition Instrument.h:620
bool is_queued() const
get the queued status of the instrument
Definition Instrument.h:626
float __gain
gain of the instrument
Definition Instrument.h:358
QString __drumkit_path
Path of the Drumkit this Instrument belongs to.
Definition Instrument.h:349
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:575
static constexpr float fPitchMax
Maximum support pitch value.
Definition Instrument.h:320
void set_midi_out_channel(int channel)
set the midi out channel of the instrument
Definition Instrument.h:447
void set_apply_velocity(bool apply_velocity)
Definition Instrument.h:721
bool m_bHasMissingSamples
does the instrument have missing sample files?
Definition Instrument.h:393
bool is_soloed() const
get the soloed status of the instrument
Definition Instrument.h:610
int get_midi_out_note() const
get the midi out note of the instrument
Definition Instrument.h:460
void set_stop_notes(bool stopnotes)
set the stop notes status of the instrument
Definition Instrument.h:631
void set_gain(float gain)
set gain of the instrument
Definition Instrument.h:500
const QString & get_drumkit_name() const
Definition Instrument.h:691
void set_midi_out_note(int note)
set the midi out note of the instrument
Definition Instrument.h:465
bool is_preview_instrument() const
Definition Instrument.h:696
void set_peak_r(float val)
set the right peak of the instrument
Definition Instrument.h:560
int __higher_cc
higher cc level
Definition Instrument.h:387
void set_hihat_grp(int hihat_grp)
Definition Instrument.h:651
void set_id(const int id)
get the id of the instrument
Definition Instrument.h:411
float get_peak_l() const
get the left peak of the instrument
Definition Instrument.h:555
void setPanWithRangeFrom0To1(float fVal)
set pan of the instrument, assuming the input range in [0;1]
Definition Instrument.h:193
float __peak_l
left current peak value
Definition Instrument.h:361
float get_random_pitch_factor() const
get the random pitch factor of the instrument
Definition Instrument.h:585
void setPan(float val)
set pan of the instrument
Definition Instrument.h:484
void set_mute_group(int group)
set the mute group of the instrument
Definition Instrument.h:432
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:422
bool __current_instr_for_export
is the instrument currently being exported?
Definition Instrument.h:392
void set_active(bool active)
set the active status of the instrument
Definition Instrument.h:595
int __lower_cc
lower cc level
Definition Instrument.h:386
std::vector< std::shared_ptr< InstrumentComponent > > * __components
InstrumentLayer array.
Definition Instrument.h:390
std::shared_ptr< ADSR > __adsr
attack delay sustain release instance
Definition Instrument.h:363
void set_currently_exported(bool isCurrentlyExported)
Definition Instrument.h:736
void set_filter_cutoff(float val)
set the filter cutoff of the instrument
Definition Instrument.h:540
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:359
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:550
bool is_filter_active() const
get the status of the filter of the instrument
Definition Instrument.h:525
int __queued
count the number of notes queued within Sampler::__playing_notes_queue or std::priority_queue m_songN...
Definition Instrument.h:383
void set_missing_samples(bool bHasMissingSamples)
Definition Instrument.h:313
void set_higher_cc(int message)
Definition Instrument.h:671
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:391
SampleSelectionAlgo sample_selection_alg() const
Definition Instrument.h:646
void set_lower_cc(int message)
Definition Instrument.h:661
std::vector< std::shared_ptr< InstrumentComponent > > * get_components()
Definition Instrument.h:716
bool is_metronome_instrument() const
Definition Instrument.h:706
int get_id() const
Returns __id.
Definition Instrument.h:417
bool is_active() const
get the active status of the instrument
Definition Instrument.h:600
float getPan() const
get pan of the instrument
Definition Instrument.h:495
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:442
void set_muted(bool muted)
set muted status of the instrument
Definition Instrument.h:474
int __midi_out_note
midi out note
Definition Instrument.h:375
float get_volume() const
get the volume of the instrument
Definition Instrument.h:515
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