using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace Service.Sound.SoundClient; public class NAudioClientDll { /**************************************常量定义**************************************/ /*返回值定义*/ public const int RET_SUCCESS = 0; public const int RET_INIT_CLIENT_FAIL = 1; //初始化客户端失败 public const int RET_OPEN_AUDIOFILE_FAIL = 2; //打开音频文件失败 public const int RET_INIT_PAGING_FAIL = 3; //初始化寻呼失败 public const int RET_START_RECORD_FAIL = 4; //启动录音失败 public const int RET_RESPONSE_TIMEOUT = 5; //响应超时 public const int RET_LOCAL_IS_CALLING = 6; //正在进行本地对讲 public const int RET_DEV_EXIST_IN_CALLINST = 7; //对讲实例中已存在该设备 public const int RET_BOTH_DEV_IS_LOCALIP = 8; //对讲实例中双方都是本地IP public const int RET_CALLINST_NOT_EXIST = 9; //对讲实例不存在 /*设备状态定义*/ public const ushort DEVS_OFFLINE = 0; //离线状态 public const ushort DEVS_IDLE = 1; //空闲状态 public const ushort DEVS_PLAYING = 2; //播放状态 public const ushort DEVS_INTERCOM = 3; //对讲状态 public const ushort DEVS_SAMPLER = 4; //采集状态 /*播放状态*/ public const int PS_STOP = 0; //播放停止状态 public const int PS_PLAYING = 1; //播放中 public const int PS_PAUSE = 2; //播放暂停状态 /*场景类型定义*/ public const byte SCENE_PLAY_AUDIOFILE = 0; //播放音频文件 public const byte SCENE_PLAY_TTS = 1; //文字转语音播放 public const byte SCENE_PLAY_PAGING = 2; //话筒寻呼播放 public const byte SCENE_PLAY_STREAM = 3; //音频流播放 //播放实例 public const byte PLAY_INST1 = 0; //播放实例1 public const byte PLAY_INST2 = 1; //播放实例2 public const byte PLAY_INST3 = 2; //播放实例3 public const byte PLAY_INST4 = 3; //播放实例4 public const byte PLAY_INST5 = 4; //播放实例5 //呼叫状态 public const int CALLS_IDLE = 0; //空闲状态 public const int CALLS_START = 1; //启动对讲 public const int CALLS_RING = 2; //响铃状态 public const int CALLS_CALLING = 3; //呼叫状态 public const int CALLS_HANGUP = 4; //挂断状态 //呼叫回调类型 public const uint CALLCB_NONE = 0; public const uint CALLCB_STATUS_CHANGE = 1; //对讲状态改变 public const uint CALLCB_SEND_AUDIO_DATA = 2; //发送到设备的语音数据 public const uint CALLCB_RECV_AUDIO_DATA = 3; //接收到的语音数据 public const uint CALLCB_CMD_REQ_TIMEOUT = 4; //命令请求超时 //命令请求超时类型 public const int START_CALL_TMO = 1; //启动呼叫超时 public const int PING_CALL_TMO = 2; //PING超时 public const int ANSWER_CALL_TMO = 3; //接听呼叫超时 public const int STOP_CALL_TMO = 4; //停止呼叫超时 public const int RECV_DATA_TMO = 5; //接收数据超时 //音频流格式 public const int ASF_PCM = 0; //PCM格式 public const int ASF_MP3 = 1; //mp3格式 public const int ASF_IMA_ADPCM = 2; //IMA-ADPCM格式 //网络话筒的音源 public const byte NM_AS_MIC = 0; //前置MIC public const byte NM_AS_LINEIN = 1; //后置LINEIN //网络话筒广播采样率定义 public const ushort NM_SMPR_8KHZ = 0; public const ushort NM_SMPR_16KHZ = 1; public const ushort NM_SMPR_24KHZ = 1; public const ushort NM_SMPR_32KHZ = 1; //回调类型定义 public const int CBT_NONE = 0; public const int CBT_RECV_AUDIOSTREAM = 1; //收到音频流 /*播放信息*/ [StructLayout(LayoutKind.Sequential)] public struct sPlayInfo { public uint playStatus; //播放状态 public int progress; //播放进度0-100 public uint totalTime; //总播放时间(单位s) public uint samplerate; //采样率 public uint bitrate; //比特率 } /*对讲实例信息*/ [StructLayout(LayoutKind.Sequential)] public struct sCallInstInfo { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string call_devip; // //主叫IP [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string called_devip; // //被叫IP uint call_status; //呼叫状态 } /*设备信息*/ [StructLayout(LayoutKind.Sequential)] public struct sDeviceInfo { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string devip; // //设备IP [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string devname; // //设备名称 } /*分组信息*/ [StructLayout(LayoutKind.Sequential)] public struct sGroupInfo { public byte groupno; // //组编号 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public byte[] reserve; //设备名称 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string devname; // //设备名称 } /*广播信息*/ [StructLayout(LayoutKind.Sequential)] public struct sCastPara { public byte audio_src; //音源,只对网络话筒有效(NM_AS_MIC \NM_AS_LINEIN) public byte priority; //优先级 (1-255) public ushort samplerate; //采样率 public byte input_gain; //输入增益 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public byte[] reserve; //reserve } /*音频流格式*/ [StructLayout(LayoutKind.Sequential)] public struct sAudioFrame { public uint audio_head; //音频包头标记 public byte scene_type; //场景,0:广播,1:对讲,2 public byte instance; //实例 public byte priority; //帧优先级 public byte audiofmt; //音频格式 public uint sampleRate; //采样率 public uint bitrate; //比特率 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] public byte[] reserve; //保留 public byte addr_type; //地址类型,0:编号,1:IP地址 public uint audio_src; //音源(设备编号/IP) public uint audio_dst; //目标(设备编号/IP),仅对讲时用 public uint frame_no; //帧序号 public ushort frame_len; //数据帧长度 public ushort reserve1; //保留 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1200)] public byte[] data; //帧数据 } /*播放状态改变回调函数*/ public delegate void PlayStatusChange(byte scene, byte inst, uint status); /*对讲回调函数*/ public delegate void CallCallback(IntPtr call_inst, uint cbtype, IntPtr data, uint len); public delegate void ClientCallBack(string devip, uint cbtype, IntPtr data, uint len); /*********************通用控制函数*********************/ /*网络音频客户端启动(初始化) localIP:本机IP地址 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_client_init(string devip); /*网络音频客户端启动(初始化) funcPlayStatusChange:播放状态回调函数 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_client_start(PlayStatusChange funcPlayStatusChange); /*设置回调函数, client_cb:回调通知函数 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_callback(ClientCallBack funcClientCB); /* 停止客户端 程序退出时调用 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_client_stop(); /*添加播放设备到指定场景的播放实例 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... devip:添加的设备IP */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_add_play_device(byte scene, byte inst, string devip); /*从指定场景的播放实例移除播放设备 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... devip:移除的设备IP */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_remove_play_device(byte scene, byte inst, string devip); /*清空指定场景播放实例的播放设备 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_clear_play_device(byte scene, byte inst); /*获取设备名称 devip:设备IP devname:获取到的设备名称,最大32byte */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_device_name(string devip, IntPtr devname); /*获取设备状态 devno:设备编号 status:设备状态,DEVS_OFFLINE、DEVS_IDLE.... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_device_status(string devip, ref ushort status); /*获取设备音量 devip:设备IP volume:设备音量,0-100.... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_device_volume(string devip, ref byte volume); /*设置设备音量 devip:设备IP volume:设备音量,0-100.... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_device_volume(string devip, byte volume); /*获取设备分组(临时组播) devip:设备IP group:设备分组,0-255.... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_device_temp_group(string devip, ref byte group); /*设置设备分组(临时组播) devno:设备IP group:设备音量,0-255.... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_device_temp_group(string devip, byte group); /*移除设备分组(临时组播) devno:设备IP */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_remove_device_temp_group(string devip); /* 获取播放设备 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... pdevlist:获取到的设备编号列表 devcnt:获取到的设备数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_play_device(byte scene, byte inst, IntPtr[] devip, ref int devcnt); /*设置播放音量(音源) scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... volume:音量(0-100) */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_play_volume(byte scene, byte inst, int volume); /*获取播放音量(音源) scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... volume:音量(0-100) */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_play_volume(byte scene, byte inst, ref int volume); /*设置TTS播放语速 inst:播放实例,PLAY_INST1、PLAY_INST2... speed:语速(-10~+10,0为正常语速) */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_tts_speed(byte inst, int speed); /*设置推流音频参数 inst:推流播放实例,PLAY_INST1、PLAY_INST2... audiofmt:音频流格式,ASF_PCM、ASF_MP3 channelnbr:通道数(仅对PCM格式有效) samplerate:采样率(仅对PCM格式有效) */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_audio_stream_para(byte inst, int audiofmt, int channelnbr, int samplerate); /*发送音频数据 inst:推流播放实例,PLAY_INST1、PLAY_INST2... data:发送的音频数据 len:音频数据长度 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_send_play_data(byte inst, byte[] data, int len); /*获取TTS播放语速 inst:播放实例,PLAY_INST1、PLAY_INST2... speed:语速(-10~+10,0为正常语速) */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_tts_speed(byte inst, ref int speed); /*启动音频播放,同时传入要播放的设备 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... audiosrc: 音频文件播放场景:audiosrc为音频文件路径,支持mp3、wma、wav 格式音频 文字转语音播放场景:audiosrc为需要播放的文字 寻呼场景:忽略此参数 priority:音源的优先级(0-255),值越大优先级越高 pdevlist:播放的设备编号列表 devcnt:设备数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_start_play(byte scene, byte inst, string audiosrc, int priority, IntPtr[] devip, ref int devcnt); /*启动同步音频播放,同时传入要播放的设备,播放完成后函数返回,实时寻呼场景除外(立即返回) scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... audiosrc: 音频文件播放场景:audiosrc为音频文件路径,支持mp3、wma、wav 格式音频 文字转语音播放场景:audiosrc为需要播放的文字 寻呼场景:忽略此参数 priority:音源的优先级(0-255),值越大优先级越高 pdevlist:播放的设备编号列表 devcnt:设备数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_start_play_sync(byte scene, byte inst, string audiosrc, int priority, IntPtr[] devip, ref int devcnt); /*启动音频播放,播放设备通过na_add_play_device和na_remove_play_device函数添加和删除 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... audiosrc: 音频文件播放场景:audiosrc为音频文件路径,支持mp3、wma、wav 格式音频 文字转语音播放场景:audiosrc为需要播放的文字 寻呼场景:忽略此参数 priority:音源的优先级(0-255),值越大优先级越高 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_start_play_1(byte scene, byte inst, string audiosrc, int priority); /*启动同步音频播放,播放设备通过na_add_play_device和na_remove_play_device函数添加和删除,播放完成后函数返回,实时寻呼场景除外(立即返回) scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... audiosrc: 音频文件播放场景:audiosrc为音频文件路径,支持mp3、wma、wav 格式音频 文字转语音播放场景:audiosrc为需要播放的文字 寻呼场景:忽略此参数 priority:音源的优先级(0-255),值越大优先级越高 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_start_play_sync_1(byte scene, byte inst, string audiosrc, int priority); /*暂停播放音频 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_pause_play(byte scene, byte inst); /*暂停之后恢复播放音频 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_restore_play(byte scene, byte inst); /*停止播放音频 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_stop_play(byte scene, byte inst); /*获取播放信息 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... playinfo:获取到的播放信息 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_playinfo(byte scene, byte inst, ref sPlayInfo playinfo); /*获取播放状态 scene:播放场景,SCENE_PLAY_AUDIOFILE、SCENE_PLAY_TTS、SCENE_PLAY_PAGING inst:播放实例,PLAY_INST1、PLAY_INST2... status:获取到的播放状态 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_play_status(byte scene, byte inst, ref uint status); /**************************************对讲接口*******************************************/ /*设置对讲回调函数, talk_cb:对讲回调函数 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_call_callback(CallCallback funcCallCallback); /*软件作为主叫方启动对讲, call_inst:对讲实例 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_start_call(out IntPtr call_inst, string called_devip); /*软件停止对讲, call_inst:对讲实例 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_stop_call(IntPtr call_inst); /*软件作为被叫方接听对讲, call_inst:对讲实例 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_answer_call(IntPtr call_inst); /*获取对讲实例信息 call_inst:对讲的实例 p_inst_info:实例信息 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_call_inst_info(IntPtr call_inst, ref sCallInstInfo instinfo); /**************************************网络话筒相关接口*******************************************/ /*设置网络话筒的广播设备 devip:话筒的IP地址 devinfo:设备信息 devcnt:设备数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_nm_set_castdev(string devip, IntPtr devinfo, int devcnt); /*设置网络话筒的广播分组 devip:话筒的IP地址 groupinfo:分组信息 groupcnt:分组数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_nm_set_castgroup(string devip, IntPtr groupinfo, int groupcnt); /*网络话筒启动广播 devip:话筒的IP地址 groupinfo:分组信息 groupcnt:分组数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_nm_start_cast(string devip, sCastPara cast_para); /*网络话筒停止广播 devip:话筒的IP地址 groupinfo:分组信息 groupcnt:分组数目 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_nm_stop_cast(string devip); /**************************************端口控制接口*******************************************/ /*获取设备端口状态, devip:设备IP iomode:端口模式,一个端口占2bit,0:悬空输入;1:上拉输入;2:下拉输入;3:推挽输出 ioval:端口值 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_get_iostatus(string devip, ref uint iomode, ref uint ioval); /*获取设备端口状态, devip:设备IP ioval:端口值,低16bit表示要设置的端口值,高16bit指定哪个端口需要设置 */ [DllImport("NAudioClientDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern int na_set_ioval(string devip, uint ioval); }