QAudio Example has inconsistent sampling rate timing
-
Hi, I've got a large project I've been working on, and I traced the source of my problem down to an issue with the QAudioFormat Sampling Rate.
In the Qt Audio Example: https://doc.qt.io/qt-6/qtcharts-audio-example.html
I tried checking if the sampling rate was consistent and found that I was getting 10ms per sample instead of my expected 0.25ms per sample for a 4000Hz sampling rate.
I created the following Timing class
timer.h
#ifndef TIMER_H #define TIMER_H #include <iostream> #include <chrono> class Timer { public: Timer(); ~Timer(); void Stop(); void StartDemand(); void StopDemand(); private: std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepoint; std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimepointDemand; }; #endif // TIMER_H
timer.cpp
#include "timer.h" #include <memory> #include <chrono> Timer::Timer() { m_StartTimepoint = std::chrono::high_resolution_clock::now(); } Timer::~Timer(){ Stop(); } void Timer::Stop(){ auto endTimepoint = std::chrono::high_resolution_clock::now(); auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepoint).time_since_epoch().count(); auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count(); auto duration = end - start; double ms = duration *0.001; std::cout << duration <<"us (" << ms << "ms)\n"; } void Timer::StartDemand(){ m_StartTimepointDemand = std::chrono::high_resolution_clock::now(); } void Timer::StopDemand(){ auto endTimepoint = std::chrono::high_resolution_clock::now(); auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_StartTimepointDemand).time_since_epoch().count(); auto end = std::chrono::time_point_cast<std::chrono::microseconds>(endTimepoint).time_since_epoch().count(); auto duration = end - start; double ms = duration *0.001; std::cout << duration <<"us (" << ms << "ms)\n"; }
and added the following code to xyseriesdevice::writeData
The timer and SampleCounter I have defined in the header.qint64 XYSeriesIODevice::writeData(const char *data, qint64 maxSize) { static const int resolution = 4; if(SampleCounter ==0){ bufferTimer.StartDemand(); SampleCounter = 1; } else if(SampleCounter ==1){ bufferTimer.StopDemand(); SampleCounter =0; }
While I am sure there are better ways I could be writing this, I can't explain why my outputs for the delta-time between data calls is the following:
10368us (10.368ms) 10717us (10.717ms) 10397us (10.397ms) 882us (0.882ms) 11009us (11.009ms) 10396us (10.396ms) 10711us (10.711ms) 10728us (10.728ms) 10383us (10.383ms) 10734us (10.734ms) 10700us (10.7ms)
I've tried testing this in multiple ways, and every time it comes out to the same response. I would be expecting for the delta-time between samples to again be 0.25ms for 4000Hz as the sampling rate.