MINI ALSA 声音架构

概述

MiniAlsa是一个在rtos上运行的声卡架构,提供基本的pcm、mixer等接口,用法类似alsa。

API接口定义

Card

相关数据结构

typedef struct snd_card_drv {
    driver_t         drv;      
    slist_t          pcm_head;  
    slist_t          mixer_head;
} snd_card_drv_t;

typedef struct snd_dev {
    aos_dev_t        device;    
    void             *priv;     
} card_dev_t;

typedef struct module{
    char test;                  
} module_t;

查找并打开一个声卡

int aos_card_attach(const char *name, card_dev_t **card);
  • 参数
    • name:声卡名
    • card:设备打开成功后所存放位置
  • 返回值:
    • 成功返回0,失败返回-1

创建声卡

int aos_card_new(int *parent, int idx, const char *xid, struct module *module, int size, snd_card_drv_t **card_ret);
  • 参数
    • parent:声卡的父设备
    • idx:声卡的编号
    • xid:声卡的标识符
    • module:top level module for locking
    • size:内存大小
    • card_ret:返回创建声卡成功的指针
  • 返回值:
    • 成功返回0,失败返回-1

释放声卡

int aos_card_free(snd_card_drv_t *card);
  • 参数
    • card:声卡指针
  • 返回值:
    • 成功返回0,失败返回-1

设置声卡低功耗模式

int aos_card_lpm(const char *name ,int state);
  • 参数
    • name:声卡名称
    • state:状态
  • 返回值:
    • 成功返回0,失败返回-1

PCM

相关数据结构

typedef enum _aos_pcm_state {
    /** Open */
    AOS_PCM_STATE_OPEN = 0,
    /** Setup installed */
    AOS_PCM_STATE_SETUP,
    /** Ready to start */
    AOS_PCM_STATE_PREPARED,
    /** Running */
    AOS_PCM_STATE_RUNNING,
    /** Stopped: underrun (playback) or overrun (capture) detected */
    AOS_PCM_STATE_XRUN,
    /** Draining: running (playback) or stopped (capture) */
    AOS_PCM_STATE_DRAINING,
    /** Paused */
    AOS_PCM_STATE_PAUSED,
    /** Hardware is suspended */
    AOS_PCM_STATE_SUSPENDED,
    /** Hardware is disconnected */
    AOS_PCM_STATE_DISCONNECTED,
    AOS_PCM_STATE_LAST = AOS_PCM_STATE_DISCONNECTED,
    /** Private - used internally in the library - do not use*/
    AOS_PCM_STATE_PRIVATE1 = 1024
} aos_pcm_state_t;

typedef enum _aos_pcm_stream {
    AOS_PCM_STREAM_PLAYBACK = 0,
    AOS_PCM_STREAM_CAPTURE,
    AOS_PCM_STREAM_LAST = AOS_PCM_STREAM_CAPTURE,
} aos_pcm_stream_t;

typedef enum _aos_pcm_access {
    /** aos_pcm_readi/aos_pcm_writei access */
    AOS_PCM_ACCESS_RW_INTERLEAVED,
    /** aos_pcm_readn/aos_pcm_writen access */
    AOS_PCM_ACCESS_RW_NONINTERLEAVED,
    AOS_PCM_ACCESS_LAST = AOS_PCM_ACCESS_RW_NONINTERLEAVED
} aos_pcm_access_t;

typedef struct _aos_pcm_drv {
    driver_t drv;
    struct aos_pcm_ops ops;
} aos_pcm_drv_t;

typedef struct aos_pcm_dev {
    aos_dev_t device;
    aos_pcm_t pcm;
} aos_pcm_dev_t;

创建一个pcm设备

int aos_pcm_new(aos_pcm_t **pcm_ret, int type, const char *name, aos_pcm_stream_t stream, int mode);
  • 参数
    • pcm:创建完成的pcm设备所返回的指针
    • type:同步异步标志
    • name:设备名
    • stream:pcm流格式
    • mode:pcm
  • 返回值:
    • 成功返回0,失败返回-1

打开pcm设备

