Ejemplo n.º 1
0
bool AndroidAccessory::configureAndroid(void)
{
    byte err;
    EP_RECORD inEp, outEp;

    if (!findEndpoints(1, &inEp, &outEp))
        return false;

    memset(&epRecord, 0x0, sizeof(epRecord));

    epRecord[inEp.epAddr] = inEp;
    if (outEp.epAddr != inEp.epAddr)
        epRecord[outEp.epAddr] = outEp;

    in = inEp.epAddr;
    out = outEp.epAddr;

    Serial.println(inEp.epAddr, HEX);
    Serial.println(outEp.epAddr, HEX);

    epRecord[0] = *(usb.getDevTableEntry(0,0));
    usb.setDevTableEntry(1, epRecord);

    err = usb.setConf( 1, 0, 1 );
    if (err) {
        Serial.print("Can't set config to 1\n");
        return false;
    }

    usb.setUsbTaskState( USB_STATE_RUNNING );

    return true;
}
/* Compare the average magnitude spectra of the signals in pcm and
   refPcm, which are of length numSamples and numRefSamples,
   respectively; both sampled at sample_rate.  The maximum deviation
   between average spectra, expressed in dB, is returned in
   maxDeviation, and the rms of all dB variations is returned in
   rmsDeviation.  Note that a lower limit is set on the frequencies that
   are compared so as to ignore irrelevant DC and rumble components.  If
   the measurement fails for some reason, return 0; else return 1, for
   success.  Causes for failure include the amplitude of one or both of
   the signals being too low, or the duration of the signals being too
   short.

   Note that the expected signal collection scenario is that the phone
   would be stimulated with a broadband signal as in a recognition
   attempt, so that there will be some "silence" regions at the start and
   end of the pcm signals.  The preferred stimulus would be pink noise,
   but any broadband signal should work. */
int compareSpectra(short* pcm, int numSamples, short* refPcm,
                   int numRefSamples, float sample_rate,
                   float* maxDeviation, float* rmsDeviation) {
    int fftSize = 512;           // must be a power of 2
    float lowestFreq = 100.0;    // don't count DC, room rumble, etc.
    float highestFreq = 3500.0;  // ignore most effects of sloppy anti alias filters.
    int lowestBin = int(0.5 + (lowestFreq * fftSize / sample_rate));
    int highestBin = int(0.5 + (highestFreq * fftSize / sample_rate));
    float signalOnRms = 1000.0; // endpointer RMS on/off threshold
    int endpointStepSize = int(0.5 + (sample_rate * 0.02)); // 20ms setp
    float rms1 = 0.0;
    float rms2 = 0.0;
    int onset = 0;
    int offset = 0;
    findEndpoints(refPcm, numRefSamples, endpointStepSize, signalOnRms,
                   &onset, &offset);
    double* spect1 = getAverageSpectrum(refPcm + onset, offset - onset,
                                        fftSize, lowestBin, highestBin, &rms1);
    findEndpoints(pcm, numSamples, endpointStepSize, signalOnRms,
                  &onset, &offset);
    double* spect2 = getAverageSpectrum(pcm + onset, offset - onset,
                                        fftSize, lowestBin, highestBin, &rms2);
    int magSize = 1 + (fftSize/2);
    if ((rms1 <= 0.0) || (rms2 <= 0.0))
        return 0; // failure because one or both signals are too short or
                  // too low in amplitude.
    float rmsNorm = rms2 / rms1; // compensate for overall gain differences
    // fprintf(stderr, "Level normalization: %f dB\n", 20.0 * log10(rmsNorm));
    *maxDeviation = 0.0;
    float sumDevSquared = 0.0;
    for (int i = lowestBin; i <= highestBin; ++i) {
        double val = 1.0;
        if ((spect1[i] > 0.0) && (spect2[i] > 0.0)) {
            val = 20.0 * log10(rmsNorm * spect1[i] / spect2[i]);
        }
        sumDevSquared += val * val;
        if (fabs(val) > fabs(*maxDeviation)) {
            *maxDeviation = val;
        }
        // fprintf(stderr, "%d %f\n", i, val);
    }
    *rmsDeviation = sqrt(sumDevSquared / (highestBin - lowestBin + 1));
    delete [] spect1;
    delete [] spect2;
    return 1; // success
}