Skip to content

Instantly share code, notes, and snippets.

@kaixiong
Created January 14, 2013 17:05
Show Gist options
  • Save kaixiong/4531566 to your computer and use it in GitHub Desktop.
Save kaixiong/4531566 to your computer and use it in GitHub Desktop.
#include <libvisual/libvisual.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <boost/format.hpp>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
typedef std::vector<LV::Time> TimingProfile;
unsigned int const frame_count = 200;
namespace {
TimingProfile record_read_durations (unsigned int frame_count, unsigned int sample_rate, unsigned int samples_per_read, double wait_time)
{
uint64_t wait_time_usecs = wait_time * VISUAL_USEC_PER_SEC;
int error;
pa_sample_spec sample_spec { PA_SAMPLE_S16LE, sample_rate, 2 };
pa_simple* snd = pa_simple_new (nullptr,
"pa_blocking_read_test",
PA_STREAM_RECORD,
nullptr,
"record",
&sample_spec,
nullptr,
nullptr,
&error);
TimingProfile read_durations (frame_count);
int16_t snd_buffer[samples_per_read*2];
LV::Timer read_timer;
for (unsigned int i = 0; i < frame_count; i++) {
read_timer.start ();
pa_simple_read (snd, snd_buffer, sizeof (snd_buffer), &error);
read_durations[i] = read_timer.elapsed ();
LV::Time::usleep (wait_time_usecs);
}
pa_simple_free (snd);
return read_durations;
}
}
int main (int argc, char** argv)
{
LV::System::init (argc, argv);
TimingProfile durations1 = record_read_durations (frame_count, 44100, 1024, 1/60.0);
TimingProfile durations2 = record_read_durations (frame_count, 44100, 1024, 1/40.0);
TimingProfile durations3 = record_read_durations (frame_count, 44100, 1024, 1/20.0);
TimingProfile durations4 = record_read_durations (frame_count, 44100, 1024, 0);
for (unsigned int i = 0; i < frame_count; i++) {
std::cout << boost::format ("%d %.8f %.8f %.8f %.8f\n")
% i
% durations1[i].to_secs ()
% durations2[i].to_secs ()
% durations3[i].to_secs ()
% durations4[i].to_secs ();
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment