Main Page   Class Hierarchy   File List  

FilterConnection.h

00001 #ifndef FILTERCONNECTION_H
00002 #define FILTERCONNECTION_H
00003 #include <stdio.h>
00004 #include <string>
00005 #include <vector>
00006 #include "Clock.h"
00007 #include "Connection.h"
00008 #define BUFFSIZE 256 //WE SHOULD HAVE A BUFFER MAKER
00009 
00010 /* 
00011  * 5.1.1.2 FilterConnection Interface
00012  * Inherits from Connection.
00013 
00014     * connectFrom(conn:Connection) Accepts a connection FROM conn connection. 
00015     * The current filterConnection accepts the connection as an input to the connection.
00016     * process() A step taken which will read in data and output data, fills and dumps 
00017     * one buffer. Must be called to move audio.
00018     * disconnectFrom(conn:Connection) Disconnects an incoming patch between the current 
00019     * connection and "conn" connection.
00020     * disconnectTo(conn:Connection)Disconnects an outgoing patch between current
00021     * connection and "conn" connection. 
00022     *
00023 */
00024 template<class SAMPLE> class FilterConnection : private Connection<SAMPLE> {
00025         public: 
00026                 void connectFrom(Connection<SAMPLE> &conn) { // Accepts a connection FROM conn connection. 
00027                         if (conn.readable()) {
00028                                 inConnections.push_back(&conn);
00029                         } else {
00030                                 throw new string("Not A Input Connection");
00031                         }
00032                 }
00033                 void connectFrom(Connection<SAMPLE> *conn) { // Accepts a connection FROM conn connection. 
00034                         if (conn->readable()) {
00035                                 inConnections.push_back(conn);
00036                         } else {
00037                                 throw new string("Not A Input Connection");
00038                         }
00039                 }
00040                 virtual void process() { // A step taken which will read in data and output data, 
00041                         //if (!writeReady() && !file->ready(BUFFER_WRITE)) {
00042                         if (!writeReady()) {
00043                                 return;
00044                         }
00045                         int i =0;               // fills and dumps one buffer. Must be called to move audio.
00046                         int c = 0;
00047                         int size = inConnections.size();
00048                         SAMPLE * b2 = buffer2;
00049                         SAMPLE * b1 = buffer;
00050                         //bzero(b1,sizeof(SAMPLE)*BUFFSIZE);
00051                         vector<Connection<SAMPLE>*>::iterator iter;
00052                         for (i = 0; i < BUFFSIZE; i++ ) {
00053                                 b1[i] = 0;
00054                         }
00055                         for (iter = inConnections.begin(); iter < inConnections.end(); iter++) {
00056                                 if ((*iter)->readReady()) {
00057                                         //int r = (*iter)->read(b2,BUFFSIZE);
00058                                         int r = (*iter)->read(b2,BUFFSIZE);
00059                                         if (r == -1) {
00060                                                 disconnectFrom(*iter);
00061                                         } else {
00062                                                 c++;
00063                                                 for (i = 0; i < BUFFSIZE; i++ ) {
00064                                                 //for (i = 0; i < r; i++ ) {
00065                                                         b1[i]+=(b2[i])/size;
00066                                                         //b1[i]=(b2[i]);
00067                                                 }
00068                                         }
00069                                 }
00070                         }
00071                         if (c > 0) {
00072                                 write(b1,BUFFSIZE);
00073                         }
00074                 }
00075                                 
00076 
00077                 void disconnectFrom(Connection<SAMPLE> *conn) {
00078                                                                 //Disconnects an incoming patch between the current 
00079                                                                 //connection and "conn" connection.
00080                         vector<Connection<SAMPLE>*>::iterator iter;
00081                         for (iter = inConnections.begin(); iter < inConnections.end(); iter++) {
00082                                 if (*iter == conn) {
00083                                         inConnections.erase(iter);
00084                                         break;
00085                                 }
00086                         }
00087                 }
00088                 void disconnectTo(FilterConnection<SAMPLE> *conn){//Disconnects an outgoing patch between current
00089                         conn->disconnectFrom(this);
00090                 }
00091                                       //connection and "conn" connection. 
00092                 //virtual ~FilterConnection() {
00093                 //      destruct();
00094                 //}     
00095                 virtual string xmlType()  { //FILTER/INPUT/OUTPUT
00096                         return "FILTER";
00097                 }
00098                 string * getXML() {
00099                         char number[12];
00100                         char number2[12];
00101                         snprintf(number,12,"%d",this->getFileHandle());
00102                         string * out = new string("<connection><type>");
00103                         (*out)+=xmlType();
00104                         (*out)+="</type><name>";
00105                         (*out)+=getName();
00106                         (*out)+="</name><id>";
00107                         (*out)+=string(number);
00108                         //(*out)+=getFileHandle();
00109                         (*out)+="</id></connection>\n";
00110                         vector<Connection<SAMPLE>*>::iterator iter;
00111                         for (iter = inConnections.begin(); iter < inConnections.end(); iter++) {
00112                                 snprintf(number2,12,"%d",(*iter)->getFileHandle());
00113                                 (*out)+="<patch><from>";
00114                                 (*out)+=string(number2);
00115                                 (*out)+="</from><to>";
00116                                 (*out)+=string(number);
00117                                 (*out)+="</to></patch>";
00118                         }
00119                         return out;
00120                 }
00121                 int lastClock;
00122                 virtual int read(SAMPLE * data, int size) {
00123                         if (lastClock!=Clock::clock) {
00124                                 Connection<SAMPLE>::read(buffer3,size);
00125                                 lastClock=Clock::clock;
00126                         }
00127                         memcpy((void*)data,buffer3,sizeof(SAMPLE)*size);
00128                         return size;
00129                 }
00130 
00131                 FilterConnection(BufferedFile * fd): inConnections() {
00132                         init(fd,"Unnamed");
00133                         initBuffer();
00134                 }
00135                 FilterConnection(BufferedFile * fd,string name): inConnections() {
00136                         init(fd,name);
00137                         initBuffer();
00138                 }
00139                 virtual ~FilterConnection() {
00140                         //cout << "Start ~FilterConnection()\n";
00141                         if (!inConnections.empty()) {
00142                                 disconnect();
00143                         }
00144                         //free(buffer);
00145                         delete [] buffer;
00146                         //free(buffer2);
00147                         //free(buffer3);
00148                         //delete [] buffer;
00149                         //delete [] buffer2;
00150                         //delete [] buffer3;
00151                         //delete [] buffer;
00155                         //if (buffer !=NULL)  { delete [] buffer; }
00156                         //if (buffer2!=NULL)  { delete [] buffer2; }
00157                         //if (buffer3!=NULL)  { delete [] buffer3; }
00158                         //buffer = buffer2 = buffer3 = NULL;
00163                         //cout << "Finish ~FilterConnection()\n";
00164                 }
00165         protected: 
00166                 vector<Connection<SAMPLE>*>  inConnections;
00167                 SAMPLE  * buffer;
00168                 SAMPLE  * buffer2;
00169                 SAMPLE  * buffer3;
00170                 void initBuffer() {
00171                         //Malloc bad but we have to
00172                         buffer =  new SAMPLE[3*BUFFSIZE];// (SAMPLE*)malloc(sizeof(SAMPLE)*3*BUFFSIZE);
00173                         //buffer =  (SAMPLE*)malloc(sizeof(SAMPLE)*3*BUFFSIZE);
00174                         buffer2 = buffer+BUFFSIZE; //hack? What's wrong?
00175                         buffer3=  buffer2+BUFFSIZE;
00176                         memset(buffer,0,BUFFSIZE);
00177                         memset(buffer2,0,BUFFSIZE);
00178                         memset(buffer3,0,BUFFSIZE);
00179                         //buffer2 = new SAMPLE[BUFFSIZE];
00180                         //buffer3 = new SAMPLE[BUFFSIZE];
00181                         //inConnections = new vector<Connection<SAMPLE>*>();
00182                         lastClock = 0;
00183                 }
00184                 //virtual void destruct() {
00185                 virtual void   disconnect() {
00186                         //vector<Connection<SAMPLE>*>::iterator iter;
00187                         //for (iter = inConnections->begin(); iter < inConnections->end(); iter++) {
00188                         //      disconnectFrom(*iter);
00189                         //}
00190                         inConnections.clear();
00191                         this->Connection<SAMPLE>::disconnect();
00192                 }
00193         private:
00194 };
00195 #endif

Generated on Sat Dec 7 21:28:09 2002 by doxygen1.2.18