Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ PCM audio under Linux     - Oracle SQL statement to retrieve data paging table (Database)

- APF firewall installation and configuration under Linux (Linux)

- React Getting Started Tutorial (Linux)

- CentOS 6.4 install and configure Squid Proxy Server (Linux)

- Use Observium to monitor your network and servers (Server)

- Manually generate AWR reports (Database)

- Using Python to find a particular file extension directory (Programming)

- Linux system Passwd file detailed analysis (Linux)

- CentOS yum install LAMP (Server)

- Ubuntu use three methods to install Ruby (Linux)

- Puppet 3.x installed on Debian 7 (Server)

- MySQL InnoDB table --BTree basic data structures (Database)

- Linux port scanning (Linux)

- Linux Security and Optimization (Linux)

- Android in the coordinate system and a method to obtain the coordinates (Programming)

- Linux Task Scheduler (at, crontab) (Linux)

- 10 useful Linux command line tips (Linux)

- Linux itself disguised illusion strengthen security (Linux)

- Multipath configuration under Linux (Linux)

- Use small network command to check whether PC Security (Linux)

  PCM audio under Linux
  Add Date : 2018-11-21      
  Test environment:

Ubuntu 14.04



1, the establishment of a shared library (shared library)

Here used audio playback library under Linux, alsa-lib. alsa is an open source project under linux, its full name is the Advanced Linux Sound Architecture. Its installation command as follows:

sudo apt-get install libasound2-dev

Use Coceblocks build a shared library project named libTest2, select the programming language C. Added at the code in the main:


snd_pcm_t * handle;
snd_pcm_sframes_t frames;

int PcmOpen ()

    if (snd_pcm_open (& handle, "hw: 0,0", SND_PCM_STREAM_PLAYBACK, 0) <0)
        printf ( "pcm open error");
        return 0;

    if (snd_pcm_set_params (handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, 8000, 1, 500000) <0) //0.5sec 500000
        printf ( "pcm set error");
        return 0;

    return 1;

void Play (unsigned char * buffer, int length)
    frames = snd_pcm_writei (handle, buffer, length);
    if (frames <0)
        frames = snd_pcm_recover (handle, frames, 0);

int PcmClose ()
    snd_pcm_close (handle);
    return 1;

At compile time, remember that link library alsa-lib. The specific method is in codeblocks compilation dialog box, find the linker settings option in the Other linker options, enter: -lasound.

Of course, you can also manually compile. cd into the directory where the main.c, execute the following command:

gcc -o main.o -c main.c

gcc -o libTest1.so -shared main.o -lasound

2, call the shared library in mono

Like dynamic library calls and .net, use the DllImport attribute to call. About mono call the shared library, mono official has described very detailed article: "Interop with Native Libraries".

Use monoDevolop establish a console project, and the previous step generated libTest1.so files are copied to / bin / Debug.

Enter the following code in the Program.cs file:

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace helloworld
    class MainClass

        public static void Main (string [] args)
            Console.WriteLine ( "the app is started");
            PlayPCM ();

            Thread thread = new Thread (new ThreadStart (PlayPCM));
            thread.Start ();
            while (true)
                if (Console.ReadLine () == "quit")
                    thread.Abort ();
                    Console.WriteLine ( "the app is stopped");


        /// Plaies the PC.
        static void PlayPCM ()
            using (FileStream fs = File.OpenRead ( "Ireland.pcm"))
                byte [] data = new byte [4000];

                PcmOpen ();

                while (true)
                    int readcount = fs.Read (data, 0, data.Length);
                    if (readcount> 0)
                        Play (data, data.Length);


                PcmClose ();

        [DllImport ( "libTest1.so")]
        static extern int PcmOpen ();

        [DllImport ( "libTest1.so")]
        static extern int Play (byte [] buffer, int length);

        [DllImport ( "libTest1.so")]
        static extern int PcmClose ();

In order to facilitate testing, the annex contains a sound file, you can directly use the sound file.

3, a brief description of some of the documents related to PCM

Annex Ireland.pcm, uses PCMU encoding, sampling rate of 8000Hz, the sampling depth of 1 byte, the number of channels to 1. These correspond to snd_pcm_set_params function, which is used in a relatively simple way to set pcm playback parameters. To play sound files properly, you must make the parameters of PCM sound file parameters and consistent.
- Locale files under Ubuntu (Linux)
- Ubuntu rights management common commands (Linux)
- Installation Docker FAQ on Ubuntu (Linux)
- Install Ubuntu 14.04 and Windows 8 / 8.1 dual-boot in UEFI mode (Linux)
- To install and deploy Apache under the CentOS (Server)
- How to use Quagga BGP (Border Gateway Protocol) router to filter BGP routing (Linux)
- OpenWRT environment to build (Linux)
- Improve the Ubuntu SSH login authentication approach speed (Linux)
- Analysis of native Java keywords (Programming)
- Some safety precautions of Linux servers (Linux)
- 20 Linux commands interview questions and answers (Linux)
- Learn about EditText little depth (Programming)
- OpenDaylight Helium version installed (Linux)
- Oracle Data Guard LOG_ARCHIVE_DEST_n optional attribute parameters (Database)
- To install and use the Doxygen under Linux (Linux)
- Shell generated using automated configuration script Orabbix (Database)
- configuration ssh without password under Linux (Linux)
- Android Launcher3 Application List Modify a transparent background (Linux)
- Use UDEV SCSI Rules configured ASM on Oracle Linux 5 and 6 (Database)
- Android media library of analysis: MediaProvider (Programming)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.