Пример #1
0
bool PortAudioDeviceDriver::renderSound(yarp::sig::Sound& sound)
{
    int freq  = sound.getFrequency();
    int chans = sound.getChannels();
    if (freq  != this->frequency ||
        chans != this->numChannels)
    {
        //wait for current playback to finish
        while (Pa_IsStreamStopped(stream )==0)
        {
            yarp::os::Time::delay(SLEEP_TIME);
        }

        //reset the driver
        printf("***** driver configuration changed, resetting *****\n");
        this->close();
        driverConfig.channels = chans;
        driverConfig.rate = freq;
        bool ok = open(driverConfig);
        if (ok == false)
        {
            printf("error occurred during driver reconfiguration, aborting\n");
            return false;
        }
    }

    if (renderMode == RENDER_IMMEDIATE)
        return immediateSound(sound);
    else if (renderMode == RENDER_APPEND)
        return appendSound(sound);

    return false;
}
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;
}
Пример #3
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;
}
Пример #4
0
bool PortAudioDeviceDriver::appendSound(yarp::sig::Sound& sound)
{
    //unsigned char* dataP= sound.getRawData();
    int num_bytes = sound.getBytesPerSample();
    int num_channels = sound.getChannels();
    int num_samples = sound.getRawDataSize()/num_channels/num_bytes;
    // memcpy(data.samplesBuffer,dataP,num_samples/**num_bytes*num_channels*/);

    for (int i=0; i<num_samples; i++)
        for (int j=0; j<num_channels; j++)
            dataBuffers.playData->write (sound.get(i,j));

    pThread.something_to_play = true;
    return true;
}
Пример #5
0
bool FfmpegGrabber::getAudioVisual(yarp::sig::ImageOf<yarp::sig::PixelRgb>& image,
                                   yarp::sig::Sound& sound) {

    FfmpegHelper& helper = HELPER(system_resource);
    DecoderState& videoDecoder = helper.videoDecoder;
    DecoderState& audioDecoder = helper.audioDecoder;

    bool tryAgain = false;
    bool triedAgain = false;

    do {

        bool gotAudio = false;
        bool gotVideo = false;
        if (startTime<0.5) {
            startTime = Time::now();
        }
        double time_target = 0;
        while(av_read_frame(pFormatCtx, &packet)>=0) {
            // Is this a packet from the video stream?
            DBG printf("frame ");
            bool done = false;
            if (packet.stream_index==videoDecoder.getIndex()) {
                DBG printf("video ");
                done = videoDecoder.getVideo(packet);
                image.resize(1,1);
                if (done) {
                    //printf("got a video frame\n");
                    gotVideo = true;
                }
            } if (packet.stream_index==audioDecoder.getIndex()) {
                DBG printf("audio ");
                done = audioDecoder.getAudio(packet,sound);
                if (done) {
                    //printf("got an audio frame\n");
                    gotAudio = true;
                }
            } else {
                DBG printf("other ");
            }
            AVRational& time_base = pFormatCtx->streams[packet.stream_index]->time_base;
            double rbase = av_q2d(time_base);

            DBG printf(" time=%g ", packet.pts*rbase);
            time_target = packet.pts*rbase;

            av_free_packet(&packet);
            DBG printf(" %d\n", done);
            if (((imageSync?gotVideo:videoDecoder.haveFrame())||!_hasVideo)&&
                ((imageSync?1:gotAudio)||!_hasAudio)) {
                if (_hasVideo) {
                    videoDecoder.getVideo(image);
                } else {
                    image.resize(0,0);
                }
                if (needRateControl) {
                    double now = (Time::now()-startTime)*pace;
                    double delay = time_target-now;
                    if (delay>0) {
                        DBG printf("DELAY %g ", delay);
                        Time::delay(delay);
                    } else {
                        DBG printf("NODELAY %g ", delay);
                    }
                }
                DBG printf("IMAGE size %dx%d  ", image.width(), image.height());
                DBG printf("SOUND size %d\n", sound.getSamples());
                if (!_hasAudio) {
                    sound.resize(0,0);
                }
                return true;
            }
        }

        tryAgain = !triedAgain;

        if (tryAgain) {
            if (!shouldLoop) {
                return false;
            }
            av_seek_frame(pFormatCtx,-1,0,AVSEEK_FLAG_BACKWARD);
            startTime = Time::now();
        }
    } while (tryAgain);

    return false;
}