hydrogen 1.2.6
DrumkitComponent.cpp
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
24
25#include <cassert>
26#include <memory>
27
28#include <core/Hydrogen.h>
29
30#include <core/Helpers/Xml.h>
32
33#include <core/Basics/Adsr.h>
34#include <core/Basics/Sample.h>
35#include <core/Basics/Drumkit.h>
39
40namespace H2Core
41{
42
43
44DrumkitComponent::DrumkitComponent( const int id, const QString& name )
45 : __id( id )
46 , __name( name )
47 , __volume( 1.0 )
48 , __muted( false )
49 , __soloed( false )
50 , __out_L( nullptr )
51 , __out_R( nullptr )
52 , __peak_l( 0.0 )
53 , __peak_r( 0.0 )
54{
55 __out_L = new float[ MAX_BUFFER_SIZE ];
56 __out_R = new float[ MAX_BUFFER_SIZE ];
57}
58
59DrumkitComponent::DrumkitComponent( std::shared_ptr<DrumkitComponent> other )
60 : __id( other->get_id() )
61 , __name( other->get_name() )
62 , __volume( other->__volume )
63 , __muted( other->__muted )
64 , __soloed( other->__soloed )
65 , __out_L( nullptr )
66 , __out_R( nullptr )
67 , __peak_l( 0.0 )
68 , __peak_r( 0.0 )
69{
70 __out_L = new float[ MAX_BUFFER_SIZE ];
71 __out_R = new float[ MAX_BUFFER_SIZE ];
72}
73
75{
76 delete[] __out_L;
77 delete[] __out_R;
78}
79
80void DrumkitComponent::reset_outs( uint32_t nFrames )
81{
82 memset( __out_L, 0, nFrames * sizeof( float ) );
83 memset( __out_R, 0, nFrames * sizeof( float ) );
84}
85
86float DrumkitComponent::get_out_L( int nBufferPos )
87{
88 return __out_L[nBufferPos];
89}
90
91float DrumkitComponent::get_out_R( int nBufferPos )
92{
93 return __out_R[nBufferPos];
94}
95
96void DrumkitComponent::load_from( std::shared_ptr<DrumkitComponent> component )
97{
98 this->set_id( component->get_id() );
99 this->set_name( component->get_name() );
100 this->set_muted( component->is_muted() );
101 this->set_volume( component->get_volume() );
102}
103
104std::shared_ptr<DrumkitComponent> DrumkitComponent::load_from(
105 XMLNode* node, bool* pLegacyFormatEncountered )
106{
107 int id = node->read_int( "id", EMPTY_INSTR_ID, false, false );
108 if ( id==EMPTY_INSTR_ID ) {
109 if ( pLegacyFormatEncountered != nullptr ) {
110 *pLegacyFormatEncountered = true;
111 }
112
113 return nullptr;
114 }
115
116 auto pDrumkitComponent =
117 std::make_shared<DrumkitComponent>( id, node->read_string( "name", "", false, false ) );
118 pDrumkitComponent->set_volume( node->read_float( "volume", 1.0f, true, false ) );
119
120 return pDrumkitComponent;
121}
122
124{
125 XMLNode ComponentNode = node->createNode( "drumkitComponent" );
126 ComponentNode.write_int( "id", __id );
127 ComponentNode.write_string( "name", __name );
128 ComponentNode.write_float( "volume", __volume );
129}
130
131QString DrumkitComponent::toQString( const QString& sPrefix, bool bShort ) const {
132 QString s = Base::sPrintIndention;
133 QString sOutput;
134 if ( ! bShort ) {
135 sOutput = QString( "%1[DrumkitComponent]\n" ).arg( sPrefix )
136 .append( QString( "%1%2id: %3\n" ).arg( sPrefix ).arg( s ).arg( __id ) )
137 .append( QString( "%1%2name: %3\n" ).arg( sPrefix ).arg( s ).arg( __name ) )
138 .append( QString( "%1%2volume: %3\n" ).arg( sPrefix ).arg( s ).arg( __volume ) )
139 .append( QString( "%1%2muted: %3\n" ).arg( sPrefix ).arg( s ).arg( __muted ) )
140 .append( QString( "%1%2soloed: %3\n" ).arg( sPrefix ).arg( s ).arg( __soloed ) )
141 .append( QString( "%1%2peak_l: %3\n" ).arg( sPrefix ).arg( s ).arg( __peak_l ) )
142 .append( QString( "%1%2peak_r: %3\n" ).arg( sPrefix ).arg( s ).arg( __peak_r ) );
143 } else {
144
145 sOutput = QString( "[DrumkitComponent]" )
146 .append( QString( " id: %1" ).arg( __id ) )
147 .append( QString( ", name: %1" ).arg( __name ) )
148 .append( QString( ", volume: %1" ).arg( __volume ) )
149 .append( QString( ", muted: %1" ).arg( __muted ) )
150 .append( QString( ", soloed: %1" ).arg( __soloed ) )
151 .append( QString( ", peak_l: %1" ).arg( __peak_l ) )
152 .append( QString( ", peak_r: %1" ).arg( __peak_r ) );
153 }
154 return sOutput;
155}
156
157};
#define EMPTY_INSTR_ID
Definition Instrument.h:34
static QString sPrintIndention
String used to format the debugging string output of some core classes.
Definition Object.h:127
QString __name
Name of the DrumkitComponent.
DrumkitComponent(const int id, const QString &name)
void set_name(const QString &name)
Sets the name of the DrumkitComponent __name.
const QString & get_name() const
Access the name of the DrumkitComponent.
static std::shared_ptr< DrumkitComponent > load_from(XMLNode *node, bool *pLegacyFormatEncountered=nullptr)
void set_volume(float volume)
void save_to(XMLNode *node)
void set_id(const int id)
QString toQString(const QString &sPrefix="", bool bShort=true) const override
Formatted string version for debugging purposes.
float get_out_L(int nBufferPos)
void reset_outs(uint32_t nFrames)
void set_muted(bool active)
float get_out_R(int nBufferPos)
XMLNode is a subclass of QDomNode with read and write values methods.
Definition Xml.h:39
int read_int(const QString &node, int default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads an integer stored into a child node
Definition Xml.cpp:151
QString read_string(const QString &node, const QString &default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads a string stored into a child node
Definition Xml.cpp:76
float read_float(const QString &node, float default_value, bool inexistent_ok=true, bool empty_ok=true, bool bSilent=false)
reads a float stored into a child node
Definition Xml.cpp:120
void write_float(const QString &node, const float value)
write a float into a child node
Definition Xml.cpp:261
XMLNode createNode(const QString &name)
create a new XMLNode that has to be appended into de XMLDoc
Definition Xml.cpp:44
void write_string(const QString &node, const QString &value)
write a string into a child node
Definition Xml.cpp:250
void write_int(const QString &node, const int value)
write an integer into a child node
Definition Xml.cpp:265
#define MAX_BUFFER_SIZE
Maximum buffer size.
Definition config.dox:87