123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #ifndef AUDIORECORD_H
- #define AUDIORECORD_H
- #include <cstdint>
- #include <list>
- #include <string>
- #include <alsa/asoundlib.h>
- #include <alsa/pcm.h>
- /* 声卡的PCM信息结构体 */
- struct PCMDevice_t
- {
- unsigned int PCMDevice; /* PCM设备编号,打开的设备编号就是这个 */
- unsigned int SubDevice; /* 子设备编号 */
- int CardNumber; /* 声卡编号 */
- std::string PCMID; /* 声卡ID */
- std::string PCMName; /* 声卡名称 */
- std::string PCMSubName; /* 子设备名称 */
- };
- /**
- * @brief 声卡设备信息结构体
- *
- */
- struct AudioDevice_t
- {
- int CardNumber; /* 声卡编号,打开录音的设备编号是这个 */
- std::string CardID; /* 声卡ID */
- std::string CardName; /* 声卡名称 */
- std::string CardDriver; /* 驱动名称 */
- std::string CardLongName; /* 声卡长名称 */
- std::string CardMixername; /* 混音器名称 */
- std::string CardComponents; /* 组件信息 */
- std::list<PCMDevice_t> PCMDevices; /* PCM设备列表 */
- };
- /**
- * @brief 直接获取到的声卡通道描述符信息,可以直接用来打开
- *
- */
- struct AudioDeviceDesc_t
- {
- std::string DeviceName; /* 设备字符名称,可以直接打开 */
- std::string DeviceDesc; /* 设备描述 */
- std::string IOID; /* 输入/输出类型 */
- std::string Card; /* 声卡编号或标识,可能会没有 */
- std::string DevNum; /* 设备编号,可能会没有 */
- };
- /* 获取声卡信息列表,包括可以用来录音的PCM列表 */
- bool getAudioDevices(std::list<AudioDevice_t> &devices);
- /* 获取声卡字符设备名称,可以直接被打开 */
- bool getPCMAudioDevice(std::list<AudioDeviceDesc_t> &devices);
- /**
- * @brief 录音类
- *
- */
- class AudioRecord
- {
- public:
- AudioRecord() = default;
- ~AudioRecord() = default;
- /**
- * @brief Set the Record Params object
- *
- * @param sampleRate 采样率
- * @param bits 位深度
- * @param channels 通道数
- */
- void setRecordParams(int sampleRate = 44100, int bits = 16, int channels = 2);
- /* 打开录音通道,通常的格式: "hw:0:0" */
- bool openRecordChannel(const std::string &deviceName);
- /**
- * @brief 读取录音大小
- *
- * @param buffer 数据缓冲区
- * @param bufferSize 缓冲区大小
- * @param recordFrames 需要读取的帧数,一帧就是一个采样点,16bit / 2 * 2 通道 = 4字节
- * 44100帧就是1秒
- * @return int 读取到的字节数
- */
- int recordAudio(char* buffer, uint32_t bufferSize, uint32_t recordFrames);
- private:
- uint32_t m_sampleRate = 44100; /* 默认采样率 */
- int m_bitDepth = 16; /* 默认位深度 */
- int m_channels = 2; /* 默认通道数 */
- int m_oneSampleSize = 2; /* 单个采样点的大小,默认16位小端格式,2字节 */
- std::string m_deviceName; /* 设备名称,打开的设备名称 */
- snd_pcm_t* m_captureHandle = nullptr; /* PCM设备句柄 */
-
- };
- #endif // AUDIORECORD_H
|