Alsa pcm voice Program

Source: Internet
Author: User

# Include "ALSA/asoundlib. H"

// 11 kHz support, longer audible time, slower sonic speed
# Define sample_rate 48000
// # Define sample_rate 11000

// It is indeed a two-channel Interaction
# Define Channels 1
// # Define channels 2

// If latency is too small, it will cause snd_pcm_writei () to lose voice data, resulting in the short write Phenomenon
# 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 abve
# 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_ext0000.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)
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.