1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#include "jackio.h" |
24 |
|
25 |
#if HAVE_JACK |
26 |
|
27 |
JackIO::JackIO() : AudioIO() { |
28 |
PendingSamples = 0; |
29 |
} |
30 |
|
31 |
int JackIO::Initialize(uint Channels, String OutputPorts[2]) { |
32 |
this->uiChannels = Channels; |
33 |
this->bInterleaved = false; |
34 |
|
35 |
if ((Client = jack_client_new("LinuxSampler")) == 0) { |
36 |
fprintf (stderr, "Seems Jack server not running.\n"); |
37 |
return -1; |
38 |
} |
39 |
|
40 |
jack_set_process_callback(Client, __libjack_process_callback, this); |
41 |
jack_on_shutdown(Client, __libjack_shutdown_callback, this); |
42 |
|
43 |
this->uiMaxSamplesPerCycle = jack_get_buffer_size(Client); |
44 |
this->uiSamplerate = jack_get_sample_rate(Client); |
45 |
this->bInitialized = true; |
46 |
|
47 |
// Acquire given output ports |
48 |
if(OutputPorts[0] != "") |
49 |
if ((this->playback_port[0] = jack_port_by_name(Client, OutputPorts[0].c_str())) == 0) |
50 |
fprintf (stderr, "JackIO: Invalid playback port %s.\n", OutputPorts[0].c_str()); |
51 |
if(OutputPorts[1] != "") |
52 |
if ((this->playback_port[1] = jack_port_by_name(Client, OutputPorts[1].c_str())) == 0) |
53 |
fprintf (stderr, "JackIO: Invalid playback port %s.\n", OutputPorts[1].c_str()); |
54 |
|
55 |
return 0; |
56 |
} |
57 |
|
58 |
void JackIO::Activate() { |
59 |
if (jack_activate(Client)) { |
60 |
fprintf (stderr, "JackIO: Cannot activate Jack client, exiting.\n"); |
61 |
exit(EXIT_FAILURE); |
62 |
} |
63 |
|
64 |
if ((Ports[0] = jack_port_register(Client, "LinuxSampler:1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0) { |
65 |
fprintf (stderr, "JackIO: Cannot register Jack output port, exiting.\n"); |
66 |
Close(); |
67 |
exit(-1); |
68 |
} |
69 |
|
70 |
if ((Ports[1] = jack_port_register(Client, "LinuxSampler:2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0) { |
71 |
fprintf (stderr, "JackIO: Cannot register Jack output port, exiting.\n"); |
72 |
Close(); |
73 |
exit(-1); |
74 |
} |
75 |
|
76 |
// Connect to given output ports |
77 |
if (playback_port[0]) |
78 |
if (jack_connect(Client, jack_port_name(Ports[0]), jack_port_name(playback_port[0]))) |
79 |
fprintf (stderr, "JackIO: Cannot connect port 0.\n"); |
80 |
if (playback_port[1]) |
81 |
if (jack_connect(Client, jack_port_name(Ports[1]), jack_port_name(playback_port[1]))) |
82 |
fprintf (stderr, "JackIO: Cannot connect port 1.\n"); |
83 |
} |
84 |
|
85 |
int JackIO::Process(uint Samples) { |
86 |
if (!pEngine) { |
87 |
fprintf(stderr, "JackIO: No Sampler Engine assigned, exiting.\n"); |
88 |
exit(EXIT_FAILURE); |
89 |
} |
90 |
PendingSamples = Samples; |
91 |
|
92 |
|
93 |
// let the engine render audio for the current fragment |
94 |
int res = pEngine->RenderAudio(Samples); |
95 |
|
96 |
|
97 |
// check clipping (16 bit) in the audio sum and copy to output buffer |
98 |
float sample_point; |
99 |
for (uint c = 0; c < uiChannels; c++) { |
100 |
float* psumbuffer = pEngine->GetAudioSumBuffer(c); |
101 |
float* poutput = (float*) GetChannelOutputBufer(c); |
102 |
for (uint s = 0; s < Samples; s++) { |
103 |
sample_point = psumbuffer[s] * pEngine->Volume; |
104 |
if (sample_point < -32768.0) sample_point = -32768.0; |
105 |
if (sample_point > 32767.0) sample_point = 32767.0; |
106 |
poutput[s] = sample_point / 32768.0; |
107 |
} |
108 |
} |
109 |
|
110 |
|
111 |
return res; |
112 |
} |
113 |
|
114 |
void JackIO::Close() { |
115 |
if (bInitialized) { |
116 |
jack_client_close (Client); |
117 |
bInitialized = false; |
118 |
} |
119 |
} |
120 |
|
121 |
void* JackIO::GetInterleavedOutputBuffer() { |
122 |
fprintf(stderr, "JackIO::GetInterleavedOutputBuffer(): No interleaved access allowed, exiting.\n"); |
123 |
exit(EXIT_FAILURE); |
124 |
// just to avoid compiler warnings |
125 |
return NULL; |
126 |
} |
127 |
|
128 |
void* JackIO::GetChannelOutputBufer(uint Channel) { |
129 |
return (jack_default_audio_sample_t *) jack_port_get_buffer(Ports[Channel], PendingSamples); |
130 |
} |
131 |
|
132 |
int __libjack_process_callback(jack_nframes_t nframes, void* arg) { |
133 |
JackIO* pJackIO = (JackIO*) arg; |
134 |
return pJackIO->Process(nframes); |
135 |
} |
136 |
|
137 |
void __libjack_shutdown_callback(void* arg) { |
138 |
JackIO* pJackIO = (JackIO*) arg; |
139 |
pJackIO->Close(); |
140 |
fprintf(stderr, "JackIO: Jack server shutdown, exiting.\n"); |
141 |
exit(EXIT_FAILURE); |
142 |
} |
143 |
|
144 |
#endif // HAVE_JACK |