void NetworkTCPSource::myProcess( realvec& in, realvec& out ) { mrs_natural inSamples = getctrl("mrs_natural/inSamples")->to<mrs_natural>(); mrs_natural inObservations = getctrl("mrs_natural/inObservations")->to<mrs_natural>(); //checkFlow(in,out); if (!valid( s_sock)) { refresh(); } int status = NetworkSocket::recvTCP( out ); if ( status < 0 ) { throw SocketException ( "Cannot read from socket." ); } else if ( status == 0 ) { throw SocketException ( "Client closed connection." ); } if ( byteSwap ) { int i = 0; mrs_real* data = out.getData(); for ( i = 0; i < (inObservations * inSamples); i++ ) { mrs_real tmp = swap( data[i] ); data[i] = tmp; } } }
/** * Function: sendTCP * Description: Sends a vector to a TCP socket connection. In this function, * we can send the controls as a seperate packet because with * TCP we know the next packet will be a vector. */ bool NetworkSocket::sendTCP ( realvec& in, mrs_natural inSamples, mrs_natural inObservations, mrs_real israte ) { // for TCP we can send the controls seperately mrs_real ctrl_send[4]; ctrl_send[0] = 1.0; // for endien check ctrl_send[1] = (mrs_real) inSamples; ctrl_send[2] = (mrs_real) inObservations; ctrl_send[3] = israte; if ( ::send(c_sock, ctrl_send, sizeof( mrs_real )*4, 0 ) <= 0 ) { MRSERR("sendTCP: Could not send TCP control data."); return false; } // vector data... if ( ::send ( s_sock, in.getData(), sizeof(mrs_real) * (inSamples*inObservations), 0 ) <= 0 ) { MRSERR("sendTCP: Could not send TCP vector data."); return false; } return true; }
/** * Function: sendUDP * Description: Sends a vector to a UDP socket connection. In this function, * the controls are included in the realvec as the first three * elements. */ bool NetworkSocket::sendUDP(realvec& in, mrs_natural inSamples, mrs_natural inObservations, mrs_real israte ) { // create buffer of proper size to include control data... mrs_real* buffer = new mrs_real[inSamples + 3]; buffer[0] = ( mrs_real ) inSamples; buffer[1] = ( mrs_real ) inObservations; buffer[2] = israte; // now fill the buffer with samples... mrs_real* data = in.getData(); int i; for ( i = 3; i < inSamples + 3; i++ ) { buffer[i] = *(data + i - 3); } if ( ::sendto ( s_sock, buffer, sizeof(mrs_real) * (inSamples+3), 0, (struct sockaddr*)&m_addr, sizeof(m_addr) ) <= 0) { MRSERR("Could not send vector over UDP socket."); delete buffer; return false; } delete buffer; return true; }
void MATLABengine::putVariable(realvec value, mrs_string MATLABname) { //---------------------------------- // send a realvec to a MATLAB matrix //---------------------------------- mwSize dims[2]; //realvec is 2D dims[0] = value.getRows(); dims[1] = value.getCols(); //realvec are by default double precision matrices => mxDOUBLE_CLASS mxArray *mxMatrix = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); mrs_real *data = value.getData(); memcpy((void *)mxGetPr(mxMatrix), (void *)(data), dims[0]*dims[1]*mxGetElementSize(mxMatrix)); engPutVariable(engine_, MATLABname.c_str(), mxMatrix); mxDestroyArray(mxMatrix); }
/** * Function: recvUDP * Description: Receive a vector from a peer on a UDP socket connection. */ int NetworkSocket::recvUDP ( realvec& out ) { int status = 0; // assume a size 512 vector mrs_natural inSamples = getctrl("mrs_natural/inSamples")->to<mrs_natural>(); mrs_natural inObservations = getctrl("mrs_natural/inObservations")->to<mrs_natural>(); mrs_real* buffer = new mrs_real[(inSamples*inObservations)+3]; int cliLen = sizeof(cliAddr); #ifndef MARSYAS_CYGWIN status = ::recvfrom(s_sock, buffer, sizeof(mrs_real) * ((inSamples*inObservations)+3), MSG_WAITALL, (struct sockaddr *) &cliAddr, (socklen_t *)&cliLen); #else status = ::recvfrom(s_sock, buffer, sizeof(mrs_real) * ((inSamples*inObservations)+3), MSG_NOSIGNAL, (struct sockaddr *) &cliAddr, (socklen_t *)&cliLen); #endif if ( status <= 0 ) { MRSERR("Could not receive UDP data..."); return 0; } else { updctrl("mrs_natural/inSamples", (mrs_natural) buffer[0]); updctrl("mrs_natural/inObservations",(mrs_natural) buffer[1]); updctrl("mrs_real/israte", buffer[2]); } // process the data to an output vector... out.create( inObservations, inSamples ); mrs_real* data = out.getData(); int i; for ( i = 0; i < 512; i++ ) { // *( data + i ) = buffer[i+3]; } // delete [] buffer; return status; }
/** * Function: recvTCP * Description: Receive a vector from a peer on a TCP socket connection. */ int NetworkSocket::recvTCP ( realvec& out ) { // create our slice with updated controls mrs_natural inSamples = getctrl("mrs_natural/inSamples")->to<mrs_natural>(); mrs_natural inObservations = getctrl("mrs_natural/inObservations")->to<mrs_natural>(); out.create(inObservations, inSamples); mrs_real* data = out.getData(); #ifndef MARSYAS_CYGWIN return ::recv ( client_d, data, sizeof(mrs_real)*(inObservations*inSamples), MSG_WAITALL ); #else return ::recv ( client_d, data, sizeof(mrs_real)*(inObservations*inSamples), MSG_NOSIGNAL); #endif }
void Yin::myProcess(realvec& in, realvec& out) { // The tolerance for the yin algorithm const mrs_real tol = ctrl_tolerance_->to<mrs_real>(); // get pointers to avoid excessive (long,long) lookups mrs_real *yin_buffer = yin_buffer_realvec_.getData(); const mrs_natural yin_buffer_size = yin_buffer_realvec_.getSize(); // ASSUME: only one channel mrs_real *input = in.getData(); mrs_real pitch = -1.0; // cout << "yin.getSize()=" << yin.getSize() << endl; // cout << "tol=" << tol << endl; const mrs_real freq_max = ctrl_frequency_max_->to<mrs_real>(); const mrs_real freq_min = ctrl_frequency_min_->to<mrs_real>(); // yes, low_sample comes from the highest pitch mrs_natural low_sample = 4; if (freq_max > 0) { low_sample = israte_ / freq_max; } mrs_natural high_sample = yin_buffer_size; if (freq_min > 0) { high_sample = israte_ / freq_min; } // Calculate the pitch with the Yin method //mrs_natural c=0; mrs_real cmndf = 0.; // cumulative mean normalized difference function std::fill(yin_buffer, yin_buffer + yin_buffer_size, 0.0); yin_buffer[0] = 1.; //for (mrs_natural tau=1; tau < yin_size_; tau++) for (mrs_natural tau=1; tau < high_sample; tau++) { // d_t( tau ) for (mrs_natural j=0; j < yin_buffer_size;j++) { const mrs_real delta = input[j] - input[j+tau]; yin_buffer[tau] += delta * delta; } cmndf += yin_buffer[tau]; yin_buffer[tau] *= tau / cmndf; if (tau > low_sample) { const mrs_natural period = tau-3; //if(tau > 4 && (yin_buffer_(c,period) < tol) && // (yin_buffer_(c,period) < yin_buffer_(c,period+1))) if((yin_buffer[period] < tol) && (yin_buffer[period] < yin_buffer[period+1])) { pitch = vec_quadint_min(&yin_buffer_realvec_,period,1); break; } } } if (pitch < 0) { pitch = vec_quadint_min(&yin_buffer_realvec_, vec_min_elem(&yin_buffer_realvec_),1); } if (pitch !=0) out(0,0) = ctrl_osrate_/pitch; else out(0,0) = 0.0; }