一、ALSA架构

ALSA(Advanced Linux Sound Architecture)即高级 Linux 声音架构。
嵌入式移动设备的音频子系统目前主要是ALSA 驱动 asoc 框架,其中包括 codec driver、 platform driver、 machine driver 等。 codec driver只关心 codec 本身; platform driver 主要包括平台 cpu dai( 如 i2s) , dma 等部分;machine driver 主要将 platform driver 和 codec driver 连接起来。 platform 和 dai driver 一般修改较少, 主要修改machine 和 codec driver,这也将是我们后续分析的重点。

ALSA架构图如下:

可以看看之前写的文章进行对比参照:
Linux—ALSA音频工具arecord、aplay、amixer使用
ALSA驱动asoc框架之machine
ALSA驱动asoc框架之Codec
ALSA驱动asoc框架之Platform

二、其他说明

1.PCM接口

最简单的音频接口是PCM(脉冲编码调制)接口,该接口由时钟脉冲(BCLK)、帧同步信号(FS)及接收数据(DR)和发送数据(DX)组成。在FS信号的上升沿数据传输从MSB开始,FS频率等于采样频率。FS信号之后开始数据字的传输,单个数据位按顺序进行传输,一个时钟周期传输一个数据字。PCM接口很容易实现,原则上能够支持任何数据方案和任何采样频率,但需要每个音频通道获得一个独立的数据队列。

2.IIS接口

IIS接口在20世纪80年代首先被PHILIPS用于消费音频产品,并在一个称为LRCLK(Left/Right CLOCK)的信号机制中经过转换,将两路音频变成单一的数据队列。当LRCLK为高时,左声道数据被传输;LRCLK为低时,右声道数据被传输。与CPM相比,IIS更适合于立体声系统,当然,IIS的变体也支持多通道的时分复用,因此可以支持多通道。

3.linux ALSA音频设备驱动

ALSA系统包括包括驱动包alsa-driver、开发包alsa-libs、开发包插件alsa-libplugins、设置管理工具包alsa-utils、其他声音相关处理小程序包alsa-tools、特殊音频固件支持包alsa-firmware、OSS接口兼容模拟层工具alsa-oss供7个子项目,其中只有驱动包是必需的。

目前ALSA内核提供给用户空间的接口有:

(1)设备信息接口(/proc/asound)
(2)设备控制接口(/dev/snd/controlCX)
(3)混音器设备接口(/dev/snd/mixerCXDX)
(4)PCM设备接口(/dev/snd/pcmCXDX)
(5)原始MIDI(迷笛)设备接口(/dev/snd/midiCXDX)
(6)声音合成(synthesizer)设备接口(/dev/snd/seq)
(7)定时器接口(/dev/snd/timer)
这些接口被提供给alsa-lib使用,而不是给应用程序使用,应用程序最好使用alsa-lib,或者更高级的接口比如jack提供的接口。

4.linux ASoC音频设备驱动

ASoC是ALSA在SoC方面的发展和演变,它的本质仍然属于ALSA,但是在ALSA架构基础上对CPU相关的代码和Codec相关的代码进行了分离,其原因是采用传统ALSA架构情况下,同一型号的Codec工作于不同的CPU时,需要不同的驱动,这是不符合代码重用的要求的。

ASoC主要由3部分组成:
(1)Codec驱动,这一部分只关系Codec本身,与CPU相关的特性不由此部分操作
(2)平台驱动,这一部分只关心CPU本身,不关系Codec,它主要处理了两个问题:DMA引擎和SoC解除的PCM、IIS或AC’97数字接口控制。
(3)板驱动,这一部分将平台驱动和Codec驱动绑定在一起,描述了板一级的硬件特征

以上3部分中,1和2基本都可以仍然是通用的驱动了,即Codec驱动认为自己可以连接任意CPU,而CPU的IIS、PCM、或AC’97接口对应的平台驱动则认为自己可以连接符号其接口类型的Codec,只有3是不通用的,由特定的电路板上具体的CPU和Codec确定,因此它很像一个插座,上面插着Codec和平台这两个插头。ASoC的用户空间编程方法与ALSA完全一致。

5.card声卡和组件管理

/* 对于每个声卡,必须创建一个card实例,该函数用于创建card参数idx为索引号参数id为标识的字符串参数module一般指向THIS_MODULE参数extra_size是要分配的额外的数据的大小,分配的extra_size大小的内存将作为card->private_data*/struct snd_card *snd_card_new(int idx, const char *id,struct module *module, int extra_size)/* 注册声卡 */
int snd_card_register(struct snd_card *card)/* 释放(注销)声卡 */
int snd_card_free(struct snd_card *card)/* 创建一个ALSA设备部件参数type为设备类型,查看宏SNDRV_DEV_XXX*/
int snd_device_new(struct snd_card *card, snd_device_type_t type,void *device_data, struct snd_device_ops *ops)/* 释放声卡的设备参数device_data指向要设备的私有数据*/
int snd_device_free(struct snd_card *card, void *device_data)

