Jack2 1.9.12

JackAlsaAdapter.cpp
1 /*
2 Copyright (C) 2008 Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #if defined(HAVE_CONFIG_H)
21 #include "config.h"
22 #endif
23
24 #include "JackAlsaAdapter.h"
25 #include "JackGlobals.h"
26 #include "JackEngineControl.h"
27
28 namespace Jack
29 {
30
31 JackAlsaAdapter::JackAlsaAdapter ( jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params ) :
32 JackAudioAdapterInterface ( buffer_size, sample_rate ),
33 fThread ( this ),
34 fAudioInterface ( buffer_size, sample_rate )
35 {
36 const JSList* node;
37 const jack_driver_param_t* param;
38
39 fCaptureChannels = 2;
40 fPlaybackChannels = 2;
41
42 fAudioInterface.fPeriod = 2;
43
44 for ( node = params; node; node = jack_slist_next ( node ) )
45 {
46 param = ( const jack_driver_param_t* ) node->data;
47
48 switch ( param->character )
49 {
50 case 'i':
51 fCaptureChannels = param->value.ui;
52 break;
53 case 'o':
54 fPlaybackChannels = param->value.ui;
55 break;
56 case 'C':
57 if (strncmp(param->value.str,"none",4) != 0) {
58 fAudioInterface.fCaptureName = strdup ( param->value.str );
59 }
60 break;
61 case 'P':
62 if (strncmp(param->value.str,"none",4) != 0) {
63 fAudioInterface.fPlaybackName = strdup ( param->value.str );
64 }
65 break;
66 case 'D':
67 break;
68 case 'n':
69 fAudioInterface.fPeriod = param->value.ui;
70 break;
71 case 'd':
72 fAudioInterface.fCardName = strdup ( param->value.str );
73 break;
74 case 'r':
75 fAudioInterface.fFrequency = param->value.ui;
76 SetAdaptedSampleRate ( param->value.ui );
77 break;
78 case 'p':
79 fAudioInterface.fBuffering = param->value.ui;
80 SetAdaptedBufferSize ( param->value.ui );
81 break;
82 case 'q':
83 fQuality = param->value.ui;
84 break;
85 case 'g':
86 fRingbufferCurSize = param->value.ui;
87 fAdaptative = false;
88 break;
89 }
90 }
91
92 fAudioInterface.setInputs ( fCaptureChannels );
93 fAudioInterface.setOutputs ( fPlaybackChannels );
94 }
95
96 int JackAlsaAdapter::Open()
97 {
98 //open audio interface
99 if ( fAudioInterface.open() )
100 return -1;
101
102 //start adapter thread
103 if ( fThread.StartSync() < 0 )
104 {
105 jack_error ( "Cannot start audioadapter thread" );
106 return -1;
107 }
108
109 //display card info
110 fAudioInterface.longinfo();
111
112 //turn the thread realtime
113 fThread.AcquireRealTime(GetEngineControl()->fClientPriority);
114 return 0;
115 }
116
117 int JackAlsaAdapter::Close()
118 {
119 #ifdef JACK_MONITOR
120 fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
121 #endif
122 switch ( fThread.GetStatus() )
123 {
124
125 // Kill the thread in Init phase
126 case JackThread::kStarting:
127 case JackThread::kIniting:
128 if ( fThread.Kill() < 0 )
129 {
130 jack_error ( "Cannot kill thread" );
131 return -1;
132 }
133 break;
134
135 // Stop when the thread cycle is finished
136 case JackThread::kRunning:
137 if ( fThread.Stop() < 0 )
138 {
139 jack_error ( "Cannot stop thread" );
140 return -1;
141 }
142 break;
143
144 default:
145 break;
146 }
147 return fAudioInterface.close();
148 }
149
150 bool JackAlsaAdapter::Init()
151 {
152 //fill the hardware buffers
153 for ( unsigned int i = 0; i < fAudioInterface.fPeriod; i++ )
154 fAudioInterface.write();
155 return true;
156 }
157
158 bool JackAlsaAdapter::Execute()
159 {
160 //read data from audio interface
161 if (fAudioInterface.read() < 0)
162 return false;
163
164 PushAndPull(fAudioInterface.fInputSoftChannels, fAudioInterface.fOutputSoftChannels, fAdaptedBufferSize);
165
166 //write data to audio interface
167 if (fAudioInterface.write() < 0)
168 return false;
169
170 return true;
171 }
172
173 int JackAlsaAdapter::SetSampleRate ( jack_nframes_t sample_rate )
174 {
175 JackAudioAdapterInterface::SetHostSampleRate ( sample_rate );
176 Close();
177 return Open();
178 }
179
180 int JackAlsaAdapter::SetBufferSize ( jack_nframes_t buffer_size )
181 {
182 JackAudioAdapterInterface::SetHostBufferSize ( buffer_size );
183 Close();
184 return Open();
185 }
186
187 } // namespace
188
189 #ifdef __cplusplus
190 extern "C"
191 {
192 #endif
193
194 SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
195 {
196 jack_driver_desc_t * desc;
197 jack_driver_desc_filler_t filler;
198 jack_driver_param_value_t value;
199
200 desc = jack_driver_descriptor_construct("audioadapter", JackDriverNone, "netjack audio <==> net backend adapter", &filler);
201
202 strcpy(value.str, "none");
203 jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set device", NULL);
204 jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set device", NULL);
205
206 strcpy(value.str, "hw:0");
207 jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "ALSA device name", NULL);
208
209 value.ui = 48000U;
210 jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
211
212 value.ui = 512U;
213 jack_driver_descriptor_add_parameter(desc, &filler, "periodsize", 'p', JackDriverParamUInt, &value, NULL, "Period size", NULL);
214
215 value.ui = 2U;
216 jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of periods of playback latency", NULL);
217
218 value.i = true;
219 jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
220
221 value.i = 0;
222 jack_driver_descriptor_add_parameter(desc, &filler, "in-channels", 'i', JackDriverParamInt, &value, NULL, "Number of capture channels (defaults to hardware max)", NULL);
223 jack_driver_descriptor_add_parameter(desc, &filler, "out-channels", 'o', JackDriverParamInt, &value, NULL, "Number of playback channels (defaults to hardware max)", NULL);
224
225 value.ui = 0;
226 jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamUInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
227
228 value.ui = 32768;
229 jack_driver_descriptor_add_parameter(desc, &filler, "ring-buffer", 'g', JackDriverParamUInt, &value, NULL, "Fixed ringbuffer size", "Fixed ringbuffer size (if not set => automatic adaptative)");
230
231 return desc;
232 }
233
234 #ifdef __cplusplus
235 }
236 #endif
237
Jack::AudioInterface::read
int read()
Definition: JackAlsaAdapter.h:367
Jack::JackAlsaAdapter::Init
virtual bool Init()
Definition: JackAlsaAdapter.cpp:150
jack_driver_param_value_t
Definition: driver_interface.h:64
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_driver_desc_filler_t
Definition: driver_interface.h:142
jack_driver_param_t
Definition: driver_interface.h:125
Jack::AudioInterface::longinfo
int longinfo()
Definition: JackAlsaAdapter.h:538
_JSList
Definition: linux/alsa/jslist.h:35
Jack::AudioInterface::write
int write()
Definition: JackAlsaAdapter.h:434
jack_driver_desc_t
Definition: driver_interface.h:132
Jack::AudioInterface::open
int open()
Definition: JackAlsaAdapter.h:222