mutterkey
KDE-first push-to-talk transcription tool for KDE Plasma
Loading...
Searching...
No Matches
audiorecorder.h
Go to the documentation of this file.
1#pragma once
2
3#include "audio/recording.h"
4#include "config.h"
5
6#include <QAudioDevice>
7#include <QAudioFormat>
8#include <QElapsedTimer>
9#include <QIODevice>
10#include <QObject>
11
12class QAudioSource;
13
22[[nodiscard]] QAudioFormat resolveRecordingFormatForConfig(const AudioConfig &config,
23 const QAudioFormat &preferredFormat,
24 bool requestedFormatSupported,
25 QString *errorMessage = nullptr);
26
35class AudioBufferDevice final : public QIODevice
36{
37 Q_OBJECT
38
39public:
44 explicit AudioBufferDevice(QObject *parent = nullptr);
45 ~AudioBufferDevice() override = default;
46
47 Q_DISABLE_COPY_MOVE(AudioBufferDevice)
48
49
52 void clear();
53
58 [[nodiscard]] QByteArray takeBuffer();
59
60protected:
65 qint64 readData(char *data, qint64 maxSize) override;
66
73 qint64 writeData(const char *data, qint64 maxSize) override;
74
75private:
77 QByteArray m_buffer;
78};
79
87class AudioRecorder final : public QObject
88{
89 Q_OBJECT
90
91public:
97 explicit AudioRecorder(AudioConfig config, QObject *parent = nullptr);
98 ~AudioRecorder() override = default;
99
100 Q_DISABLE_COPY_MOVE(AudioRecorder)
101
102
107 bool start(QString *errorMessage = nullptr);
108
113 Recording stop();
114
119 [[nodiscard]] bool isRecording() const;
120
121private:
126 [[nodiscard]] QAudioDevice resolveDevice() const;
127
134 QAudioFormat resolveFormat(const QAudioDevice &device, QString *errorMessage) const;
135
137 AudioConfig m_config;
139 AudioBufferDevice m_buffer;
141 QAudioSource *m_audioSource = nullptr;
143 QAudioFormat m_activeFormat;
145 QElapsedTimer m_elapsed;
146};
QAudioFormat resolveRecordingFormatForConfig(const AudioConfig &config, const QAudioFormat &preferredFormat, bool requestedFormatSupported, QString *errorMessage=nullptr)
Resolves the active capture format from config and device capabilities.
QIODevice sink that accumulates raw captured PCM bytes in memory.
void clear()
Discards all buffered audio bytes.
AudioBufferDevice(QObject *parent=nullptr)
Creates an empty in-memory audio sink.
QByteArray takeBuffer()
Moves the buffered audio payload out of the device.
qint64 readData(char *data, qint64 maxSize) override
Audio capture is write-only for this device.
qint64 writeData(const char *data, qint64 maxSize) override
Appends captured audio bytes to the in-memory buffer.
Captures microphone audio into a Recording value object.
AudioRecorder(AudioConfig config, QObject *parent=nullptr)
Creates a recorder with a fixed audio configuration snapshot.
Runtime configuration types, defaults, and JSON loading entrypoints.
Shared recorded-audio payload passed between capture and transcription.
Audio capture configuration passed to the recorder.
Definition config.h:48
Immutable-style value object holding one captured audio segment.
Definition recording.h:14