linux获取音频及播放代码实例

本文提供了一个实例,展示了如何在Linux系统中获取音频数据并进行播放,具体操作涉及441帧的采集和处理。
摘要由CSDN通过智能技术生成

代码功能:采集441帧然后播放出来

详细功能参考代码:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <alsa/asoundlib.h>
  4 
  5 
  6 int main(void){
  7         /*Name of the PCM device ,like "default"*/
  8         char    *dev_name;
  9         int     rate = 44100;/*Sample rate*/
 10         int     exact_rate;/*Sample rate returned by*/
 11         int     dir;/*(1)exact_rate == rate --> dir=0,(2)exact_rate < rate --> dir=-1,(3)exact_rate > rate*/
 12         int     periods = 3;/*Number of periods*/
 13         int     err;
 14         int     size;
 15         int     pcmreturn;
 16 //      snd_pcm_uframes_t       periodsize = 8192;
 17         snd_pcm_uframes_t       periodsize = 441;
 18         snd_pcm_t       *play_handle;
 19         snd_pcm_t       *capture_handle;
 20         snd_pcm_stream_t        play_stream = SND_PCM_STREAM_PLAYBACK;
 21         snd_pcm_stream_t        capture_stream = SND_PCM_STREAM_CAPTURE;
 22         /*This structure contains information about */
 23         /*the hardware and can be used to specify the */
 24         /*configuration to be used for the PCM stream*/
 25         snd_pcm_hw_params_t     *hwparams;
 26         snd_pcm_hw_params_t     *c_hwparams;
 27         /*Init dev_name, Of course, later you will make this configure*/
 28         dev_name = strdup("default");
 29         /*Allocate the snd_pcm_hw_params_t structure on the stack*/
 30         snd_pcm_hw_params_alloca(&hwparams);
 31         snd_pcm_hw_params_alloca(&c_hwparams);
 32         /* Open PCM. The last parameter of this function is the mode. */
 33         /* If this is set to 0, the standard mode is used. Possible   */
 34         /* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC.       */
 35         /* If SND_PCM_NONBLOCK is used, read / write access to the    */
 36         /* PCM device will return immediately. If SND_PCM_ASYNC is    */
 37         /* specified, SIGIO will be emitted whenever a period has     */
 38         /* been completely processed by the soundcard.                */
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值