AudioRecord.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef AUDIORECORD_H
  2. #define AUDIORECORD_H
  3. #include <cstdint>
  4. #include <list>
  5. #include <string>
  6. #include <alsa/asoundlib.h>
  7. #include <alsa/pcm.h>
  8. /**
  9. * @file AudioRecord.h
  10. * @brief 声卡录音类,使用ALSA库进行录音
  11. *
  12. * 该文件定义了声卡设备信息结构体和录音类,提供获取声卡信息和录音功能。
  13. * 依赖库:libasound2-dev
  14. *
  15. * @author Your Name
  16. * @date 2025-06-29
  17. */
  18. /* 声卡的PCM信息结构体 */
  19. struct PCMDevice_t
  20. {
  21. unsigned int PCMDevice; /* PCM设备编号,打开的设备编号就是这个 */
  22. unsigned int SubDevice; /* 子设备编号 */
  23. int CardNumber; /* 声卡编号 */
  24. std::string PCMID; /* 声卡ID */
  25. std::string PCMName; /* 声卡名称 */
  26. std::string PCMSubName; /* 子设备名称 */
  27. };
  28. /**
  29. * @brief 声卡设备信息结构体
  30. *
  31. */
  32. struct AudioDevice_t
  33. {
  34. int CardNumber; /* 声卡编号,打开录音的设备编号是这个 */
  35. std::string CardID; /* 声卡ID */
  36. std::string CardName; /* 声卡名称 */
  37. std::string CardDriver; /* 驱动名称 */
  38. std::string CardLongName; /* 声卡长名称 */
  39. std::string CardMixername; /* 混音器名称 */
  40. std::string CardComponents; /* 组件信息 */
  41. std::list<PCMDevice_t> PCMDevices; /* PCM设备列表 */
  42. };
  43. /**
  44. * @brief 直接获取到的声卡通道描述符信息,可以直接用来打开
  45. *
  46. */
  47. struct AudioDeviceDesc_t
  48. {
  49. std::string DeviceName; /* 设备字符名称,可以直接打开 */
  50. std::string DeviceDesc; /* 设备描述 */
  51. std::string IOID; /* 输入/输出类型 */
  52. std::string Card; /* 声卡编号或标识,可能会没有 */
  53. std::string DevNum; /* 设备编号,可能会没有 */
  54. };
  55. /* 使用命名空间包装一下 */
  56. namespace AudioDevice
  57. {
  58. /* 获取声卡信息列表,包括可以用来录音的PCM列表 */
  59. bool getAudioDevices(std::list<AudioDevice_t> &devices);
  60. /* 获取声卡字符设备名称,可以直接被打开 */
  61. bool getPCMAudioDevice(std::list<AudioDeviceDesc_t> &devices);
  62. }
  63. /**
  64. * @brief 录音类
  65. *
  66. */
  67. class AudioRecord
  68. {
  69. public:
  70. AudioRecord() = default;
  71. ~AudioRecord() = default;
  72. /**
  73. * @brief Set the Record Params object
  74. *
  75. * @param sampleRate 采样率
  76. * @param bits 位深度
  77. * @param channels 通道数
  78. */
  79. void setRecordParams(int sampleRate = 44100, int bits = 16, int channels = 2);
  80. /* 打开录音通道,通常的格式: "hw:0:0" */
  81. bool openRecordChannel(const std::string &deviceName);
  82. /**
  83. * @brief 读取录音大小
  84. *
  85. * @param buffer 数据缓冲区
  86. * @param bufferSize 缓冲区大小
  87. * @param recordFrames 需要读取的帧数,一帧就是一个采样点,16bit / 2 * 2 通道 = 4字节
  88. * 44100帧就是1秒
  89. * @return int 读取到的字节数
  90. */
  91. int recordAudio(char* buffer, int32_t bufferSize, int32_t recordFrames);
  92. private:
  93. int32_t m_sampleRate = 44100; /* 默认采样率 */
  94. int m_bitDepth = 16; /* 默认位深度 */
  95. int m_channels = 2; /* 默认通道数 */
  96. int m_oneSampleSize = 2; /* 单个采样点的大小,默认16位小端格式,2字节 */
  97. std::string m_deviceName; /* 设备名称,打开的设备名称 */
  98. snd_pcm_t* m_captureHandle = nullptr; /* PCM设备句柄 */
  99. };
  100. #endif // AUDIORECORD_H