Installation and API use of PulseAudio under Ubuntu12.04

Source: Internet
Author: User
Tags doxygen
1. Install the audio library client software libpulse-devPackagelibpulse-dev: composer in Ubuntu12.04 by running the following command: sudoapt-getinstalllibpulse-dev: libavahi-client-

1. Install the audio library client software libpulse-dev in Ubuntu 12.04

Package libpulse-dev: PulseAudio client development headers and libraries

Run the following command:

Sudo apt-get install libpulse-dev

During installation, you will be prompted to install these software together:

Libpulse0 libavahi-client-dev libavahi-common-dev libpulse-mainloop-glib0

You can also query the libpulse-dev dependency on the official packages.ubuntu.com website. The related links are as follows:

Http://packages.ubuntu.com/raring/libpulse-dev

As shown in:

Ii. Use of pulseaudio Library (synchronous simple API)

The official pulseaudio website has a user manual for the pulseaudio API doxygen. The URL is as follows:

Http://freedesktop.org/software/pulseaudio/doxygen/

1. Broadcasting sample

A simple playback tool using the simple API

// Pacat-simple.c
/***
This file is part of PulseAudio.
PulseAudio is free software; you can redistribute it and/or modify
It under the terms of the GNU Lesser General Public License as published
By the Free Software Foundation; either version 2.1 of the License,
Or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful,
Without any warranty; without even the implied warranty
MERCHANTABILITY or fitness for a particle PURPOSE. See the GNU
General Public License for more details.
You shoshould have your ed a copy of the GNU Lesser General Public License
Along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
# Ifdef HAVE_CONFIG_H
# Include
# Endif
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define BUFSIZE 1024
Int main (int argc, char * argv []) {
/* The Sample format to use */
Static const pa_sample_spec ss = {
. Format = PA_SAMPLE_S16LE,
. Speed = 44100,
. Channels = 2
};
Pa_simple * s = NULL;
Int ret = 1;
Int error;
/* Replace STDIN with the specified file if needed */
If (argc> 1 ){
Int fd;
If (fd = open (argv [1], O_RDONLY) <0 ){
Fprintf (stderr, _ FILE _ ": open () failed: % s \ n", strerror (errno ));
Goto finish;
}
If (dup2 (fd, STDIN_FILENO) <0 ){
Fprintf (stderr, _ FILE _ ": dup2 () failed: % s \ n", strerror (errno ));
Goto finish;
}
Close (fd );
}
/* Create a new playback stream */
If (! (S = pa_simple_new (NULL, argv [0], PA_STREAM_PLAYBACK, NULL, "playback", & ss, NULL, NULL, & error ))){
Fprintf (stderr, _ FILE _ ": pa_simple_new () failed: % s \ n", pa_strerror (error ));
Goto finish;
}
For (;;){
Uint8_t buf [BUFSIZE];
Ssize_t r;
# If 0
Pa_usec_t latency;
If (latency = pa_simple_get_latency (s, & error) = (pa_usec_t)-1 ){
Fprintf (stderr, _ FILE _ ": pa_simple_get_latency () failed: % s \ n", pa_strerror (error ));
Goto finish;
}
Fprintf (stderr, "% 0.0f usec \ r", (float) latency );
# Endif
/* Read some data ...*/
If (r = read (STDIN_FILENO, buf, sizeof (buf) <= 0 ){
If (r = 0)/* EOF */
Break;
Fprintf (stderr, _ FILE _ ": read () failed: % s \ n", strerror (errno ));
Goto finish;
}
/*... And play it */
If (pa_simple_write (s, buf, (size_t) r, & error) <0 ){
Fprintf (stderr, _ FILE _ ": pa_simple_write () failed: % s \ n", pa_strerror (error ));
Goto finish;
}
}
/* Make sure that every single sample was played */
If (pa_simple_drain (s, & error) <0 ){
Fprintf (stderr, _ FILE _ ": pa_simple_drain () failed: % s \ n", pa_strerror (error ));
Goto finish;
}
Ret = 0;
Finish:
If (s)
Pa_simple_free (s );
Return ret;
}

