Playing PCM audio in mono in Linux

Source: Internet
Author: User

Playing PCM audio in mono in Linux

Test environment:

Ubuntu 14.04

MonoDevelop

CodeBlocks

1. Create a shared library)

The audio playing library in Linux, alsa-lib, is used here. Alsa is an open-source project in linux. Its full name is Advanced Linux Sound Architecture. Its Installation command is as follows:

Sudo apt-get install libasound2-dev

Use Coceblocks to create a shared library project named libTest2. Select C as the programming language. Add the following code to main:

# Include <alsa/asoundlib. h>
# Include <stdio. h>


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;
}

Remember to link the alsa-lib library during compilation. The specific method is to find the linker settings option in the codeblocks compilation dialog box and enter-lasound in Other linker options.

:

Of course, it can also be compiled manually. Cd to the directory where main. c is located, and 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. net calling a dynamic library, it is called using the DllImport feature. Mono has an official article about mono calling the Shared Library: Interop with Native Libraries.

Use monoteclop to create a console project and copy the libTest1.so file generated in the previous step 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 ");
Return;
}
}

}

/// <Summary>
/// Plaies the PC.
/// </Summary>
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 );
}
Else
{

Break;
}
}

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 ();
}
}

To facilitate testing, the attachment contains a sound file, which can be used directly.

3. Brief description of PCM files

The Ireland. pcm in the attachment uses PCMU encoding. The sampling rate is 8000Hz, the sampling depth is 1 byte, and the number of audio channels is 1. These correspond to the snd_pcm_set_params function, which is used to set the pcm playback parameters in a simple way. To play a sound file correctly, the PCM parameter must be consistent with that of the sound file.

For more information about alsa, see the official website. Http://www.alsa-project.org/main/index.php/Main_Page

Attachment download:

------------------------------------------ Split line ------------------------------------------

Free in http://linux.bkjia.com/

The username and password are both www.bkjia.com

The specific download directory will be used to play PCM audio/

For the download method, see

------------------------------------------ Split line ------------------------------------------

This article permanently updates the link address:

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.