50 lines
1.2 KiB
C++
50 lines
1.2 KiB
C++
/**
|
|
* @file audio_player.h
|
|
* @brief 音频播放封装 - 基于 ALSA
|
|
*/
|
|
|
|
#ifndef AUDIO_PLAYER_H
|
|
#define AUDIO_PLAYER_H
|
|
|
|
#include <alsa/asoundlib.h>
|
|
#include <string>
|
|
#include <cstdint>
|
|
|
|
class AudioPlayer {
|
|
public:
|
|
/**
|
|
* @brief 构造函数
|
|
* @param device ALSA 设备名,如 "hw:0,0" 或 "default"
|
|
* @param sample_rate 采样率 (Hz)
|
|
* @param channels 声道数 (1=单声道, 2=立体声)
|
|
*/
|
|
AudioPlayer(const std::string& device, int sample_rate, int channels);
|
|
~AudioPlayer();
|
|
|
|
/**
|
|
* @brief 初始化 ALSA 播放设备
|
|
* @return true 成功, false 失败
|
|
*/
|
|
bool init();
|
|
|
|
/**
|
|
* @brief 写入 PCM 数据到扬声器
|
|
* @param buffer 输入缓冲区 (int16_t 数组)
|
|
* @param frames 帧数 (1帧 = channels * 2字节)
|
|
* @return 实际写入的帧数,<0 表示错误
|
|
*/
|
|
snd_pcm_sframes_t write(const int16_t* buffer, snd_pcm_uframes_t frames);
|
|
|
|
private:
|
|
std::string m_device;
|
|
int m_sample_rate;
|
|
int m_channels;
|
|
|
|
snd_pcm_t* m_pcm_handle;
|
|
|
|
// 私有方法: 配置混音器
|
|
bool setup_mixer(const char* card_name);
|
|
};
|
|
|
|
#endif // AUDIO_PLAYER_H
|