int aos_pcm_open(aos_pcm_t **pcm_ret, const char *name, aos_pcm_stream_t stream, int mode);
  • 参数
    • pcm:打开成功的pcm设备所返回的指针
    • name:设备名
    • stream:pcm流格式
    • mode:pcm 操作方式(阻塞或非阻塞)
  • 返回值:
    • 成功返回0,失败返回-1

关闭pcm设备

int aos_pcm_close(aos_pcm_t *pcm);
  • 参数:
    • pcm:aos_pcm_t指针
  • 返回值:
    • 成功返回0,失败返回-1

暂停/播放 pcm数据

int aos_pcm_pause(aos_pcm_t *pcm, int enable);
  • 参数:
    • pcm:aos_pcm_t指针
    • enable:0->播放;1->暂停
  • 返回值:
    • 成功返回0,失败返回-1

退出pcm播放

int aos_pcm_drop(aos_pcm_t *pcm);
  • 参数
    • pcm:aos_pcm_t指针
  • 返回值:
    • 成功返回0,失败返回-1

等待缓存中的数据播放完成再退出pcm播放

int aos_pcm_drain(aos_pcm_t *pcm);
  • 参数
    • pcm:aos_pcm_t指针
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm的配置选项

void aos_pcm_set_ops(aos_pcm_t *pcm, int direction, struct aos_pcm_ops *ops);
  • 参数
    • pcm:aos_pcm_t指针
    • direction:方向选择
    • ops:配置选项
  • 返回值:

设置pcm硬件参数并起效

int aos_pcm_hw_params(aos_pcm_t *pcm, aos_pcm_hw_params_t *params);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
  • 返回值:
    • 成功返回0,失败返回-1

申请一块pcm硬件参数的空间

int aos_pcm_hw_params_alloca(aos_pcm_hw_params_t **p);
  • 参数:
    • p:aos_pcm_hw_params_t指针
  • 返回值:
    • 成功返回0,失败返回-1

申请一块pcm软件参数的空间

int aos_pcm_sw_params_alloca(aos_pcm_sw_params_t **p);
  • 参数:
    • p:pcm软件指针参数
  • 返回值:
    • 成功返回0,失败返回-1

写入交错型pcm数据

aos_pcm_sframes_t aos_pcm_writei(aos_pcm_t *pcm, const void *buffer, aos_pcm_uframes_t size);
aos_pcm_sframes_t aos_pcm_writen(aos_pcm_t *pcm, void **bufs, aos_pcm_uframes_t size);
  • 参数:
    • pcm:pcm软件指针参数
    • buffer/bufs:pcm 数据
    • size:pcm 数据长度,以frame为单位
  • 返回值:
    • 成功返回0,失败返回-1

读取交错型pcm数据

aos_pcm_sframes_t aos_pcm_readi(aos_pcm_t *pcm, void *buffer, aos_pcm_uframes_t size);
aos_pcm_sframes_t aos_pcm_readn(aos_pcm_t *pcm, void **bufs, aos_pcm_uframes_t size);
  • 参数
    • pcm:aos_pcm_t指针
    • buffer/bufs:pcm 数据
    • size:pcm 数据长度,以frame为单位
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的access参数

int aos_pcm_hw_params_set_access(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, aos_pcm_access_t _access);
  • 参数
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • access:交错模式
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件的format参数

int aos_pcm_hw_params_set_format(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, int val);
  • 参数
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的buffer_size参数