6.PCM 设备接口

/* 创建PCM实例参数card指向声卡参数id是标识字符串参数device为PCM设备引索(0表示第1个PCM设备)参数playback_count为播放设备的子流数参数capture_count为录音设备的子流数参数指向构造的PCM实例*/
int snd_pcm_new(struct snd_card *card, const char *id, int device,int playback_count, int capture_count,struct snd_pcm ** rpcm)/* 设置PCM操作函数 参数direction,查看宏SNDRV_PCM_STREAM_XXX*/
void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)/* 分配DMA缓冲区,仅当DMA缓冲区已预分配的情况下才可调用该函数 */
int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size)/* 释放由snd_pcm_lib_malloc_pages函数分配的一致性DMA缓冲区 */
int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)/* 分配缓冲区的最简单的方法是调用该函数type的取值可查看宏SNDRV_DMA_TYPE_* */
int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,int type, void *data,size_t size, size_t max)

7.控制接口说明

/* 创建一个control实例----struct snd_kcontrol结构体参数ncontrol为初始化记录private_data为设置的私有数据 */
struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, void *private_data)/* 为声卡添加一个控制实例 */
int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)/* 驱动程序可中断服务程序中调用该函数来改变或更新一个control*/
void snd_ctl_notify(struct snd_card *card, unsigned int mask,struct snd_ctl_elem_id *id)

8.基于ALSA音频框架的驱动程序设计

1:struct snd_card *snd_card_new(int idx, const char *id,struct module *module, int extra_size);/* 创建一个声卡 */2:static struct snd_device_ops ops = {.dev_free =     xxx_free,};3:struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,void *private_data); /*  创建control实例 */4:int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol);    /* 为声卡添加控制实例 */5:int snd_device_new(struct snd_card *card, SNDRV_DEV_CODEC,void *device_data, struct snd_device_ops *ops);  /* 创建一个ALSA设备部件-----编解码设备 */6:int snd_pcm_new(struct snd_card *card, const char *id, int device,int playback_count, int capture_count,struct snd_pcm ** rpcm) /*  创建PCM实例 */8:int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,int type, void *data,size_t size, size_t max)  /* 分配缓冲区 */9:void snd_pcm_set_ops(struct snd_pcm *pcm, SNDRV_PCM_STREAM_PLAYBACK, struct snd_pcm_ops *ops)  /* 设置PCM操作函数----播放  */10:void snd_pcm_set_ops(struct snd_pcm *pcm, SNDRV_PCM_STREAM_CAPTURE, struct snd_pcm_ops *ops)  /* 设置PCM操作函数 ----录音 */11:/*音频相关的初始化:音频流相关、引脚等相关的初始化*/12:DMA相关的设置13:int snd_card_register(struct snd_card *card);  /* 注册声卡 */IRQ_AC97

9.ASoC音频驱动重要结构体

