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