double get_syscall_elapse(sc_state_t *state) { struct timespec st, ed; unsigned long elapse; int loopcnt, loop = 20; int index = loop >> 2; int iter, iteration = get_iteration(); result_t *r = (result_t *)malloc(sizeof(result_t) * loop); loopcnt = loop; while(loopcnt --) { if(state->prepare) { state->prepare(state); } iter = iteration; clock_gettime(CLOCK_REALTIME, &st); while(iter--) { state->bench(state); }; clock_gettime(CLOCK_REALTIME, &ed); elapse = get_total_us(&st, &ed); save_result(r, loopcnt, elapse, iteration); if(state->cooldown) state->cooldown(state); } qsort(r, loop, sizeof(result_t ), cmp_result); double latency = get_latency(r[index].elapse, r[index].iter); return latency; }
int get_net_latency(void) { int spm = speeds_net_latency_measured; if (speeds_net_latency) { return speeds_net_latency; } if (! spm || spm == LATENCY0) { speeds_net_latency_measured = get_latency(); } if (speeds_net_latency_measured) { return speeds_net_latency_measured; } return 0; }
void osc_pad_t::impl_t::root_latency() { set_latency(get_latency()); }
/* * Iterate the main loop while recording is on. * This function runs under it's own thread called by audio_pulse_start * args: * data - pointer to user data (audio context) * * asserts: * data is not null * * returns: pointer to error code */ static void *pulse_read_audio(void *data) { audio_context_t *audio_ctx = (audio_context_t *) data; /*assertions*/ assert(audio_ctx != NULL); if(verbosity > 0) printf("AUDIO: (pulseaudio) read thread started\n"); pa_mainloop *pa_ml; pa_mainloop_api *pa_mlapi; pa_buffer_attr bufattr; pa_sample_spec ss; pa_stream_flags_t flags = 0; int r; int pa_ready = 0; /* Create a mainloop API and connection to the default server */ pa_ml = pa_mainloop_new(); pa_mlapi = pa_mainloop_get_api(pa_ml); pa_ctx = pa_context_new(pa_mlapi, "guvcview Pulse API"); if(pa_context_connect(pa_ctx, NULL, 0, NULL) < 0) { fprintf(stderr,"AUDIO: PULSE - unable to connect to server: pa_context_connect failed\n"); finish(pa_ctx, pa_ml); return ((void *) -1); } /* * This function defines a callback so the server will tell us it's state. * Our callback will wait for the state to be ready. The callback will * modify the variable to 1 so we know when we have a connection and it's * ready. * If there's an error, the callback will set pa_ready to 2 */ pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready); /* * This function defines a time event callback (called every TIME_EVENT_USEC) */ //pa_context_rttime_new(pa_ctx, pa_rtclock_now() + TIME_EVENT_USEC, time_event_callback, NULL); /* * We can't do anything until PA is ready, so just iterate the mainloop * and continue */ while (pa_ready == 0) { pa_mainloop_iterate(pa_ml, 1, NULL); } if (pa_ready == 2) { finish(pa_ctx, pa_ml); return ((void *) -1); } /* set the sample spec (frame rate, channels and format) */ ss.rate = audio_ctx->samprate; ss.channels = audio_ctx->channels; ss.format = PA_SAMPLE_FLOAT32LE; /*for PCM -> PA_SAMPLE_S16LE*/ recordstream = pa_stream_new(pa_ctx, "Record", &ss, NULL); if (!recordstream) fprintf(stderr, "AUDIO: (pulseaudio) pa_stream_new failed (chan:%d rate:%d)\n", ss.channels, ss.rate); /* define the callbacks */ pa_stream_set_read_callback(recordstream, stream_request_cb, (void *) audio_ctx); // Set properties of the record buffer pa_zero(bufattr); /* optimal value for all is (uint32_t)-1 ~= 2 sec */ bufattr.maxlength = (uint32_t) -1; bufattr.prebuf = (uint32_t) -1; bufattr.minreq = (uint32_t) -1; if (audio_ctx->latency > 0) { bufattr.fragsize = bufattr.tlength = pa_usec_to_bytes((audio_ctx->latency * 1000) * PA_USEC_PER_MSEC, &ss); flags |= PA_STREAM_ADJUST_LATENCY; } else bufattr.fragsize = bufattr.tlength = (uint32_t) -1; flags |= PA_STREAM_INTERPOLATE_TIMING; flags |= PA_STREAM_AUTO_TIMING_UPDATE; char * dev = audio_ctx->list_devices[audio_ctx->device].name; if(verbosity > 0) printf("AUDIO: (pulseaudio) connecting to device %s\n\t (channels %d rate %d)\n", dev, ss.channels, ss.rate); r = pa_stream_connect_record(recordstream, dev, &bufattr, flags); if (r < 0) { fprintf(stderr, "AUDIO: (pulseaudio) skip latency adjustment\n"); /* * Old pulse audio servers don't like the ADJUST_LATENCY flag, * so retry without that */ r = pa_stream_connect_record(recordstream, dev, &bufattr, PA_STREAM_INTERPOLATE_TIMING| PA_STREAM_AUTO_TIMING_UPDATE); } if (r < 0) { fprintf(stderr, "AUDIO: (pulseaudio) pa_stream_connect_record failed\n"); finish(pa_ctx, pa_ml); return ((void *) -1); } get_latency(recordstream); /* * Iterate the main loop while streaming. The second argument is whether * or not the iteration should block until something is ready to be * done. Set it to zero for non-blocking. */ while (audio_ctx->stream_flag == AUDIO_STRM_ON) { pa_mainloop_iterate(pa_ml, 1, NULL); } if(verbosity > 0) printf("AUDIO: (pulseaudio) stream terminated(%i)\n", audio_ctx->stream_flag); pa_stream_disconnect (recordstream); pa_stream_unref (recordstream); finish(pa_ctx, pa_ml); return ((void *) 0); }
/* * audio record callback * args: * s - pointer to pa_stream * length - buffer length * data - pointer to user data * * asserts: * none * * returns: none */ static void stream_request_cb(pa_stream *s, size_t length, void *data) { audio_context_t *audio_ctx = (audio_context_t *) data; if(audio_ctx->channels == 0) { fprintf(stderr, "AUDIO: (pulseaudio) stream_request_cb failed: channels = 0\n"); return; } if(audio_ctx->samprate == 0) { fprintf(stderr, "AUDIO: (pulseaudio) stream_request_cb failed: samprate = 0\n"); return; } uint64_t frame_length = NSEC_PER_SEC / audio_ctx->samprate; /*in nanosec*/ int64_t ts = 0; int64_t buff_ts = 0; uint32_t i = 0; while (pa_stream_readable_size(s) > 0) { const void *inputBuffer; size_t length; /*read from stream*/ if (pa_stream_peek(s, &inputBuffer, &length) < 0) { fprintf(stderr, "AUDIO: (pulseaudio) pa_stream_peek() failed\n"); return; } if(length == 0) { fprintf(stderr, "AUDIO: (pulseaudio) empty buffer!\n"); return; /*buffer is empty*/ } get_latency(s); ts = ns_time_monotonic() - (latency * 1000); if(audio_ctx->last_ts <= 0) audio_ctx->last_ts = ts; uint32_t numSamples = (uint32_t) length / sizeof(sample_t); const sample_t *rptr = (const sample_t*) inputBuffer; sample_t *capture_buff = (sample_t *) audio_ctx->capture_buff; int chan = 0; /*store capture samples or silence if inputBuffer == NULL (hole)*/ for( i = 0; i < numSamples; ++i ) { capture_buff[sample_index] = inputBuffer ? *rptr++ : 0; sample_index++; /*store peak value*/ if(audio_ctx->capture_buff_level[chan] < capture_buff[sample_index]) audio_ctx->capture_buff_level[chan] = capture_buff[sample_index]; chan++; if(chan >= audio_ctx->channels) chan = 0; if(sample_index >= audio_ctx->capture_buff_size) { buff_ts = ts + ( i / audio_ctx->channels ) * frame_length; audio_fill_buffer(audio_ctx, buff_ts); /*reset*/ audio_ctx->capture_buff_level[0] = 0; audio_ctx->capture_buff_level[1] = 0; sample_index = 0; } } pa_stream_drop(s); /*clean the samples*/ } }
int main( int argc, char *argv[] ) { // unsigned long in_buf; // char *data_buff; // int ret; int c; optparams_t oparams; int fd; int oflag; printf( "read_ft245 test program is started\n" ); oparams.dev_name = strdup( "//9/dev/ft245" ); //!!!! oparams.fname = NULL; oparams.nonblock = 0; oparams.devinfo = 0; oparams.rx_timeout = 0; oparams.psize = 8; oparams.npackets = 30; oparams.verbose = 0; oparams.reset = 0; oparams.latency = 0; while ( ( c = getopt( argc, argv, "d:f:nivrs:p:t:l:" ) ) != -1 ) { switch( c ) { case 'd': oparams.dev_name = optarg; break; case 'f': oparams.fname = optarg; break; case 'n': oparams.nonblock = 1; break; case 'i': oparams.devinfo = 1; break; case 'v': oparams.verbose++; break; case 's': oparams.psize = strtoul( optarg, NULL, 0 ); break; case 'p': oparams.npackets = strtoul( optarg, NULL, 0 ); break; case 't': TM = 1; break; case 'r': oparams.reset = 1; break; case 'l': oparams.latency = strtoul( optarg, NULL, 0 ); if ( oparams.latency == 0 ) { fprintf( stderr, "Command line: Bad latency value. Correct range is 1 - 255\n" ); oparams.latency = 0; } break; default: fprintf( stderr, "Unsupported comand line option\n" ); break; } } open_shmem(); // open device in block or nonblock mode oflag = O_RDONLY | O_NONBLOCK; if ( ( fd = open( oparams.dev_name, oflag ) ) == -1 ) { fprintf( stderr, "open() failed. %s\n", strerror( errno ) ); return ( -1 ); } //if ( oparams.reset ) reset_device( fd ); if ( oparams.devinfo ) get_dev_info( fd ); // timeout test if ( oparams.rx_timeout ) set_rx_timeout( fd, oparams.rx_timeout ); if ( oparams.latency ) set_latency( fd, oparams.latency ); printf( "Device latency time is %d\n", get_latency( fd ) ); //if ( oparams.reset ) //reset_device( fd ); printf( "Start data read. \n"); read_data_nonblock1( fd, oparams.psize, oparams.npackets, oparams.fname ); printf( "read_ft245 test program is finished %d\n",sum_ret ); close( fd ); return( 0 ); }