/**1:ASoC Codec驱动:*/struct snd_soc_dai { /* 数字音频接口,描述了Codec DAI(数字音频接口---Digital Audio Interface)和PCM配置 *//* DAI description */char *name;  /* 名字 */unsigned int id;  /* ID */int ac97_control;struct device *dev;/* DAI callbacks */int (*probe)(struct platform_device *pdev,struct snd_soc_dai *dai);void (*remove)(struct platform_device *pdev,struct snd_soc_dai *dai);int (*suspend)(struct snd_soc_dai *dai);int (*resume)(struct snd_soc_dai *dai);/* ops */struct snd_soc_dai_ops *ops;  /* DAI操作函数 *//* DAI capabilities */struct snd_soc_pcm_stream capture;  /* 录音流 */struct snd_soc_pcm_stream playback; /* 播放流 *//* DAI runtime info */struct snd_pcm_runtime *runtime;  /* PCM运行时 */struct snd_soc_codec *codec;  /* 编解码器 */unsigned int active;unsigned char pop_wait:1;void *dma_data;/* DAI private data */void *private_data;/* parent codec/platform */union {struct snd_soc_codec *codec;  /* 编解码器 */struct snd_soc_platform *platform;  /* 平台驱动----CPU */};struct list_head list; /* 用于形成链表 */
};struct snd_soc_codec { /* SoC音频编解码器---IO操作、动态音频电源管理以及时钟、PLL等控制 */char *name;  /* 名字 */struct module *owner; /* THIS_MODULE */struct mutex mutex;struct device *dev;struct list_head list; /* 用于形成链表 *//* callbacks */int (*set_bias_level)(struct snd_soc_codec *,enum snd_soc_bias_level level); /* 回调函数 *//* runtime */struct snd_card *card;  /* 声卡 */struct snd_ac97 *ac97;  /* for ad-hoc ac97 devices */ /* AC97设备 */unsigned int active;unsigned int pcm_devs;void *private_data;/* codec IO */void *control_data; /* codec control (i2c/3wire) data */unsigned int (*read)(struct snd_soc_codec *, unsigned int);int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);int (*display_register)(struct snd_soc_codec *, char *,size_t, unsigned int);hw_write_t hw_write;hw_read_t hw_read;void *reg_cache;short reg_cache_size;short reg_cache_step;/* dapm */u32 pop_time;struct list_head dapm_widgets;struct list_head dapm_paths;enum snd_soc_bias_level bias_level;enum snd_soc_bias_level suspend_bias_level;struct delayed_work delayed_work;/* codec DAI's */struct snd_soc_dai *dai; /* SoC层接口 */unsigned int num_dai;#ifdef CONFIG_DEBUG_FSstruct dentry *debugfs_reg;struct dentry *debugfs_pop_time;
#endif
};struct snd_soc_dai_ops { /* 数字音频接口DAI操作函数集 *//** DAI clocking configuration, all optional.* Called by soc_card drivers, normally in their hw_params.*/int (*set_sysclk)(struct snd_soc_dai *dai,int clk_id, unsigned int freq, int dir); /* 设置系统时钟 */int (*set_pll)(struct snd_soc_dai *dai,int pll_id, unsigned int freq_in, unsigned int freq_out); /* 设置PLL */int (*set_clkdiv)(struct snd_soc_dai *dai, int div_id, int div);  /* 设置时钟分频 *//** DAI format configuration* Called by soc_card drivers, normally in their hw_params.*//* DAI格式配置 */int (*set_fmt)(struct snd_soc_dai *dai, unsigned int fmt); /* 设置格式 */int (*set_tdm_slot)(struct snd_soc_dai *dai,unsigned int mask, int slots);int (*set_tristate)(struct snd_soc_dai *dai, int tristate);/** DAI digital mute - optional.* Called by soc-core to minimise any pops.*/int (*digital_mute)(struct snd_soc_dai *dai, int mute); /* 数字静音 *//** ALSA PCM audio operations - all optional.* Called by soc-core during audio PCM operations.*//* ALSA PCM音频操作 */int (*startup)(struct snd_pcm_substream *,struct snd_soc_dai *);void (*shutdown)(struct snd_pcm_substream *,struct snd_soc_dai *);int (*hw_params)(struct snd_pcm_substream *,struct snd_pcm_hw_params *, struct snd_soc_dai *);int (*hw_free)(struct snd_pcm_substream *,struct snd_soc_dai *);int (*prepare)(struct snd_pcm_substream *,struct snd_soc_dai *);int (*trigger)(struct snd_pcm_substream *, int,struct snd_soc_dai *);
};struct snd_soc_ops { /* SoC操作函数----Codec音频操作 */int (*startup)(struct snd_pcm_substream *);void (*shutdown)(struct snd_pcm_substream *);int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);int (*hw_free)(struct snd_pcm_substream *);int (*prepare)(struct snd_pcm_substream *);int (*trigger)(struct snd_pcm_substream *, int);
};/**2:ASoC平台驱动:**在ASoC平台驱动部分,同样存在着Codec驱动中的snd_soc_dai、snd_soc_dai_ops、snd_soc_ops这三个结构体的实例用于描述*DAI和DAI的操作,不过不同的是,在平台驱动中,它们只描述CPU相关的部分而不描述Codec。除此之外,在ASoC*平台驱动中,必须实现完整的DMA驱动,即传统ALSA的and_pcm_ops结构体成员函数trigger()、pointer()等,因此ASoC平台*驱动通常由DAI和DMA两部分组成。*//**ASoC板驱动:*在板驱动的模块初始化函数中,会通过platform_device_add()注册一个名为"soc-audio"的platform设备,这是因为soc-core.c*注册了一个名为"soc-audio"的platform驱动*/struct snd_soc_device { /* SoC设备 */struct device *dev; /* 内嵌的设备模型的设备 */struct snd_soc_card *card; /* SoC卡 */struct snd_soc_codec_device *codec_dev;  /* SoC编解码设备 */void *codec_data;  /* 编解码设备使用的数据 */
};struct snd_soc_dai_link  { /* 绑定ASoC Codec驱动和CPU端的平台驱动数据结构 */char *name;          /* Codec name */ /* 编解码器的名字 */char *stream_name;        /* Stream name */ /* 流的名字 *//* DAI */struct snd_soc_dai *codec_dai; /* SoC层的接口----编解码器端 */struct snd_soc_dai *cpu_dai;     /* SoC层的接口----CPU端*//* machine stream operations */struct snd_soc_ops *ops; /* SoC操作函数----流操作函数 *//* codec/machine specific init - e.g. add machine controls */int (*init)(struct snd_soc_codec *codec);  /* 初始化 *//* DAI pcm */struct snd_pcm *pcm; /* 指向pcm */
};

