ImageVerifierCode 换一换
格式:DOCX , 页数:18 ,大小:41.06KB ,
资源ID:7645346      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7645346.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(IntroductiontoSoundProgrammingwithALSA.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

IntroductiontoSoundProgrammingwithALSA.docx

1、IntroductiontoSoundProgrammingwithALSAIntroduction to Sound Programming with ALSAALSA stands for the Advanced Linux Sound Architecture. It consists of a set of kernel drivers, an application programming interface (API) library and utility programs for supporting sound under Linux. In this article, I

2、 present a brief overview of the ALSA Project and its software components. The focus is on programming the PCM interfaces of ALSA, including programming examples with which you can experiment.You may want to explore ALSA simply because it is new, but it is not the only sound API available. ALSA is a

3、 good choice if you are performing low-level audio functions for maximum control and performance or want to make use of special features not supported by other sound APIs. If you already have written an audio application, you may want to add native support for the ALSA sound drivers. If your primary

4、 interest isnt audio and you simply want to play sound files, using one of the higher-level sound toolkits, such as SDL, OpenAL or those provided in desktop environments, may be a better choice. By using ALSA you are restricted to using systems running a Linux kernel with ALSA support.History of ALS

5、AThe ALSA Project was started because the sound drivers in the Linux kernel (OSS/Free drivers) were not being maintained actively and were lagging behind the capabilities of new sound technology. Jaroslav Kysela, who previously had written a sound card driver, started the project. Over time, more de

6、velopers joined, support for many sound cards was added and the structure of the API was refined.During development of the 2.5 series of Linux kernel, ALSA was merged into the official kernel source. With the release of the 2.6 kernel, ALSA will be part of the stable Linux kernel and should be in wi

7、de use.Digital Audio BasicsSound, consisting of waves of varying air pressure, is converted to its electrical form by a transducer, such as a microphone. An analog-to-digital converter (ADC) converts the analog voltages into discrete values, called samples, at regular intervals in time, known as the

8、 sampling rate. By sending the samples to a digital-to-analog converter and an output transducer, such as a loudspeaker, the original sound can be reproduced.The size of the samples, expressed in bits, is one factor that determines how accurately the sound is represented in digital form. The other m

9、ajor factor affecting sound quality is the sampling rate. The Nyquist Theorem states that the highest frequency that can be represented accurately is at most one-half the sampling rate.ALSA BasicsALSA consists of a series of kernel device drivers for many different sound cards, and it also provides

10、an API library, libasound. Application developers are encouraged to program using the library API and not the kernel interface. The library provides a higher-level and more developer-friendly programming interface along with a logical naming of devices so that developers do not need to be aware of l

11、ow-level details such as device files.In contrast, OSS/Free drivers are programmed at the kernel system call level and require the developer to specify device filenames and perform many functions using ioctl calls. For backward compatibility, ALSA provides kernel modules that emulate the OSS/Free so

12、und drivers, so most existing sound applications continue to run unchanged. An emulation wrapper library, libaoss, is available to emulate the OSS/Free API without kernel modules.ALSA has a capability called plugins that allows extension to new devices, including virtual devices implemented entirely

13、 in software. ALSA provides a number of command-line utilities, including a mixer, sound file player and tools for controlling special features of specific sound cards.ALSA ArchitectureThe ALSA API can be broken down into the major interfaces it supports: Control interface: a general-purpose facilit

14、y for managing registers of sound cards and querying the available devices. PCM interface: the interface for managing digital audio capture and playback. The rest of this article focuses on this interface, as it is the one most commonly used for digital audio applications. Raw MIDI interface: suppor

15、ts MIDI (Musical Instrument Digital Interface), a standard for electronic musical instruments. This API provides access to a MIDI bus on a sound card. The raw interface works directly with the MIDI events, and the programmer is responsible for managing the protocol and timing. Timer interface: provi

16、des access to timing hardware on sound cards used for synchronizing sound events. Sequencer interface: a higher-level interface for MIDI programming and sound synthesis than the raw MIDI interface. It handles much of the MIDI protocol and timing. Mixer interface: controls the devices on sound cards

17、that route signals and control volume levels. It is built on top of the control interface.Device NamingThe library API works with logical device names rather than device files. The device names can be real hardware devices or plugins. Hardware devices use the format hw:i,j, where i is the card numbe

18、r and j is the device on that card. The first sound device is hw:0,0. The alias default refers to the first sound device and is used in all of the examples in this article. Plugins use other unique names; plughw:, for example, is a plugin that provides access to the hardware device but provides feat

19、ures, such as sampling rate conversion, in software for hardware that does not directly support it. The dmix and dshare plugins allow you to downmix several streams and split a single stream dynamically among different applications.Sound Buffers and Data TransferA sound card has a hardware buffer th

20、at stores recorded samples. When the buffer is sufficiently full, it generates an interrupt. The kernel sound driver then uses direct memory access (DMA) to transfer samples to an application buffer in memory. Similarly, for playback, another application buffer is transferred from memory to the soun

21、d cards hardware buffer using DMA.These hardware buffers are ring buffers, meaning the data wraps back to the start when the end of the buffer is reached. A pointer is maintained to keep track of the current positions in both the hardware buffer and the application buffer. Outside of the kernel, onl

22、y the application buffer is of interest, so from here on we discuss only the application buffer.The size of the buffer can be programmed by ALSA library calls. The buffer can be quite large, and transferring it in one operation could result in unacceptable delays, called latency. To solve this, ALSA

23、 splits the buffer up into a series of periods (called fragments in OSS/Free) and transfers the data in units of a period.A period stores frames, each of which contains the samples captured at one point in time. For a stereo device, the frame would contain samples for two channels. Figure 1 illustra

24、tes the breakdown of a buffer into periods, frames and samples with some hypothetical values. Here, left and right channel information is stored alternately within a frame; this is called interleaved mode. A non-interleaved mode, where all the sample data for one channel is stored followed by the da

25、ta for the next channel, also is supported.Figure 1. The Application BufferOver and Under RunWhen a sound device is active, data is transferred continuously between the hardware and application buffers. In the case of data capture (recording), if the application does not read the data in the buffer

26、rapidly enough, the circular buffer is overwritten with new data. The resulting data loss is known as overrun. During playback, if the application does not pass data into the buffer quickly enough, it becomes starved for data, resulting in an error called underrun. The ALSA documentation sometimes r

27、efers to both of these conditions using the term XRUN. Properly designed applications can minimize XRUN and recover if it occurs.A Typical Sound ApplicationPrograms that use the PCM interface generally follow this pseudo-code:open interface for capture or playbackset hardware parameters(access mode,

28、 data format, channels, rate, etc.)while there is data to be processed: read PCM data (capture) or write PCM data (playback)close interfaceWe look at some working code in the following sections. I recommend you compile and run these on your Linux system, look at the output and try some of the sugges

29、ted modifications. The full listings for the example programs that accompany this article are available for download from Listing 1. Display Some PCM Types and Formats#include int main() int val; printf(ALSA library version: %sn, SND_LIB_VERSION_STR); printf(nPCM stream types:n); for (val = 0; val =

30、 SND_PCM_STREAM_LAST; val+) printf( %sn, snd_pcm_stream_name(snd_pcm_stream_t)val); printf(nPCM access types:n); for (val = 0; val = SND_PCM_ACCESS_LAST; val+) printf( %sn, snd_pcm_access_name(snd_pcm_access_t)val); printf(nPCM formats:n); for (val = 0; val = SND_PCM_FORMAT_LAST; val+) if (snd_pcm_f

31、ormat_name(snd_pcm_format_t)val) != NULL) printf( %s (%s)n, snd_pcm_format_name(snd_pcm_format_t)val), snd_pcm_format_description( (snd_pcm_format_t)val); printf(nPCM subformats:n); for (val = 0; val = SND_PCM_SUBFORMAT_LAST; val+) printf( %s (%s)n, snd_pcm_subformat_name( snd_pcm_subformat_t)val),

32、snd_pcm_subformat_description( snd_pcm_subformat_t)val); printf(nPCM states:n); for (val = 0; val = SND_PCM_STATE_LAST; val+) printf( %sn, snd_pcm_state_name(snd_pcm_state_t)val); return 0;Listing 1 displays some of the PCM data types and parameters used by ALSA. The first requirement is to include the header file that brings in the definitions for all of the ALSA library functions. One of the definit

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1