JackPort.cpp

00001 /*
00002 Copyright (C) 2001-2003 Paul Davis
00003 Copyright (C) 2004-2006 Grame
00004 
00005 This program is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009 
00010   This program is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013   GNU General Public License for more details.
00014 
00015   You should have received a copy of the GNU General Public License
00016   along with this program; if not, write to the Free Software
00017   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 */
00020 
00021 #include "JackPort.h"
00022 #include "JackError.h"
00023 #include <stdio.h>
00024 #include <assert.h>
00025 
00026 namespace Jack
00027 {
00028 
00029 JackPort::JackPort()
00030         : fFlags(JackPortIsInput), fRefNum( -1), fLatency(0), fMonitorRequests(0), fInUse(false), fLocked(false), fTied(NO_PORT)
00031 {}
00032 
00033 JackPort::~JackPort()
00034 {}
00035 
00036 void JackPort::Allocate(int refnum, const char* port_name, JackPortFlags flags)
00037 {
00038     fFlags = flags;
00039     fRefNum = refnum;
00040     strcpy(fName, port_name);
00041     memset(fBuffer, 0, BUFFER_SIZE_MAX * sizeof(float));
00042     fInUse = true;
00043     fLocked = false;
00044     fLatency = 0;
00045     fTied = NO_PORT;
00046 }
00047 
00048 void JackPort::Release()
00049 {
00050     fFlags = JackPortIsInput;
00051     fRefNum = -1;
00052     fInUse = false;
00053     fLocked = false;
00054     fLatency = 0;
00055     fTied = NO_PORT;
00056         fAlias1[0] = '\0';
00057         fAlias2[0] = '\0';
00058 }
00059 
00060 bool JackPort::IsUsed() const
00061 {
00062     return fInUse;
00063 }
00064 
00065 float* JackPort::GetBuffer()
00066 {
00067     return fBuffer;
00068 }
00069 
00070 int JackPort::GetRefNum() const
00071 {
00072     return fRefNum;
00073 }
00074 
00075 int JackPort::Lock()
00076 {
00077     fLocked = true;
00078     return 0;
00079 }
00080 
00081 int JackPort::Unlock()
00082 {
00083     fLocked = false;
00084     return 0;
00085 }
00086 
00087 jack_nframes_t JackPort::GetLatency() const
00088 {
00089     return fLatency;
00090 }
00091 
00092 void JackPort::SetLatency(jack_nframes_t nframes)
00093 {
00094     fLatency = nframes;
00095 }
00096 
00097 int JackPort::Tie(jack_port_id_t port_index)
00098 {
00099     fTied = port_index;
00100     return 0;
00101 }
00102 
00103 int JackPort::UnTie()
00104 {
00105     fTied = NO_PORT;
00106     return 0;
00107 }
00108 
00109 int JackPort::RequestMonitor(bool onoff)
00110 {
00120     if (onoff) {
00121         fMonitorRequests++;
00122     } else if (fMonitorRequests) {
00123         fMonitorRequests--;
00124     }
00125 
00126     return 0;
00127 }
00128 
00129 int JackPort::EnsureMonitor(bool onoff)
00130 {
00140     if (onoff) {
00141         if (fMonitorRequests == 0) {
00142             fMonitorRequests++;
00143         }
00144     } else {
00145         if (fMonitorRequests > 0) {
00146             fMonitorRequests = 0;
00147         }
00148     }
00149 
00150     return 0;
00151 }
00152 
00153 bool JackPort::MonitoringInput()
00154 {
00155     return (fMonitorRequests > 0);
00156 }
00157 
00158 const char* JackPort::GetName() const
00159 {
00160     return fName;
00161 }
00162 
00163 const char* JackPort::GetShortName() const
00164 {
00165     /* we know there is always a colon, because we put
00166        it there ...
00167     */ 
00168     return strchr(fName, ':') + 1;
00169 }
00170 
00171 int JackPort::Flags() const
00172 {
00173     return fFlags;
00174 }
00175 
00176 const char* JackPort::Type() const
00177 {
00178     // TO IMPROVE
00179     return JACK_DEFAULT_AUDIO_TYPE;
00180 }
00181 
00182 int JackPort::SetName(const char* new_name)
00183 {
00184     char* colon = strchr(fName, ':');
00185     int len = sizeof(fName) - ((int) (colon - fName)) - 2;
00186     snprintf(colon + 1, len, "%s", new_name);
00187     return 0;
00188 }
00189 
00190 void JackPort::Rename(const char* name, int index)
00191 {
00192         SetAlias(GetName());
00193         snprintf(fName, sizeof(fName) - 1, name, index);
00194 }
00195 
00196 bool JackPort::NameEquals(const char* target)
00197 {
00198         return (strcmp(fName, target) == 0 
00199                 || strcmp(fAlias1, target) == 0 
00200                 || strcmp(fAlias2, target) == 0);
00201 }
00202 
00203 int JackPort::GetAliases(char* const aliases[2])
00204 {
00205         int cnt = 0;
00206         
00207         if (fAlias1[0] != '\0') {
00208                 snprintf(aliases[0], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias1);
00209                 cnt++;
00210         }
00211 
00212         if (fAlias2[0] != '\0') {
00213                 snprintf(aliases[1], JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE, "%s", fAlias2);
00214                 cnt++;
00215         }
00216 
00217         return cnt;
00218 }
00219 
00220 int JackPort::SetAlias(const char* alias)
00221 {
00222         if (fAlias1[0] == '\0') {
00223                 snprintf(fAlias1, sizeof(fAlias1), "%s", alias);
00224         } else if (fAlias2[0] == '\0') {
00225                 snprintf(fAlias2, sizeof(fAlias2), "%s", alias);
00226         } else {
00227                 return -1;
00228         }
00229 
00230         return 0;
00231 }
00232 
00233 int JackPort::UnsetAlias(const char* alias)
00234 {
00235         if (strcmp(fAlias1, alias) == 0) {
00236                 fAlias1[0] = '\0';
00237         } else if (strcmp(fAlias2, alias) == 0) {
00238                 fAlias2[0] = '\0';
00239         } else {
00240                 return -1;
00241         }
00242 
00243         return 0;
00244 }
00245 
00246 void JackPort::MixBuffer(float* mixbuffer, float* buffer, jack_nframes_t frames)
00247 {
00248     jack_nframes_t frames_group = frames / 4;
00249     frames = frames % 4;
00250 
00251     while (frames_group > 0) {
00252         register float mixFloat1 = *mixbuffer;
00253         register float sourceFloat1 = *buffer;
00254         register float mixFloat2 = *(mixbuffer + 1);
00255         register float sourceFloat2 = *(buffer + 1);
00256         register float mixFloat3 = *(mixbuffer + 2);
00257         register float sourceFloat3 = *(buffer + 2);
00258         register float mixFloat4 = *(mixbuffer + 3);
00259         register float sourceFloat4 = *(buffer + 3);
00260 
00261         buffer += 4;
00262         frames_group--;
00263 
00264         mixFloat1 += sourceFloat1;
00265         mixFloat2 += sourceFloat2;
00266         mixFloat3 += sourceFloat3;
00267         mixFloat4 += sourceFloat4;
00268 
00269         *mixbuffer = mixFloat1;
00270         *(mixbuffer + 1) = mixFloat2;
00271         *(mixbuffer + 2) = mixFloat3;
00272         *(mixbuffer + 3) = mixFloat4;
00273 
00274         mixbuffer += 4;
00275     }
00276 
00277     while (frames > 0) {
00278         register float mixFloat1 = *mixbuffer;
00279         register float sourceFloat1 = *buffer;
00280         buffer++;
00281         frames--;
00282         mixFloat1 += sourceFloat1;
00283         *mixbuffer = mixFloat1;
00284         mixbuffer++;
00285     }
00286 }
00287 
00288 } // end of namespace

Generated on Mon Oct 15 17:11:14 2007 for Jackdmp by  doxygen 1.5.1