音频ALSA架构简介相关推荐

  1. Linux ALSA 之一:ALSA 架构简介

    一.概述 ALSA是 Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构. 在 Linux 内核设备驱动层,ALSA 提供了 als ...

  2. Linux ALSA声卡驱动之一:ALSA架构简介和ASOC架构简介

    ALSA声卡驱动: 1.Linux ALSA声卡驱动之一:ALSA架构简介和ASOC架构简介 2.Linux ALSA声卡驱动之二:Platform 3. Linux ALSA声卡驱动之三:Platf ...

  3. Linux ALSA驱动框架(一)--ALSA架构简介--声卡的创建

    (1)ALSA简介 (1) Native ALSA Application:tinyplay/tinycap/tinymix,这些用户程序直接调用 alsa 用户库接口来实现放音.录音.控制 ALSA ...

  4. linux alsa声卡命令,Linux ALSA声卡驱动之一:ALSA架构简介

    一.  概述 ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构,想了解更多的关于ALSA的这一开源项目的信息和知识,请查 ...

  5. Linux ALSA声卡驱动之一:ALSA架构简介

    一.  概述 ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构,想了解更多的关于ALSA的这一开源项目的信息和知识,请查 ...

  6. Linux ALSA 声卡驱动之一:ALSA架构简介

    声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! 一.  概述 ALSA是Advanced Linux Sound Architecture ...

  7. Android ALSA音频系统架构分析(1)----从Loopback了解Audio

    /*********************************** * Author:刘江明 * Environment:MTK Android 6.0 * Date:2017年05月25日 * ...

  8. LINUX音频驱动架构

    https://www.jianshu.com/p/0f5fe8f51b66 简介 LINUX下音频驱动开发,要遵循标准的ALSA架构, 下面分别从硬件架构.软件架构.驱动程序,3个方面分析. 硬件架 ...

  9. a55计算机主板,A55架构简介与A55主板赏析

    A55架构简介与主板赏析: A55 FCH芯片架构 FCH芯片的角色大概相当于以往的南桥芯片,自身并没有整合显示核心,而是通过UMI接口(而不是HT总线)连接APU整合的显示核心进行输出.A55 FC ...

最新文章

  1. python字符串基本形式_python字符串常用方式
  2. Java 开发技巧详细知识体系总结
  3. 2011年8月51CTO壁纸点评活动获奖名单【已结束】
  4. mysql获取当月数据_MySQL中获取天、周、月等数据
  5. ubuntu权限管理
  6. 行业观察丨激荡二十年——货代软件1999—2019
  7. idea上一些比较有用的插件
  8. 西瓜书学习记录-神经网络(第五章)
  9. 45K!拿下 AI 技术岗,这些知识点全考了!
  10. Java Excel(jxl)学习笔记
  11. Linux中的Tomcat500异常,Linux处理Tomcat异常的方法
  12. 【AD10】Altium Designer 10导入元件库图文教程
  13. 压缩包上传 压缩并解压缩Rar/Zip
  14. 【微信小程序多人开发的配置流程】
  15. 隐函数求导公式(多元隐函数存在定理)
  16. AI上推荐 之 NeuralCF与PNN模型(改变特征交叉方式)
  17. 对MYSQL的explain中extra using where的理解
  18. 12.2烤仔建工×MetaEstate | K歌之王在元宇宙的秘密据点
  19. 基于AForge.Net框架的扑克牌识别
  20. NBA名人堂之-威尔特·张伯伦|乔治·迈肯|比尔·沃顿|戴夫·考恩斯|威利斯·里德

热门文章

  1. 我的服装DRP之在线升级
  2. 蓝桥杯模块学习17——AT24C02存储器(深夜学习——单片机)
  3. 爬虫:SNKRS电商网站应该怎么反爬,反BOT
  4. 高通SDM450 Android9上调试RS485接口温湿度传感器调试介绍
  5. Oracle PL/SQL Nutshell
  6. CVE-2020-3119 Cisco CDP 协议栈溢出漏洞分析
  7. Derby的安装与使用
  8. 初探Eigen(error C2719: “Qcw”: 具有 __declspec(align('16')) 的形参将不被对齐)
  9. 动态调整模型中的学习率
  10. pc端以及移动端页面响应布局,同比例缩放