--- linuxsampler/trunk/src/drivers/midi/MidiInstrumentMapper.cpp 2006/12/17 22:35:01 981 +++ linuxsampler/trunk/src/drivers/midi/MidiInstrumentMapper.cpp 2007/11/14 23:42:15 1481 @@ -1,6 +1,6 @@ /*************************************************************************** * * - * Copyright (C) 2006 Christian Schoenebeck * + * Copyright (C) 2006 - 2007 Christian Schoenebeck * * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -20,10 +20,10 @@ #include "MidiInstrumentMapper.h" +#include "../../common/global_private.h" #include "../../common/Mutex.h" #include "../../engines/EngineFactory.h" #include "../../engines/Engine.h" -#include "../../network/lscpserver.h" namespace LinuxSampler { @@ -48,6 +48,67 @@ // for synchronization of midiMaps Mutex midiMapsMutex; + ListenerList MidiInstrumentMapper::llMidiInstrumentCountListeners; + ListenerList MidiInstrumentMapper::llMidiInstrumentInfoListeners; + ListenerList MidiInstrumentMapper::llMidiInstrumentMapCountListeners; + ListenerList MidiInstrumentMapper::llMidiInstrumentMapInfoListeners; + int MidiInstrumentMapper::DefaultMap; + + void MidiInstrumentMapper::AddMidiInstrumentCountListener(MidiInstrumentCountListener* l) { + llMidiInstrumentCountListeners.AddListener(l); + } + + void MidiInstrumentMapper::RemoveMidiInstrumentCountListener(MidiInstrumentCountListener* l) { + llMidiInstrumentCountListeners.RemoveListener(l); + } + + void MidiInstrumentMapper::fireMidiInstrumentCountChanged(int MapId, int NewCount) { + for (int i = 0; i < llMidiInstrumentCountListeners.GetListenerCount(); i++) { + llMidiInstrumentCountListeners.GetListener(i)->MidiInstrumentCountChanged(MapId, NewCount); + } + } + + void MidiInstrumentMapper::AddMidiInstrumentInfoListener(MidiInstrumentInfoListener* l) { + llMidiInstrumentInfoListeners.AddListener(l); + } + + void MidiInstrumentMapper::RemoveMidiInstrumentInfoListener(MidiInstrumentInfoListener* l) { + llMidiInstrumentInfoListeners.RemoveListener(l); + } + + void MidiInstrumentMapper::fireMidiInstrumentInfoChanged(int MapId, int Bank, int Program) { + for (int i = 0; i < llMidiInstrumentInfoListeners.GetListenerCount(); i++) { + llMidiInstrumentInfoListeners.GetListener(i)->MidiInstrumentInfoChanged(MapId, Bank, Program); + } + } + + void MidiInstrumentMapper::AddMidiInstrumentMapCountListener(MidiInstrumentMapCountListener* l) { + llMidiInstrumentMapCountListeners.AddListener(l); + } + + void MidiInstrumentMapper::RemoveMidiInstrumentMapCountListener(MidiInstrumentMapCountListener* l) { + llMidiInstrumentMapCountListeners.RemoveListener(l); + } + + void MidiInstrumentMapper::fireMidiInstrumentMapCountChanged(int NewCount) { + for (int i = 0; i < llMidiInstrumentMapCountListeners.GetListenerCount(); i++) { + llMidiInstrumentMapCountListeners.GetListener(i)->MidiInstrumentMapCountChanged(NewCount); + } + } + + void MidiInstrumentMapper::AddMidiInstrumentMapInfoListener(MidiInstrumentMapInfoListener* l) { + llMidiInstrumentMapInfoListeners.AddListener(l); + } + + void MidiInstrumentMapper::RemoveMidiInstrumentMapInfoListener(MidiInstrumentMapInfoListener* l) { + llMidiInstrumentMapInfoListeners.RemoveListener(l); + } + + void MidiInstrumentMapper::fireMidiInstrumentMapInfoChanged(int MapId) { + for (int i = 0; i < llMidiInstrumentMapInfoListeners.GetListenerCount(); i++) { + llMidiInstrumentMapInfoListeners.GetListener(i)->MidiInstrumentMapInfoChanged(MapId); + } + } void MidiInstrumentMapper::AddOrReplaceEntry(int Map, midi_prog_index_t Index, entry_t Entry, bool bInBackground) throw (Exception) { if (bInBackground) { @@ -73,6 +134,7 @@ midiMapsMutex.Unlock(); if (!Entry.InstrumentFile.size()) throw Exception("No instrument file name given"); + // TODO: an easy one - we should check here if given file exists and throw an exception if it doesn't if (Entry.Volume < 0.0) throw Exception("Volume may not be a negative value"); Engine* pEngine = EngineFactory::Create(Entry.EngineName); @@ -83,7 +145,7 @@ InstrumentManager::instrument_id_t id; id.FileName = Entry.InstrumentFile; id.Index = Entry.InstrumentIndex; - if (Entry.LoadMode != VOID) { + if (Entry.LoadMode != DONTCARE) { if (bInBackground) pEngine->GetInstrumentManager()->SetModeInBackground(id, static_cast(Entry.LoadMode)); else @@ -118,9 +180,9 @@ if (Replaced) { int Bank = (int(Index.midi_bank_msb) << 7) & int(Index.midi_bank_lsb); - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_info, Map, Bank, Index.midi_prog)); + fireMidiInstrumentInfoChanged(Map, Bank, Index.midi_prog); } else { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, Map, InstrCount)); + fireMidiInstrumentCountChanged(Map, InstrCount); } } @@ -136,7 +198,7 @@ midiMapsMutex.Unlock(); if (InstrCount != -1) { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, Map, InstrCount)); + fireMidiInstrumentCountChanged(Map, InstrCount); } } @@ -152,7 +214,7 @@ midiMapsMutex.Unlock(); if (InstrCount != -1) { - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_count, Map, InstrCount)); + fireMidiInstrumentCountChanged(Map, InstrCount); } } @@ -217,6 +279,13 @@ return result; } + int MidiInstrumentMapper::GetMapCount() { + midiMapsMutex.Lock(); + int i = midiMaps.size(); + midiMapsMutex.Unlock(); + return i; + } + int MidiInstrumentMapper::AddMap(String MapName) throw (Exception) { int ID; midiMapsMutex.Lock(); @@ -239,9 +308,12 @@ } __create_map: midiMaps[ID].name = MapName; + + fireMidiInstrumentMapCountChanged(Maps().size()); + // If there were no maps until now we must set a default map. + if (midiMaps.size() == 1) SetDefaultMap(ID); midiMapsMutex.Unlock(); - - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, Maps().size())); + return ID; } @@ -267,21 +339,40 @@ } iterMap->second.name = NewName; midiMapsMutex.Unlock(); - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_info, Map)); + fireMidiInstrumentMapInfoChanged(Map); } void MidiInstrumentMapper::RemoveMap(int Map) { midiMapsMutex.Lock(); midiMaps.erase(Map); + if(Map == GetDefaultMap()) { + SetDefaultMap(midiMaps.empty() ? -1 : (*(midiMaps.begin())).first); + } + fireMidiInstrumentMapCountChanged(Maps().size()); midiMapsMutex.Unlock(); - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, Maps().size())); } void MidiInstrumentMapper::RemoveAllMaps() { midiMapsMutex.Lock(); midiMaps.clear(); + SetDefaultMap(-1); + fireMidiInstrumentMapCountChanged(Maps().size()); + midiMapsMutex.Unlock(); + } + + int MidiInstrumentMapper::GetDefaultMap() { + midiMapsMutex.Lock(); + int i = DefaultMap; midiMapsMutex.Unlock(); - LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_midi_instr_map_count, Maps().size())); + return i; + } + + void MidiInstrumentMapper::SetDefaultMap(int MapId) { + midiMapsMutex.Lock(); + DefaultMap = MapId; + midiMapsMutex.Unlock(); + + if (MapId != -1) fireMidiInstrumentMapInfoChanged(MapId); } optional MidiInstrumentMapper::GetEntry(int Map, midi_prog_index_t Index) {