IntroductiontoSoundProgrammingwithALSAWord文档格式.docx
- 文档编号:20767308
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:18
- 大小:41.06KB
IntroductiontoSoundProgrammingwithALSAWord文档格式.docx
《IntroductiontoSoundProgrammingwithALSAWord文档格式.docx》由会员分享,可在线阅读,更多相关《IntroductiontoSoundProgrammingwithALSAWord文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
Sound,consistingofwavesofvaryingairpressure,isconvertedtoitselectricalformbyatransducer,suchasamicrophone.Ananalog-to-digitalconverter(ADC)convertstheanalogvoltagesintodiscretevalues,calledsamples,atregularintervalsintime,knownasthesamplingrate.Bysendingthesamplestoadigital-to-analogconverterandanoutputtransducer,suchasaloudspeaker,theoriginalsoundcanbereproduced.
Thesizeofthesamples,expressedinbits,isonefactorthatdetermineshowaccuratelythesoundisrepresentedindigitalform.Theothermajorfactoraffectingsoundqualityisthesamplingrate.TheNyquistTheoremstatesthatthehighestfrequencythatcanberepresentedaccuratelyisatmostone-halfthesamplingrate.
ALSABasics
ALSAconsistsofaseriesofkerneldevicedriversformanydifferentsoundcards,anditalsoprovidesanAPIlibrary,libasound.ApplicationdevelopersareencouragedtoprogramusingthelibraryAPIandnotthekernelinterface.Thelibraryprovidesahigher-levelandmoredeveloper-friendlyprogramminginterfacealongwithalogicalnamingofdevicessothatdevelopersdonotneedtobeawareoflow-leveldetailssuchasdevicefiles.
Incontrast,OSS/Freedriversareprogrammedatthekernelsystemcalllevelandrequirethedevelopertospecifydevicefilenamesandperformmanyfunctionsusingioctlcalls.Forbackwardcompatibility,ALSAprovideskernelmodulesthatemulatetheOSS/Freesounddrivers,somostexistingsoundapplicationscontinuetorununchanged.Anemulationwrapperlibrary,libaoss,isavailabletoemulatetheOSS/FreeAPIwithoutkernelmodules.
ALSAhasacapabilitycalledpluginsthatallowsextensiontonewdevices,includingvirtualdevicesimplementedentirelyinsoftware.ALSAprovidesanumberofcommand-lineutilities,includingamixer,soundfileplayerandtoolsforcontrollingspecialfeaturesofspecificsoundcards.
ALSAArchitecture
TheALSAAPIcanbebrokendownintothemajorinterfacesitsupports:
Controlinterface:
ageneral-purposefacilityformanagingregistersofsoundcardsandqueryingtheavailabledevices.
PCMinterface:
theinterfaceformanagingdigitalaudiocaptureandplayback.Therestofthisarticlefocusesonthisinterface,asitistheonemostcommonlyusedfordigitalaudioapplications.
RawMIDIinterface:
supportsMIDI(MusicalInstrumentDigitalInterface),astandardforelectronicmusicalinstruments.ThisAPIprovidesaccesstoaMIDIbusonasoundcard.TherawinterfaceworksdirectlywiththeMIDIevents,andtheprogrammerisresponsibleformanagingtheprotocolandtiming.
Timerinterface:
providesaccesstotiminghardwareonsoundcardsusedforsynchronizingsoundevents.
Sequencerinterface:
ahigher-levelinterfaceforMIDIprogrammingandsoundsynthesisthantherawMIDIinterface.IthandlesmuchoftheMIDIprotocolandtiming.
Mixerinterface:
controlsthedevicesonsoundcardsthatroutesignalsandcontrolvolumelevels.Itisbuiltontopofthecontrolinterface.
DeviceNaming
ThelibraryAPIworkswithlogicaldevicenamesratherthandevicefiles.Thedevicenamescanberealhardwaredevicesorplugins.Hardwaredevicesusetheformathw:
i,j,whereiisthecardnumberandjisthedeviceonthatcard.Thefirstsounddeviceishw:
0,0.Thealiasdefaultreferstothefirstsounddeviceandisusedinalloftheexamplesinthisarticle.Pluginsuseotheruniquenames;
plughw:
forexample,isapluginthatprovidesaccesstothehardwaredevicebutprovidesfeatures,suchassamplingrateconversion,insoftwareforhardwarethatdoesnotdirectlysupportit.Thedmixanddsharepluginsallowyoutodownmixseveralstreamsandsplitasinglestreamdynamicallyamongdifferentapplications.
SoundBuffersandDataTransfer
Asoundcardhasahardwarebufferthatstoresrecordedsamples.Whenthebufferissufficientlyfull,itgeneratesaninterrupt.Thekernelsounddriverthenusesdirectmemoryaccess(DMA)totransfersamplestoanapplicationbufferinmemory.Similarly,forplayback,anotherapplicationbufferistransferredfrommemorytothesoundcard'
shardwarebufferusingDMA.
Thesehardwarebuffersareringbuffers,meaningthedatawrapsbacktothestartwhentheendofthebufferisreached.Apointerismaintainedtokeeptrackofthecurrentpositionsinboththehardwarebufferandtheapplicationbuffer.Outsideofthekernel,onlytheapplicationbufferisofinterest,sofromhereonwediscussonlytheapplicationbuffer.
ThesizeofthebuffercanbeprogrammedbyALSAlibrarycalls.Thebuffercanbequitelarge,andtransferringitinoneoperationcouldresultinunacceptabledelays,calledlatency.Tosolvethis,ALSAsplitsthebufferupintoaseriesofperiods(calledfragmentsinOSS/Free)andtransfersthedatainunitsofaperiod.
Aperiodstoresframes,eachofwhichcontainsthesamplescapturedatonepointintime.Forastereodevice,theframewouldcontainsamplesfortwochannels.Figure1illustratesthebreakdownofabufferintoperiods,framesandsampleswithsomehypotheticalvalues.Here,leftandrightchannelinformationisstoredalternatelywithinaframe;
thisiscalledinterleavedmode.Anon-interleavedmode,whereallthesampledataforonechannelisstoredfollowedbythedataforthenextchannel,alsoissupported.
Figure1.TheApplicationBuffer
OverandUnderRun
Whenasounddeviceisactive,dataistransferredcontinuouslybetweenthehardwareandapplicationbuffers.Inthecaseofdatacapture(recording),iftheapplicationdoesnotreadthedatainthebufferrapidlyenough,thecircularbufferisoverwrittenwithnewdata.Theresultingdatalossisknownasoverrun.Duringplayback,iftheapplicationdoesnotpassdataintothebufferquicklyenough,itbecomesstarvedfordata,resultinginanerrorcalledunderrun.TheALSAdocumentationsometimesreferstobothoftheseconditionsusingthetermXRUN.ProperlydesignedapplicationscanminimizeXRUNandrecoverifitoccurs.
ATypicalSoundApplication
ProgramsthatusethePCMinterfacegenerallyfollowthispseudo-code:
openinterfaceforcaptureorplayback
sethardwareparameters
(accessmode,dataformat,channels,rate,etc.)
whilethereisdatatobeprocessed:
readPCMdata(capture)
orwritePCMdata(playback)
closeinterface
Welookatsomeworkingcodeinthefollowingsections.IrecommendyoucompileandruntheseonyourLinuxsystem,lookattheoutputandtrysomeofthesuggestedmodifications.Thefulllistingsfortheexampleprogramsthataccompanythisarticleareavailablefordownloadfrom
Listing1.DisplaySomePCMTypesandFormats
#include<
alsa/asoundlib.h>
intmain(){
intval;
printf("
ALSAlibraryversion:
%s\n"
SND_LIB_VERSION_STR);
\nPCMstreamtypes:
\n"
);
for(val=0;
val<
=SND_PCM_STREAM_LAST;
val++)
snd_pcm_stream_name((snd_pcm_stream_t)val));
\nPCMaccesstypes:
=SND_PCM_ACCESS_LAST;
snd_pcm_access_name((snd_pcm_access_t)val));
\nPCMformats:
=SND_PCM_FORMAT_LAST;
if(snd_pcm_format_name((snd_pcm_format_t)val)
!
=NULL)
%s(%s)\n"
snd_pcm_format_name((snd_pcm_format_t)val),
snd_pcm_format_description(
(snd_pcm_format_t)val));
\nPCMsubformats:
=SND_PCM_SUBFORMAT_LAST;
snd_pcm_subformat_name((
snd_pcm_subformat_t)val),
snd_pcm_subformat_description((
snd_pcm_subformat_t)val));
\nPCMstates:
=SND_PCM_STATE_LAST;
snd_pcm_state_name((snd_pcm_state_t)val));
return0;
}
Listing1displayssomeofthePCMdatatypesandparametersusedbyALSA.ThefirstrequirementistoincludetheheaderfilethatbringsinthedefinitionsforalloftheALSAlibraryfunctions.Oneofthedefinit
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- IntroductiontoSoundProgrammingwithALSA
链接地址:https://www.bdocx.com/doc/20767308.html