3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* 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 * |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
* (at your option) any later version. * |
12 |
* * |
* * |
13 |
* This program is distributed in the hope that it will be useful, * |
* This library is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
* GNU General Public License for more details. * |
17 |
* * |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this program; if not, write to the Free Software * |
* along with this library; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
27 |
#include <unistd.h> |
#include <unistd.h> |
28 |
#include <sys/types.h> |
#include <sys/types.h> |
29 |
#include <sys/socket.h> |
#include <sys/socket.h> |
30 |
|
#include <sys/select.h> |
31 |
|
#include <sys/time.h> |
32 |
#include <netinet/in.h> |
#include <netinet/in.h> |
33 |
#include <netinet/tcp.h> |
#include <netinet/tcp.h> |
34 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
35 |
#include <netdb.h> |
#include <netdb.h> |
36 |
|
|
37 |
|
#include <list> |
38 |
|
|
39 |
#include "lscp.h" |
#include "lscp.h" |
40 |
#include "lscpparser.h" |
#include "lscpparser.h" |
41 |
|
#include "lscp.h" |
42 |
|
#include "lscpevent.h" |
43 |
|
#include "lscpinstrumentloader.h" |
44 |
#include "../Sampler.h" |
#include "../Sampler.h" |
45 |
#include "../common/Thread.h" |
#include "../common/Thread.h" |
46 |
|
#include "../common/Mutex.h" |
47 |
|
#include "../common/Condition.h" |
48 |
|
|
49 |
/// TCP Port on which the server should listen for connection requests. |
/// TCP Port on which the server should listen for connection requests. |
50 |
#define LSCP_PORT 8888 |
#define LSCP_PORT 8888 |
51 |
|
|
52 |
using namespace LinuxSampler; |
/// try up to 3 minutes to bind server socket |
53 |
|
#define LSCP_SERVER_BIND_TIMEOUT 180 |
54 |
|
|
55 |
/// Handle for a client connection (FIXME: doesn't work for more than one network connections of course, thus has to be included to the yyparse() parameters instead). |
using namespace LinuxSampler; |
|
extern int hSession; |
|
56 |
|
|
57 |
// External references to the main scanner and parser functions |
// External references to the main scanner and parser functions |
58 |
extern int yyparse(void* YYPARSE_PARAM); |
extern int yyparse(void* YYPARSE_PARAM); |
59 |
extern int yylex_init(yyscan_t* scanner); |
extern void restart(yyparse_param_t* pparam, int& yychar); |
|
extern int yylex_destroy(yyscan_t yyscanner); |
|
60 |
|
|
61 |
/** |
/** |
62 |
* Network server for the LinuxSampler Control Protocol (LSCP). |
* Network server for the LinuxSampler Control Protocol (LSCP). |
64 |
class LSCPServer : public Thread { |
class LSCPServer : public Thread { |
65 |
public: |
public: |
66 |
LSCPServer(Sampler* pSampler); |
LSCPServer(Sampler* pSampler); |
67 |
|
int WaitUntilInitialized(long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); |
68 |
|
|
69 |
// Methods called by the parser |
// Methods called by the parser |
|
String CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters = StringMap()); |
|
70 |
String DestroyAudioOutputDevice(uint DeviceIndex); |
String DestroyAudioOutputDevice(uint DeviceIndex); |
71 |
|
String DestroyMidiInputDevice(uint DeviceIndex); |
72 |
String LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground = false); |
String LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground = false); |
73 |
String LoadEngine(String EngineName, uint uiSamplerChannel); |
String LoadEngine(String EngineName, uint uiSamplerChannel); |
74 |
String GetChannels(); |
String GetChannels(); |
75 |
|
String ListChannels(); |
76 |
String AddChannel(); |
String AddChannel(); |
77 |
String RemoveChannel(uint uiSamplerChannel); |
String RemoveChannel(uint uiSamplerChannel); |
78 |
String GetAvailableEngines(); |
String GetAvailableEngines(); |
82 |
String GetStreamCount(uint uiSamplerChannel); |
String GetStreamCount(uint uiSamplerChannel); |
83 |
String GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel); |
String GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel); |
84 |
String GetAvailableAudioOutputDrivers(); |
String GetAvailableAudioOutputDrivers(); |
85 |
|
String GetAvailableMidiInputDrivers(); |
86 |
String GetAudioOutputDriverInfo(String Driver); |
String GetAudioOutputDriverInfo(String Driver); |
87 |
|
String GetMidiInputDriverInfo(String Driver); |
88 |
#ifdef __GNUC__ |
#ifdef __GNUC__ |
89 |
typedef std::map<String,String> StringMap; // nasty workaround for a GCC bug (see GCC bug #15980, #57) |
typedef std::map<String,String> StringMap; // nasty workaround for a GCC bug (see GCC bug #15980, #57) |
90 |
String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = StringMap()); |
String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = StringMap()); |
91 |
|
String GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = StringMap()); |
92 |
|
String CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters = StringMap()); |
93 |
|
String CreateMidiInputDevice(String Driver, std::map<String,String> Parameters = StringMap()); |
94 |
#else |
#else |
95 |
String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = std::map<String,String>()); |
String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = std::map<String,String>()); |
96 |
|
String GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map<String,String> DependencyList = std::map<String,String>()); |
97 |
|
String CreateAudioOutputDevice(String Driver, std::map<String,String> Parameters = std::map<String,String>()); |
98 |
|
String CreateMidiInputDevice(String Driver, std::map<String,String> Parameters = std::map<String,String>()); |
99 |
#endif // __GNUC__ |
#endif // __GNUC__ |
100 |
String GetAudioOutputDeviceCount(); |
String GetAudioOutputDeviceCount(); |
101 |
|
String GetMidiInputDeviceCount(); |
102 |
String GetAudioOutputDevices(); |
String GetAudioOutputDevices(); |
103 |
|
String GetMidiInputDevices(); |
104 |
String GetAudioOutputDeviceInfo(uint DeviceIndex); |
String GetAudioOutputDeviceInfo(uint DeviceIndex); |
105 |
|
String GetMidiInputDeviceInfo(uint DeviceIndex); |
106 |
|
String GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex); |
107 |
|
String GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName); |
108 |
String GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId); |
String GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId); |
109 |
String GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName); |
String GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName); |
110 |
String SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal); |
String SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal); |
111 |
String SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); |
String SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); |
112 |
|
String SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); |
113 |
|
String SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal); |
114 |
String SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel); |
String SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel); |
|
String SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel); |
|
|
String SetMIDIInputPort(String MIDIInputPort, uint uiSamplerchannel); |
|
|
String SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel); |
|
115 |
String SetAudioOutputDevice(uint AudioDeviceId, uint SamplerChannel); |
String SetAudioOutputDevice(uint AudioDeviceId, uint SamplerChannel); |
116 |
String SetVolume(double Volume, uint uiSamplerChannel); |
String SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel); |
117 |
|
String SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel); |
118 |
|
String SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel); |
119 |
|
String SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel); |
120 |
|
String SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel); |
121 |
|
String SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel); |
122 |
|
String SetVolume(double dVolume, uint uiSamplerChannel); |
123 |
String ResetChannel(uint uiSamplerChannel); |
String ResetChannel(uint uiSamplerChannel); |
124 |
String SubscribeNotification(event_t Event); |
String ResetSampler(); |
125 |
String UnsubscribeNotification(event_t Event); |
String SubscribeNotification(LSCPEvent::event_t); |
126 |
|
String UnsubscribeNotification(LSCPEvent::event_t); |
127 |
|
String SetEcho(yyparse_param_t* pSession, double boolean_value); |
128 |
void AnswerClient(String ReturnMessage); |
void AnswerClient(String ReturnMessage); |
129 |
|
|
130 |
|
static int currentSocket; |
131 |
|
static std::map<int,String> bufferedCommands; |
132 |
|
|
133 |
|
static void SendLSCPNotify( LSCPEvent Event ); |
134 |
|
static int EventSubscribers( std::list<LSCPEvent::event_t> events ); |
135 |
|
static void LockRTNotify( void ) { RTNotifyMutex.Lock(); } |
136 |
|
static void UnlockRTNotify( void ) { RTNotifyMutex.Unlock(); } |
137 |
|
|
138 |
protected: |
protected: |
139 |
int hSocket; |
int hSocket; |
140 |
sockaddr_in SocketAddress; |
sockaddr_in SocketAddress; |
141 |
Sampler* pSampler; |
Sampler* pSampler; |
142 |
|
Condition Initialized; |
143 |
|
LSCPInstrumentLoader InstrumentLoader; ///< thread responsible for loading instruments in the background |
144 |
|
|
145 |
int Main(); ///< Implementation of virtual method from class Thread |
int Main(); ///< Implementation of virtual method from class Thread |
146 |
|
|
147 |
private: |
private: |
148 |
|
|
149 |
/** |
/** |
150 |
* Converts a result_t structure into a valid LSCP answer message. |
* Find a created audio output device index. |
151 |
*/ |
*/ |
152 |
inline String ConvertResult(result_t result) { |
int GetAudioOutputDeviceIndex (AudioOutputDevice *pDevice); |
|
switch (result.type) { |
|
|
case result_type_success: { |
|
|
return "OK\r\n"; |
|
|
} |
|
|
case result_type_warning: { |
|
|
std::stringstream ss; |
|
|
ss << "WRN:" << result.code << ":" << result.message << "\r\n"; |
|
|
return ss.str(); |
|
|
} |
|
|
case result_type_error: { |
|
|
std::stringstream ss; |
|
|
ss << "ERR:" << result.code << ":" << result.message << "\r\n"; |
|
|
return ss.str(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
template<class T> inline String ToString(T o) { |
|
|
std::stringstream ss; |
|
|
ss << o; |
|
|
return ss.str(); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
* Instrument loader thread for the LinuxSampler Control Protocol (LSCP). |
|
|
*/ |
|
|
class LSCPLoadInstrument : public Thread { |
|
|
|
|
|
public: |
|
|
LSCPLoadInstrument(Engine* pEngine, String Filename, uint uiInstrument); |
|
153 |
|
|
154 |
protected: |
/** |
155 |
// Instance variables. |
* Find a created midi input device index. |
156 |
Engine* pEngine; |
*/ |
157 |
String Filename; |
int GetMidiInputDeviceIndex (MidiInputDevice *pDevice); |
|
uint uiInstrument; |
|
158 |
|
|
159 |
// Implementation of virtual method from class Thread. |
static std::map<int,String> bufferedNotifies; |
160 |
int Main(); |
static Mutex NotifyMutex; |
161 |
|
static Mutex NotifyBufferMutex; |
162 |
|
static bool GetLSCPCommand( std::vector<yyparse_param_t>::iterator iter ); |
163 |
|
static void CloseConnection( std::vector<yyparse_param_t>::iterator iter ); |
164 |
|
static std::vector<yyparse_param_t> Sessions; |
165 |
|
static Mutex SubscriptionMutex; |
166 |
|
static std::map< LSCPEvent::event_t, std::list<int> > eventSubscriptions; |
167 |
|
static fd_set fdSet; |
168 |
|
|
169 |
|
//Protect main thread that generates real time notify messages |
170 |
|
//like voice count, stream count and buffer fill |
171 |
|
//from LSCP server removing engines and channels from underneath |
172 |
|
static Mutex RTNotifyMutex; |
173 |
}; |
}; |
174 |
|
|
175 |
#endif // __LSCPSERVER_H_ |
#endif // __LSCPSERVER_H_ |