QtFM::AudioOutput Class
The AudioOutput class is the interface of a sound output that an Engine renders into. More…
| Header | #include <QtFM> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::FM) |
| Module | Nt FM C++ Classes |
List of all members, including inherited members
Public Functions
| AudioOutput() | |
| virtual | ~AudioOutput() |
| virtual bool | open(int sampleRate, int channels, SampleFormat format) |
| virtual void | close() |
Protected Functions
| int | pull(float *interleaved, int frames) |
| int | pull(qint16 *interleaved, int frames) |
| void | reportUnderrun() |
Detailed Description
Implement open() to start a device or callback at the given rate and format, and call pull() from its audio thread to fill each buffer; close() stops it and must not return until the audio thread has stopped calling pull().
// An output with no device: a thread that pulls blocks as fast as it can, to measure the engine.
class BenchmarkOutput : public AudioOutput {
public:
bool open(int sampleRate, int channels, SampleFormat format) override {
Q_UNUSED(format);
m_rate = sampleRate;
m_buffer.assign(static_cast<std::size_t>(256 * channels), 0.0f);
m_running = true;
m_thread = std::thread([this] {
while (m_running.load())
m_frames += pull(m_buffer.data(), 256);
});
return true;
}
void close() override {
m_running = false;
if (m_thread.joinable())
m_thread.join();
}
qint64 frames() const { return m_frames.load(); }
int rate() const { return m_rate; }
private:
std::vector<float> m_buffer;
std::atomic<bool> m_running{false};
std::atomic<qint64> m_frames{0};
std::thread m_thread;
int m_rate = 0;
};
Differences from Qt
There is no Qt Multimedia backend. Nt FM has ALSA on Linux as the default output.
Member Function Documentation
QtFM::AudioOutput::AudioOutput()
Constructs an output, not attached to an engine.
QtFM::AudioOutput::~AudioOutput()
Destroys the output.
void QtFM::AudioOutput::close()
Stops calling pull() and closes the output.
bool QtFM::AudioOutput::open(int sampleRate, int channels, SampleFormat format)
Opens the output for sampleRate frames a second of channels channels in format, and starts calling pull(). Returns false if it cannot.
int QtFM::AudioOutput::pull(float *interleaved, int frames)
Renders frames frames of interleaved stereo into interleaved from the attached engine, and returns the number of frames rendered. Writes silence and returns 0 if no engine is attached.
int QtFM::AudioOutput::pull(qint16 *interleaved, int frames)
This is an overloaded function.
Renders frames frames of 16-bit interleaved stereo into interleaved.
void QtFM::AudioOutput::reportUnderrun()
Tells the attached engine that the device ran out of samples, so that it emits Engine::underrun(). Safe to call from the audio thread.