23 |
#include "AudioOutputDeviceJack.h" |
#include "AudioOutputDeviceJack.h" |
24 |
#include "AudioOutputDeviceFactory.h" |
#include "AudioOutputDeviceFactory.h" |
25 |
|
|
26 |
|
#include <errno.h> |
27 |
|
|
28 |
#if HAVE_JACK |
#if HAVE_JACK |
29 |
|
|
30 |
namespace LinuxSampler { |
namespace LinuxSampler { |
31 |
|
|
32 |
REGISTER_AUDIO_OUTPUT_DRIVER(AudioOutputDeviceJack); |
/// number of currently existing JACK audio output devices in LinuxSampler |
33 |
|
static int existingJackDevices = 0; |
34 |
|
|
35 |
|
// *************** AudioChannelJack::ParameterName *************** |
36 |
|
// * |
37 |
|
|
38 |
|
AudioOutputDeviceJack::AudioChannelJack::ParameterName::ParameterName(AudioChannelJack* pChannel) : AudioChannel::ParameterName(ToString(pChannel->ChannelNr)) { |
39 |
|
this->pChannel = pChannel; |
40 |
|
} |
41 |
|
|
42 |
|
void AudioOutputDeviceJack::AudioChannelJack::ParameterName::OnSetValue(String s) { |
43 |
|
if (jack_port_set_name(pChannel->hJackPort, s.c_str())) throw AudioOutputException("Failed to rename JACK port"); |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
// *************** AudioChannelJack::ParameterJackBindings *************** |
49 |
|
// * |
50 |
|
|
51 |
|
AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::ParameterJackBindings(AudioChannelJack* pChannel) : DeviceRuntimeParameterStrings(std::vector<String>()) { |
52 |
|
this->pChannel = pChannel; |
53 |
|
} |
54 |
|
|
55 |
|
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Description() { |
56 |
|
return "Bindings to other JACK clients"; |
57 |
|
} |
58 |
|
|
59 |
|
bool AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Fix() { |
60 |
|
return false; |
61 |
|
} |
62 |
|
|
63 |
|
std::vector<String> AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::PossibilitiesAsString() { |
64 |
|
const char** pPortNames = jack_get_ports(pChannel->pDevice->hJackClient, NULL, NULL, JackPortIsInput); |
65 |
|
if (!pPortNames) return std::vector<String>(); |
66 |
|
std::vector<String> result; |
67 |
|
for (int i = 0; pPortNames[i]; i++) result.push_back(pPortNames[i]); |
68 |
|
//free(pPortNames); FIXME: pPortNames should be freed here |
69 |
|
return result; |
70 |
|
} |
71 |
|
|
72 |
|
void AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::OnSetValue(std::vector<String> vS) { |
73 |
|
// TODO: we should remove all existing bindings before we connect new ones here |
74 |
|
String src_name = "LinuxSampler:" + ((DeviceRuntimeParameterString*)pChannel->Parameters["NAME"])->ValueAsString(); |
75 |
|
for (int i = 0; i < vS.size(); i++) { |
76 |
|
String dst_name = vS[i]; |
77 |
|
int res = jack_connect(pChannel->pDevice->hJackClient, src_name.c_str(), dst_name.c_str()); |
78 |
|
if (res == EEXIST) throw AudioOutputException("Jack: Connection to port '" + dst_name + "' already established"); |
79 |
|
else if (res) throw AudioOutputException("Jack: Cannot connect port '" + src_name + "' to port '" + dst_name + "'"); |
80 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Name() { |
84 |
|
return "JACK_BINDINGS"; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
// *************** AudioChannelJack *************** |
90 |
|
// * |
91 |
|
|
92 |
|
AudioOutputDeviceJack::AudioChannelJack::AudioChannelJack(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) : AudioChannel(ChannelNr, CreateJackPort(ChannelNr, pDevice), pDevice->uiMaxSamplesPerCycle) { |
93 |
|
this->pDevice = pDevice; |
94 |
|
this->ChannelNr = ChannelNr; |
95 |
|
Parameters["NAME"] = new ParameterName(this); |
96 |
|
Parameters["JACK_BINDINGS"] = new ParameterJackBindings(this); |
97 |
|
} |
98 |
|
|
99 |
|
AudioOutputDeviceJack::AudioChannelJack::~AudioChannelJack() { |
100 |
|
//TODO: delete JACK port |
101 |
|
} |
102 |
|
|
103 |
|
float* AudioOutputDeviceJack::AudioChannelJack::CreateJackPort(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) { |
104 |
|
String port_id = ToString(ChannelNr); |
105 |
|
hJackPort = jack_port_register(pDevice->hJackClient, port_id.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); |
106 |
|
if (!hJackPort) throw AudioOutputException("Jack: Cannot register Jack output port."); |
107 |
|
return (float*) jack_port_get_buffer(hJackPort, pDevice->uiMaxSamplesPerCycle); |
108 |
|
} |
109 |
|
|
110 |
/* Common parameters for now they'll have to be registered here. */ |
|
111 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterActive); |
|
112 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterSampleRate); |
// *************** AudioOutputDeviceJack::ParameterName *************** |
113 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterChannels); |
// * |
114 |
|
|
115 |
|
AudioOutputDeviceJack::ParameterName::ParameterName() : DeviceCreationParameterString() { |
116 |
|
InitWithDefault(); // use default name |
117 |
|
} |
118 |
|
|
119 |
|
AudioOutputDeviceJack::ParameterName::ParameterName(String s) throw (LinuxSamplerException) : DeviceCreationParameterString(s) { |
120 |
|
} |
121 |
|
|
122 |
|
String AudioOutputDeviceJack::ParameterName::Description() { |
123 |
|
return "Arbitrary JACK client name"; |
124 |
|
} |
125 |
|
|
126 |
|
bool AudioOutputDeviceJack::ParameterName::Fix() { |
127 |
|
return true; |
128 |
|
} |
129 |
|
|
130 |
|
bool AudioOutputDeviceJack::ParameterName::Mandatory() { |
131 |
|
return false; |
132 |
|
} |
133 |
|
|
134 |
|
std::map<String,DeviceCreationParameter*> AudioOutputDeviceJack::ParameterName::DependsAsParameters() { |
135 |
|
return std::map<String,DeviceCreationParameter*>(); // no dependencies |
136 |
|
} |
137 |
|
|
138 |
|
std::vector<String> AudioOutputDeviceJack::ParameterName::PossibilitiesAsString(std::map<String,String> Parameters) { |
139 |
|
return std::vector<String>(); |
140 |
|
} |
141 |
|
|
142 |
|
optional<String> AudioOutputDeviceJack::ParameterName::DefaultAsString(std::map<String,String> Parameters) { |
143 |
|
return (existingJackDevices) ? "LinuxSampler" + ToString(existingJackDevices) : "LinuxSampler"; |
144 |
|
} |
145 |
|
|
146 |
|
void AudioOutputDeviceJack::ParameterName::OnSetValue(String s) throw (LinuxSamplerException) { |
147 |
|
// not possible, as parameter is fix |
148 |
|
} |
149 |
|
|
150 |
|
String AudioOutputDeviceJack::ParameterName::Name() { |
151 |
|
return "NAME"; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
// *************** AudioOutputDeviceJack *************** |
157 |
|
// * |
158 |
|
|
159 |
/** |
/** |
160 |
* Open and initialize connection to the JACK system. |
* Open and initialize connection to the JACK system. |
163 |
* @throws AudioOutputException on error |
* @throws AudioOutputException on error |
164 |
* @see AcquireChannels() |
* @see AcquireChannels() |
165 |
*/ |
*/ |
166 |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(std::map<String,DeviceCreationParameter*>()) { |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters) { |
167 |
if ((hJackClient = jack_client_new("LinuxSampler")) == 0) |
if (((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString().size() >= jack_client_name_size()) |
168 |
|
throw LinuxSamplerException("JACK client name too long"); |
169 |
|
|
170 |
|
if ((hJackClient = jack_client_new(((DeviceCreationParameterString*)Parameters["NAME"])->ValueAsString().c_str())) == 0) |
171 |
throw AudioOutputException("Seems Jack server not running."); |
throw AudioOutputException("Seems Jack server not running."); |
172 |
|
|
173 |
|
existingJackDevices++; |
174 |
|
|
175 |
jack_set_process_callback(hJackClient, __libjack_process_callback, this); |
jack_set_process_callback(hJackClient, __libjack_process_callback, this); |
176 |
jack_on_shutdown(hJackClient, __libjack_shutdown_callback, this); |
jack_on_shutdown(hJackClient, __libjack_shutdown_callback, this); |
177 |
if (jack_activate(hJackClient)) |
if (jack_activate(hJackClient)) |
179 |
|
|
180 |
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
181 |
|
|
182 |
#if 0 |
// create audio channels |
183 |
// create amount of audio channels and jack output ports we need for autoconnect |
AcquireChannels(((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt()); |
184 |
for (uint p = 0; p < AutoConnectPorts; p++) { |
|
185 |
// create jack output port |
// finally activate device if desired |
186 |
std::stringstream portid; portid << "LinuxSampler:" << p; |
if (((DeviceCreationParameterBool*)Parameters["ACTIVE"])->ValueAsBool()) Play(); |
|
jack_port_t* newport; |
|
|
if (newport = jack_port_register(hJackClient, portid.str().c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) { |
|
|
hJackPorts.push_back(newport); |
|
|
} |
|
|
else throw AudioOutputException("Jack: Cannot register Jack output port."); |
|
|
|
|
|
// create LS audio channel |
|
|
std::stringstream chanid; chanid << "Jack:" << p; |
|
|
Channels.push_back(new AudioChannel((float*) jack_port_get_buffer(newport, uiMaxSamplesPerCycle), uiMaxSamplesPerCycle, chanid.str())); |
|
|
|
|
|
// autoconnect port |
|
|
if (jack_connect(hJackClient, portid.str().c_str(), AutoConnectPortIDs[p].c_str())) { |
|
|
std::stringstream err; err << "Jack: Cannot auto connect port " << p; |
|
|
throw AudioOutputException(err.str()); |
|
|
} |
|
|
} |
|
|
#endif |
|
187 |
} |
} |
188 |
|
|
189 |
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
|
// destroy all audio channels |
|
|
for (int c = 0; c < Channels.size(); c++) delete Channels[c]; |
|
190 |
// destroy jack client |
// destroy jack client |
191 |
jack_client_close(hJackClient); |
jack_client_close(hJackClient); |
192 |
|
existingJackDevices--; |
193 |
} |
} |
194 |
|
|
195 |
/** |
/** |
219 |
csIsPlaying.PushAndUnlock(false); |
csIsPlaying.PushAndUnlock(false); |
220 |
} |
} |
221 |
|
|
222 |
void AudioOutputDeviceJack::AcquireChannels(uint uiChannels) { |
AudioChannel* AudioOutputDeviceJack::CreateChannel(uint ChannelNr) { |
223 |
if (uiChannels > this->Channels.size()) { |
return new AudioChannelJack(ChannelNr, this); |
|
for (int c = this->Channels.size(); c < uiChannels; c++) { |
|
|
// create new jack output port |
|
|
std::stringstream portid; portid << "LinuxSampler:" << c; |
|
|
jack_port_t* newport; |
|
|
if (newport = jack_port_register(hJackClient, portid.str().c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) { |
|
|
hJackPorts.push_back(newport); |
|
|
} |
|
|
else throw AudioOutputException("Jack: Cannot register Jack output port."); |
|
|
|
|
|
// create LS audio channel |
|
|
std::stringstream chanid; chanid << "Jack:" << c; |
|
|
Channels.push_back(new AudioChannel((float*) jack_port_get_buffer(newport, uiMaxSamplesPerCycle), uiMaxSamplesPerCycle, chanid.str())); |
|
|
} |
|
|
} |
|
224 |
} |
} |
225 |
|
|
226 |
uint AudioOutputDeviceJack::MaxSamplesPerCycle() { |
uint AudioOutputDeviceJack::MaxSamplesPerCycle() { |
244 |
} |
} |
245 |
|
|
246 |
String AudioOutputDeviceJack::Version() { |
String AudioOutputDeviceJack::Version() { |
247 |
String s = "$Revision: 1.10 $"; |
String s = "$Revision: 1.15 $"; |
248 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
249 |
} |
} |
250 |
|
|