1 ALSA 分析 下载本文

内容发布更新时间 : 2024/5/7 14:47:00星期一 下面是文章的全部内容请认真阅读。

ALSA 音频子系统分析

一. 概述

ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构。

在内核设备驱动层,ALSA提供了alsa-driver,同时在应用层,ALSA为我们提供了alsa-lib,应用程序只要调用alsa-lib提供的API,即可以完成对底层音频硬件的控制。

二. ALSA设备文件结构

? ? ? ? ? ?

controlC0 --> 用于声卡的控制,例如通道选择,混音,麦克风的控制等 midiC0D0 --> 用于播放midi音频 pcmC0D0c --〉 用于录音的pcm设备 pcmC0D0p --〉 用于播放的pcm设备 seq --〉 音序器 timer --〉 定时器

其中,C0D0代表的是声卡0中的设备0,pcmC0D0c最后一个c代表capture,pcmC0D0p最后一个p代表playback,这些都是alsa-driver中的命名规则。从上面的列表可以看出,我的声卡下挂了6个设备,根据声卡的实际能力,驱动实际上可以挂上更多种类的设备。

三.声卡驱动分析 1. struct snd_card

1.1. snd_card是什么

snd_card可以说是整个ALSA音频驱动最顶层的一个结构,整个声卡的软件逻辑结构开始于该结构,几乎所有与声音相关的逻辑设备都是在snd_card的管理之下,声卡驱动的第一个动作通常就是创建一个snd_card结构体。正因为如此,本节中,我们也从 struct cnd_card开始吧。

1.2. snd_card的定义

snd_card的定义位于改头文件中:include/sound/core.h /* main structure for soundcard */ struct snd_card {

int number;

/* number of soundcard (index to

snd_cards) */

char id[16]; char driver[16];

/* id string of this card */ /* driver name */

/* short name of this soundcard */ /* name of this soundcard */ /* mixer name */

/* card components delimited with space */

/* top-level module */

char shortname[32]; char longname[80];

char mixername[80]; char components[128];

struct module *module; void *private_data;

/* private data for soundcard */

void (*private_free) (struct snd_card *card); /* callback for freeing of

private data */

struct list_head devices; /* devices */

unsigned int last_numid; /* last used numeric ID */ struct rw_semaphore controls_rwsem; /* controls list lock */ rwlock_t ctl_files_rwlock; /* ctl_files list lock */

int controls_count; int user_ctl_count;

/* count of all controls */ /* count of all user controls */

struct list_head controls; /* all controls for this card */ struct list_head ctl_files;

/* active control files */

struct snd_info_entry *proc_root; /* root for soundcard specific files */

struct snd_info_entry *proc_id; /* the card id */ struct proc_dir_entry *proc_root_link;

/* number link to real id */

struct list_head files_list; /* all files associated to this card */

struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown

state */

spinlock_t files_lock; int shutdown;

/* lock the files for this card */

/* this card is going down */

/* free in context of file_release */

int free_on_last_close;

wait_queue_head_t shutdown_sleep; struct device *dev;

/* device assigned to this card */

#ifndef CONFIG_SYSFS_DEPRECATED

struct device *card_dev;

/* cardX object for sysfs */

#endif