日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > > 充電吧
[導(dǎo)讀]一. 介紹 ????? ALSA 標(biāo)準(zhǔn)是一個(gè)先進(jìn)的linux聲音體系。它包含內(nèi)核驅(qū)動(dòng)集合,API庫(kù)和工具對(duì)Linux聲音進(jìn)行支持。ALSA 包含一系列內(nèi)核驅(qū)動(dòng)對(duì)不同的聲卡進(jìn)行支持,還提供了libaso

一. 介紹

????? ALSA 標(biāo)準(zhǔn)是一個(gè)先進(jìn)的linux聲音體系。它包含內(nèi)核驅(qū)動(dòng)集合,API庫(kù)和工具對(duì)Linux聲音進(jìn)行支持。ALSA 包含一系列內(nèi)核驅(qū)動(dòng)對(duì)不同的聲卡進(jìn)行支持,還提供了libasound的API庫(kù)。用這些進(jìn)行寫(xiě)程序不需要打開(kāi)設(shè)備等操作,所以編程人員在寫(xiě)程序的時(shí)候不會(huì)被底層的東西困擾。與此相反OSS/Free 驅(qū)動(dòng)在內(nèi)核層次調(diào)用,需要指定設(shè)備名和調(diào)用ioctl。為提供向后兼容, ALSA 提供內(nèi)核模塊模仿 OSS/Free 驅(qū)動(dòng),所以大多數(shù)的程序不需要改動(dòng)。 ALSA 擁有調(diào)用插件的能力對(duì)新設(shè)備提供擴(kuò)展,包括那些用軟件模擬出來(lái)的虛擬設(shè)備。 ALSA 還提供一組命令行工具包括? mixer, sound file player 和工具控制一些特別的聲卡的特別的作用。

?

二.ALSA 體系:

ALSA API 被主要分為以下幾種接口:

l???????? 控制接口:提供靈活的方式管理注冊(cè)的聲卡和對(duì)存在的聲卡進(jìn)行查詢。

l???????? PCM接口:提供管理數(shù)字音頻的捕捉和回放。

l???????? 原始 MIDI 接口: 支持 MIDI (Musical Instrument Digital Interface), 一種標(biāo)準(zhǔn)電子音樂(lè)指令集。 這些 API 提供訪問(wèn)聲卡上的 MIDI 總線。這些原始借口直接工作在 The? MIDI 事件上,程序員只需要管理協(xié)議和時(shí)間。

l???????? 記時(shí)接口: 為支持聲音的同步事件提供訪問(wèn)聲卡上的定時(shí)器。

l???????? 音序器接口:一個(gè)比原始MIDI接口高級(jí)的MIDI編程和聲音同步高層接口。它可以處理很多的MIDI協(xié)議和定時(shí)器。

l???????? 混音器接口:控制發(fā)送信號(hào)和控制聲音大小的聲卡上的設(shè)備。

?

三.聲卡的緩存和數(shù)據(jù)的傳輸:

????? 一塊聲卡有一個(gè)聲卡內(nèi)存用來(lái)存儲(chǔ)記錄的樣本。當(dāng)它被寫(xiě)滿時(shí)就產(chǎn)生中斷。內(nèi)核驅(qū)動(dòng)就使用DMA將數(shù)據(jù)傳輸?shù)絻?nèi)存中。同樣地,當(dāng)在播放時(shí)就將內(nèi)存中的聲音樣本使用DMA傳到聲卡的內(nèi)存中!

????? 聲卡的緩存是環(huán)狀的,這里只討論應(yīng)用程序中的內(nèi)存結(jié)構(gòu):ALSA將數(shù)據(jù)分成連續(xù)的片段然后傳到按單元片段傳輸。

?

四:典型的聲音程序結(jié)構(gòu):

??????? open interface for capture or playback

??????? set hardware parameters

??????? (access mode, data format, channels, rate, etc.)

??????? while there is data to be processed:

??????? read PCM data (capture)

?????? ?or write PCM data (playback)

??????? close interface

