⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myalsa.c

📁 The purpose of this program is to run as a server, which can receive requirement from clients and se
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <alsa/asoundlib.h>#include "myalsa.h"snd_pcm_t *pcm_handler;          /* handler for PCM devices */snd_pcm_hw_params_t *hwparams;  /* hold configuration to be used for PCM stream */int rate, framesize, periods;      /* some hardware parameters */snd_pcm_uframes_t frames;voidalsa_init(void) {    int ret_val;        ret_val = snd_pcm_open(&pcm_handler, PCM_NAME, SND_PCM_STREAM_PLAYBACK, 0);    if (ret_val < 0)        err_exit("open PCM device error!");}//void//alsa_config(int rate, int channels) voidalsa_config(void){    int ret_val;        /* allocate space for hwparams */    snd_pcm_hw_params_alloca(&hwparams);        /* fill it in with default values */    ret_val = snd_pcm_hw_params_any(pcm_handler, hwparams);    if (ret_val < 0)        err_exit("config error");            /* set access type */    ret_val = snd_pcm_hw_params_set_access(pcm_handler, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);    if (ret_val < 0)         err_cont("set access sype error");        /* signed 16 bit little endian format */    ret_val = snd_pcm_hw_params_set_format(pcm_handler, hwparams, SND_PCM_FORMAT_S16_LE);    if (ret_val < 0)        err_cont("set format error");        /* set rate */    rate = 44100;    ret_val = snd_pcm_hw_params_set_rate_near(pcm_handler, hwparams, &rate, 0);    if (ret_val < 0)        err_cont("set rate error");    /* two channels */    ret_val = ret_val = snd_pcm_hw_params_set_channels(pcm_handler, hwparams, 2);    if (ret_val < 0)        err_cont("set channels error");        /* set period size */    frames = 32;    ret_val = snd_pcm_hw_params_set_period_size_near(pcm_handler, hwparams, &frames, 0);    if (ret_val < 0)        err_cont("set period size error");    /* apply hardware parameters */    ret_val = snd_pcm_hw_params(pcm_handler, hwparams);    if (ret_val < 0)        err_exit("set config error");}voidalsa_play(unsigned char* buf, int len){    int ret_val, nloops;    int size;    unsigned char *alsa_buf;    unsigned char *ptr = buf;            /* Use a buffer large enough to hold one period */    size = frames * 4; /* 2 bytes/sample, 2 channels */    alsa_buf = (char *) malloc(size);    nloops = (len/size);            while (nloops-- > 0) {        memmove(alsa_buf, ptr, size);        ptr += size;        len -= size;        ret_val = snd_pcm_writei(pcm_handler, alsa_buf, frames);        if (ret_val == -EPIPE) {            fprintf(stderr, "%d, underrun occurred\n", nloops);            snd_pcm_prepare(pcm_handler);            break;        }    }    if (len > 0 && len < size) {        frames = (len/4);        memmove(alsa_buf, ptr, len);        ret_val = snd_pcm_writei(pcm_handler, alsa_buf, frames);        if (ret_val == -EPIPE) {            fprintf(stderr, "underrun occurred\n");            snd_pcm_prepare(pcm_handler);        }    }}voidalsa_finish(void) {    int ret_val;        ret_val = snd_pcm_drain(pcm_handler);    if (ret_val < 0)        err_exit("snd_pcm_drain error");        ret_val = snd_pcm_close(pcm_handler);    if (ret_val < 0)        err_exit("snd_pcm_close error");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -