2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
4 Copyright (C) 2013 Samsung Electronics
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "JackGoldfishDriver.h"
23 #include "JackDriverLoader.h"
24 #include "JackThreadedDriver.h"
25 #include "JackEngineControl.h"
26 #include "JackGraphManager.h"
27 #include "JackCompilerDeps.h"
33 #define JACK_GOLDFISH_BUFFER_SIZE 4096
38 static char const * const kAudioDeviceName
= "/dev/eac";
40 int JackGoldfishDriver::Open(jack_nframes_t buffer_size
,
41 jack_nframes_t samplerate
,
47 const char* capture_driver_uid
,
48 const char* playback_driver_uid
,
49 jack_nframes_t capture_latency
,
50 jack_nframes_t playback_latency
) {
51 jack_log("JackGoldfishDriver::Open");
53 // Generic JackAudioDriver Open
54 if (JackAudioDriver::Open(buffer_size
, samplerate
, capturing
, playing
, inchannels
, outchannels
, monitor
,
55 capture_driver_uid
, playback_driver_uid
, capture_latency
, playback_latency
) != 0) {
59 mFd
= ::open(kAudioDeviceName
, O_RDWR
);
60 jack_log("JackGoldfishDriver::Open(mFd=%d)", mFd
);
63 mBuffer
= (short *) malloc(sizeof(short) * JACK_GOLDFISH_BUFFER_SIZE
* 2);
65 //JackAudioDriver::SetBufferSize(buffer_size);
66 //JackAudioDriver::SetSampleRate(samplerate);
71 int JackGoldfishDriver::Close() {
72 jack_log("JackGoldfishDriver::Close");
74 // Generic audio driver close
75 int res
= JackAudioDriver::Close();
77 if (mFd
>= 0) ::close(mFd
);
87 int JackGoldfishDriver::Read() {
88 jack_log("JackGoldfishDriver::Read");
89 for (int i
= 0; i
< fCaptureChannels
; i
++) {
91 memset(GetInputBuffer(i
), 0, sizeof(jack_default_audio_sample_t
) * JACK_GOLDFISH_BUFFER_SIZE
/* fEngineControl->fBufferSize */);
96 int JackGoldfishDriver::Write() {
97 jack_log("JackGoldfishDriver::Write");
98 //write(mFd, GetOutputBuffer(i), sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
100 jack_default_audio_sample_t
* outputBuffer_1
= GetOutputBuffer(0);
101 jack_default_audio_sample_t
* outputBuffer_2
= GetOutputBuffer(1);
103 for(int i
=0, j
=0; i
<JACK_GOLDFISH_BUFFER_SIZE
/* fEngineControl->fBufferSize */; i
++) {
104 //convert float to short
105 *(mBuffer
+ j
) = (short) (*(outputBuffer_1
+ i
) * 32640); j
++;
106 *(mBuffer
+ j
) = (short) (*(outputBuffer_2
+ i
) * 32640); j
++;
108 write(mFd
, mBuffer
, sizeof(short) * JACK_GOLDFISH_BUFFER_SIZE
* 2);
112 int JackGoldfishDriver::SetBufferSize(jack_nframes_t buffer_size
) {
113 jack_log("JackGoldfishDriver::SetBufferSize");
114 JackAudioDriver::SetBufferSize(buffer_size
);
118 } // end of namespace
126 SERVER_EXPORT jack_driver_desc_t
* driver_get_descriptor () {
127 jack_driver_desc_t
* desc
;
128 jack_driver_desc_filler_t filler
;
129 jack_driver_param_value_t value
;
131 desc
= jack_driver_descriptor_construct("goldfish", JackDriverMaster
, "Timer based backend", &filler
);
134 jack_driver_descriptor_add_parameter(desc
, &filler
, "capture", 'C', JackDriverParamUInt
, &value
, NULL
, "Number of capture ports", NULL
);
135 jack_driver_descriptor_add_parameter(desc
, &filler
, "playback", 'P', JackDriverParamUInt
, &value
, NULL
, "Number of playback ports", NULL
);
138 jack_driver_descriptor_add_parameter(desc
, &filler
, "rate", 'r', JackDriverParamUInt
, &value
, NULL
, "Sample rate", NULL
);
141 jack_driver_descriptor_add_parameter(desc
, &filler
, "monitor", 'm', JackDriverParamBool
, &value
, NULL
, "Provide monitor ports for the output", NULL
);
143 value
.ui
= JACK_GOLDFISH_BUFFER_SIZE
;
144 jack_driver_descriptor_add_parameter(desc
, &filler
, "period", 'p', JackDriverParamUInt
, &value
, NULL
, "Frames per period", NULL
);
147 jack_driver_descriptor_add_parameter(desc
, &filler
, "wait", 'w', JackDriverParamUInt
, &value
, NULL
, "Number of usecs to wait between engine processes", NULL
);
152 SERVER_EXPORT
Jack::JackDriverClientInterface
* driver_initialize(Jack::JackLockedEngine
* engine
, Jack::JackSynchro
* table
, const JSList
* params
) {
153 jack_nframes_t sample_rate
= 44100;
154 jack_nframes_t buffer_size
= JACK_GOLDFISH_BUFFER_SIZE
;
155 unsigned int capture_ports
= 2;
156 unsigned int playback_ports
= 2;
159 const jack_driver_param_t
* param
;
160 bool monitor
= false;
162 for (node
= params
; node
; node
= jack_slist_next (node
)) {
163 param
= (const jack_driver_param_t
*) node
->data
;
165 switch (param
->character
) {
168 capture_ports
= param
->value
.ui
;
172 playback_ports
= param
->value
.ui
;
176 sample_rate
= param
->value
.ui
;
180 buffer_size
= param
->value
.ui
;
184 wait_time
= param
->value
.ui
;
188 monitor
= param
->value
.i
;
194 buffer_size
= lroundf((wait_time
* sample_rate
) / 1000000.0f
);
195 if (buffer_size
> BUFFER_SIZE_MAX
) {
196 buffer_size
= BUFFER_SIZE_MAX
;
197 jack_error("Buffer size set to %d", BUFFER_SIZE_MAX
);
201 Jack::JackDriverClientInterface
* driver
= new Jack::JackThreadedDriver(new Jack::JackGoldfishDriver("system", "goldfish_pcm", engine
, table
));
202 if (driver
->Open(buffer_size
, sample_rate
, 1, 1, capture_ports
, playback_ports
, monitor
, "goldfish", "goldfish", 0, 0) == 0) {