?

五.一些例子: 1.顯示一些PCM的類型和格式:

?

#include

#include

?

int main()

{

?????? std::cout << "ALSA library version: " << SND_LIB_VERSION_STR << std::endl;

?

?????? std::cout << "PCM stream types: " << std::endl;

?

?????? for (int val=0; val <= SND_PCM_STREAM_LAST; ++val)

????????????? std::cout << snd_pcm_stream_name((snd_pcm_stream_t)val) << std::endl;

?????? std::cout << std::endl;

?

?????? std::cout << "PCM access types: " << std::endl;

?????? for (int val=0; val <= SND_PCM_ACCESS_LAST; ++val)

????????????? std::cout << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl;

?????? std::cout << std::endl;

?

?????? std::cout << "PCM subformats: " << std::endl;

?????? for (int val=0; val <= SND_PCM_SUBFORMAT_LAST; ++val)

????????????? std::cout << snd_pcm_subformat_name((snd_pcm_subformat_t)val) << " (" << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl;

?????? std::cout << std::endl;

?

?????? std::cout << "PCM states: " << std::endl;

?????? for (int val=0; val <= SND_PCM_STATE_LAST; ++val)

????????????? std::cout << snd_pcm_state_name((snd_pcm_state_t)val) << std::endl;

?????? std::cout << std::endl;

?

?

?????? std::cout << "PCM formats: " << std::endl;

?????? for (int val=0; val <= SND_PCM_FORMAT_LAST; ++val)

????????????? std::cout << snd_pcm_format_name((snd_pcm_format_t)val) << " (" << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl;

?????? std::cout << std::endl;

??????

}

?

2.打開(kāi)PCM設(shè)備和設(shè)置參數(shù)

?

#include

#include

?

int main()

{

?????? int ????????????????????????????? rc;

?????? snd_pcm_t*???????????????????????? handle;

?????? snd_pcm_hw_params_t*????? params;

?????? unsigned int????????????????? val, val2;

?????? int ????????????????????????????? dir;

?????? snd_pcm_uframes_t???????????? frames;

?

?????? if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0)

?????? {

????????????? std::cerr << "unable to open pcm devices: " << snd_strerror(rc) << std::endl;

????????????? exit(1);

?????? }

?

?????? snd_pcm_hw_params_alloca(¶ms);

?

?????? snd_pcm_hw_params_any(handle, params);

?

?????? snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

?

?????? snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

?

?????? snd_pcm_hw_params_set_channels(handle, params, 2);

?

?????? val = 44100;

?

?????? snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);

?

?????? if ( (rc = snd_pcm_hw_params(handle, params)) < 0)

?????? {

????????????? std::cerr << "unable to set hw parameters: " << snd_strerror(rc) << std::endl;

????????????? exit(1);

?????? }

?

?????? std::cout << "PCM handle name = " << snd_pcm_name(handle) << std::endl;

?

?????? std::cout << "PCM state = " << snd_pcm_state_name(snd_pcm_state(handle)) << std::endl;

?

?????? snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *)&val);

?

?????? std::cout << "access type = " << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl;

?

?????? snd_pcm_hw_params_get_format(params, (snd_pcm_format_t*)(&val));

??????

?????? std::cout << "format = '" << snd_pcm_format_name((snd_pcm_format_t)val) << "' (" << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl;

?

? ??? snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);

? ??? std::cout << "subformat = '" <<

??? snd_pcm_subformat_name((snd_pcm_subformat_t)val) << "' (" << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl;

?

? ??? snd_pcm_hw_params_get_channels(params, &val);

? ??? std::cout << "channels = " << val << std::endl;

?

????? snd_pcm_hw_params_get_rate(params, &val, &dir);

? ??? std::cout << "rate = " << val << " bps" << std::endl;

?

?????? snd_pcm_hw_params_get_period_time(params, &val, &dir);

? ??? std::cout << "period time = " << val << " us" << std::endl;

?

