|
Test environment:
Ubuntu 14.04
MonoDevelop
CodeBlocks
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:
#include
#include
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");
return;
}
}
}
///
/// 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 ();
}
}
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. |
|
|
|