思路是,先从mp3中提取pcm(raw原始数据),再将原始数据转成speex。

貌似不能播放,可能还存在其他问题,需要继续研究。

使用了两个类库NSpeex和NAudio

            using (var waveStream = new NAudio.Wave.Mp3FileReader(@"D:\我的项目源码\Record\Record\bin\Debug\2.mp3"))
{
using (var fileOutputStream = new FileStream(@"D:\我的项目源码\Record\Record\bin\Debug\xxx.spx", FileMode.Create, FileAccess.Write))
{
byte[] buff = new byte[waveStream.Length];
var r = waveStream.Read(buff, , buff.Length);
var bytes = EncodeSpeech(buff, buff.Length);
fileOutputStream.Write(bytes, , bytes.Length);
}
}
private static byte[] EncodeSpeech(byte[] buf, int len)
{
SpeexEncoder encoder = new SpeexEncoder(BandMode.Narrow); // set encoding quality to lowest (which will generate the smallest size in the fastest time)
encoder.Quality = ; int inDataSize = len / ;
// convert to short array
short[] data = new short[inDataSize];
int sampleIndex = ;
for (int index = ; index < len; index += , sampleIndex++)
{
data[sampleIndex] = BitConverter.ToInt16(buf, index);
} // note: the number of samples per frame must be a multiple of encoder.FrameSize
inDataSize = inDataSize - inDataSize % encoder.FrameSize; var encodedData = new byte[len];
int encodedBytes = encoder.Encode(data, , inDataSize, encodedData, , len);
if (encodedBytes != )
{
// each chunk is laid out as follows:
// | 4-byte total chunk size | 4-byte encoded buffer size | <encoded-bytes> |
byte[] inDataSizeBuf = BitConverter.GetBytes(inDataSize);
byte[] sizeBuf = BitConverter.GetBytes(encodedBytes + inDataSizeBuf.Length);
byte[] returnBuf = new byte[encodedBytes + sizeBuf.Length + inDataSizeBuf.Length];
sizeBuf.CopyTo(returnBuf, );
inDataSizeBuf.CopyTo(returnBuf, sizeBuf.Length);
Array.Copy(encodedData, , returnBuf, sizeBuf.Length + inDataSizeBuf.Length, encodedBytes);
return returnBuf;
}
else
return buf;
}
/// <summary>
/// byte数组转short数组
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
private static short[] BytesToShorts(byte[] bytes)
{
//short[] data = new short[bytes.Length / 2];
//Buffer.BlockCopy(bytes, 0, data, 0, bytes.Length);
//return data; //convert to short
short[] data = new short[bytes.Length / ];
int sampleIndex = ;
for (int index = ; sampleIndex < data.Length; index += , sampleIndex++)
{
data[sampleIndex] = BitConverter.ToInt16(bytes, index);
}
return data;
} /// <summary>
/// 获取音频时长
/// </summary>
/// <param name="voiceFile"></param>
/// <returns></returns>
private static int GetVoiceTimeLength(string voiceFile)
{
ShellClass sh = new ShellClass();
var dir = sh.NameSpace(Path.GetDirectoryName(voiceFile));
var item = dir.ParseName(Path.GetFileName(voiceFile));
string str = dir.GetDetailsOf(item, );// 获取歌曲时长。 if (!String.IsNullOrEmpty(str))
{
var arr = str.Split(':');
var i = int.Parse(arr[]) * + int.Parse(arr[]) * + int.Parse(arr[]);
return i;
}
else
return ;
}

第二种获取时长方法

        private static double GetVoiceTimeLength2(string voiceFile)
{
using (var waveStream = new NAudio.Wave.Mp3FileReader(voiceFile))
{
return Math.Floor(waveStream.TotalTime.TotalSeconds);
}
}

