Add zalsa configure flag, enabled by default if possible
[jack2.git] / android / JackGoldfishDriver.cpp
blob0765d8b356e7a7e4e87c6bca8c9e9fe73e010cfe
1 /*
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"
28 #include <iostream>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <math.h>
33 #define JACK_GOLDFISH_BUFFER_SIZE 4096
35 namespace Jack
38 static char const * const kAudioDeviceName = "/dev/eac";
40 int JackGoldfishDriver::Open(jack_nframes_t buffer_size,
41 jack_nframes_t samplerate,
42 bool capturing,
43 bool playing,
44 int inchannels,
45 int outchannels,
46 bool monitor,
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) {
56 return -1;
59 mFd = ::open(kAudioDeviceName, O_RDWR);
60 jack_log("JackGoldfishDriver::Open(mFd=%d)", mFd);
62 if (!mBuffer)
63 mBuffer = (short *) malloc(sizeof(short) * JACK_GOLDFISH_BUFFER_SIZE * 2);
65 //JackAudioDriver::SetBufferSize(buffer_size);
66 //JackAudioDriver::SetSampleRate(samplerate);
68 return 0;
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);
79 if (mBuffer) {
80 free(mBuffer);
81 mBuffer = NULL;
84 return res;
87 int JackGoldfishDriver::Read() {
88 jack_log("JackGoldfishDriver::Read");
89 for (int i = 0; i < fCaptureChannels; i++) {
90 //silence
91 memset(GetInputBuffer(i), 0, sizeof(jack_default_audio_sample_t) * JACK_GOLDFISH_BUFFER_SIZE /* fEngineControl->fBufferSize */);
93 return 0;
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);
109 return 0;
112 int JackGoldfishDriver::SetBufferSize(jack_nframes_t buffer_size) {
113 jack_log("JackGoldfishDriver::SetBufferSize");
114 JackAudioDriver::SetBufferSize(buffer_size);
115 return 0;
118 } // end of namespace
121 #ifdef __cplusplus
122 extern "C"
124 #endif
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);
133 value.ui = 2U;
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);
137 value.ui = 44100U;
138 jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
140 value.i = 0;
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);
146 value.ui = 21333U;
147 jack_driver_descriptor_add_parameter(desc, &filler, "wait", 'w', JackDriverParamUInt, &value, NULL, "Number of usecs to wait between engine processes", NULL);
149 return desc;
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;
157 int wait_time = 0;
158 const JSList * node;
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) {
167 case 'C':
168 capture_ports = param->value.ui;
169 break;
171 case 'P':
172 playback_ports = param->value.ui;
173 break;
175 case 'r':
176 sample_rate = param->value.ui;
177 break;
179 case 'p':
180 buffer_size = param->value.ui;
181 break;
183 case 'w':
184 wait_time = param->value.ui;
185 break;
187 case 'm':
188 monitor = param->value.i;
189 break;
193 if (wait_time > 0) {
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) {
203 return driver;
204 } else {
205 delete driver;
206 return NULL;
210 #ifdef __cplusplus
212 #endif