Files
live-caption/src/core/audio.h
T

52 lines
1.7 KiB
C++
Raw Normal View History

#pragma once
#include <cstddef>
#include <functional>
#include <string>
#include <vector>
namespace lc {
// 一个可选的采集源(输出设备的 monitor)
struct AudioOutputDevice {
std::string sink; // 设备名
std::string description; // 人类可读描述
bool isDefault = false;
};
// 列出所有输出设备(供设置界面选择采集源)
std::vector<AudioOutputDevice> listOutputDevices(std::string * error = nullptr);
// 读取 WAV(PCM 16/24/32 位或 float32),降混为单声道并重采样到 targetRate。
bool readWavMono(const std::string & path, int targetRate,
std::vector<float> & out, std::string * error);
// 采集默认输出设备的 monitor(即"系统正在播放的声音"),
// 输出 targetRate 单声道 float32。回调在 PulseAudio 线程上调用,必须快速返回。
class MonitorCapture {
public:
using ChunkCallback = std::function<void(const float * mono, size_t nSamples)>;
MonitorCapture();
~MonitorCapture();
MonitorCapture(const MonitorCapture &) = delete;
MonitorCapture & operator=(const MonitorCapture &) = delete;
// device: 空 = 跟随系统默认输出;否则是 sink 名(见 listOutputDevices)
bool start(int targetRate, ChunkCallback callback, std::string * error,
const std::string & device = std::string());
void stop();
// 实际采集的 monitor 源名(start 成功后有效)
std::string deviceName() const;
// 前向声明(定义在 .cpp)。公开仅为让文件内的 C 回调能引用该类型,
// 头文件里它始终是不完整类型。
struct Impl;
private:
Impl * d = nullptr;
};
} // namespace lc