00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "JackPort.h"
00022 #include "JackError.h"
00023 #include "JackPortType.h"
00024 #include <stdio.h>
00025 #include <assert.h>
00026
00027 namespace Jack
00028 {
00029
00030 JackPort::JackPort()
00031 : fTypeId(0),
00032 fFlags(JackPortIsInput),
00033 fRefNum( -1),
00034 fLatency(0),
00035 fTotalLatency(0),
00036 fMonitorRequests(0),
00037 fInUse(false),
00038 fTied(NO_PORT)
00039 {}
00040
00041 bool JackPort::Allocate(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
00042 {
00043 jack_port_type_id_t id = GetPortTypeId(port_type);
00044 if (id == PORT_TYPES_MAX)
00045 return false;
00046 fTypeId = id;
00047 fFlags = flags;
00048 fRefNum = refnum;
00049 strcpy(fName, port_name);
00050 fInUse = true;
00051 fLatency = 0;
00052 fTotalLatency = 0;
00053 fTied = NO_PORT;
00054
00055
00056
00057
00058
00059 ClearBuffer(0);
00060 return true;
00061 }
00062
00063 void JackPort::Release()
00064 {
00065 fTypeId = 0;
00066 fFlags = JackPortIsInput;
00067 fRefNum = -1;
00068 fInUse = false;
00069 fLatency = 0;
00070 fTotalLatency = 0;
00071 fTied = NO_PORT;
00072 fAlias1[0] = '\0';
00073 fAlias2[0] = '\0';
00074 }
00075
00076 int JackPort::GetRefNum() const
00077 {
00078 return fRefNum;
00079 }
00080
00081 jack_nframes_t JackPort::GetLatency() const
00082 {
00083 return fLatency;
00084 }
00085
00086 jack_nframes_t JackPort::GetTotalLatency() const
00087 {
00088 return fTotalLatency;
00089 }
00090
00091 void JackPort::SetLatency(jack_nframes_t nframes)
00092 {
00093 fLatency = nframes;
00094 }
00095
00096 int JackPort::Tie(jack_port_id_t port_index)
00097 {
00098 fTied = port_index;
00099 return 0;
00100 }
00101
00102 int JackPort::UnTie()
00103 {
00104 fTied = NO_PORT;
00105 return 0;
00106 }
00107
00108 int JackPort::RequestMonitor(bool onoff)
00109 {
00119 if (onoff) {
00120 fMonitorRequests++;
00121 } else if (fMonitorRequests) {
00122 fMonitorRequests--;
00123 }
00124
00125 return 0;
00126 }
00127
00128 int JackPort::EnsureMonitor(bool onoff)
00129 {
00139 if (onoff) {
00140 if (fMonitorRequests == 0) {
00141 fMonitorRequests++;
00142 }
00143 } else {
00144 if (fMonitorRequests > 0) {
00145 fMonitorRequests = 0;
00146 }
00147 }
00148
00149 return 0;
00150 }
00151
00152 const char* JackPort::GetName() const
00153 {
00154 return fName;
00155 }
00156
00157 const char* JackPort::GetShortName() const
00158 {
00159
00160
00161
00162 return strchr(fName, ':') + 1;
00163 }
00164
00165 int JackPort::GetFlags() const
00166 {
00167 return fFlags;
00168 }
00169
00170 const char* JackPort::GetType() const
00171 {
00172 const JackPortType* type = GetPortType(fTypeId);
00173 return type->name;
00174 }
00175
00176 void JackPort::SetName(const char* new_name)
00177 {
00178 char* colon = strchr(fName, ':');
00179 int len = sizeof(fName) - ((int) (colon - fName)) - 2;
00180 snprintf(colon + 1, len, "%s", new_name);
00181 }
00182
00183 bool JackPort::NameEquals(const char* target)
00184 {
00185 char buf[JACK_PORT_NAME_SIZE + 1];
00186
00187
00188
00189
00190
00191
00192
00193
00194 if (strncmp(target, "ALSA:capture", 12) == 0 || strncmp(target, "ALSA:playback", 13) == 0) {
00195 snprintf(buf, sizeof(buf), "alsa_pcm%s", target + 4);
00196 target = buf;
00197 }
00198
00199 return (strcmp(fName, target) == 0
00200 || strcmp(fAlias1, target) == 0
00201 || strcmp(fAlias2, target) == 0);
00202 }
00203
00204 int JackPort::GetAliases(char* const aliases[2])
00205 {
00206 int cnt = 0;
00207
00208 if (fAlias1[0] != '\0') {
00209 snprintf(aliases[0], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias1);
00210 cnt++;
00211 }
00212
00213 if (fAlias2[0] != '\0') {
00214 snprintf(aliases[1], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias2);
00215 cnt++;
00216 }
00217
00218 return cnt;
00219 }
00220
00221 int JackPort::SetAlias(const char* alias)
00222 {
00223 if (fAlias1[0] == '\0') {
00224 snprintf(fAlias1, sizeof(fAlias1), "%s", alias);
00225 } else if (fAlias2[0] == '\0') {
00226 snprintf(fAlias2, sizeof(fAlias2), "%s", alias);
00227 } else {
00228 return -1;
00229 }
00230
00231 return 0;
00232 }
00233
00234 int JackPort::UnsetAlias(const char* alias)
00235 {
00236 if (strcmp(fAlias1, alias) == 0) {
00237 fAlias1[0] = '\0';
00238 } else if (strcmp(fAlias2, alias) == 0) {
00239 fAlias2[0] = '\0';
00240 } else {
00241 return -1;
00242 }
00243
00244 return 0;
00245 }
00246
00247 void JackPort::ClearBuffer(jack_nframes_t frames)
00248 {
00249 const JackPortType* type = GetPortType(fTypeId);
00250 (type->init)(GetBuffer(), frames * sizeof(float), frames);
00251 }
00252
00253 void JackPort::MixBuffers(void** src_buffers, int src_count, jack_nframes_t buffer_size)
00254 {
00255 const JackPortType* type = GetPortType(fTypeId);
00256 (type->mixdown)(GetBuffer(), src_buffers, src_count, buffer_size);
00257 }
00258
00259 }