2. Recording sample

A simple recording tool using the simple API

// Parec-simple.c
/***
This file is part of PulseAudio.
PulseAudio is free software; you can redistribute it and/or modify
It under the terms of the GNU Lesser General Public License as published
By the Free Software Foundation; either version 2.1 of the License,
Or (at your option) any later version.
PulseAudio is distributed in the hope that it will be useful,
Without any warranty; without even the implied warranty
MERCHANTABILITY or fitness for a particle PURPOSE. See the GNU
General Public License for more details.
You shoshould have your ed a copy of the GNU Lesser General Public License
Along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
# Ifdef HAVE_CONFIG_H
# Include
# Endif
# Include
# Include
# Include
# Include
# Include
# Include
# Define BUFSIZE 1024
/* A simple routine calling UNIX write () in a loop */
Static ssize_t loop_write (int fd, const void * data, size_t size ){
Ssize_t ret = 0;
While (size> 0 ){
Ssize_t r;
If (r = write (fd, data, size) <0)
Return r;
If (r = 0)
Break;
Ret + = r;
Data = (const uint8_t *) data + r;
Size-= (size_t) r;
}
Return ret;
}
Int main (int argc, char * argv []) {
/* The sample type to use */
Static const pa_sample_spec ss = {
. Format = PA_SAMPLE_S16LE,
. Speed = 44100,
. Channels = 2
};
Pa_simple * s = NULL;
Int ret = 1;
Int error;
/* Create the recording stream */
If (! (S = pa_simple_new (NULL, argv [0], PA_STREAM_RECORD, NULL, "record", & ss, NULL, NULL, & error ))){
Fprintf (stderr, _ FILE _ ": pa_simple_new () failed: % s \ n", pa_strerror (error ));
Goto finish;
}
For (;;){
Uint8_t buf [BUFSIZE];
/* Record some data ...*/
If (pa_simple_read (s, buf, sizeof (buf), & error) <0 ){
Fprintf (stderr, _ FILE _ ": pa_simple_read () failed: % s \ n", pa_strerror (error ));
Goto finish;
}
/* And write it to STDOUT */
If (loop_write (STDOUT_FILENO, buf, sizeof (buf ))! = Sizeof (buf )){
Fprintf (stderr, _ FILE _ ": write () failed: % s \ n", strerror (errno ));
Goto finish;
}
}
Ret = 0;
Finish:
If (s)
Pa_simple_free (s );
Return ret;
}

3. When compiling a pulseaudio library, you must add the dynamic link library libpulse of the pulseaudio library,
 
(You can find the pulse dynamic library under the/usr/lib/i386-linux-gnu/directory
 
/Usr/lib/i386-linux-gnu/libpulsecommon-1.1.so
/Usr/lib/i386-linux-gnu/libpulsedsp. so
/Usr/lib/i386-linux-gnu/libpulse-mainloop-glib.so
/Usr/lib/i386-linux-gnu/libpulse-mainloop-glib.so.0
/Usr/lib/i386-linux-gnu/libpulse-mainloop-glib.so.0.0.4
/Usr/lib/i386-linux-gnu/libpulse-simple.so
/Usr/lib/i386-linux-gnu/libpulse-simple.so.0
/Usr/lib/i386-linux-gnu/libpulse-simple.so.0.0.3
/Usr/lib/i386-linux-gnu/libpulse. so
/Usr/lib/i386-linux-gnu/libpulse. so.0
/Usr/lib/i386-linux-gnu/libpulse. so.0.13.5)

For example:
 
Gcc-o pacat-simple pacat-simple.c-lpulse-lpulsecommon-1.1-lpulse-simple

Iii. ALSA audio library

ALSA is fully called: Advanced Linux Sound Architecture
 

A Web site: http://www.alsa-project.org/main/index.php/Main_Page

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

Related Article

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.