你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux下mono播放PCM音频

[日期:2015-03-07] 来源:Linux社区  作者:dehai [字体: ]

测试环境:

Ubuntu 14.04

MonoDevelop

CodeBlocks

1、建立一个共享库(shared library)

这里用到了Linux下的音频播放库,alsa-lib。 alsa是linux下的一个开源项目,它的全名是Advanced Linux Sound Architecture。它的安装命令如下:

sudo apt-get install libasound2-dev

使用 Coceblocks 建立一个 shared library 项目,命名为libTest2,编程语言选择C。在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;
}

在编译的时候,记得链接alsa-lib库。具体方法是在codeblocks的编译对话框中,找到linker settings选项,在Other linker options中输入:-lasound。

如图所示:

当然,也可以手工编译。cd 进main.c所在的目录,执行以下命令:

gcc -o main.o -c main.c

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

2、在mono中调用共享库

与.net调用动态库一样,使用DllImport特性来调用。关于mono调用共享库,mono官方有一篇文章介绍得很详细:《Interop with Native Libraries》。

使用monoDevolop建立一个控制台项目,并将上一步中生成的libTest1.so文件拷贝到/bin/Debug下。

在Program.cs文件中输入以下代码:

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

为了便于测试,附件中包含了一个声音文件,可以直接使用这个声音文件。

3、有关PCM文件的一些简要说明

附件中的Ireland.pcm,采用的是PCMU编码,采样率为8000Hz,采样深度为1字节,声道数为1。这些对应于snd_pcm_set_params函数,这个函数用于以较简单的方式来设定pcm的播放参数。要正确地播放声音文件,必须使PCM的参数与声音文件的参数一致。

更多有关alsa的信息,请查看官方网站。http://www.alsa-project.org/main/index.php/Main_Page

附件下载:

------------------------------------------分割线------------------------------------------

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2015年资料/3月/7日/Linux下mono播放PCM音频/

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

------------------------------------------分割线------------------------------------------

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-03/114658.htm

linux
相关资讯       PCM音频 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款