/[svn]/gigedit/trunk/src/plugin/linuxsamplerplugin.cpp
ViewVC logotype

Annotation of /gigedit/trunk/src/plugin/linuxsamplerplugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2841 - (hide annotations) (download)
Sun Aug 30 10:00:49 2015 UTC (8 years, 7 months ago) by persson
File size: 7478 byte(s)
* allow building with G_DISABLE_DEPRECATED
* fixed building without liblinuxsampler on Mac
* fixed some compiler and cppcheck warnings

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

  ViewVC Help
Powered by ViewVC