1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2008 Andreas Persson * |
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., 51 Franklin St, Fifth Floor, Boston, * |
18 |
* MA 02110-1301 USA * |
19 |
***************************************************************************/ |
20 |
|
21 |
#include <sstream> |
22 |
|
23 |
#include "Plugin.h" |
24 |
#include "audio/AudioOutputDeviceFactory.h" |
25 |
#include "midi/MidiInputDeviceFactory.h" |
26 |
|
27 |
namespace LinuxSampler { |
28 |
|
29 |
// *************** PluginGlobal *************** |
30 |
// * |
31 |
|
32 |
PluginGlobal::PluginGlobal() : |
33 |
RefCount(0) { |
34 |
// we need to remove the ASIO driver, otherwise the lscp info |
35 |
// methods will lock up the audio device |
36 |
AudioOutputDeviceFactory::InnerFactories.erase("ASIO"); |
37 |
AudioOutputDeviceFactory::ParameterFactories.erase("ASIO"); |
38 |
|
39 |
REGISTER_AUDIO_OUTPUT_DRIVER(AudioOutputDevicePlugin); |
40 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterActive); |
41 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterSampleRate); |
42 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterChannels); |
43 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDevicePlugin, ParameterFragmentSize); |
44 |
|
45 |
REGISTER_MIDI_INPUT_DRIVER(MidiInputDevicePlugin); |
46 |
REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDevicePlugin, ParameterActive); |
47 |
REGISTER_MIDI_INPUT_DRIVER_PARAMETER(MidiInputDevicePlugin, ParameterPorts); |
48 |
|
49 |
pSampler = new Sampler; |
50 |
|
51 |
// using LOOPBACK instead of ANY to prevent windows firewall |
52 |
// warnings |
53 |
pLSCPServer = new LSCPServer(pSampler, htonl(INADDR_LOOPBACK), |
54 |
htons(LSCP_PORT)); |
55 |
pLSCPServer->StartThread(); |
56 |
pLSCPServer->WaitUntilInitialized(); |
57 |
|
58 |
pEventThread = new EventThread(pSampler); |
59 |
pEventThread->StartThread(); |
60 |
} |
61 |
|
62 |
|
63 |
PluginGlobal::~PluginGlobal() { |
64 |
pEventThread->StopThread(); |
65 |
pLSCPServer->StopThread(); |
66 |
pLSCPServer->RemoveListeners(); |
67 |
|
68 |
delete pEventThread; |
69 |
delete pSampler; |
70 |
delete pLSCPServer; |
71 |
} |
72 |
|
73 |
|
74 |
// *************** EventThread *************** |
75 |
// * |
76 |
|
77 |
|
78 |
EventThread::EventThread(Sampler* pSampler) : |
79 |
Thread(false, false, 0, 0), |
80 |
pSampler(pSampler) { |
81 |
} |
82 |
|
83 |
int EventThread::Main() { |
84 |
for (;;) { |
85 |
sleep(1); |
86 |
pSampler->fireStatistics(); |
87 |
} |
88 |
} |
89 |
|
90 |
|
91 |
// *************** Plugin *************** |
92 |
// * |
93 |
|
94 |
PluginGlobal* Plugin::global = 0; |
95 |
|
96 |
Plugin::Plugin(bool bDoPreInit) : |
97 |
pAudioDevice(0), |
98 |
pMidiDevice(0) { |
99 |
bPreInitDone = false; |
100 |
if (bDoPreInit) PreInit(); |
101 |
} |
102 |
|
103 |
void Plugin::PreInit() { |
104 |
if (bPreInitDone) return; |
105 |
|
106 |
bPreInitDone = true; |
107 |
if (!global) { |
108 |
global = new PluginGlobal; |
109 |
} |
110 |
global->RefCount++; |
111 |
} |
112 |
|
113 |
void Plugin::Init(int SampleRate, int FragmentSize, int Channels) { |
114 |
std::map<String, String> params; |
115 |
params["SAMPLERATE"] = ToString(SampleRate); |
116 |
params["FRAGMENTSIZE"] = ToString(FragmentSize); |
117 |
if (Channels > 0) params["CHANNELS"] = ToString(Channels); |
118 |
pAudioDevice = dynamic_cast<AudioOutputDevicePlugin*>( |
119 |
global->pSampler->CreateAudioOutputDevice(AudioOutputDevicePlugin::Name(), params)); |
120 |
|
121 |
pMidiDevice = dynamic_cast<MidiInputDevicePlugin*>( |
122 |
global->pSampler->CreateMidiInputDevice(MidiInputDevicePlugin::Name(), |
123 |
std::map<String,String>())); |
124 |
} |
125 |
|
126 |
Plugin::~Plugin() { |
127 |
RemoveChannels(); |
128 |
if (pAudioDevice) global->pSampler->DestroyAudioOutputDevice(pAudioDevice); |
129 |
if (pMidiDevice) global->pSampler->DestroyMidiInputDevice(pMidiDevice); |
130 |
if (bPreInitDone) { |
131 |
if (--global->RefCount == 0) { |
132 |
delete global; |
133 |
global = 0; |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
void Plugin::InitState() { |
139 |
SamplerChannel* channel = global->pSampler->AddSamplerChannel(); |
140 |
channel->SetEngineType("gig"); |
141 |
channel->SetAudioOutputDevice(pAudioDevice); |
142 |
channel->SetMidiInputDevice(pMidiDevice); |
143 |
channel->SetMidiInputChannel(midi_chan_1); |
144 |
} |
145 |
|
146 |
|
147 |
String Plugin::GetState() { |
148 |
std::stringstream s; |
149 |
|
150 |
s << GLOBAL_VOLUME << '\n'; |
151 |
std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels(); |
152 |
for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ; |
153 |
iter != channels.end() ; iter++) { |
154 |
SamplerChannel* channel = iter->second; |
155 |
if (channel->GetAudioOutputDevice() == pAudioDevice) { |
156 |
EngineChannel* engine_channel = channel->GetEngineChannel(); |
157 |
String filename = engine_channel->InstrumentFileName(); |
158 |
s << channel->GetMidiInputChannel() << ' ' << |
159 |
engine_channel->Volume() << ' ' << |
160 |
filename << '\n' << |
161 |
engine_channel->InstrumentIndex() << ' ' << |
162 |
engine_channel->GetSolo() << ' ' << |
163 |
(engine_channel->GetMute() == 1) << '\n'; |
164 |
} |
165 |
} |
166 |
return s.str(); |
167 |
} |
168 |
|
169 |
void Plugin::RemoveChannels() { |
170 |
if(global == NULL) return; |
171 |
|
172 |
std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels(); |
173 |
|
174 |
for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ; |
175 |
iter != channels.end() ; iter++) { |
176 |
if (iter->second->GetAudioOutputDevice() == pAudioDevice) { |
177 |
global->pSampler->RemoveSamplerChannel(iter->second); |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
bool Plugin::SetState(String State) { |
183 |
RemoveChannels(); |
184 |
|
185 |
std::stringstream s(State); |
186 |
s >> GLOBAL_VOLUME; |
187 |
|
188 |
int midiChannel; |
189 |
float volume; |
190 |
while (s >> midiChannel >> volume) { |
191 |
s.ignore(); |
192 |
String filename; |
193 |
std::getline(s, filename); |
194 |
int index; |
195 |
bool solo; |
196 |
bool mute; |
197 |
s >> index >> solo >> mute; |
198 |
|
199 |
SamplerChannel* channel = global->pSampler->AddSamplerChannel(); |
200 |
channel->SetEngineType("gig"); |
201 |
channel->SetAudioOutputDevice(pAudioDevice); |
202 |
channel->SetMidiInputDevice(pMidiDevice); |
203 |
channel->SetMidiInputChannel(midi_chan_t(midiChannel)); |
204 |
|
205 |
EngineChannel* engine_channel = channel->GetEngineChannel(); |
206 |
engine_channel->Volume(volume); |
207 |
if (!filename.empty() && index != -1) { |
208 |
engine_channel->PrepareLoadInstrument(filename.c_str(), index); |
209 |
engine_channel->LoadInstrument(); |
210 |
} |
211 |
if (solo) engine_channel->SetSolo(solo); |
212 |
if (mute) engine_channel->SetMute(1); |
213 |
} |
214 |
|
215 |
return true; |
216 |
} |
217 |
} |