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 <hydrogen/basics/drumkit_component.h>
24 
25 #include <cassert>
26 
27 #include <hydrogen/audio_engine.h>
28 
29 #include <hydrogen/helpers/xml.h>
30 #include <hydrogen/helpers/filesystem.h>
31 
32 #include <hydrogen/basics/adsr.h>
33 #include <hydrogen/basics/sample.h>
34 #include <hydrogen/basics/drumkit.h>
35 #include <hydrogen/basics/instrument_list.h>
36 #include <hydrogen/basics/instrument_layer.h>
37 
38 namespace H2Core
39 {
40 
41 const char* DrumkitComponent::__class_name = "DrumkitComponent";
42 
DrumkitComponent(const int id,const QString & name)43 DrumkitComponent::DrumkitComponent( const int id, const QString& name )
44 	: Object( __class_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 
DrumkitComponent(DrumkitComponent * other)59 DrumkitComponent::DrumkitComponent( DrumkitComponent* other )
60 	: Object( __class_name )
61 	, __id( other->get_id() )
62 	, __name( other->get_name() )
63 	, __volume( other->__volume )
64 	, __muted( other->__muted )
65 	, __soloed( other->__soloed )
66 	, __out_L( nullptr )
67 	, __out_R( nullptr )
68 	, __peak_l( 0.0 )
69 	, __peak_r( 0.0 )
70 {
71 	__out_L = new float[ MAX_BUFFER_SIZE ];
72 	__out_R = new float[ MAX_BUFFER_SIZE ];
73 }
74 
~DrumkitComponent()75 DrumkitComponent::~DrumkitComponent()
76 {
77 	delete[] __out_L;
78 	delete[] __out_R;
79 }
80 
reset_outs(uint32_t nFrames)81 void DrumkitComponent::reset_outs( uint32_t nFrames )
82 {
83 	memset( __out_L, 0, nFrames * sizeof( float ) );
84 	memset( __out_R, 0, nFrames * sizeof( float ) );
85 }
86 
set_outs(int nBufferPos,float valL,float valR)87 void DrumkitComponent::set_outs( int nBufferPos, float valL, float valR )
88 {
89 	__out_L[nBufferPos] += valL;
90 	__out_R[nBufferPos] += valR;
91 }
92 
get_out_L(int nBufferPos)93 float DrumkitComponent::get_out_L( int nBufferPos )
94 {
95 	return __out_L[nBufferPos];
96 }
97 
get_out_R(int nBufferPos)98 float DrumkitComponent::get_out_R( int nBufferPos )
99 {
100 	return __out_R[nBufferPos];
101 }
102 
load_from(DrumkitComponent * component,bool is_live)103 void DrumkitComponent::load_from( DrumkitComponent* component, bool is_live )
104 {
105 	if ( is_live ) {
106 		AudioEngine::get_instance()->lock( RIGHT_HERE );
107 	}
108 
109 	this->set_id( component->get_id() );
110 	this->set_name( component->get_name() );
111 	this->set_muted( component->is_muted() );
112 	this->set_volume( component->get_volume() );
113 
114 	if ( is_live ) {
115 		AudioEngine::get_instance()->unlock();
116 	}
117 }
118 
load_from(XMLNode * node,const QString & dk_path)119 DrumkitComponent* DrumkitComponent::load_from( XMLNode* node, const QString& dk_path )
120 {
121 	int id = node->read_int( "id", EMPTY_INSTR_ID, false, false );
122 	if ( id==EMPTY_INSTR_ID ) {
123 		return nullptr;
124 	}
125 
126 	DrumkitComponent* pDrumkitComponent = new DrumkitComponent( id, node->read_string( "name", "" ) );
127 	pDrumkitComponent->set_volume( node->read_float( "volume", 1.0f, true, false ) );
128 
129 	return pDrumkitComponent;
130 }
131 
save_to(XMLNode * node)132 void DrumkitComponent::save_to( XMLNode* node )
133 {
134 	XMLNode ComponentNode = node->createNode( "drumkitComponent" );
135 	ComponentNode.write_int( "id", __id );
136 	ComponentNode.write_string( "name", __name );
137 	ComponentNode.write_float( "volume", __volume );
138 }
139 
140 };
141