/// <summary>
/// 语音【文件、上传、解码、保存(WAV)】
/// </summary>
[DeveloperEx("Liwei:秘书语音需求单")]
public class AudioController : ClubBaseController
{
#region Android和IOS的一些音频参数
/****************
//格式
#define NAOMI_SPEEX_FORMAT kAudioFormatLinearPCM
//采样
#define NAOMI_SPEEX_RATE 8000
//声道
#define NAOMI_SPEEX_NUMBER_CHANNEL 1
//采样位数
#define NAOMI_SPEEX_BITDEPTH 16 private static final int FREQUENCY = 8000;
private static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private static final int CHANNEL = AudioFormat.CHANNEL_IN_MONO;//0x10
***************/
#endregion
private ILogProvider log = LogFactory.Create();
private FileStream fileStream = null;
private BinaryWriter binaryWriter = null; /// <summary>
/// PCM To WAV
/// 添加Wav头文件
/// </summary>
[NonAction]
private void CreateSoundFile(string path)
{
fileStream = new FileStream(path, FileMode.Create);
binaryWriter = new BinaryWriter(fileStream); //Set up file with RIFF chunk info. 每个WAVE文件的头四个字节便是“RIFF”。
char[] ChunkRiff = { 'R', 'I', 'F', 'F' };
char[] ChunkType = { 'W', 'A', 'V', 'E' };
char[] ChunkFmt = { 'f', 'm', 't', ' ' };
char[] ChunkData = { 'd', 'a', 't', 'a' }; short shPad = ; // File padding
int nFormatChunkLength = 0x10; // Format chunk length.
int nLength = ; // File length, minus first 8 bytes of RIFF description. This will be filled in later. short bitsPerSample = ; //每个采样需要的bit数
//short khCaiYang = 8000; //16KHz 采样频率
//short bitSecondRate = 16000; //-每秒所需字节数
short channels = ; //声道数目,1-- 单声道;2-- 双声道
short shBytesPerSample = ; //一个样本点的字节数目 //------- RIFF 块 -------
binaryWriter.Write(ChunkRiff);
binaryWriter.Write(nLength);
binaryWriter.Write(ChunkType); //------- WAVE块 ---------
binaryWriter.Write(ChunkFmt);
binaryWriter.Write(nFormatChunkLength);
binaryWriter.Write(shPad); binaryWriter.Write(channels); //Mono,声道数目,1-- 单声道;2-- 双声道
binaryWriter.Write(); //16KHz 采样频率
binaryWriter.Write(); //每秒所需字节数
binaryWriter.Write(shBytesPerSample); //数据块对齐单位(每个采样需要的字节数)
binaryWriter.Write(bitsPerSample); //16Bit,每个采样需要的bit数 //------- 数据块 ---------
binaryWriter.Write(ChunkData);
binaryWriter.Write((int)); // The sample length will be written in later.
} /// <summary>
///【上传、保存、PCM源数据文件】
/// </summary>
[AllowAnonymous]
public ResponseModel UploadAudio()
{
try
{
//-------上传文件---------
var hash = CommonUpload("/UploadAudio/", (string i) =>
{
i = Guid.NewGuid().ToString("n");
return i;
}, isFile: true); if (hash["retInt"].Equals(""))
{
string uploadPcmFile = hash["retSrc"].ToString();
//--------获取pcm的文件名------------
string pcmFileName = uploadPcmFile.Substring(, uploadPcmFile.IndexOf(Path.GetExtension(uploadPcmFile))); string wavFile = pcmFileName + ".wav";
string physicPCMPath = hash["retSrcDirPath"].ToString();
string tempWavPath = Path.Combine(HttpContext.Current.Server.MapPath(physicPCMPath), wavFile); //--------添加wav文件头-----
CreateSoundFile(tempWavPath); #region 读取上传的PCM源文件
string fileName = Path.Combine(HttpContext.Current.Server.MapPath(physicPCMPath), uploadPcmFile);
FileInfo fileinfo = new FileInfo(fileName);
FileStream fs = fileinfo.OpenRead();
int length = (int)fs.Length;
byte[] bytes = new byte[length];
fs.Read(bytes, , length);
fs.Close();
fs.Dispose();
#endregion #region 向WAV音频中写入数据
binaryWriter.Write(bytes, , bytes.Length);
binaryWriter.Seek(, SeekOrigin.Begin);
binaryWriter.Write((int)(bytes.Length + )); // 写文件长度
binaryWriter.Seek(, SeekOrigin.Begin);
binaryWriter.Write(bytes.Length);
fileStream.Close();
#endregion //----------删除用户PCM的源文件---------------
if (System.IO.File.Exists(fileName))
{
FileInfo fi = new FileInfo(fileName);
if (fi.Attributes.ToString().IndexOf("ReadOnly") != -)
fi.Attributes = FileAttributes.Normal;
System.IO.File.Delete(fileName);
}
return SetOfMessage(data: new { filename = base.domainSite + physicPCMPath + wavFile });
}
else
{
return SetOfMessage(status: , message: hash["retMsg"].ToString());
}
}
catch (Exception ex)
{
log.Log(LogLevel.Info, "上传语音出错误了!", ex.Message);
//--------记录日志-------------
return SetOfMessage(data: null, message: "语音上传出现错误了!", status: ); ;
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
}
}

Web Api 中使用 PCM TO WAV 的语音操作的更多相关文章