mp3转speex的一些研究(貌似不能播放,暂存着)的更多相关文章

  1. HTML5+学习笔记2-------边看代码边研究貌似还是有点问题...还在研究中api中

    // 拍照 function getImage() { outSet( "开始拍照:" ); var cmr = plus.camera.getCamera(); cmr.capt ...

  2. (原创)speex与wav格式音频文件的互相转换(二)

    之前写过了如何将speex与wav格式的音频互相转换,如果没有看过的请看一下连接 http://www.cnblogs.com/dongweiq/p/4515186.html 虽然自己实现了相关的压缩 ...

  3. [C++] 将 mp3 等音乐资源以资源形式嵌入 exe 文件中

    引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...

  4. 将 mp3 等音乐资源以资源形式嵌入 exe 文件中

    引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...

  5. VC播放mp3的方法

    1.使用msi库 #include <mmsystem.h> #pragma comment(lib,"winmm.lib") ....... //打开文件 MCI_O ...

  6. Ubuntu终端命令行播放音乐(mp3)

    有很多在终端命令行播放mp3的工具,有的甚至可以生成播放列表.也只有命令行重度使用者有这个需求,下面我们来看一看这些工具. Sox Sox(Sound eXchange)是操作声音文件的瑞士军刀,它可 ...

  7. 嵌入式mp3播放器

    分四部分:按键驱动,声卡驱动,Madplay播放器移植,MP3主播放器处理 按键1:播放,按键2:停止,按键3:上一曲,按键4:下一曲 UA1341内核自带声卡驱动 .解压内核: tar zxvf l ...

  8. Java NIO ByteBuffer 的使用与源码研究

    一.结论 ByteBuffer 是Java NIO体系中的基础类,所有与Channel进行数据交互操作的都是以ByteBuffer作为数据的载体(即缓冲区).ByteBuffer的底层是byte数组, ...

  9. git的初步研究1

    工作中很多项目再往git上迁移,所以打算研究下git git是个版本控制系统 理解git工作区.暂存区.版本库的概念 工作区:在电脑中能看到的目录 暂存区:index即索引 即首先add加入暂存区 c ...

随机推荐

  1. [Selenium With C#基础教程] Lesson-03 超级链接

    作者:Surpassme 来源:http://www.jianshu.com/p/83809943e751 声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢. 超级链接或链接是We ...

  2. DRBD+Heartbeat实现自动切换

    1>HeartBeat介绍 Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组件,在 Heartbeat 项 ...

  3. CentOS 7.4 shell 不显示当前用户和路径的问题

    步骤如下:   vim ~/.bash_profile (不用管.bash_profile这个文件有几个,自己新建一个也是可以的)    在最后加上 export PS1='[\u@\h \W]\$' ...

  4. DATASNAP远程方法返回TSTREAM正解

    DATASNAP远程方法返回TSTREAM正解 DATASNAP远程方法返回TSTREAM,如果数据大小超过32K是会报错的.许多DELPHIER栽在这个上头,甚至开始怀疑TSTREAM返回数据的可行 ...

  5. HighCharts使用总结

    1.常用属性 chart: type:areaspline(线面图).arearange(区间图) zoomType: 缩放类型(沿着'xy'轴缩放) alignTicks:设置坐标轴刻度对齐. 当有 ...

  6. Oracle索引技术研究

    Oracle索引类型 B树索引 特定类型索引 确定索引列 主键和唯一键值列的索引 外键索引 其他合适的索引列 B树索引 B树索引算法 B树是指B-tree(Balanced Tree),B树的存在是为 ...

  7. Spring中ApplicationContext和beanfactory区别---解析一

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  8. Linq与数据库的连接显示查询(一)

    使用linq查询sql数据库是首先需要创建一个 linq  to  sql 类文件 创建linq  to  sql的步骤: 1在Visual  Studio 2015开发环境中建立一个目标框架 Fra ...

  9. sql 常用基础查询

    *查询第一条记录:select   top   1   *   from   tablename *随机查询N条记录:select top N * from table order by newid( ...

  10. iOS 使用 TestFlight 测试

    TestFlight 已经并入 Itunes connect. 测试方法: 1. itunes connect 上创建应用 2. xcode 里 archive 应用并 submit 到 itunes ...