1 /*************************************************************************** 2 * * 3 * LinuxSampler - modular, streaming capable sampler * 4 * * 5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * 6 * Copyright (C) 2005 - 2014 Christian Schoenebeck * 7 * * 8 * This library is free software; you can redistribute it and/or modify * 9 * it under the terms of the GNU General Public License as published by * 10 * the Free Software Foundation; either version 2 of the License, or * 11 * (at your option) any later version. * 12 * * 13 * This library is distributed in the hope that it will be useful, * 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 16 * GNU General Public License for more details. * 17 * * 18 * You should have received a copy of the GNU General Public License * 19 * along with this library; if not, write to the Free Software * 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * 21 * MA 02111-1307 USA * 22 ***************************************************************************/ 23 24 #ifndef __LSCPSERVER_H_ 25 #define __LSCPSERVER_H_ 26 27 #if defined(WIN32) 28 #include <windows.h> 29 typedef int socklen_t; 30 #else 31 #include <unistd.h> 32 #include <sys/types.h> 33 #include <sys/socket.h> 34 #include <sys/select.h> 35 #include <sys/time.h> 36 #include <netinet/in.h> 37 #include <netinet/tcp.h> 38 #include <arpa/inet.h> 39 #include <netdb.h> 40 #endif 41 42 #include <list> 43 44 #include "lscp.h" 45 #include "lscpparser.h" 46 #include "lscpevent.h" 47 #include "../Sampler.h" 48 #include "../common/Thread.h" 49 #include "../common/Mutex.h" 50 #include "../common/Condition.h" 51 #include "../common/global_private.h" 52 53 #include "../drivers/midi/MidiInstrumentMapper.h" 54 #include "../drivers/midi/VirtualMidiDevice.h" 55 56 #if HAVE_SQLITE3 57 #include "../db/InstrumentsDb.h" 58 #endif 59 60 /// TCP Port on which the server should listen for connection requests. 61 #define LSCP_ADDR INADDR_ANY 62 #define LSCP_PORT 8888 63 64 /// try up to 3 minutes to bind server socket 65 #define LSCP_SERVER_BIND_TIMEOUT 180 66 67 // External references to the main scanner and parser functions 68 extern int yyparse(void* YYPARSE_PARAM); 69 70 namespace LinuxSampler { 71 72 extern void restart(yyparse_param_t* pparam, int& yychar); 73 74 /** 75 * Network server for the LinuxSampler Control Protocol (LSCP). 76 */ 77 class LSCPServer : public Thread { 78 public: 79 LSCPServer(Sampler* pSampler, long int addr, short int port); 80 virtual ~LSCPServer(); 81 int WaitUntilInitialized(long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); 82 void RemoveListeners(); 83 84 // Methods called by the parser 85 String DestroyAudioOutputDevice(uint DeviceIndex); 86 String DestroyMidiInputDevice(uint DeviceIndex); 87 String LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground = false); 88 String SetEngineType(String EngineName, uint uiSamplerChannel); 89 String GetChannels(); 90 String ListChannels(); 91 String AddChannel(); 92 String RemoveChannel(uint uiSamplerChannel); 93 String GetAvailableEngines(); 94 String ListAvailableEngines(); 95 String GetEngineInfo(String EngineName); 96 String GetChannelInfo(uint uiSamplerChannel); 97 String GetVoiceCount(uint uiSamplerChannel); 98 String GetStreamCount(uint uiSamplerChannel); 99 String GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel); 100 String GetAvailableAudioOutputDrivers(); 101 String ListAvailableAudioOutputDrivers(); 102 String GetAvailableMidiInputDrivers(); 103 String ListAvailableMidiInputDrivers(); 104 String GetAudioOutputDriverInfo(String Driver); 105 String GetMidiInputDriverInfo(String Driver); 106 #ifdef __GNUC__ 107 typedef std::map<String,String> StringMap; // nasty workaround for a GCC bug (see GCC bug #15980, #57) 108 String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = StringMap()); 109 String GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = StringMap()); 110 String CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters = StringMap()); 111 String CreateMidiInputDevice(String Driver, std::map<String,String> Parameters = StringMap()); 112 #else 113 String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = std::map<String,String>()); 114 String GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = std::map<String,String>()); 115 String CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters = std::map<String,String>()); 116 String CreateMidiInputDevice(String Driver, std::map<String,String> Parameters = std::map<String,String>()); 117 #endif // __GNUC__ 118 String GetAudioOutputDeviceCount(); 119 String GetMidiInputDeviceCount(); 120 String GetAudioOutputDevices(); 121 String GetMidiInputDevices(); 122 String GetAudioOutputDeviceInfo(uint DeviceIndex); 123 String GetMidiInputDeviceInfo(uint DeviceIndex); 124 String GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex); 125 String GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName); 126 String GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId); 127 String GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName); 128 String SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal); 129 String SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); 130 String SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); 131 String SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal); 132 String SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel); 133 String SetAudioOutputDevice(uint AudioDeviceId, uint SamplerChannel); 134 String SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel); 135 String AddChannelMidiInput(uint uiSamplerChannel, uint MIDIDeviceId, uint MIDIPort = 0); 136 String RemoveChannelMidiInput(uint uiSamplerChannel); 137 String RemoveChannelMidiInput(uint uiSamplerChannel, uint MIDIDeviceId); 138 String RemoveChannelMidiInput(uint uiSamplerChannel, uint MIDIDeviceId, uint MIDIPort); 139 String ListChannelMidiInputs(uint uiSamplerChannel); 140 String SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel); 141 String SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel); 142 String SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel); 143 String SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel); 144 String SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel); 145 String SetVolume(double dVolume, uint uiSamplerChannel); 146 String SetChannelMute(bool bMute, uint uiSamplerChannel); 147 String SetChannelSolo(bool bSolo, uint uiSamplerChannel); 148 String AddOrReplaceMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg, String EngineType, String InstrumentFile, uint InstrumentIndex, float Volume, MidiInstrumentMapper::mode_t LoadMode, String Name, bool bModal); 149 String RemoveMIDIInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg); 150 String GetMidiInstrumentMappings(uint MidiMapID); 151 String GetAllMidiInstrumentMappings(); 152 String GetMidiInstrumentMapping(uint MidiMapID, uint MidiBank, uint MidiProg); 153 String ListMidiInstrumentMappings(uint MidiMapID); 154 String ListAllMidiInstrumentMappings(); 155 String ClearMidiInstrumentMappings(uint MidiMapID); 156 String ClearAllMidiInstrumentMappings(); 157 String AddMidiInstrumentMap(String MapName = ""); 158 String RemoveMidiInstrumentMap(uint MidiMapID); 159 String RemoveAllMidiInstrumentMaps(); 160 String GetMidiInstrumentMaps(); 161 String ListMidiInstrumentMaps(); 162 String GetMidiInstrumentMap(uint MidiMapID); 163 String SetMidiInstrumentMapName(uint MidiMapID, String NewName); 164 String SetChannelMap(uint uiSamplerChannel, int MidiMapID); 165 String CreateFxSend(uint uiSamplerChannel, uint MidiCtrl, String Name = ""); 166 String DestroyFxSend(uint uiSamplerChannel, uint FxSendID); 167 String GetFxSends(uint uiSamplerChannel); 168 String ListFxSends(uint uiSamplerChannel); 169 String GetFxSendInfo(uint uiSamplerChannel, uint FxSendID); 170 String SetFxSendName(uint uiSamplerChannel, uint FxSendID, String Name); 171 String SetFxSendAudioOutputChannel(uint uiSamplerChannel, uint FxSendID, uint FxSendChannel, uint DeviceChannel); 172 String SetFxSendMidiController(uint uiSamplerChannel, uint FxSendID, uint MidiController); 173 String SetFxSendLevel(uint uiSamplerChannel, uint FxSendID, double dLevel); 174 String SetFxSendEffect(uint uiSamplerChannel, uint FxSendID, int iSendEffectChain, int iEffectChainPosition); 175 176 // effect commands 177 String GetAvailableEffects(); 178 String ListAvailableEffects(); 179 String GetEffectInfo(int iEffectIndex); 180 String CreateEffectInstance(int iEffectIndex); 181 String CreateEffectInstance(String effectSystem, String module, String effectName); 182 String DestroyEffectInstance(int iEffectInstance); 183 String GetEffectInstances(); 184 String ListEffectInstances(); 185 String GetEffectInstanceInfo(int iEffectInstance); 186 String GetEffectInstanceInputControlInfo(int iEffectInstance, int iInputControlIndex); 187 String SetEffectInstanceInputControlValue(int iEffectInstance, int iInputControlIndex, double dValue); 188 String GetSendEffectChains(int iAudioOutputDevice); 189 String ListSendEffectChains(int iAudioOutputDevice); 190 String AddSendEffectChain(int iAudioOutputDevice); 191 String RemoveSendEffectChain(int iAudioOutputDevice, int iSendEffectChain); 192 String GetSendEffectChainInfo(int iAudioOutputDevice, int iSendEffectChain); 193 String AppendSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectInstance); 194 String InsertSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition, int iEffectInstance); 195 String RemoveSendEffectChainEffect(int iAudioOutputDevice, int iSendEffectChain, int iEffectChainPosition); 196 197 String AddDbInstrumentDirectory(String Dir); 198 String RemoveDbInstrumentDirectory(String Dir, bool Force = false); 199 String GetDbInstrumentDirectoryCount(String Dir, bool Recursive = false); 200 String GetDbInstrumentDirectories(String Dir, bool Recursive = false); 201 String GetDbInstrumentDirectoryInfo(String Dir); 202 String SetDbInstrumentDirectoryName(String Dir, String Name); 203 String MoveDbInstrumentDirectory(String Dir, String Dst); 204 String CopyDbInstrumentDirectory(String Dir, String Dst); 205 String SetDbInstrumentDirectoryDescription(String Dir, String Desc); 206 String FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive = true); 207 String AddDbInstruments(String DbDir, String FilePath, int Index = -1, bool bBackground = false); 208 String AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground = false, bool insDir = false); 209 String RemoveDbInstrument(String Instr); 210 String GetDbInstrumentCount(String Dir, bool Recursive = false); 211 String GetDbInstruments(String Dir, bool Recursive = false); 212 String GetDbInstrumentInfo(String Instr); 213 String SetDbInstrumentName(String Instr, String Name); 214 String MoveDbInstrument(String Instr, String Dst); 215 String CopyDbInstrument(String Instr, String Dst); 216 String SetDbInstrumentDescription(String Instr, String Desc); 217 String SetDbInstrumentFilePath(String OldPath, String NewPath); 218 String FindLostDbInstrumentFiles(); 219 String FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive = true); 220 String FormatInstrumentsDb(); 221 String EditSamplerChannelInstrument(uint uiSamplerChannel); 222 String GetDbInstrumentsJobInfo(int JobId); 223 String ResetChannel(uint uiSamplerChannel); 224 String ResetSampler(); 225 String GetServerInfo(); 226 String GetTotalStreamCount(); 227 String GetTotalVoiceCount(); 228 String GetTotalVoiceCountMax(); 229 String GetGlobalMaxVoices(); 230 String SetGlobalMaxVoices(int iVoices); 231 String GetGlobalMaxStreams(); 232 String SetGlobalMaxStreams(int iStreams); 233 String GetGlobalVolume(); 234 String SetGlobalVolume(double dVolume); 235 String GetFileInstruments(String Filename); 236 String ListFileInstruments(String Filename); 237 String GetFileInstrumentInfo(String Filename, uint InstrumentID); 238 String SendChannelMidiData(String MidiMsg, uint uiSamplerChannel, uint Arg1, uint Arg2); 239 String SubscribeNotification(LSCPEvent::event_t); 240 String UnsubscribeNotification(LSCPEvent::event_t); 241 String SetEcho(yyparse_param_t* pSession, double boolean_value); 242 String SetShellInteract(yyparse_param_t* pSession, double boolean_value); 243 String SetShellDoc(yyparse_param_t* pSession, double boolean_value); 244 String SetShellAutoCorrect(yyparse_param_t* pSession, double boolean_value); 245 void AnswerClient(String ReturnMessage); 246 void CloseAllConnections(); 247 248 static int currentSocket; 249 static std::map<int,String> bufferedCommands; 250 251 static void SendLSCPNotify( LSCPEvent Event ); 252 static int EventSubscribers( std::list<LSCPEvent::event_t> events ); 253 static String FilterEndlines(String s); 254 255 //Protect main thread that generates real time notify messages 256 //like voice count, stream count and buffer fill 257 //from LSCP server removing engines and channels from underneath 258 static Mutex RTNotifyMutex; 259 260 protected: 261 int hSocket; 262 sockaddr_in SocketAddress; 263 Sampler* pSampler; 264 Condition Initialized; 265 266 int Main(); ///< Implementation of virtual method from class Thread 267 268 private: 269 270 /** 271 * Find a created audio output device index. 272 */ 273 int GetAudioOutputDeviceIndex (AudioOutputDevice *pDevice); 274 275 /** 276 * Find a created midi input device index. 277 */ 278 int GetMidiInputDeviceIndex (MidiInputDevice *pDevice); 279 280 EngineChannel* GetEngineChannel(uint uiSamplerChannel); 281 282 /** 283 * Gets the specified effect send on the specified sampler channel. 284 */ 285 FxSend* GetFxSend(uint uiSamplerChannel, uint FxSendID); 286 287 bool HasSoloChannel(); 288 void MuteNonSoloChannels(); 289 void UnmuteChannels(); 290 291 /** 292 * Throws an exception if the specified file is not found or 293 * if directory is specified. 294 */ 295 static void VerifyFile(String Filename); 296 297 static std::map<int,String> bufferedNotifies; 298 static Mutex NotifyMutex; 299 static Mutex NotifyBufferMutex; 300 String generateLSCPDocReply(const String& line, yyparse_param_t* param); 301 bool GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ); 302 static void CloseConnection( std::vector<yyparse_param_t>::iterator iter ); 303 static std::vector<yyparse_param_t> Sessions; 304 static Mutex SubscriptionMutex; 305 static std::map< LSCPEvent::event_t, std::list<int> > eventSubscriptions; 306 static fd_set fdSet; 307 308 class EventHandler : public ChannelCountListener, public AudioDeviceCountListener, 309 public MidiDeviceCountListener, public MidiInstrumentCountListener, 310 public MidiInstrumentInfoListener, public MidiInstrumentMapCountListener, 311 public MidiInstrumentMapInfoListener, public FxSendCountListener, 312 public VoiceCountListener, public StreamCountListener, public BufferFillListener, 313 public TotalStreamCountListener, public TotalVoiceCountListener, 314 public EngineChangeListener, public MidiPortCountListener { 315 316 public: 317 EventHandler(LSCPServer* pParent); 318 319 /** 320 * Invoked when the number of sampler channels has changed. 321 * @param NewCount The new number of sampler channels. 322 */ 323 virtual void ChannelCountChanged(int NewCount); 324 virtual void ChannelAdded(SamplerChannel* pChannel); 325 virtual void ChannelToBeRemoved(SamplerChannel* pChannel); 326 327 /** 328 * Invoked when the number of audio output devices has changed. 329 * @param NewCount The new number of audio output devices. 330 */ 331 virtual void AudioDeviceCountChanged(int NewCount); 332 333 /** 334 * Invoked when the number of MIDI input devices has changed. 335 * @param NewCount The new number of MIDI input devices. 336 */ 337 virtual void MidiDeviceCountChanged(int NewCount); 338 339 /** 340 * Invoked right before the supplied MIDI input device is going 341 * to be destroyed. 342 * @param pDevice MidiInputDevice to be deleted 343 */ 344 virtual void MidiDeviceToBeDestroyed(MidiInputDevice* pDevice); 345 346 /** 347 * Invoked to inform that a new MidiInputDevice has just been 348 * created. 349 * @param pDevice newly created MidiInputDevice 350 */ 351 virtual void MidiDeviceCreated(MidiInputDevice* pDevice); 352 353 /** 354 * Invoked when the number of MIDI input ports has changed. 355 * @param NewCount The new number of MIDI input ports. 356 */ 357 virtual void MidiPortCountChanged(int NewCount); 358 359 /** 360 * Invoked right before the supplied MIDI input port is going 361 * to be destroyed. 362 * @param pPort MidiInputPort to be deleted 363 */ 364 virtual void MidiPortToBeRemoved(MidiInputPort* pPort); 365 366 /** 367 * Invoked to inform that a new MidiInputPort has just been 368 * added. 369 * @param pPort newly created MidiInputPort 370 */ 371 virtual void MidiPortAdded(MidiInputPort* pPort); 372 373 /** 374 * Invoked when the number of MIDI instruments has changed. 375 * @param MapId The numerical ID of the MIDI instrument map. 376 * @param NewCount The new number of MIDI instruments. 377 */ 378 virtual void MidiInstrumentCountChanged(int MapId, int NewCount); 379 380 /** 381 * Invoked when a MIDI instrument in a MIDI instrument map is changed. 382 * @param MapId The numerical ID of the MIDI instrument map. 383 * @param Bank The index of the MIDI bank, containing the instrument. 384 * @param Program The MIDI program number of the instrument. 385 */ 386 virtual void MidiInstrumentInfoChanged(int MapId, int Bank, int Program); 387 388 /** 389 * Invoked when the number of MIDI instrument maps has changed. 390 * @param NewCount The new number of MIDI instruments. 391 */ 392 virtual void MidiInstrumentMapCountChanged(int NewCount); 393 394 /** 395 * Invoked when the settings of a MIDI instrument map are changed. 396 * @param MapId The numerical ID of the MIDI instrument map. 397 */ 398 virtual void MidiInstrumentMapInfoChanged(int MapId); 399 400 /** 401 * Invoked when the number of effect sends 402 * on the specified sampler channel has changed. 403 * @param ChannelId The numerical ID of the sampler channel. 404 * @param NewCount The new number of effect sends. 405 */ 406 virtual void FxSendCountChanged(int ChannelId, int NewCount); 407 408 /** 409 * Invoked when the number of active voices 410 * on the specified sampler channel has changed. 411 * @param ChannelId The numerical ID of the sampler channel. 412 * @param NewCount The new number of active voices. 413 */ 414 virtual void VoiceCountChanged(int ChannelId, int NewCount); 415 416 /** 417 * Invoked when the number of active disk streams 418 * on the specified sampler channel has changed. 419 * @param ChannelId The numerical ID of the sampler channel. 420 * @param NewCount The new number of active disk streams. 421 */ 422 virtual void StreamCountChanged(int ChannelId, int NewCount); 423 424 /** 425 * Invoked when the fill state of the disk stream 426 * buffers on the specified sampler channel is changed. 427 * @param ChannelId The numerical ID of the sampler channel. 428 * @param FillData The buffer fill data for the specified sampler channel. 429 */ 430 virtual void BufferFillChanged(int ChannelId, String FillData); 431 432 /** 433 * Invoked when the total number of active voices is changed. 434 * @param NewCount The new number of active voices. 435 */ 436 virtual void TotalVoiceCountChanged(int NewCount); 437 virtual void TotalStreamCountChanged(int NewCount); 438 439 virtual void EngineToBeChanged(int ChannelId); 440 virtual void EngineChanged(int ChannelId); 441 442 virtual ~EventHandler(); 443 444 struct midi_listener_entry { 445 SamplerChannel* pSamplerChannel; 446 EngineChannel* pEngineChannel; 447 VirtualMidiDevice* pMidiListener; 448 }; 449 450 std::vector<midi_listener_entry> channelMidiListeners; 451 452 struct device_midi_listener_entry { 453 MidiInputPort* pPort; 454 VirtualMidiDevice* pMidiListener; 455 uint uiDeviceID; 456 }; 457 458 std::vector<device_midi_listener_entry> deviceMidiListeners; 459 460 private: 461 LSCPServer* pParent; 462 } eventHandler; 463 464 #if HAVE_SQLITE3 465 class DbInstrumentsEventHandler : public InstrumentsDb::Listener { 466 public: 467 virtual void DirectoryCountChanged(String Dir); 468 virtual void DirectoryInfoChanged(String Dir); 469 virtual void DirectoryNameChanged(String Dir, String NewName); 470 virtual void InstrumentCountChanged(String Dir); 471 virtual void InstrumentInfoChanged(String Instr); 472 virtual void InstrumentNameChanged(String Instr, String NewName); 473 virtual void JobStatusChanged(int JobId); 474 } dbInstrumentsEventHandler; 475 #endif // HAVE_SQLITE3 476 }; 477 478 } 479 480 #endif // __LSCPSERVER_H_ 481