  1. Web Api中的get传值和post传值

    GET 方式 get方式传参 我们一般用于获取数据做条件筛选,也就是 “查” 1.无参 var look = function () { $.ajax({ type: "GET", ...

  2. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在Web Api中,我们对资源的CRUD操作都是通过相应的Http方法来实现——Post(新 ...

  3. Web Api中实现Http方法(Put,Post,Delete)

    在Web Api中实现Http方法(Put,Post,Delete) 系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在Web Api中,我 ...

  4. Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化

    9-3. 找出Web API中发生了什么变化 问题 想通过基于REST的Web API服务对数据库进行插入,删除和修改对象图,而不必为每个实体类编写单独的更新方法. 此外, 用EF6的Code Fri ...

  5. ASP.NET Web API中的Controller

    虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...

  6. Web APi 2.0优点和特点?在Web APi中如何启动Session状态?

    前言 曾几何时,微软基于Web服务技术给出最流行的基于XML且以扩展名为.asmx结尾的Web Service,此服务在.NET Framework中风靡一时同时也被.NET业界同仁所青睐,几年后在此 ...

  7. 在ASP.NET Web API中使用OData

    http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...

  8. WEB API 中HTTP的get、post、put,delete 请求方式

    一.WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: 1.POST 用于新建资源,服务端在指定的URI 上创 ...

  9. ASP.NET Web API 中的异常处理(转载)

    转载地址:ASP.NET Web API 中的异常处理

随机推荐

  1. Exception in thread "http-bio-8081-exec-3" java.lang.OutOfMemoryError: PermGen space

    前言: 在http://www.cnblogs.com/wql025/p/4865673.html一文中我曾描述这种异常也提供了解决方式,但效果不太理想,现在用本文的方式,效果显著. 目前此项目只能登 ...

  2. CSS3技巧:利用css3径向渐变做一张优惠券(转)

    在很多购物网站上都能看到优惠券,代金券,什么什么的券,但基本都是图片直接放上去,那么你有没有想过css来做一个呢,反正我是这样想过.那么你怎么做呢,切图做背景平铺边缘,嗯,有这样想过,如今css3技术 ...

  3. Leetcode#81 Search in Rotated Sorted Array II

    原题地址 如果不存在重复元素,仅通过判断数组的首尾元素即可判断数组是否连续,但是有重复元素的话就不行了,最坏情况下所有元素都一样,此时只能通过线性扫描确定是否连续. 设对于规模为n的问题的工作量为T( ...

  4. display:none和visibility: hidden二三事

    display:none属性后,HTML元素(对象)的宽度.高度等各种属性值都将“丢失”;而使用visibility:hidden属性后,HTML元素(对象)仅仅是在视觉上看不见(完全透明),而它所占 ...

  5. linux杀掉80端口线程命令

    80端口被其他程序占用, fuser -k -n tcp 80

  6. Unity3D脚本中文系列教程(十四)

    http://dong2008hong.blog.163.com/blog/static/469688272014032134394/ WWWFrom 类Unity3D脚本中文系列教程(十三)辅助类. ...

  7. Lua print on the same line

    In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we h ...

  8. 如何在linux系统下对文件夹名有空格的文件夹进行操作

    http://www.2cto.com/os/201409/335119.html 在Windows操作系统中可以轻易地创建\移动\删除文件夹名带有空格的文件夹, 而在linux则需要进行一些特殊的处 ...

  9. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

  10. Pythonxy下载链接

    http://pythonxy.connectmv.com/ 版权声明:本文为博主原创文章,未经博主允许不得转载.