/[svn]/linuxsampler/trunk/src/drivers/Plugin.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/drivers/Plugin.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1907 by iliev, Sat May 16 17:04:37 2009 UTC revision 1922 by persson, Fri Jun 26 18:55:02 2009 UTC
# Line 18  Line 18 
18   *   MA  02110-1301  USA                                                   *   *   MA  02110-1301  USA                                                   *
19   ***************************************************************************/   ***************************************************************************/
20    
21    #include <limits>
22  #include <sstream>  #include <sstream>
23    
24  #include "Plugin.h"  #include "Plugin.h"
# Line 160  namespace LinuxSampler { Line 161  namespace LinuxSampler {
161          channel->SetMidiInputChannel(midi_chan_1);          channel->SetMidiInputChannel(midi_chan_1);
162      }      }
163    
164        /*
165          The sampler state is stored in a text base format, designed to
166          be easy to parse with the istream >> operator. Values are
167          separated by spaces or newlines. All string values that may
168          contain spaces end with a newline.
169    
170          The first line contains the global volume.
171    
172          The rest of the lines have an integer first representing the
173          type of information on the line, except for the two lines that
174          describe each sampler channel. The first of these two starts
175          with an integer from 0 to 16 (the midi channel for the sampler
176          channel).
177    
178          Note that we should try to keep the parsing of this format both
179          backwards and forwards compatible between versions. The parser
180          ignores lines with unknown type integers and accepts that new
181          types are missing.
182        */
183    
184        enum {
185            FXSEND = 17,
186            MIDIMAP,
187            MIDIMAPPING,
188            DEFAULTMIDIMAP
189        };
190    
191      String Plugin::GetState() {      String Plugin::GetState() {
192          std::stringstream s;          std::stringstream s;
193    
194          s << GLOBAL_VOLUME << '\n';          s << GLOBAL_VOLUME << '\n';
195    
196            std::vector<int> maps = MidiInstrumentMapper::Maps();
197            for (int i = 0 ; i < maps.size() ; i++) {
198                s << MIDIMAP << ' ' <<
199                    maps[i] << ' ' <<
200                    MidiInstrumentMapper::MapName(maps[i]) << '\n';
201    
202                std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t> mappings = MidiInstrumentMapper::Entries(maps[i]);
203                for (std::map<midi_prog_index_t, MidiInstrumentMapper::entry_t>::iterator iter = mappings.begin() ;
204                     iter != mappings.end(); iter++) {
205                    s << MIDIMAPPING << ' ' <<
206                        ((int(iter->first.midi_bank_msb) << 7) |
207                         int(iter->first.midi_bank_lsb)) << ' ' <<
208                        int(iter->first.midi_prog) << ' ' <<
209                        iter->second.EngineName << ' ' <<
210                        iter->second.InstrumentFile << '\n' <<
211                        MIDIMAPPING << ' ' <<
212                        iter->second.InstrumentIndex << ' ' <<
213                        iter->second.Volume << ' ' <<
214                        iter->second.LoadMode << ' ' <<
215                        iter->second.Name << '\n';
216                }
217            }
218            if (maps.size()) {
219                s << DEFAULTMIDIMAP << ' ' <<
220                    MidiInstrumentMapper::GetDefaultMap() << '\n';
221            }
222    
223          std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();          std::map<uint, SamplerChannel*> channels = global->pSampler->GetSamplerChannels();
224          for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;          for (std::map<uint, SamplerChannel*>::iterator iter = channels.begin() ;
225               iter != channels.end() ; iter++) {               iter != channels.end() ; iter++) {
# Line 177  namespace LinuxSampler { Line 232  namespace LinuxSampler {
232                      filename << '\n' <<                      filename << '\n' <<
233                      engine_channel->InstrumentIndex() << ' ' <<                      engine_channel->InstrumentIndex() << ' ' <<
234                      engine_channel->GetSolo() << ' ' <<                      engine_channel->GetSolo() << ' ' <<
235                      (engine_channel->GetMute() == 1) << '\n';                      (engine_channel->GetMute() == 1) << ' ' <<
236                        engine_channel->OutputChannel(0) << ' ' <<
237                        engine_channel->OutputChannel(1) << ' ' <<
238                        (engine_channel->UsesNoMidiInstrumentMap() ? -2 :
239                         (engine_channel->UsesDefaultMidiInstrumentMap() ? -1 :
240                          engine_channel->GetMidiInstrumentMap())) << '\n';
241    
242                    for (int i = 0 ; i < engine_channel->GetFxSendCount() ; i++) {
243                        FxSend* fxsend = engine_channel->GetFxSend(i);
244                        s << FXSEND << ' ' <<
245                            fxsend->Level() << ' ' <<
246                            int(fxsend->MidiController()) << ' ' <<
247                            fxsend->DestinationChannel(0) << ' ' <<
248                            fxsend->DestinationChannel(1) << ' ' <<
249                            fxsend->Name() << '\n';
250                    }
251              }              }
252          }          }
253          return s.str();          return s.str();
# Line 198  namespace LinuxSampler { Line 268  namespace LinuxSampler {
268    
269      bool Plugin::SetState(String State) {      bool Plugin::SetState(String State) {
270          RemoveChannels();          RemoveChannels();
271            MidiInstrumentMapper::RemoveAllMaps();
272    
273          std::stringstream s(State);          std::stringstream s(State);
274          s >> GLOBAL_VOLUME;          s >> GLOBAL_VOLUME;
275    
276          int midiChannel;          EngineChannel* engine_channel;
277          float volume;          int midiMapId;
278          while (s >> midiChannel >> volume) {          std::map<int, int> oldToNewId;
279              s.ignore();          int type;
280              String filename;          while (s >> type) {
281              std::getline(s, filename);  
282              int index;              if (type <= 16) { // sampler channel
283              bool solo;                  int midiChannel = type;
284              bool mute;                  float volume;
285              s >> index >> solo >> mute;                  s >> volume;
286                    s.ignore();
287              SamplerChannel* channel = global->pSampler->AddSamplerChannel();                  String filename;
288              channel->SetEngineType("gig");                  std::getline(s, filename);
289              channel->SetAudioOutputDevice(pAudioDevice);                  int index;
290              channel->SetMidiInputDevice(pMidiDevice);                  bool solo;
291              channel->SetMidiInputChannel(midi_chan_t(midiChannel));                  bool mute;
292                    s >> index >> solo >> mute;
293              EngineChannel* engine_channel = channel->GetEngineChannel();  
294              engine_channel->Volume(volume);                  SamplerChannel* channel = global->pSampler->AddSamplerChannel();
295              if (!filename.empty() && index != -1) {                  channel->SetEngineType("gig");
296                  InstrumentManager::instrument_id_t id;                  channel->SetAudioOutputDevice(pAudioDevice);
297                  id.FileName = filename;                  channel->SetMidiInputDevice(pMidiDevice);
298                  id.Index    = index;                  channel->SetMidiInputChannel(midi_chan_t(midiChannel));
299                  InstrumentManager::LoadInstrumentInBackground(id, engine_channel);  
300                    engine_channel = channel->GetEngineChannel();
301                    engine_channel->Volume(volume);
302                    if (s.get() == ' ') {
303                        int left;
304                        int right;
305                        int oldMapId;
306                        s >> left >> right >> oldMapId;
307                        engine_channel->SetOutputChannel(0, left);
308                        engine_channel->SetOutputChannel(1, right);
309    
310                        if (oldMapId == -1) {
311                            engine_channel->SetMidiInstrumentMapToDefault();
312                        } else if (oldMapId >= 0) {
313                            engine_channel->SetMidiInstrumentMap(oldToNewId[oldMapId]);
314                        }
315                        // skip rest of line
316                        s.ignore(std::numeric_limits<int>::max(), '\n');
317                    }
318                    if (!filename.empty() && index != -1) {
319                        InstrumentManager::instrument_id_t id;
320                        id.FileName = filename;
321                        id.Index    = index;
322                        InstrumentManager::LoadInstrumentInBackground(id, engine_channel);
323                    }
324                    if (solo) engine_channel->SetSolo(solo);
325                    if (mute) engine_channel->SetMute(1);
326    
327                } else if (type == FXSEND) {
328                    float level;
329                    int controller;
330                    int fxleft;
331                    int fxright;
332                    String name;
333    
334                    s >> level >> controller >> fxleft >> fxright;
335                    s.ignore();
336                    std::getline(s, name);
337                    FxSend* send = engine_channel->AddFxSend(controller, name);
338                    send->SetLevel(level);
339                    send->SetDestinationChannel(0, fxleft);
340                    send->SetDestinationChannel(1, fxright);
341    
342                } else if (type == MIDIMAP) {
343                    int oldId;
344                    s >> oldId;
345                    String name;
346                    s.ignore();
347                    std::getline(s, name);
348                    midiMapId = MidiInstrumentMapper::AddMap(name);
349                    oldToNewId[oldId] = midiMapId;
350    
351                } else if (type == MIDIMAPPING) {
352                    int bank;
353                    int prog;
354                    String engine;
355                    String file;
356                    int index;
357                    float volume;
358                    int loadmode;
359                    String name;
360    
361                    s >> bank >> prog >> engine;
362                    s.ignore();
363                    std::getline(s, file);
364                    s >> type >> index >> volume >> loadmode;
365                    s.ignore();
366                    std::getline(s, name);
367    
368                    global->pLSCPServer->AddOrReplaceMIDIInstrumentMapping(
369                        midiMapId, bank, prog, engine, file, index, volume,
370                        MidiInstrumentMapper::mode_t(loadmode), name, false);
371    
372                } else if (type == DEFAULTMIDIMAP) {
373                    int oldId;
374                    s >> oldId;
375                    MidiInstrumentMapper::SetDefaultMap(oldToNewId[oldId]);
376    
377                } else { // unknown type
378                    // try to be forward-compatible and just skip the line
379                    s.ignore(std::numeric_limits<int>::max(), '\n');
380              }              }
             if (solo) engine_channel->SetSolo(solo);  
             if (mute) engine_channel->SetMute(1);  
381          }          }
382    
383          return true;          return true;

Legend:
Removed from v.1907  
changed lines
  Added in v.1922

  ViewVC Help
Powered by ViewVC