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
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 template<class SAMPLE> class FilterConnection : private Connection<SAMPLE> {
00025 public:
00026 void connectFrom(Connection<SAMPLE> &conn) {
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) {
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() {
00041
00042 if (!writeReady()) {
00043 return;
00044 }
00045 int i =0;
00046 int c = 0;
00047 int size = inConnections.size();
00048 SAMPLE * b2 = buffer2;
00049 SAMPLE * b1 = buffer;
00050
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
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
00065 b1[i]+=(b2[i])/size;
00066
00067 }
00068 }
00069 }
00070 }
00071 if (c > 0) {
00072 write(b1,BUFFSIZE);
00073 }
00074 }
00075
00076
00077 void disconnectFrom(Connection<SAMPLE> *conn) {
00078
00079
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){
00089 conn->disconnectFrom(this);
00090 }
00091
00092
00093
00094
00095 virtual string xmlType() {
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
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
00141 if (!inConnections.empty()) {
00142 disconnect();
00143 }
00144
00145 delete [] buffer;
00146
00147
00148
00149
00150
00151
00155
00156
00157
00158
00163
00164 }
00165 protected:
00166 vector<Connection<SAMPLE>*> inConnections;
00167 SAMPLE * buffer;
00168 SAMPLE * buffer2;
00169 SAMPLE * buffer3;
00170 void initBuffer() {
00171
00172 buffer = new SAMPLE[3*BUFFSIZE];
00173
00174 buffer2 = buffer+BUFFSIZE;
00175 buffer3= buffer2+BUFFSIZE;
00176 memset(buffer,0,BUFFSIZE);
00177 memset(buffer2,0,BUFFSIZE);
00178 memset(buffer3,0,BUFFSIZE);
00179
00180
00181
00182 lastClock = 0;
00183 }
00184
00185 virtual void disconnect() {
00186
00187
00188
00189
00190 inConnections.clear();
00191 this->Connection<SAMPLE>::disconnect();
00192 }
00193 private:
00194 };
00195 #endif