全部文档
OneOS简介 硬件支持 编译构造工具 API参考文档 高级语言 用户编程手册 应用笔记 FAQ

ADUIO设备


API 列表

函数 说明
os_device_find() 根据AUDIO设备名称寻找设备并获取设备指针
os_device_open() 打开设备
os_device_write() 往设备写数据
os_device_close() 关闭设备
os_device_control() 控制设备音量与参数

os_device_find

该函数根据设备名查找对应的AUDIO设备,函数原型如下:

os_device_t *os_device_find(const char *name);
参数 说明
name AUDIO设备名称
返回 说明
设备指针 查找到对应设备将返回相应的设备指针
OS_NULL 没有找到设备

os_device_open

该函数根据AUDIO设备名及flag信息使能AUDIO设备,函数原型如下:

os_err_t os_device_open(struct os_device *dev, os_uint16_t oflag)
参数 说明
dev AUDIO设备指针
oflag 设备模式标志,以写方式打开,即取值:OS_DEVICE_OFLAG_WRONLY
返回 说明
OS_EOK 成功
其他错误码 失败

os_device_close

该函数用于关闭AUDIO设备,函数原型如下:

os_err_t os_device_close(struct os_device *dev)
参数 说明
dev AUDIO设备指针
返回 说明
OS_EOK 成功
其他错误码 失败

os_device_control

该函数用于控制AUDIO设备的参数,函数原型如下:

os_err_t os_device_control(os_device_t *dev, int cmd, void *arg);
参数 说明
dev 设备指针
cmd 命令控制字
arg 控制的参数
返回 说明
OS_EOK 函数执行成功
其他错误码 失败

使用示例

AUDIO设备使用示例

本例开启一个获得AUDIO播放器,播放格式为.wav的音乐。

#include <os_task.h>
#include <os_device.h>
#include <vfs_posix.h>
#include <audio/audio.h>
#include <shell.h>

#define BUFSZ   1024                                           
#define SOUND_DEVICE_NAME    "audio0"    /* Audio device name */
static os_device_t *snd_dev;              /* Audio device handle */  

struct RIFF_HEADER_DEF
{
    char riff_id[4];     /* 'R','I','F','F' */
    uint32_t riff_size;
    char riff_format[4]; /* 'W','A','V','E' */
};

struct WAVE_FORMAT_DEF
{
    uint16_t FormatTag;
    uint16_t Channels;
    uint32_t SamplesPerSec;
    uint32_t AvgBytesPerSec;
    uint16_t BlockAlign;
    uint16_t BitsPerSample;
};

struct FMT_BLOCK_DEF
{
    char fmt_id[4];    /* 'f','m','t',' ' */
    uint32_t fmt_size;
    struct WAVE_FORMAT_DEF wav_format;
};

struct DATA_BLOCK_DEF
{
    char data_id[4];     /* 'R','I','F','F' */
    uint32_t data_size;
};

struct wav_info
{
    struct RIFF_HEADER_DEF header;
    struct FMT_BLOCK_DEF   fmt_block;
    struct DATA_BLOCK_DEF  data_block;
};

int wavplay_sample(int argc, char **argv)
{
    int fd = -1;
    uint8_t *buffer = NULL;
    struct wav_info *info = NULL;
    struct os_audio_caps caps = {0};

    if (argc != 2)
    {
        os_kprintf("Usage:\n");
        os_kprintf("wavplay_sample song.wav\n");
        return 0;
    }

    fd = open(argv[1], O_RDONLY);
    if (fd < 0)
    {
        os_kprintf("open file failed!\n");
        goto __exit;
    }

    buffer = os_malloc(BUFSZ);
    if (buffer == OS_NULL)
        goto __exit;

    info = (struct wav_info *) os_malloc(sizeof * info);
    if (info == OS_NULL)
        goto __exit;

    if (read(fd, &(info->header), sizeof(struct RIFF_HEADER_DEF)) <= 0)    
        goto __exit;
    if (read(fd, &(info->fmt_block),  sizeof(struct FMT_BLOCK_DEF)) <= 0)
        goto __exit;
    if (read(fd, &(info->data_block), sizeof(struct DATA_BLOCK_DEF)) <= 0)
        goto __exit;

    os_kprintf("wav information:\n");
    os_kprintf("samplerate %d\n", info->fmt_block.wav_format.SamplesPerSec);
    os_kprintf("channel %d\n", info->fmt_block.wav_format.Channels);

    snd_dev = os_device_find(SOUND_DEVICE_NAME);

    os_device_open(snd_dev, OS_DEVICE_OFLAG_WRONLY);

    /* parameter settings */                         
    caps.config_type = AUDIO_PARAM_CMD;    
    caps.udata.config.samplerate = info->fmt_block.wav_format.SamplesPerSec;    
    caps.udata.config.channels   = info->fmt_block.wav_format.Channels;                                                  
    os_device_control(snd_dev, AUDIO_CTL_CONFIGURE, &caps);      
    caps.config_type = AUDIO_VOLUME_CMD;    
    caps.udata.value = 35;
    os_device_control(snd_dev, AUDIO_CTL_CONFIGURE, &caps);                                    

    while (1)
    {
        int length;

        length = read(fd, buffer, BUFSZ);  
        if (length <= 0)
            break;

        os_device_write(snd_dev, 0, buffer, length);
    }

    os_device_close(snd_dev);

__exit:

    if (fd >= 0)
        close(fd);

    if (buffer)
        os_free(buffer);

    if (info)
        os_free(info);

    return 0;
}

SH_CMD_EXPORT(wav_player, wavplay_sample, "play wav file");

运行结果如下:

sdmmc_test
sh />sdmmc_test
[255] I/VFS_FS tshell: Mount fat to / [vfs_mount][402]
Filesystem initialized!
sh />
sh />
sh />ls
Directory /:
System Volume Information<DIR>
zhou.wav            50883344
lin.wav             52616064
3.wav               43159596
zhang.wav           38382556
liu.wav             37643974
fei.wav             14018886
MUSIC               <DIR>
sh />
sh />
sh />wav_player
Usage:
wavplay_sample song.wav
sh />
sh />
sh />wav_player zhang.wav
wav information:
samplerate 44100
channel 2

注意事项

因为需要使用到TF CARD,因此需要添加相关的测试用例sdmmc_test.c。

results matching ""

    No results matching ""

    返回顶部