? ??? snd_pcm_hw_params_get_period_size(params, &frames, &dir);

? ??? std::cout << "period size = " << static_cast(frames) << " frames" << std::endl;

?

?????? snd_pcm_hw_params_get_buffer_time(params, &val, &dir);

? ??? std::cout << "buffer time = " << val << " us" << std::endl;

??????

?????? snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val);

? ??? std::cout << "buffer size = " << val << " frames" << std::endl;

?

? ??? snd_pcm_hw_params_get_periods(params, &val, &dir);

? ??? std::cout << "periods per buffer = " << val << " frames" << std::endl;

?

?????? snd_pcm_hw_params_get_rate_numden(params, &val, &val2);

? ??? std::cout << "exact rate = " << val/val2 << " bps" << std::endl;

??????

? ??? val = snd_pcm_hw_params_get_sbits(params);

? ??? std::cout << "significant bits = " << val << std::endl;

?

? ??? snd_pcm_hw_params_get_tick_time(params, &val, &dir);

? ??? std::cout << "tick time = " << val << " us" << std::endl;

?

? ??? val = snd_pcm_hw_params_is_batch(params);

? ??? std::cout << "is batch = " << val << std::endl;

?

? ??? val = snd_pcm_hw_params_is_block_transfer(params);

? ??? std::cout << "is block transfer = " << val << std::endl;

?

? ??? val = snd_pcm_hw_params_is_double(params);

? ??? std::cout << "is double = " << val << std::endl;

?

?????? val = snd_pcm_hw_params_is_half_duplex(params);

? ??? std::cout << "is half duplex = " << val << std::endl;

?

?????? val = snd_pcm_hw_params_is_joint_duplex(params);

? ??? std::cout << "is joint duplex = " << val << std::endl;

?

?????? val = snd_pcm_hw_params_can_overrange(params);

? ??? std::cout << "can overrange = " << val << std::endl;

?

? ??? val = snd_pcm_hw_params_can_mmap_sample_resolution(params);

? ??? std::cout << "can mmap = " << val << std::endl;

?

? ??? val = snd_pcm_hw_params_can_pause(params);

? ??? std::cout << "can pause = " << val << std::endl;

?

?????? val = snd_pcm_hw_params_can_resume(params);

? ??? std::cout << "can resume = " << val << std::endl;

?

? ??? val = snd_pcm_hw_params_can_sync_start(params);

? ??? std::cout << "can sync start = " << val << std::endl;

?

????? snd_pcm_close(handle);

?

? ??? return 0;

}

?

3.一個(gè)簡(jiǎn)單的聲音播放程序

?

#include

#include

?

int main()

{

?????? long???????????????????????????? loops;

?????? int ????????????????????????????? rc;

?????? int?????????????????????????????????????? size;

?????? snd_pcm_t*???????????????????????? handle;

?????? snd_pcm_hw_params_t*????? params;

?????? unsigned int????????????????? val;

?????? int ????????????????????????????? dir;

?????? snd_pcm_uframes_t???????????? frames;

?????? char*????????????????????????????????? buffer;

?

?????? if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0)

?????? {

????????????? std::cerr << "unable to open pcm device: " << snd_strerror(rc) << std::endl;

????????????? exit(1);

?????? }

?

?????? snd_pcm_hw_params_alloca(¶ms);

?

?????? snd_pcm_hw_params_any(handle, params);

?

?????? snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

?

?????? snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

?

?????? snd_pcm_hw_params_set_channels(handle, params, 2);

?

?????? val = 44100;

?

?????? snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);

?

?????? frames = 32;

?????? snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);

?

?????? if ( (rc = snd_pcm_hw_params(handle, params)) < 0)

?????? {

????????????? std::cerr << "unable to set hw paramseters: " << snd_strerror(rc) << std::endl;

????????????? exit(1);

?????? }

?

?????? snd_pcm_hw_params_get_period_size(params, &frames, &dir);

?????? size = frames * 4;

?????? buffer = new char[size];

?

