2 Copyright (C) 2008 Grame
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.
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.
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.
20 #include "JackAudioAdapter.h"
21 #include "JackError.h"
22 #include "JackCompilerDeps.h"
23 #include "JackTools.h"
35 //static methods ***********************************************************
36 int JackAudioAdapter::Process (jack_nframes_t frames
, void* arg
)
38 JackAudioAdapter
* adapter
= static_cast<JackAudioAdapter
*>(arg
);
39 jack_default_audio_sample_t
* inputBuffer
[adapter
->fAudioAdapter
->GetInputs()];
40 jack_default_audio_sample_t
* outputBuffer
[adapter
->fAudioAdapter
->GetOutputs()];
42 // Always clear output
43 for (int i
= 0; i
< adapter
->fAudioAdapter
->GetInputs(); i
++) {
44 inputBuffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(adapter
->fCapturePortList
[i
], frames
);
45 memset(inputBuffer
[i
], 0, frames
* sizeof(jack_default_audio_sample_t
));
48 for (int i
= 0; i
< adapter
->fAudioAdapter
->GetOutputs(); i
++) {
49 outputBuffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(adapter
->fPlaybackPortList
[i
], frames
);
52 adapter
->fAudioAdapter
->PullAndPush(inputBuffer
, outputBuffer
, frames
);
56 int JackAudioAdapter::BufferSize ( jack_nframes_t buffer_size
, void* arg
)
58 JackAudioAdapter
* adapter
= static_cast<JackAudioAdapter
*> ( arg
);
60 adapter
->fAudioAdapter
->SetHostBufferSize ( buffer_size
);
64 int JackAudioAdapter::SampleRate ( jack_nframes_t sample_rate
, void* arg
)
66 JackAudioAdapter
* adapter
= static_cast<JackAudioAdapter
*> ( arg
);
68 adapter
->fAudioAdapter
->SetHostSampleRate ( sample_rate
);
72 //JackAudioAdapter *********************************************************
74 JackAudioAdapter::JackAudioAdapter (jack_client_t
* jack_client
, JackAudioAdapterInterface
* audio_io
, const JSList
* params
, bool system
)
75 :fJackClient(jack_client
), fAudioAdapter(audio_io
)
78 const jack_driver_param_t
* param
;
81 for (node
= params
; node
; node
= jack_slist_next(node
)) {
82 param
= (const jack_driver_param_t
*) node
->data
;
83 switch (param
->character
) {
91 JackAudioAdapter::~JackAudioAdapter()
93 // When called, Close has already been used for the client, thus ports are already unregistered.
97 void JackAudioAdapter::FreePorts()
99 for (int i
= 0; i
< fAudioAdapter
->GetInputs(); i
++ )
100 if ( fCapturePortList
[i
] )
101 jack_port_unregister ( fJackClient
, fCapturePortList
[i
] );
102 for (int i
= 0; i
< fAudioAdapter
->GetOutputs(); i
++ )
103 if ( fPlaybackPortList
[i
] )
104 jack_port_unregister ( fJackClient
, fPlaybackPortList
[i
] );
106 delete[] fCapturePortList
;
107 delete[] fPlaybackPortList
;
110 void JackAudioAdapter::ConnectPorts()
114 ports
= jack_get_ports(fJackClient
, NULL
, NULL
, JackPortIsPhysical
| JackPortIsInput
);
116 for (int i
= 0; i
< fAudioAdapter
->GetInputs() && ports
[i
]; i
++) {
117 jack_connect(fJackClient
,jack_port_name(fCapturePortList
[i
]), ports
[i
]);
122 ports
= jack_get_ports(fJackClient
, NULL
, NULL
, JackPortIsPhysical
| JackPortIsOutput
);
124 for (int i
= 0; i
< fAudioAdapter
->GetOutputs() && ports
[i
]; i
++) {
125 jack_connect(fJackClient
, ports
[i
], jack_port_name(fPlaybackPortList
[i
]));
131 void JackAudioAdapter::Reset()
133 fAudioAdapter
->Reset();
136 int JackAudioAdapter::Open()
139 jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter
->GetInputs(), fAudioAdapter
->GetOutputs());
140 fAudioAdapter
->Create();
143 fCapturePortList
= new jack_port_t
*[fAudioAdapter
->GetInputs()];
144 fPlaybackPortList
= new jack_port_t
*[fAudioAdapter
->GetOutputs()];
146 for (int i
= 0; i
< fAudioAdapter
->GetInputs(); i
++)
148 sprintf(name
, "capture_%d", i
+ 1);
149 if ((fCapturePortList
[i
] = jack_port_register(fJackClient
, name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0)) == NULL
)
153 for (int i
= 0; i
< fAudioAdapter
->GetOutputs(); i
++)
155 sprintf(name
, "playback_%d", i
+ 1);
156 if ((fPlaybackPortList
[i
] = jack_port_register(fJackClient
, name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0 )) == NULL
)
160 //callbacks and activation
161 if ( jack_set_process_callback ( fJackClient
, Process
, this ) < 0 )
163 if ( jack_set_buffer_size_callback ( fJackClient
, BufferSize
, this ) < 0 )
165 if ( jack_set_sample_rate_callback ( fJackClient
, SampleRate
, this ) < 0 )
167 if ( jack_activate ( fJackClient
) < 0 )
173 // Ring buffer are now allocated..
174 return fAudioAdapter
->Open();
178 fAudioAdapter
->Destroy();
182 int JackAudioAdapter::Close()
184 fAudioAdapter
->Close();
185 fAudioAdapter
->Destroy();