Пример #1
0
bool PortAudioDeviceDriver::getSound(yarp::sig::Sound& sound)
{
    if (pThread.something_to_record == false)
    {
        this->startRecording();
    }

    int buff_size = 0;
    static int buff_size_wdt = 0;
    while (buff_size<this->numSamples*this->numChannels)
    {
         buff_size = dataBuffers.recData->size();
         if (buff_size == 0 && buff_size_wdt++ == 100) break;
         yarp::os::SystemClock::delaySystem(SLEEP_TIME);
    }
    buff_size_wdt = 0;

    if (sound.getChannels()!=this->numChannels && sound.getSamples() != this->numSamples)
    {
        sound.resize(this->numSamples,this->numChannels);
    }
    sound.setFrequency(this->driverConfig.rate);

    for (int i=0; i<this->numSamples; i++)
        for (int j=0; j<this->numChannels; j++)
            {
                SAMPLE s = dataBuffers.recData->read();
                sound.set(s,i,j);
            }
    return true;
}
bool MicrophoneDeviceDriver::getSound(yarp::sig::Sound& sound) {
    // main loop to capture the waveform audio data
    unsigned char *data = pulsecode;
    int begun = 1;
    int bytes_to_grab = NUM_SAMPLES / 16;
    int bytes_to_save = NUM_SAMPLES;
    while ( bytes_to_save > 0 )
        {
            int nbytes = read( dsp, data, bytes_to_grab );
            if ( !begun )
                {
                    int max = 0;
                    for (int i = 0; i < nbytes; i++)
                        if ( data[i] > max ) max = data[i];
                    if ( max > 0x99 ) begun = 1;
                    else continue;
                }
            bytes_to_save -= nbytes;
            data += nbytes;
        }
    sound.resize(NUM_SAMPLES);
    double total = 0;
    for (int i=0; i<NUM_SAMPLES; i++) {
        sound.set(pulsecode[i],i);
        total += abs(pulsecode[i]-128);
    }
    printf( "grabbing a chunk of %d samples... magnitude: %g\n",
            NUM_SAMPLES, total);
    return true;
}