ALSA PCM发声程序

#include "alsa/asoundlib.h"

//11KHz支持,发声时间加长,音速变慢
#define  SAMPLE_RATE  48000
//#define  SAMPLE_RATE  11000

//的确是两个声道交叉响
#define  CHANNELS    1
//#define  CHANNELS    2

//如果latency过小,会使得snd_pcm_writei()丢发声数据,产生short write现象
#define  LATENCY   (1000000)//1sec

#define NBLOCKS 16
#define BLOCK_SIZE 1024
unsigned char buffer[NBLOCKS*BLOCK_SIZE];     /* Here:some random da

ta :Future: User Layer Sound Data*/

static char *device = "default";   /* playback device */

#define  RANDOM_PLAY 1
#define  COMMON_PLAY 2

static unsigned char * get_user_layer_pcm_data(int flag)
{
    int i;
    for (i = 0; i < sizeof(buffer); i++) {
        if(flag == RANDOM_PLAY)
            buffer[i] = random() & 0xff;
        else
            buffer[i] = i & 0xff;
    }
    return buffer;
}

int main(int argc, char ** argv)
{
    int i;
    int err, ret_code = 0;
    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;

    if(argc == 2)
        get_user_layer_pcm_data(RANDOM_PLAY);//Here Future to get pcm data from user layer
    else
        get_user_layer_pcm_data(COMMON_PLAY);//Here Future to get pcm data from user layer

    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        printf("Playback open error: %s/n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_set_params(handle,
                    SND_PCM_FORMAT_U8,
                    SND_PCM_ACCESS_RW_INTERLEAVED,/* snd_pcm_readi/snd_pcm_writei access */
                    CHANNELS, //Channels
                    SAMPLE_RATE, //sample rate in Hz
                    1, //soft_resample
                    LATENCY)) < 0) {   /* latency: us 0.5sec */
        printf("Playback open error: %s/n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    for (i = 0; i < 10; i++) { //play 10 times, every play 16KB snd size
        frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
        if (frames < 0)
            frames = snd_pcm_recover(handle, frames, 0);
        if (frames < 0) {
            printf("snd_pcm_writei failed: %s/n", snd_strerror(err));
            ret_code = -1;
            break;
        }
        if (frames > 0 && frames < (long)sizeof(buffer))
            printf("Short write (expected %li, wrote %li)/n", (long)sizeof(buffer), frames);
    }

    snd_pcm_close(handle);

    return ret_code;
}
=======================
Makefile:
#Usage:
#make  ==> arm compile
#make ARCH=arm ==>same as above
#make rebuild ==>make clean+make ARCH=arm
#make ARCH=arm rebuild

#make ARCH=i386 ==>x86 compile
#make ARCH=i386 rebuild ==>make clean+make ARCH=i386

#default to compile on arm platform
ARCH=arm
CROSS_COMPILE = arm-
BIN_EXT=
LIBDIR  =  /pavo/library/lib
INCLUDE_DIR = /pavo/library/include

ifeq ("$(origin ARCH)", "command line")
    ARCH = $(ARCH)
endif

ifeq ($(ARCH), i386)
    CROSS_COMPILE =
    BIN_EXT=.exe
    LIBDIR  =  /usr/lib
    INCLUDE_DIR = /usr/include
endif

ifeq ($(ARCH), arm)
    CROSS_COMPILE = arm-
    BIN_EXT=
    LIBDIR  =  /pavo/library/lib
    INCLUDE_DIR = /pavo/library/include
endif

CC = $(CROSS_COMPILE)gcc
CPP    = $(CC) -E
LD = $(CROSS_COMPILE)gcc
AS    = $(CROSS_COMPILE)as
AR    = $(CROSS_COMPILE)ar
NM    = $(CROSS_COMPILE)nm
STRIP    = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
RANLIB    = $(CROSS_COMPILE)RANLIB

TARGET=pcm_play$(BIN_EXT)
SOURCES=pcm_play.c
OBJS=pcm_play.o

del_files :=$(OBJS)
del_files +=pcm_play
del_files +=pcm_play.exe

CFLAGS  =  -I$(INCLUDE_DIR)
LDFLAGS =  -L$(LIBDIR)

LIBS    =  $(LDFLAGS) -lasound  -lm -ldl -lpthread

all:
    @echo compiling $@ ...
    $(CC) -c $(SOURCES)  $(CFLAGS) -o $(OBJS)
    $(LD)  $(OBJS) -o $(TARGET) $(LIBS) $(CFLAGS)

clean:
    @echo "make clean"
    @for f in $(del_files); do /
        if test -f $$f; then /
            echo "$$f exist,rm it"; /
            rm -f $$f; /
        fi; /
    done;

rebuild:clean
    make all
ALSA lib confmisc.c:855:(parse_card) cannot find card '0' ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory ALSA lib confmisc.c:1342:(snd_func_refer) error evaluating name ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5727:(snd_config_expand) Evaluate error: No such file or directory ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM sysdefault ALSA lib confmisc.c:855:(parse_card) cannot find card '0' ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory ALSA lib confmisc.c:1342:(snd_func_refer) error evaluating name ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5727:(snd_config_expand) Evaluate error: No such file or directory ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM sysdefault ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp ALSA lib pcm_a52.c:1001:(_snd_pcm_a52_open) a52 is only for playback ALSA lib conf.c:5694:(snd_config_expand) Unknown parameters {AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2 CARD 0} ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:165:(snd_config_get_card) Cannot get card index for 0 ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card' ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card' ALSA lib confmisc.c:855:(parse_card) cannot find card '0' ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_card_id returned error: No such file or directory ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory ALSA lib confmisc.c:1342:(snd_func_refer) error evaluating name ALSA lib conf.c:5204:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5727:(snd_config_expand) Evaluate error: No such file or directory ALSA lib pcm.c:2721:(snd_pcm_open_noupdate) Unknown PCM dmix Cannot connect to server socket err = No such file or directory Cannot connect to server request channel jack server is not running or cannot be started JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock中文给出解决方案同时我用的是wsl
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值