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) { |
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 |
return 0; |
48 |
} |
49 |
|
50 |
void JackIO::Activate() { |
51 |
if (jack_activate(Client)) { |
52 |
fprintf (stderr, "JackIO: Cannot activate Jack client, exiting.\n"); |
53 |
exit(EXIT_FAILURE); |
54 |
} |
55 |
|
56 |
if ((Ports[0] = jack_port_register(Client, "LinuxSampler:1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0) { |
57 |
fprintf (stderr, "JackIO: Cannot register Jack output port, exiting.\n"); |
58 |
Close(); |
59 |
exit(-1); |
60 |
} |
61 |
|
62 |
if ((Ports[1] = jack_port_register(Client, "LinuxSampler:2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == 0) { |
63 |
fprintf (stderr, "JackIO: Cannot register Jack output port, exiting.\n"); |
64 |
Close(); |
65 |
exit(-1); |
66 |
} |
67 |
} |
68 |
|
69 |
int JackIO::Process(uint Samples) { |
70 |
if (!pEngine) { |
71 |
fprintf(stderr, "JackIO: No Sampler Engine assigned, exiting.\n"); |
72 |
exit(EXIT_FAILURE); |
73 |
} |
74 |
PendingSamples = Samples; |
75 |
|
76 |
|
77 |
// let the engine render audio for the current fragment |
78 |
int res = pEngine->RenderAudio(Samples); |
79 |
|
80 |
|
81 |
// check clipping (16 bit) in the audio sum and copy to output buffer |
82 |
float sample_point; |
83 |
for (uint c = 0; c < uiChannels; c++) { |
84 |
float* psumbuffer = pEngine->GetAudioSumBuffer(c); |
85 |
float* poutput = (float*) GetChannelOutputBufer(c); |
86 |
for (uint s = 0; s < Samples; s++) { |
87 |
sample_point = psumbuffer[s] * pEngine->Volume; |
88 |
if (sample_point < -32768.0) sample_point = -32768.0; |
89 |
if (sample_point > 32767.0) sample_point = 32767.0; |
90 |
poutput[s] = sample_point / 32768.0; |
91 |
} |
92 |
} |
93 |
|
94 |
|
95 |
return res; |
96 |
} |
97 |
|
98 |
void JackIO::Close() { |
99 |
if (bInitialized) { |
100 |
jack_client_close (Client); |
101 |
bInitialized = false; |
102 |
} |
103 |
} |
104 |
|
105 |
void* JackIO::GetInterleavedOutputBuffer() { |
106 |
fprintf(stderr, "JackIO::GetInterleavedOutputBuffer(): No interleaved access allowed, exiting.\n"); |
107 |
exit(EXIT_FAILURE); |
108 |
// just to avoid compiler warnings |
109 |
return NULL; |
110 |
} |
111 |
|
112 |
void* JackIO::GetChannelOutputBufer(uint Channel) { |
113 |
return (jack_default_audio_sample_t *) jack_port_get_buffer(Ports[Channel], PendingSamples); |
114 |
} |
115 |
|
116 |
int __libjack_process_callback(jack_nframes_t nframes, void* arg) { |
117 |
JackIO* pJackIO = (JackIO*) arg; |
118 |
return pJackIO->Process(nframes); |
119 |
} |
120 |
|
121 |
void __libjack_shutdown_callback(void* arg) { |
122 |
JackIO* pJackIO = (JackIO*) arg; |
123 |
pJackIO->Close(); |
124 |
fprintf(stderr, "JackIO: Jack server shutdown, exiting.\n"); |
125 |
exit(EXIT_FAILURE); |
126 |
} |
127 |
|
128 |
#endif // HAVE_JACK |