1 |
/* |
/* |
2 |
* Copyright (C) 2007 Andreas Persson |
* Copyright (C) 2007 - 2017 Andreas Persson |
3 |
* |
* |
4 |
* This program is free software; you can redistribute it and/or |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
* modify it under the terms of the GNU General Public License as |
19 |
|
|
20 |
#include "linuxsamplerplugin.h" |
#include "linuxsamplerplugin.h" |
21 |
|
|
22 |
#include <gig.h> |
#ifdef LIBLINUXSAMPLER_HEADER_FILE |
23 |
#include <linuxsampler/engines/InstrumentEditorFactory.h> |
# include LIBLINUXSAMPLER_HEADER_FILE(plugins/InstrumentEditorFactory.h) |
24 |
#include "../gigedit.h" |
# include LIBLINUXSAMPLER_HEADER_FILE(engines/Engine.h) |
25 |
|
# include LIBLINUXSAMPLER_HEADER_FILE(engines/EngineChannel.h) |
26 |
|
#else |
27 |
|
# include <linuxsampler/plugins/InstrumentEditorFactory.h> |
28 |
|
# include <linuxsampler/engines/Engine.h> |
29 |
|
# include <linuxsampler/engines/EngineChannel.h> |
30 |
|
#endif |
31 |
|
|
32 |
|
#include "../gigedit/gigedit.h" |
33 |
|
#include "../gigedit/global.h" |
34 |
|
|
35 |
#include <iostream> |
#include <iostream> |
36 |
|
#include <sigc++/bind.h> |
37 |
|
#include <glibmm/main.h> |
38 |
|
|
39 |
REGISTER_INSTRUMENT_EDITOR(LinuxSamplerPlugin) |
REGISTER_INSTRUMENT_EDITOR(LinuxSamplerPlugin) |
40 |
|
|
41 |
LinuxSamplerPlugin::LinuxSamplerPlugin() { |
LinuxSamplerPlugin::LinuxSamplerPlugin() { |
42 |
|
pApp = new GigEdit; |
43 |
|
} |
44 |
|
|
45 |
|
LinuxSamplerPlugin::~LinuxSamplerPlugin() { |
46 |
|
if (pApp) delete static_cast<GigEdit*>(pApp); |
47 |
|
} |
48 |
|
|
49 |
|
int LinuxSamplerPlugin::Main(void* pInstrument, String sTypeName, String sTypeVersion, void* /*pUserData*/) { |
50 |
|
return Main(pInstrument, sTypeName, sTypeVersion); |
51 |
} |
} |
52 |
|
|
53 |
int LinuxSamplerPlugin::Main(void* pInstrument, String sTypeName, String sTypeVersion) { |
int LinuxSamplerPlugin::Main(void* pInstrument, String sTypeName, String sTypeVersion) { |
54 |
std::cout << "Entered Gigedit Main() loop :)\n" << std::flush; |
std::cout << "Entered Gigedit Main() loop :)\n" << std::flush; |
55 |
gig::Instrument* pGigInstr = static_cast<gig::Instrument*>(pInstrument); |
gig::Instrument* pGigInstr = static_cast<gig::Instrument*>(pInstrument); |
56 |
return GigEdit::run(pGigInstr); |
GigEdit* app = static_cast<GigEdit*>(pApp); |
57 |
|
|
58 |
|
// connect notification signals |
59 |
|
app->signal_file_structure_to_be_changed().connect( |
60 |
|
sigc::bind( |
61 |
|
sigc::mem_fun( |
62 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureToBeChanged |
63 |
|
), |
64 |
|
"gig::File" |
65 |
|
) |
66 |
|
); |
67 |
|
app->signal_file_structure_changed().connect( |
68 |
|
sigc::bind( |
69 |
|
sigc::mem_fun( |
70 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureChanged |
71 |
|
), |
72 |
|
"gig::File" |
73 |
|
) |
74 |
|
); |
75 |
|
app->signal_samples_to_be_removed().connect( |
76 |
|
sigc::mem_fun(*this, &LinuxSamplerPlugin::__onSamplesToBeRemoved) |
77 |
|
); |
78 |
|
app->signal_samples_removed().connect( |
79 |
|
sigc::mem_fun(*this, &LinuxSamplerPlugin::NotifySamplesRemoved) |
80 |
|
); |
81 |
|
app->signal_region_to_be_changed().connect( |
82 |
|
sigc::bind( |
83 |
|
sigc::mem_fun( |
84 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureToBeChanged |
85 |
|
), |
86 |
|
"gig::Region" |
87 |
|
) |
88 |
|
); |
89 |
|
app->signal_region_changed().connect( |
90 |
|
sigc::bind( |
91 |
|
sigc::mem_fun( |
92 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureChanged |
93 |
|
), |
94 |
|
"gig::Region" |
95 |
|
) |
96 |
|
); |
97 |
|
app->signal_dimreg_to_be_changed().connect( |
98 |
|
sigc::bind( |
99 |
|
sigc::mem_fun( |
100 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureToBeChanged |
101 |
|
), |
102 |
|
"gig::DimensionRegion" |
103 |
|
) |
104 |
|
); |
105 |
|
app->signal_dimreg_changed().connect( |
106 |
|
sigc::bind( |
107 |
|
sigc::mem_fun( |
108 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureChanged |
109 |
|
), |
110 |
|
"gig::DimensionRegion" |
111 |
|
) |
112 |
|
); |
113 |
|
app->signal_sample_changed().connect( |
114 |
|
sigc::bind( |
115 |
|
sigc::mem_fun( |
116 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureChanged |
117 |
|
), |
118 |
|
"gig::Sample" |
119 |
|
) |
120 |
|
); |
121 |
|
app->signal_sample_ref_changed().connect( |
122 |
|
sigc::mem_fun(*this, &LinuxSamplerPlugin::NotifySampleReferenceChanged) |
123 |
|
); |
124 |
|
|
125 |
|
app->signal_keyboard_key_hit().connect( |
126 |
|
sigc::mem_fun(*this, &LinuxSamplerPlugin::__onVirtualKeyboardKeyHit) |
127 |
|
); |
128 |
|
app->signal_keyboard_key_released().connect( |
129 |
|
sigc::mem_fun(*this, &LinuxSamplerPlugin::__onVirtualKeyboardKeyReleased) |
130 |
|
); |
131 |
|
app->signal_switch_sampler_instrument().connect( |
132 |
|
sigc::mem_fun(*this, &LinuxSamplerPlugin::__requestSamplerToSwitchInstrument) |
133 |
|
); |
134 |
|
app->signal_script_to_be_changed.connect( |
135 |
|
sigc::bind( |
136 |
|
sigc::mem_fun( |
137 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureToBeChanged |
138 |
|
), |
139 |
|
"gig::Script" |
140 |
|
) |
141 |
|
); |
142 |
|
app->signal_script_changed.connect( |
143 |
|
sigc::bind( |
144 |
|
sigc::mem_fun( |
145 |
|
*this, &LinuxSamplerPlugin::NotifyDataStructureChanged |
146 |
|
), |
147 |
|
"gig::Script" |
148 |
|
) |
149 |
|
); |
150 |
|
|
151 |
|
// register a timeout job to gigedit's main loop, so we can poll the |
152 |
|
// the sampler periodically for MIDI events (I HOPE it works on all |
153 |
|
// archs, because gigedit is actually running in another thread than |
154 |
|
// the one that is calling this timeout handler register code) |
155 |
|
const Glib::RefPtr<Glib::TimeoutSource> timeout_source = |
156 |
|
Glib::TimeoutSource::create(100); // poll every 100ms |
157 |
|
timeout_source->connect( |
158 |
|
sigc::mem_fun(this, &LinuxSamplerPlugin::__onPollPeriod) |
159 |
|
); |
160 |
|
timeout_source->attach(Glib::MainContext::get_default()); |
161 |
|
|
162 |
|
// run gigedit application |
163 |
|
return app->run(pGigInstr); |
164 |
|
} |
165 |
|
|
166 |
|
bool LinuxSamplerPlugin::__onPollPeriod() { |
167 |
|
#if HAVE_LINUXSAMPLER_VIRTUAL_MIDI_DEVICE |
168 |
|
GigEdit* app = static_cast<GigEdit*>(pApp); |
169 |
|
if (!NotesChanged()) return true; |
170 |
|
for (int iKey = 0; iKey < 128; iKey++) |
171 |
|
if (NoteChanged(iKey)) |
172 |
|
NoteIsActive(iKey) ? |
173 |
|
app->on_note_on_event(iKey, NoteOnVelocity(iKey)) : |
174 |
|
app->on_note_off_event(iKey, NoteOffVelocity(iKey)); |
175 |
|
return true; |
176 |
|
#else |
177 |
|
return false; |
178 |
|
#endif |
179 |
|
} |
180 |
|
|
181 |
|
void LinuxSamplerPlugin::__onSamplesToBeRemoved(std::list<gig::Sample*> lSamples) { |
182 |
|
// we have to convert the gig::Sample* list to a void* list first |
183 |
|
std::set<void*> samples; |
184 |
|
for ( |
185 |
|
std::list<gig::Sample*>::iterator iter = lSamples.begin(); |
186 |
|
iter != lSamples.end(); ++iter |
187 |
|
) samples.insert((void*)*iter); |
188 |
|
// finally send notification to sampler |
189 |
|
NotifySamplesToBeRemoved(samples); |
190 |
|
} |
191 |
|
|
192 |
|
void LinuxSamplerPlugin::__onVirtualKeyboardKeyHit(int Key, int Velocity) { |
193 |
|
#if HAVE_LINUXSAMPLER_VIRTUAL_MIDI_DEVICE |
194 |
|
SendNoteOnToSampler(Key, Velocity); |
195 |
|
#endif |
196 |
|
} |
197 |
|
|
198 |
|
void LinuxSamplerPlugin::__onVirtualKeyboardKeyReleased(int Key, int Velocity) { |
199 |
|
#if HAVE_LINUXSAMPLER_VIRTUAL_MIDI_DEVICE |
200 |
|
SendNoteOffToSampler(Key, Velocity); |
201 |
|
#endif |
202 |
|
} |
203 |
|
|
204 |
|
void LinuxSamplerPlugin::__requestSamplerToSwitchInstrument(gig::Instrument* pInstrument) { |
205 |
|
if (!pInstrument) return; |
206 |
|
|
207 |
|
LinuxSampler::EngineChannel* pEngineChannel = GetEngineChannel(); |
208 |
|
if (!pEngineChannel) return; |
209 |
|
|
210 |
|
LinuxSampler::Engine* pEngine = pEngineChannel->GetEngine(); |
211 |
|
if (!pEngine) return; |
212 |
|
|
213 |
|
LinuxSampler::InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager(); |
214 |
|
if (!pInstrumentManager) return; |
215 |
|
|
216 |
|
gig::File* pFile = (gig::File*) pInstrument->GetParent(); |
217 |
|
|
218 |
|
// resolve instrument's index number in its gig file |
219 |
|
int index = -1; |
220 |
|
for (int i = 0; pFile->GetInstrument(i); ++i) { |
221 |
|
if (pFile->GetInstrument(i) == pInstrument) { |
222 |
|
index = i; |
223 |
|
break; |
224 |
|
} |
225 |
|
} |
226 |
|
if (index < 0) return; |
227 |
|
|
228 |
|
LinuxSampler::InstrumentManager::instrument_id_t id; |
229 |
|
id.FileName = pFile->GetFileName(); |
230 |
|
id.Index = index; |
231 |
|
pInstrumentManager->LoadInstrumentInBackground(id, pEngineChannel); |
232 |
} |
} |
233 |
|
|
234 |
bool LinuxSamplerPlugin::IsTypeSupported(String sTypeName, String sTypeVersion) { |
bool LinuxSamplerPlugin::IsTypeSupported(String sTypeName, String sTypeVersion) { |