?????? snd_pcm_hw_params_get_period_time(params, &val, &dir);

?

?????? loops = 5000000 / val;

?

?????? while (loops > 0) {

????????????? loops--;

????????????? if ( (rc = read(0, buffer, size)) == 0)

????????????? {

???????????????????? std::cerr << "end of file on input" << std::endl;

???????????????????? break;

????????????? }

????????????? else if (rc != size)

???????????????????? std::cerr << "short read: read " << rc << " bytes" << std::endl;

?

????????????? if ( (rc = snd_pcm_writei(handle, buffer, frames)) == -EPIPE)

????????????? {

???????????????????? std::cerr << "underrun occurred" << std::endl;

???????????????????? snd_pcm_prepare(handle);

????????????? }

????????????? else if (rc < 0)

???????????????????? std::cerr << "error from writei: " << snd_strerror(rc) << std::endl;

????????????? else if (rc != (int)frames)

???????????????????? std::cerr << "short write, write " << rc << " frames" << std::endl;

?????? }

?

?????? snd_pcm_drain(handle);

?????? snd_pcm_close(handle);

?????? free(buffer);

?

?????? return 0;

}

4.一個(gè)簡(jiǎn)單的記錄聲音的程序

?

#include

#include

?

int main()

{

?????? long???????????????????????????? loops;

?????? int?????????????????????????????????????? rc;

?????? int?????????????????????????????????????? size;

?????? snd_pcm_t*???????????????????????? handle;

?????? snd_pcm_hw_params_t*????? params;

?????? unsigned int????????????????? val;

?????? int?????????????????????????????????????? dir;

?????? snd_pcm_uframes_t???????????? frames;

?????? char*????????????????????????????????? buffer;

?

?????? if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0)

?????? {

????????????? std::cerr << "unable to open pcm device: " << snd_strerror(rc) << std::endl;

????????????? exit(1);

?????? }

?

?????? snd_pcm_hw_params_alloca(¶ms);

?

?????? snd_pcm_hw_params_any(handle, params);

?

?????? snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);

?

?????? snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);

?

?????? snd_pcm_hw_params_set_channels(handle, params, 2);

?

?????? val = 44100;

?????? snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);

?

?????? if ( (rc = snd_pcm_hw_params(handle, params)) < 0)

?????? {

????????????? std::cerr << "unable to set hw parameters: " << snd_strerror(rc) << std::endl;

????????????? exit(1);

?????? }

?

?????? snd_pcm_hw_params_get_period_size(params, &frames, &dir);

?

?????? size = frames * 4;

?????? buffer = new char[size];

?

?????? snd_pcm_hw_params_get_period_time(params, &val, &dir);

?

?????? loops = 5000000 / val;

?

?????? while (loops > 0)

?????? {

????????????? loops --;

????????????? rc = snd_pcm_readi(handle, buffer, frames);

????????????? if (rc == -EPIPE)

????????????? {

???????????????????? std::cerr << "overrun occurred" << std::endl;

???????????????????? snd_pcm_prepare(handle);

????????????? }

????????????? else if (rc < 0)

???????????????????? std::cerr << "error from read: " << snd_strerror(rc) << std::endl;

?????? ?????? else if ( rc != (int)frames)

???????????????????? std::cerr << "short read, read " << rc << " frames" << std::endl;

????????????? rc = write(1, buffer, size);

????????????? if (rc != size)

???????????????????? std::cerr << "short write: wrote " << rc << " bytes" << std::endl;

?????? }

?

?????? snd_pcm_drain(handle);

?????? snd_pcm_close(handle);

?????? free(buffer);

??????

?????? return 0;

}

?

編譯的參數(shù):g++ xxx.cpp -o xxx -lasound


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢(shì)抑制與過(guò)流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問(wèn)題卻十分常見(jiàn),不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問(wèn)題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場(chǎng)照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢(shì)逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問(wèn)題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對(duì)周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來(lái)解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開(kāi)關(guān)電源具有效率高的特性,而且開(kāi)關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開(kāi)關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