void GUtils::Trace(cppstring msg, gchar * psFile, int nLine) { int nLen = 0; if (psFile != NULL) { #ifdef _UNICODE nLen = wcslen(psFile); #else nLen = strlen(psFile); #endif } if ((nLen > 0) && (nLine != -1) ) { // Only print File and Line if we have something.. cppsstream ss; ss << psFile << GSTD_S("(") << nLine << GSTD_S(") : ") << msg << kOSNewlineString; OSTrace(ss.str().c_str()); } else OSTrace(msg.c_str()); GSTD_LOG(msg); }
/* bool GUtils::IsBetween(real range1, // one end of the range to test real value, // value to see if it's between range1 and range2 real range2) // the other end of the range { // RETURN true if value is between the ranges (inclusive) bool bBetween = false; if (range1 <= range2) bBetween = ((value >= range1) && (value <= range2)); else bBetween = ((value <= range1) && (value >= range2)); return bBetween; } StdIDVector GUtils::ResolveIDVector(StdIDVector *pvIDs) // vector of IDs { // RETURN a vector of column or FunctionModel IDs based on this vector of IDs StdIDVector dataIDVector; if (pvIDs != NULL) { // Get a pointer to our dataworld GDataWorld *pDataWorld = GetAppBrain()->GetDataWorld(); // We need to resolve the pvIDs into a list of GData objects only // (it may contain data sets). We also remove any duplicate GData IDs. StdIDVector dataSetDataVector; StdIDVectorIterator iter = pvIDs->begin(); while(iter != pvIDs->end()) { EStdObjectType type = GMessenger::GetDataWorldChildType(*iter); if (type == kStdObject_DataSet) { dataSetDataVector = pDataWorld->GetObjectsInDataSet(*iter); for (size_t ix=0; ix<dataSetDataVector.size(); ix++) { GStdObject *pObj = dynamic_cast<GStdObject *>(GMessenger::GetDataWorldChildPtr(dataSetDataVector[ix])); if ((pObj != NULL) && ((pObj->GetType() == kStdObject_DataColumn) || (pObj->GetType() == kStdObject_FunctionModel))) // add if we don't find this ID already... if (std::find(dataIDVector.begin(), dataIDVector.end(), *iter) == dataIDVector.end()) dataIDVector.push_back(dataSetDataVector[ix]); } } else if ((type == kStdObject_DataColumn) || (type == kStdObject_FunctionModel)) { // add if we don't find this ID already... if (std::find(dataIDVector.begin(), dataIDVector.end(), *iter) == dataIDVector.end()) dataIDVector.push_back(*iter); } *iter++; } } return dataIDVector; } StdIDVector GUtils::ConvertIDListToVector(const StdIDList * pIDList) // pointer to list of StdIDs { // RETURN a vector of IDs based on this list of IDs StdIDVector IDVector; if (!pIDList || pIDList->size() <= 0) return IDVector; for (StdIDList::const_iterator iTheID = pIDList->begin(); iTheID != pIDList->end(); iTheID++) IDVector.push_back(*iTheID); return IDVector; } StdIDList GUtils::ConvertIDVectorToList(const StdIDVector * pIDVector) // pointer to vector of StdIDs { // RETURN a list of IDs based on this vector of IDs StdIDList IDList; if (!pIDVector || pIDVector->size() <= 0) return IDList; for (size_t ix = 0; ix < pIDVector->size(); ix++) IDList.push_back(pIDVector->at(ix)); return IDList; } // Temp hack #ifdef TARGET_OS_MAC #include <DateTimeUtils.h> char *_strtime(char *pBuffer); char *_strtime(char *pBuffer) { unsigned long nSeconds; GetDateTime(&nSeconds); Str255 sDate; DateString(nSeconds, shortDate, sDate, NULL); Str255 sTime; TimeString(nSeconds, false, sTime, NULL); if ((sTime[0] + sDate[0] > 0) && (sTime[0] + sDate[0] < 126)) { memcpy(pBuffer, &sTime[1], sTime[0]); pBuffer[sTime[0]] = ' '; memcpy(pBuffer + sTime[0] + 1, &sDate[1], sDate[0]); pBuffer[sTime[0] + sDate[0] + 1] = 0; } return pBuffer; } #endif cppstring GUtils::GetCurrentStdDateTimeText(void) { // Return the current date and time in a string gchar text[128]; cppstring sDate; #ifdef _UNICODE #ifndef TARGET_OS_MAC TCHAR timeStr[256]; GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, timeStr, 256); _wstrtime(text); sDate = sDate + GSTD_S(" ") + text; #else _wstrtime(text); sDate = text; sDate.erase(sDate.length()-1, 1); // bad last character #endif #else #ifndef TARGET_OS_MAC TCHAR timeStr[256]; GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, timeStr, 256); sDate = timeStr; _strtime(text); sDate = sDate + GSTD_S(" ") + text; #else _strtime(text); sDate = text; #endif #endif return sDate; } cppstring GUtils::GetCurrentStdTimeText(void) { // Return the current time in a string gchar text[128]; cppstring sTime; #ifdef _UNICODE _wstrtime(text); #else _strtime(text); #endif sTime = text; return sTime; } cppstring GUtils::ConvertIDVectorToString(const StdIDVector &idVect) { // RETURN a string of the size and IDs in the vector cppsstream ss; ss << idVect.size() << GSTD_S(" "); for (size_t ix = 0; ix < idVect.size(); ix++) ss << idVect[ix] << GSTD_S(" "); return ss.str(); } StdIDVector GUtils::ConvertStringToIDVector(const cppstring &sVect) { // RETURN a vector of the ids in the passed-in string StdIDVector idVect; if (!sVect.empty()) { cppsstream ss; ss << sVect; int nSize; ss >> nSize; for (int i=0; i<nSize; i++) { StdID id; ss >> id; idVect.push_back(id); } } return idVect; } cppstring GUtils::ConvertIDListToString(const StdIDList &idList) { // RETURN a string of the size and IDs in the list StdIDVector idVect = GUtils::ConvertIDListToVector(&idList); return GUtils::ConvertIDVectorToString(idVect); } StdIDList GUtils::ConvertStringToIDList(const cppstring &sList) { // RETURN a vector of the ids in the passed-in string StdIDVector idVect = GUtils::ConvertStringToIDVector(sList); return GUtils::ConvertIDVectorToList(&idVect); } cppstring GUtils::ConvertIDPairsToString(const SStdIDPairVector &vIDPairs) { cppsstream ss; ss << vIDPairs.size() << " "; for (size_t ix = 0; ix < vIDPairs.size(); ix++) ss << vIDPairs[ix].nBaseID << " " << vIDPairs[ix].nTraceID << " "; return ss.str(); } SStdIDPairVector GUtils::ConvertStringToIDPairs(const cppstring &sVect) { // RETURN a vector of the id pairs in the passed-in string SStdIDPairVector vIDPairs; if (!sVect.empty()) { cppsstream ss; ss << sVect; int nSize; ss >> nSize; for (int i=0; i<nSize; i++) { SStdIDPair idPair; ss >> idPair.nBaseID; ss >> idPair.nTraceID; vIDPairs.push_back(idPair); } } return vIDPairs; } StdIDVector GUtils::GetUniqueIDs(const StdIDVector &vIDs) // input vector with (possible) duplicate IDs { // Return a vector of just the unique IDs from vIDs i.e Remove duplicates StdIDVector goodIDs; for (size_t kx=0; kx < vIDs.size(); kx++) if (std::find(goodIDs.begin(), goodIDs.end(), vIDs[kx]) == goodIDs.end()) goodIDs.push_back(vIDs[kx]); return goodIDs; } StringVector GUtils::GetUniqueStrings(const StringVector &vs) // input vector with (possible) duplicate Strings { // Return a vector of just the unique strings i.e Remove duplicates StringVector vsGood; for (size_t ix = 0; ix < vs.size(); ix++) { bool bFound = false; for (size_t jx = 0; jx < vsGood.size(); jx++) if (vs[ix] == vsGood[jx]) { bFound = true; break; } if (!bFound) vsGood.push_back(vs[ix]); } return vsGood; } StringVector GUtils::GetNewStrings(const StringVector &oldVec, const StringVector &newVec) { // Return a vector of strings that are in newVec, but not in oldVec. StringVector vsDelta; for (size_t ix = 0; ix < newVec.size(); ix++) { bool bFound = false; for (size_t jx = 0; jx < oldVec.size(); jx++) if (newVec[ix] == oldVec[jx]) { bFound = true; break; } if (!bFound) vsDelta.push_back(newVec[ix]); } return vsDelta; } StringVector GUtils::GetOldStrings(const StringVector &oldVec, const StringVector &newVec) { // Return a vector of strings that are in oldVec, but not in newVec. return GetNewStrings(newVec, oldVec); } StdIDList GUtils::GetPrunedIDList(StdIDList theList) { // Remove any dead and duplicate IDs from this list // Remove dead IDs StdIDListIterator iter = theList.begin(); while (iter != theList.end()) { GStdObject *pObj = GMessenger::GetStdObjectPtr(*iter); if (pObj == NULL) iter = theList.erase(iter); // item is dead, remove from list else iter++; } // Remove duplicates theList.sort(); theList.unique(); return theList; } StdIDVector GUtils::GetPrunedIDVector(StdIDVector *pvIDs) { // Remove any dead and duplicate IDs from this vector StdIDList theList = GUtils::GetPrunedIDList(ConvertIDVectorToList(pvIDs)); return GUtils::ConvertIDListToVector(&theList); } // Curve fitting helper functions // The following list contains all of the resources used for the curve fitting functions // LinearFit:: " An Introduction to Error Analysis", pages 156-157 - correlation is from page 180 of same book long GUtils::CalculateLinearFit(SRealPointVector const &vPoints, // vector of points to fit line to real *pfSlope, // [out] slope of linear fit real *pfIntercept, // [out] intercept of linear fit line real *pfCorrelation, // [out] correlation of line to data; if NULL don't fill in real *pfStdDevSlope, // [out] standard deviation of slope (uncertainity) real *pfStdDevIntercept) // [out] standard deviation of intercept (uncertainity) { // RETURN kResponse_OK if fit went well, ID of error string otherwise // zero out values in case calc fails int i = 0; *pfSlope = 0.0; *pfIntercept = 0.0; if (pfCorrelation != NULL) *pfCorrelation = 0.0; int nNumPoints = vPoints.size(); // REVIST error condition if (nNumPoints < 2) return GSTD_INDEX(IDSX_NOT_ENOUGH_POINTS); else if (nNumPoints == 2) { // check for vertical line (error) if (vPoints[0].x == vPoints[1].x) return GSTD_INDEX(IDSX_VERTICAL_LINE); *pfSlope = (vPoints[1].y - vPoints[0].y) / (vPoints[1].x - vPoints[0].x); *pfIntercept = - *pfSlope * vPoints[0].x + vPoints[0].y; if (pfCorrelation != NULL) *pfCorrelation = 1.0; if (pfStdDevSlope != NULL) *pfStdDevSlope = 0.0; if (pfStdDevIntercept != NULL) *pfStdDevIntercept = 0.0; } else { real sumX = 0; // Sum of 1..N of x values real sumY = 0; // Sum of 1..N of y values real sumXSqr = 0; // Sum of 1..N of x*x real sumXY = 0; // Sum of 1..N of x*y real cur_x = 0; real cur_y = 0; real meanx = 0; // average of x values real meany = 0; // average of y values // first I calculate the necessary sums: sum(x) sum (x*x) sum (y) sum (y*y) and sum (x*y) // from these I then calculate the specific values for intercept, slope etc. // Use the following equations (from book mentioned above) // delta = nNumPoints* ( sumXSqr) - (sumX)*sumX) // intercept = (sumXSqr*sumY - sumX*sumXY)/delta // slope = (nNumPoints*sumXY - sumX*sumY)/delta // Correlation coefficeint is // // SUM( ( xi -meanx)(yi-meany))/ sqrt( SUM((xi -meanx)*(xi-meanx))* SUM((yi-meany)*(yi-meany)) ) for (i=0; i<nNumPoints; i++) { cur_x = vPoints[i].x; cur_y = vPoints[i].y; sumX += cur_x; sumY += cur_y; sumXSqr += cur_x * cur_x; sumXY += cur_x * cur_y; } meanx = sumX/nNumPoints; meany = sumY/nNumPoints; double delta = nNumPoints * sumXSqr - sumX*sumX; // handle strange case if delta is zero... // REVISIT error handling if (delta == 0.0) return GSTD_INDEX(IDSX_VERTICAL_LINE); *pfSlope = (nNumPoints*sumXY - sumX*sumY)/delta; *pfIntercept = (sumXSqr*sumY - sumX*sumXY)/delta; if ((pfCorrelation != NULL) || (pfStdDevSlope != NULL) || (pfStdDevIntercept != NULL)) { double sumXYmean = 0; // Sum (xi- meanx)*(yi -y) double sumXmeanXSqr = 0; // Sum (xi -meanx)*(xi-meanx); double sumYmeanYSqr = 0; // Sum (yi-meany)*(yi-meany); for (i=0; i<nNumPoints; i ++) { sumXYmean += (vPoints[i].x - meanx)*(vPoints[i].y-meany); sumXmeanXSqr += (vPoints[i].x - meanx)*(vPoints[i].x - meanx); sumYmeanYSqr += (vPoints[i].y - meany)*(vPoints[i].y - meany); } if (pfCorrelation != NULL) { // compute correlation real denominator = sumXmeanXSqr * sumYmeanYSqr; if (denominator == 0.0) *pfCorrelation = 0.0; // double check for 0 denominator else *pfCorrelation = sumXYmean/ sqrt(denominator); } if ((pfStdDevSlope != NULL) || (pfStdDevIntercept != NULL)) { real fSumXSqr = 0; for (i=0; i<nNumPoints; i++) if (GUtils::OSIsValidNumber(vPoints[i].x)) fSumXSqr += vPoints[i].x * vPoints[i].x; real fSumX = 0; for (i=0; i<nNumPoints; i++) if (GUtils::OSIsValidNumber(vPoints[i].x)) fSumX += vPoints[i].x; real fSumY = 0; for (i=0; i<nNumPoints; i++) if (GUtils::OSIsValidNumber(vPoints[i].y)) fSumY += vPoints[i].y; real fSumXY = 0; for (i=0; i<nNumPoints; i++) if (GUtils::OSIsValidNumber(vPoints[i].x) && GUtils::OSIsValidNumber(vPoints[i].y)) fSumXY += vPoints[i].x * vPoints[i].y; real fDelta = nNumPoints * fSumXSqr - (fSumX * fSumX); if (fDelta == 0.0) *pfStdDevSlope = *pfStdDevIntercept = 0.0; // error case just set to 0 and don;t do anything else else { // fDelta is OK, keep going real fB = (fSumXSqr * fSumY - fSumX * fSumXY) / fDelta; real fM = (nNumPoints * fSumXY - fSumX * fSumY) / fDelta; real fSigmaYSqr = 0; for (i=0; i<nNumPoints; i++) if (GUtils::OSIsValidNumber(vPoints[i].x) && GUtils::OSIsValidNumber(vPoints[i].y)) { real fTemp = (vPoints[i].y - fB -fM * vPoints[i].x); fSigmaYSqr += fTemp * fTemp; } fSigmaYSqr = fSigmaYSqr / (nNumPoints - 2); if (pfStdDevSlope != NULL) *pfStdDevSlope = sqrt(nNumPoints * fSigmaYSqr / fDelta); if (pfStdDevIntercept != NULL) *pfStdDevIntercept = sqrt(fSigmaYSqr * fSumXSqr / fDelta); } } } } return kResponse_OK; } long GUtils::CalculateLinearFit(realvector const & vX, // x Values realvector const & vY, // y Values real *pfSlope, // [out] slope of linear fit real *pfIntercept, // [out] intercept of linear fit line real *pfCorrelation, // [out] correlation of line to data; if NULL don't fill in real *pfStdDevSlope, // [out] standard deviation of slope (uncertainity) real *pfStdDevIntercept) // [out] standard deviation of intercept (uncertainity) { // RETURN kResponse_OK if fit went well, ID of error string otherwise // REVISIT optimize (by copying code not creating new point array) size_t nMin = min(vX.size(), vY.size()); SRealPointVector vPoints(nMin); for (size_t i=0; i<nMin; i++) { vPoints[i].x = vX[i]; vPoints[i].y = vY[i]; } return GUtils::CalculateLinearFit(vPoints, pfSlope, pfIntercept, pfCorrelation, pfStdDevSlope, pfStdDevIntercept); } bool GUtils::IsMonotonic(SRealPointVector const &vPoints) { // RETURN true if x component of points is monotonic (not both increasing and decreasing) bool bMonotonic = true; if (vPoints.size() > 1) { bool bNotDecreasing = (vPoints[1].x >= vPoints[0].x); bool bNotIncreasing = (vPoints[1].x <= vPoints[0].x); for (size_t ix = 2; ix < vPoints.size(); ix++) { if ((vPoints[ix].x > vPoints[ix-1].x) && bNotIncreasing) { bMonotonic = false; break; } if ((vPoints[ix].x < vPoints[ix-1].x) && bNotDecreasing) { bMonotonic = false; break; } } } return bMonotonic; } bool GUtils::IsMonotonic(realvector const &vReal) { // RETURN true if points are monotonic (i.e. same or increasing or same or decreasing) bool bMonotonic = true; if (vReal.size() > 1) { bool bNotDecreasing = (vReal[1] >= vReal[0]); bool bNotIncreasing = (vReal[1] <= vReal[0]); for (size_t ix = 2; ix < vReal.size(); ix++) { if ((vReal[ix] > vReal[ix-1]) && bNotIncreasing) { bMonotonic = false; break; } if ((vReal[ix] < vReal[ix-1]) && bNotDecreasing) { bMonotonic = false; break; } } } return bMonotonic; } //#include <DriverServices.h> unsigned long GUtils::TraceClock(void) { cppsstream ss; unsigned long nClock = (unsigned long)clock(); ss << " clock: " << nClock << kOSNewlineChar; GUtils::Trace(ss.str()); return nClock; } short GUtils::MakeDataChecksum(unsigned char * pBuffer, // pointer to nSize unsigned bytes unsigned short nSize) // number of bytes to sum { // RETURN the 2 byte checksum of nSize 1 byte (unsigned) values unsigned short nSum = 0; for (unsigned short i = 0; i < nSize; i++) nSum += pBuffer[i]; return nSum; } */ void GUtils::Trace(void * pointer, gchar * psFile, int nLine) { cppsstream ss; ss << pointer; Trace(ss.str(),psFile,nLine); GSTD_LOG(ss.str()); }
int GSkipBaseDevice::OSOpen(GPortRef * /*pPortRef*/) { int nResult = kResponse_Error; if (LockDevice(1) && IsOKToUse()) { TUSBBulkDevice usbDevice = static_cast<TUSBBulkDevice>(GetOSData()); // port may be NULL if initialization failed if (usbDevice == NULL) return kResponse_Error; int err = VST_OpenUSBPortForIO((VST_USBBulkDevice *)usbDevice); if (err == (int)kCFM_IOExclusiveAccess) { // If another app (including Classic) has the device we // can simply retry a few times. The driver should be able to get a lock // on the device at some point. int nTry = 0; while ((err == (int)kCFM_IOExclusiveAccess) && (nTry < 4)) { err = VST_OpenUSBPortForIO((VST_USBBulkDevice *)usbDevice); nTry++; } } if (err == 0) nResult = kResponse_OK; else { cppsstream ssErr; ssErr << "GUSBDirectTempDevice:OSOpen -- Error: " << err; GSTD_LOG(ssErr.str()); } UnlockDevice(); } else GSTD_ASSERT(0); return nResult; }