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 { |
33 |
|
|
34 |
/* Common parameters for now they'll have to be registered here. */ |
/* Common parameters for now they'll have to be registered here. */ |
35 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterActive); |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterActive); |
|
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterSampleRate); |
|
36 |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterChannels); |
REGISTER_AUDIO_OUTPUT_DRIVER_PARAMETER(AudioOutputDeviceJack, ParameterChannels); |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
// *************** 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 |
|
// *************** 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 |
|
std::vector<String> AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::PossibilitiesAsString() { |
69 |
|
const char** pPortNames = jack_get_ports(pChannel->pDevice->hJackClient, NULL, NULL, JackPortIsInput); |
70 |
|
if (!pPortNames) return std::vector<String>(); |
71 |
|
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 |
|
// TODO: we should remove all existing bindings before we connect new ones here |
79 |
|
String src_name = "LinuxSampler:" + ((DeviceRuntimeParameterString*)pChannel->Parameters["NAME"])->ValueAsString(); |
80 |
|
for (int i = 0; i < vS.size(); i++) { |
81 |
|
String dst_name = vS[i]; |
82 |
|
int res = jack_connect(pChannel->pDevice->hJackClient, src_name.c_str(), dst_name.c_str()); |
83 |
|
if (res == EEXIST) throw AudioOutputException("Jack: Connection to port '" + dst_name + "' already established"); |
84 |
|
else if (res) throw AudioOutputException("Jack: Cannot connect port '" + src_name + "' to port '" + dst_name + "'"); |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
|
String AudioOutputDeviceJack::AudioChannelJack::ParameterJackBindings::Name() { |
89 |
|
return "JACK_BINDINGS"; |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
// *************** AudioChannelJack *************** |
95 |
|
// * |
96 |
|
|
97 |
|
AudioOutputDeviceJack::AudioChannelJack::AudioChannelJack(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) : AudioChannel(ChannelNr, CreateJackPort(ChannelNr, pDevice), pDevice->uiMaxSamplesPerCycle) { |
98 |
|
this->pDevice = pDevice; |
99 |
|
this->ChannelNr = ChannelNr; |
100 |
|
Parameters["NAME"] = new ParameterName(this); |
101 |
|
Parameters["JACK_BINDINGS"] = new ParameterJackBindings(this); |
102 |
|
} |
103 |
|
|
104 |
|
AudioOutputDeviceJack::AudioChannelJack::~AudioChannelJack() { |
105 |
|
//TODO: delete JACK port |
106 |
|
} |
107 |
|
|
108 |
|
float* AudioOutputDeviceJack::AudioChannelJack::CreateJackPort(uint ChannelNr, AudioOutputDeviceJack* pDevice) throw (AudioOutputException) { |
109 |
|
String port_id = ToString(ChannelNr); |
110 |
|
hJackPort = jack_port_register(pDevice->hJackClient, port_id.c_str(), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); |
111 |
|
if (!hJackPort) throw AudioOutputException("Jack: Cannot register Jack output port."); |
112 |
|
return (float*) jack_port_get_buffer(hJackPort, pDevice->uiMaxSamplesPerCycle); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
// *************** AudioOutputDeviceJack *************** |
118 |
|
// * |
119 |
|
|
120 |
/** |
/** |
121 |
* Open and initialize connection to the JACK system. |
* Open and initialize connection to the JACK system. |
122 |
* |
* |
124 |
* @throws AudioOutputException on error |
* @throws AudioOutputException on error |
125 |
* @see AcquireChannels() |
* @see AcquireChannels() |
126 |
*/ |
*/ |
127 |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(std::map<String,DeviceCreationParameter*>()) { |
AudioOutputDeviceJack::AudioOutputDeviceJack(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters) { |
128 |
if ((hJackClient = jack_client_new("LinuxSampler")) == 0) |
if ((hJackClient = jack_client_new("LinuxSampler")) == 0) |
129 |
throw AudioOutputException("Seems Jack server not running."); |
throw AudioOutputException("Seems Jack server not running."); |
130 |
|
|
135 |
|
|
136 |
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
uiMaxSamplesPerCycle = jack_get_buffer_size(hJackClient); |
137 |
|
|
138 |
#if 0 |
// create audio channels |
139 |
// create amount of audio channels and jack output ports we need for autoconnect |
AcquireChannels(((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt()); |
140 |
for (uint p = 0; p < AutoConnectPorts; p++) { |
|
141 |
// create jack output port |
// finally activate device if desired |
142 |
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 |
|
143 |
} |
} |
144 |
|
|
145 |
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
AudioOutputDeviceJack::~AudioOutputDeviceJack() { |
|
// destroy all audio channels |
|
|
for (int c = 0; c < Channels.size(); c++) delete Channels[c]; |
|
146 |
// destroy jack client |
// destroy jack client |
147 |
jack_client_close(hJackClient); |
jack_client_close(hJackClient); |
148 |
} |
} |
174 |
csIsPlaying.PushAndUnlock(false); |
csIsPlaying.PushAndUnlock(false); |
175 |
} |
} |
176 |
|
|
177 |
void AudioOutputDeviceJack::AcquireChannels(uint uiChannels) { |
AudioChannel* AudioOutputDeviceJack::CreateChannel(uint ChannelNr) { |
178 |
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())); |
|
|
} |
|
|
} |
|
179 |
} |
} |
180 |
|
|
181 |
uint AudioOutputDeviceJack::MaxSamplesPerCycle() { |
uint AudioOutputDeviceJack::MaxSamplesPerCycle() { |
187 |
} |
} |
188 |
|
|
189 |
String AudioOutputDeviceJack::Name() { |
String AudioOutputDeviceJack::Name() { |
190 |
return "Jack"; |
return "JACK"; |
191 |
} |
} |
192 |
|
|
193 |
String AudioOutputDeviceJack::Driver() { |
String AudioOutputDeviceJack::Driver() { |
199 |
} |
} |
200 |
|
|
201 |
String AudioOutputDeviceJack::Version() { |
String AudioOutputDeviceJack::Version() { |
202 |
String s = "$Revision: 1.9 $"; |
String s = "$Revision: 1.13 $"; |
203 |
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 |
204 |
} |
} |
205 |
|
|