/[svn]/linuxsampler/trunk/src/plugins/InstrumentEditor.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/plugins/InstrumentEditor.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1655 - (show annotations) (download)
Wed Jan 30 20:22:47 2008 UTC (16 years, 2 months ago) by schoenebeck
File size: 6797 byte(s)
- refactoring of recent commit

1 /***************************************************************************
2 * *
3 * Copyright (C) 2007, 2008 Christian Schoenebeck *
4 * *
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 #include "../common/global_private.h"
24 #include "../common/atomic.h"
25
26 #include <algorithm>
27 #include <functional>
28
29 #define MIDI_KEYS 128
30
31 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 namespace LinuxSampler {
38
39 InstrumentEditor::InstrumentEditor() : Thread(false, false, -1, 0) {
40 pInstrument = NULL;
41 _private_data_t* p = new _private_data_t;
42 pPrivateData = p;
43 atomic_t zero = ATOMIC_INIT(0);
44 p->notesChanged = zero;
45 for (int i = 0; i < MIDI_KEYS; i++) {
46 p->pNoteChanged[i] = zero;
47 p->pNoteIsActive[i] = zero;
48 }
49 }
50
51 InstrumentEditor::~InstrumentEditor() {
52 if (pPrivateData) delete (_private_data_t*)pPrivateData;
53 }
54
55 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 void InstrumentEditor::SendNoteOnToEditor(uint8_t Key, uint8_t /*Velocity*/) {
84 if (Key >= MIDI_KEYS) return;
85 _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 }
90
91 void InstrumentEditor::SendNoteOffToEditor(uint8_t Key, uint8_t /*Velocity*/) {
92 if (Key >= MIDI_KEYS) return;
93 _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 }
98
99 bool InstrumentEditor::NotesChanged() {
100 _private_data_t* p = (_private_data_t*)pPrivateData;
101 int c = atomic_read( &p->notesChanged );
102 atomic_sub(c, &p->notesChanged );
103 return c;
104 }
105
106 bool InstrumentEditor::NoteChanged(uint8_t Key) {
107 _private_data_t* p = (_private_data_t*)pPrivateData;
108 int c = atomic_read( &(p->pNoteChanged)[Key] );
109 atomic_sub(c, &(p->pNoteChanged)[Key] );
110 return c;
111 }
112
113 bool InstrumentEditor::NoteIsActive(uint8_t Key) {
114 _private_data_t* p = (_private_data_t*)pPrivateData;
115 return atomic_read( &(p->pNoteIsActive)[Key] );
116 }
117
118 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

  ViewVC Help
Powered by ViewVC