int aos_pcm_hw_params_set_buffer_size_near(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, aos_pcm_uframes_t *val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的buffer参数

int aos_pcm_hw_params_set_buffer_size(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, aos_pcm_uframes_t val);
int aos_pcm_hw_params_set_buffer_time(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val, int dir);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
    • dir:方向选择
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的period_size参数

int aos_pcm_hw_params_set_period_size_near(aos_pcm_t *pcm,aos_pcm_hw_params_t *params, aos_pcm_uframes_t *val, int *dir);
  • 参数
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
    • dir:方向选择
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件汇总的current参数

int aos_pcm_hw_params_current(aos_pcm_t *pcm, aos_pcm_hw_params_t *params);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
  • 返回值:
    • 成功返回0,失败返回-1

释放pcm资源

int aos_pcm_hw_free(aos_pcm_t *pcm);
  • 参数:
    • pcm:aos_pcm_t指针
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的部分参数,access/channel/rate/sample_bits

int aos_pcm_hw_params_any(aos_pcm_t *pcm, aos_pcm_hw_params_t *params);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
  • 返回值:
    • 成功返回0,失败返回-1

准备pcm设备

int aos_pcm_prepare(aos_pcm_t *pcm);
  • 参数:
    • pcm:aos_pcm_t指针
  • 返回值:
    • 成功返回0,失败返回-1

等待pcm设备

int aos_pcm_wait(aos_pcm_t *pcm, int timeout);
  • 参数:
    • pcm:aos_pcm_t指针
    • timeout:超时时间
  • 返回值:
    • 成功返回0,失败返回-1

覆盖pcm设备

int aos_pcm_recover(aos_pcm_t *pcm, int err, int silent);
  • 参数:
    • pcm:aos_pcm_t指针
    • err:错误码
    • silent:静音模式
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的channels参数

int aos_pcm_hw_params_set_channels(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的rate参数

int aos_pcm_hw_params_set_rate(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val, int dir);
int aos_pcm_hw_params_set_rate_near(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int *val, int *dir);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
    • dif:方向选择
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的采样率

int aos_pcm_hw_params_set_rate_resample(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中buffer

int aos_pcm_hw_params_set_export_buffer(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件周期

int aos_pcm_hw_params_set_period_wakeup(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val);
int aos_pcm_hw_params_set_period_time(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val, int dir);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
    • dir:方向选择
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件中的部分参数,format/access/channels/rate

int aos_pcm_set_params(aos_pcm_t *pcm, int format, aos_pcm_access_t acc, unsigned int channels, unsigned int rate, int soft_resample, unsigned int latency);
  • 参数:
    • pcm:aos_pcm_t指针
    • format:格式
    • acc:交错模式
    • channels:通道
    • soft_resample:采样率
    • latency:延迟
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm硬件周期

int aos_pcm_hw_params_set_periods(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val, int dir);
int aos_pcm_hw_params_set_period_time(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, unsigned int val, int dir);
int aos_pcm_hw_params_set_period_size(aos_pcm_t *pcm, aos_pcm_hw_params_t *params, aos_pcm_uframes_t val, int dir);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:硬件参数
    • val:参数值
    • dir:方向选择
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件当前参数

int aos_pcm_sw_params_current(aos_pcm_t *pcm, aos_pcm_sw_params_t *params);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件时间戳模式

int aos_pcm_sw_params_set_tstamp_mode(aos_pcm_t *pcm,aos_pcm_sw_params_t *params, int val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件avail_min参数

int aos_pcm_sw_params_set_avail_min(aos_pcm_t *pcm, aos_pcm_sw_params_t *params, aos_pcm_uframes_t val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件事件周期

int aos_pcm_sw_params_set_period_event(aos_pcm_t *pcm, aos_pcm_sw_params_t *params, int val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件开始阈值

int aos_pcm_sw_params_set_start_threshold(aos_pcm_t *pcm, aos_pcm_sw_params_t *params, aos_pcm_uframes_t val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件停止阈值

int aos_pcm_sw_params_set_stop_threshold(aos_pcm_t *pcm,aos_pcm_sw_params_t *params, aos_pcm_uframes_t val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件静音阈值

int aos_pcm_sw_params_set_silence_threshold(aos_pcm_t *pcm, aos_pcm_sw_params_t *params, aos_pcm_uframes_t val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

设置pcm软件静音大小

int aos_pcm_sw_params_set_silence_size(aos_pcm_t *pcm, aos_pcm_sw_params_t *params, aos_pcm_uframes_t val);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
    • val:参数值
  • 返回值:
    • 成功返回0,失败返回-1

检查pcm软件参数合法性

int aos_pcm_sw_params(aos_pcm_t *pcm, aos_pcm_sw_params_t *params);
  • 参数:
    • pcm:aos_pcm_t指针
    • params:软件参数
  • 返回值:
    • 成功返回0,失败返回-1

字节数据转换成帧数据

aos_pcm_sframes_t aos_pcm_bytes_to_frames(aos_pcm_t *pcm, ssize_t bytes);
  • 参数:
    • pcm:aos_pcm_t指针
    • bytes:字节
  • 返回值:
    • aos_pcm_sframes_t: 返回帧格式数据

帧数据转换成字节数据

ssize_t aos_pcm_frames_to_bytes(aos_pcm_t *pcm, aos_pcm_sframes_t frames);
  • 参数:
    • pcm:aos_pcm_t 指针
    • frames:帧格式数据
  • 返回值:
    • ssize_t: 返回字节数。

MIXER

相关数据结构

/** Mixer simple element channel identifier */
typedef enum _aos_mixer_selem_channel_id {
    /** Unknown */
    AOS_MIXER_SCHN_UNKNOWN = -1,
    /** Front left */
    AOS_MIXER_SCHN_FRONT_LEFT = 0,
    /** Front right */
    AOS_MIXER_SCHN_FRONT_RIGHT,
    /** Rear left */
    AOS_MIXER_SCHN_REAR_LEFT,
    /** Rear right */
    AOS_MIXER_SCHN_REAR_RIGHT,
    /** Front center */
    AOS_MIXER_SCHN_FRONT_CENTER,
    /** Woofer */
    AOS_MIXER_SCHN_WOOFER,
    /** Side Left */
    AOS_MIXER_SCHN_SIDE_LEFT,
    /** Side Right */
    AOS_MIXER_SCHN_SIDE_RIGHT,
    /** Rear Center */
    AOS_MIXER_SCHN_REAR_CENTER,
    AOS_MIXER_SCHN_LAST = 31,
    /** Mono (Front left alias) */
    AOS_MIXER_SCHN_MONO = AOS_MIXER_SCHN_FRONT_LEFT
} aos_mixer_selem_channel_id_t;

typedef struct aos_mixer_selem_regopt {
    int ver;
    enum aos_mixer_selem_regopt_abstract abstract;
    const char *device;
    aos_pcm_t  *playback_pcm;
    aos_pcm_t  *capture_pcm;
} aos_mixer_selem_regopt_t;

typedef struct elem_inter {
    int cur;
    int min;
    int max;
} elem_inter_t;

typedef struct sm_elem_ops {
    int (*set_dB)(aos_mixer_elem_t *elem, int l_dB, int r_dB);
    int (*volume_to_dB)(aos_mixer_elem_t *elem, int value);
    // will deprecate
    int (*set_volume)(aos_mixer_elem_t *elem, int l_value, int r_value);
} sm_elem_ops_t;

typedef struct aos_mixer aos_mixer_t;
typedef struct _aos_mixer_elem aos_mixer_elem_t;

打开一个mixer

int aos_mixer_open(aos_mixer_t **mixer, int mode);
  • 参数:
    • mixer:aos_mixer_t 指针
    • mode:暂未使用,默认填0
  • 返回值:
    • 成功返回0,失败返回-1

关闭mixer

int aos_mixer_close(aos_mixer_t *mixer);
  • 参数:
    • mixer:aos_mixer_t 指针
  • 返回值:
    • 成功返回0,失败返回-1

设置elem播放的音量

int aos_mixer_selem_set_playback_volume(aos_mixer_elem_t *elem, aos_mixer_selem_channel_id_t channel, int value);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • channel:通道
    • value:音量值
  • 返回值:
    • 成功返回0,失败返回-1

设置elem捕获的音量

int aos_mixer_selem_set_capture_volume(aos_mixer_elem_t *elem, aos_mixer_selem_channel_id_t channel, int value);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • channel:通道
    • value:音量值
  • 返回值:
    • 成功返回0,失败返回-1

设置elem播放的所有音量

int aos_mixer_selem_set_playback_volume_all(aos_mixer_elem_t *elem, int value);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • value:音量值
  • 返回值:
    • 成功返回0,失败返回-1

设置elem捕获的所有音量

int aos_mixer_selem_set_capture_volume_all(aos_mixer_elem_t *elem, int value);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • value:音量值
  • 返回值:
    • 成功返回0,失败返回-1

设置elem播放一定范围的音量

int aos_mixer_selem_set_playback_volume_range(aos_mixer_elem_t *elem, int min, int max);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • min:音量最小值
    • max:音量最大值
  • 返回值:
    • 成功返回0,失败返回-1

设置elem捕获一定范围的音量

int aos_mixer_selem_set_capture_volume_range(aos_mixer_elem_t *elem, int min, int max);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • min:音量最小值
    • max:音量最大值
  • 返回值:
    • 成功返回0,失败返回-1

获取elem播放的音量

int aos_mixer_selem_get_playback_volume(aos_mixer_elem_t *elem, aos_mixer_selem_channel_id_t channel, int *value);
  • 参数:
    • elem:aos_mixer_elem_t 指针
    • channel:通道
    • value:音量值
  • 返回值:
    • 成功返回0,失败返回-1

加载mixer

int aos_mixer_load(aos_mixer_t *mixer);
  • 参数:
    • mixer:aos_mixer_t指针
  • 返回值:
    • 成功返回0,失败返回-1

创建声音元素

int snd_elem_new(aos_mixer_elem_t **elem_ret, const char *name, sm_elem_ops_t *ops);
  • 参数:
    • elem_ret:elem句柄
    • name:elem名称
    • ops:回调
  • 返回值:
    • 成功返回0,失败返回-1

释放mixer

void aos_mixer_free(aos_mixer_t *mixer);
  • 参数:
    • mixer:aos_mixer_t指针
  • 返回值:
    • 成功返回0,失败返回-1

查找第一个elem

aos_mixer_elem_t *aos_mixer_first_elem(aos_mixer_t *mixer);
  • 参数:
    • mixer:aos_mixer_t 指针
  • 返回值:
    • 指针:成功
    • NULL:失败

查找最后一个elem

aos_mixer_elem_t *aos_mixer_last_elem(aos_mixer_t *mixer);
  • 参数:
    • mixer:aos_mixer_t 指针
  • 返回值:
    • 指针:成功
    • NULL:失败

查找下一个elem

aos_mixer_elem_t *aos_mixer_elem_next(aos_mixer_elem_t *elem);
  • 参数:
    • elem:aos_mixer_elem_t指针
  • 返回值:
    • 指针:成功
    • NULL:失败

查找前一个elem

aos_mixer_elem_t *aos_mixer_elem_prev(aos_mixer_elem_t *elem);
  • 参数:
    • elem:aos_mixer_elem_t指针
  • 返回值:
    • 指针:成功
    • NULL:失败

查找并打开一个mixer

int aos_mixer_attach(aos_mixer_t *mixer, const char *name);
  • 参数:
    • mixer:aos_mixer_t 指针
    • name:名称
  • 返回值:
    • 0:成功
    • -1:失败

驱动层

CARD

static snd_card_drv_t snd_card_drv = {
    .drv = {
        .name   = "card",
        .init   = card_init, //声卡初始化,并注册pcm及mixer设备
        .uninit = card_uninit, //声卡去初始化,销毁pcm及mixer设备
        .open   = card_open,//打开声卡
        .close  = card_close,//关闭声卡
    }
};

PCM

static aos_pcm_drv_t aos_pcm_drv[] = {
    {
        .drv = {
            .name               = "pcmP",
            .init               = pcm_init,//playback设备初始化
            .uninit             = pcm_uninit,//playback设备去初始化
            .open               = pcmp_open,//打开playback设备
            .close              = pcmp_close,//关闭playback设备
        },
        .ops = {
            .hw_params_set      = pcmp_param_set,//设置playback设备
            .write              = pcm_send,//playback设备发送pcm数据
            .pause              = pcm_pause,//playback设备暂停发送数据
        },
    },
    {
        .drv = {
            .name               = "pcmC",
            .init               = pcm_init,//capture设备初始化
            .uninit             = pcm_uninit,//capture设备去初始化
            .open               = pcmc_open,//打开capture设备
            .close              = pcmc_close,//关闭capture设备
        },
        .ops = {
            .hw_params_set = pcmc_param_set,//设置capture设备
            .read = pcm_recv,//capture设备接收数据
        },
    }
};

MIXER

static sm_elem_ops_t elem_codec1_ops = {
    .set_dB       = snd_set_gain,//设置设备音量(单位:dB)
    .volume_to_dB = snd_volume_to_dB,//0-100转换为dB值
};

示例代码

A Minimal Playback Program

void playback (void)
{
    int i;
    char buf[80];
    aos_pcm_t *playback_handle;
    aos_pcm_hw_params_t *hw_params;

    aos_pcm_open (&playback_handle, "pcmP0", AOS_PCM_STREAM_PLAYBACK, 0); //打开设备“pcmP0”

    aos_pcm_hw_params_alloca(&hw_params); //申请硬件参数内存空间

    aos_pcm_hw_params_any(playback_handle, hw_params); //初始化硬件参数
    aos_pcm_hw_params_set_access(playback_handle, hw_params, AOS_PCM_ACCESS_RW_INTERLEAVED); // 设置音频数据参数为交错模式
    aos_pcm_hw_params_set_format(playback_handle, hw_params, 16); //设置音频数据参数为小端16bit
    aos_pcm_hw_params_set_rate_near(playback_handle, hw_params, 16000, 0); //设置音频数据参数采样率为16K
    aos_pcm_hw_params_set_channels(playback_handle, hw_params, 2); //设置音频数据参数为2通道
    aos_pcm_hw_params_set_period_size_near(pcm, params, 20, 0);//设置音频事件触发周期为20帧
    aos_pcm_hw_params_set_buffer_size_near(pcm, params, 60);//设置播放缓存为60帧

    aos_pcm_hw_params(playback_handle, hw_params); //设置硬件参数到具体硬件中

    for (i = 0; i < 10; ++i) {
        aos_pcm_writei(playback_handle, buf, 128);//发送交错音频数据
    }

    aos_pcm_close (playback_handle); //关闭设备
}

A Minimal Capture Program

void captrue(void)
{
    int i;
    int ret;
    char buf[80];
    aos_pcm_t *capture_handle;
    aos_pcm_hw_params_t *hw_params;

    aos_pcm_open (&capture_handle, "pcmC0", AOS_PCM_STREAM_CAPTURE, 0);//打开设备“pcmC0”

    aos_pcm_hw_params_alloca (&hw_params);//申请硬件参数内存空间

    aos_pcm_hw_params_any (capture_handle, hw_params);//初始化硬件参数
    aos_pcm_hw_params_set_access (capture_handle, hw_params, AOS_PCM_ACCESS_RW_INTERLEAVED);// 设置音频数据参数为交错模式
    aos_pcm_hw_params_set_format (capture_handle, hw_params, 16);//设置音频数据参数为小端16bit
    aos_pcm_hw_params_set_rate_near (capture_handle, hw_params, 16000, 0);//设置音频数据参数采样率为16K
    aos_pcm_hw_params_set_channels (capture_handle, hw_params, 2);//设置音频数据参数为2通道
    aos_pcm_hw_params_set_period_size_near(pcm, params, 20, 0);//设置录音事件触发周期为20帧
    aos_pcm_hw_params_set_buffer_size_near(pcm, params, 60);//设置录音缓存为60帧

    aos_pcm_hw_params (capture_handle, hw_params);//设置硬件参数到具体硬件中


    for (i = 0; i < 100; ++i) {
        aos_pcm_wait (capture_handle);
        aos_pcm_readi (capture_handle, buf, 20);//接收交错音频数据
    }

    aos_pcm_close (capture_handle);//关闭设备
}

A Minimal Mixer

void mixer(void)
{
    aos_mixer_t *mixer_hdl;
    aos_mixer_elem_t *elem;

    aos_mixer_open(&mixer_hdl, 0);  //申请mixer
    aos_mixer_attach(mixer_hdl, "card0"); //查找声卡 ”card0“
    aos_mixer_load(mixer_hdl); // 装载mxier
    elem = aos_mixer_first_elem(mixer_hdl); //查找第一个元素

    aos_mixer_selem_set_playback_volume_all(elem, 60); //设置音量为60
}

results matching ""

    No results matching ""