1 |
schoenebeck |
1374 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
schoenebeck |
1653 |
* Copyright (C) 2007, 2008 Christian Schoenebeck * |
4 |
schoenebeck |
1374 |
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the Free Software * |
17 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
|
|
* MA 02111-1307 USA * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
|
21 |
|
|
#include "InstrumentEditor.h" |
22 |
|
|
|
23 |
schoenebeck |
1425 |
#include "../common/global_private.h" |
24 |
schoenebeck |
1653 |
#include "../common/atomic.h" |
25 |
schoenebeck |
1425 |
|
26 |
schoenebeck |
1374 |
#include <algorithm> |
27 |
|
|
#include <functional> |
28 |
|
|
|
29 |
schoenebeck |
1653 |
#define MIDI_KEYS 128 |
30 |
|
|
|
31 |
schoenebeck |
1655 |
struct _private_data_t { |
32 |
|
|
atomic_t notesChanged; // whether some key changed at all |
33 |
|
|
atomic_t pNoteChanged[MIDI_KEYS]; // which key(s) changed |
34 |
|
|
atomic_t pNoteIsActive[MIDI_KEYS]; // status of each key (either active or inactive) |
35 |
|
|
}; |
36 |
|
|
|
37 |
schoenebeck |
1374 |
namespace LinuxSampler { |
38 |
|
|
|
39 |
|
|
InstrumentEditor::InstrumentEditor() : Thread(false, false, -1, 0) { |
40 |
|
|
pInstrument = NULL; |
41 |
schoenebeck |
1655 |
_private_data_t* p = new _private_data_t; |
42 |
|
|
pPrivateData = p; |
43 |
schoenebeck |
1653 |
atomic_t zero = ATOMIC_INIT(0); |
44 |
schoenebeck |
1655 |
p->notesChanged = zero; |
45 |
schoenebeck |
1653 |
for (int i = 0; i < MIDI_KEYS; i++) { |
46 |
schoenebeck |
1655 |
p->pNoteChanged[i] = zero; |
47 |
|
|
p->pNoteIsActive[i] = zero; |
48 |
schoenebeck |
1653 |
} |
49 |
schoenebeck |
1374 |
} |
50 |
|
|
|
51 |
schoenebeck |
1653 |
InstrumentEditor::~InstrumentEditor() { |
52 |
schoenebeck |
1655 |
if (pPrivateData) delete (_private_data_t*)pPrivateData; |
53 |
schoenebeck |
1653 |
} |
54 |
|
|
|
55 |
schoenebeck |
1374 |
void InstrumentEditor::Launch(void* pInstrument, String sTypeName, String sTypeVersion) { |
56 |
|
|
dmsg(1,("InstrumentEditor::Launch(instr=%x,type=%s,version=%s)\n", pInstrument, sTypeName.c_str(), sTypeVersion.c_str())); |
57 |
|
|
// prepare the editor's mandatory parameters |
58 |
|
|
this->pInstrument = pInstrument; |
59 |
|
|
this->sTypeName = sTypeName; |
60 |
|
|
this->sTypeVersion = sTypeVersion; |
61 |
|
|
// start the editor in its own thread |
62 |
|
|
StartThread(); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
int InstrumentEditor::Main() { |
66 |
|
|
dmsg(1,("InstrumentEditor::Main()\n")); |
67 |
|
|
// run the editor's main loop |
68 |
|
|
int iResult = Main(pInstrument, sTypeName, sTypeVersion); |
69 |
|
|
// reset editor parameters |
70 |
|
|
this->pInstrument = NULL; |
71 |
|
|
this->sTypeName = ""; |
72 |
|
|
this->sTypeVersion = ""; |
73 |
|
|
dmsg(1,("Instrument editor '%s' returned with exit status %d\n", Name().c_str(), iResult)); |
74 |
|
|
// notify all registered listeners |
75 |
|
|
std::for_each( |
76 |
|
|
listeners.begin(), listeners.end(), |
77 |
|
|
std::bind2nd(std::mem_fun(&InstrumentEditorListener::OnInstrumentEditorQuit), this) |
78 |
|
|
); |
79 |
|
|
// done |
80 |
|
|
return iResult; |
81 |
|
|
} |
82 |
|
|
|
83 |
schoenebeck |
1653 |
void InstrumentEditor::SendNoteOnToEditor(uint8_t Key, uint8_t /*Velocity*/) { |
84 |
|
|
if (Key >= MIDI_KEYS) return; |
85 |
schoenebeck |
1655 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
86 |
|
|
atomic_inc( &(p->pNoteIsActive)[Key] ); |
87 |
|
|
atomic_inc( &(p->pNoteChanged)[Key] ); |
88 |
|
|
atomic_inc( &p->notesChanged ); |
89 |
schoenebeck |
1653 |
} |
90 |
|
|
|
91 |
|
|
void InstrumentEditor::SendNoteOffToEditor(uint8_t Key, uint8_t /*Velocity*/) { |
92 |
|
|
if (Key >= MIDI_KEYS) return; |
93 |
schoenebeck |
1655 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
94 |
|
|
atomic_dec( &(p->pNoteIsActive)[Key] ); |
95 |
|
|
atomic_inc( &(p->pNoteChanged)[Key] ); |
96 |
|
|
atomic_inc( &p->notesChanged ); |
97 |
schoenebeck |
1653 |
} |
98 |
|
|
|
99 |
|
|
bool InstrumentEditor::NotesChanged() { |
100 |
schoenebeck |
1655 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
101 |
|
|
int c = atomic_read( &p->notesChanged ); |
102 |
|
|
atomic_sub(c, &p->notesChanged ); |
103 |
schoenebeck |
1653 |
return c; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
bool InstrumentEditor::NoteChanged(uint8_t Key) { |
107 |
schoenebeck |
1655 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
108 |
|
|
int c = atomic_read( &(p->pNoteChanged)[Key] ); |
109 |
|
|
atomic_sub(c, &(p->pNoteChanged)[Key] ); |
110 |
schoenebeck |
1653 |
return c; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
bool InstrumentEditor::NoteIsActive(uint8_t Key) { |
114 |
schoenebeck |
1655 |
_private_data_t* p = (_private_data_t*)pPrivateData; |
115 |
|
|
return atomic_read( &(p->pNoteIsActive)[Key] ); |
116 |
schoenebeck |
1653 |
} |
117 |
|
|
|
118 |
schoenebeck |
1374 |
void InstrumentEditor::AddListener(InstrumentEditorListener* pListener) { |
119 |
|
|
listeners.insert(pListener); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
void InstrumentEditor::RemoveListener(InstrumentEditorListener* pListener) { |
123 |
|
|
listeners.erase(pListener); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
void InstrumentEditor::NotifySamplesToBeRemoved(std::set<void*> Samples) { |
127 |
|
|
for ( // notify all registered listeners |
128 |
|
|
std::set<InstrumentEditorListener*>::iterator iter = listeners.begin(); |
129 |
|
|
iter != listeners.end(); iter++ |
130 |
|
|
) (*iter)->OnSamplesToBeRemoved(Samples, this); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
void InstrumentEditor::NotifySamplesRemoved() { |
134 |
|
|
for ( // notify all registered listeners |
135 |
|
|
std::set<InstrumentEditorListener*>::iterator iter = listeners.begin(); |
136 |
|
|
iter != listeners.end(); iter++ |
137 |
|
|
) (*iter)->OnSamplesRemoved(this); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
void InstrumentEditor::NotifyDataStructureToBeChanged(void* pStruct, String sStructType) { |
141 |
|
|
for ( // notify all registered listeners |
142 |
|
|
std::set<InstrumentEditorListener*>::iterator iter = listeners.begin(); |
143 |
|
|
iter != listeners.end(); iter++ |
144 |
|
|
) (*iter)->OnDataStructureToBeChanged(pStruct, sStructType, this); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
void InstrumentEditor::NotifyDataStructureChanged(void* pStruct, String sStructType) { |
148 |
|
|
for ( // notify all registered listeners |
149 |
|
|
std::set<InstrumentEditorListener*>::iterator iter = listeners.begin(); |
150 |
|
|
iter != listeners.end(); iter++ |
151 |
|
|
) (*iter)->OnDataStructureChanged(pStruct, sStructType, this); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
void InstrumentEditor::NotifySampleReferenceChanged(void* pOldSample, void* pNewSample) { |
155 |
|
|
for ( // notify all registered listeners |
156 |
|
|
std::set<InstrumentEditorListener*>::iterator iter = listeners.begin(); |
157 |
|
|
iter != listeners.end(); iter++ |
158 |
|
|
) (*iter)->OnSampleReferenceChanged(pOldSample, pNewSample, this); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
} // namespace LinuxSampler |