mp3转speex的一些研究(貌似不能播放,暂存着)
思路是,先从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的一些研究(貌似不能播放,暂存着)的更多相关文章
- HTML5+学习笔记2-------边看代码边研究貌似还是有点问题...还在研究中api中
// 拍照 function getImage() { outSet( "开始拍照:" ); var cmr = plus.camera.getCamera(); cmr.capt ...
- (原创)speex与wav格式音频文件的互相转换(二)
之前写过了如何将speex与wav格式的音频互相转换,如果没有看过的请看一下连接 http://www.cnblogs.com/dongweiq/p/4515186.html 虽然自己实现了相关的压缩 ...
- [C++] 将 mp3 等音乐资源以资源形式嵌入 exe 文件中
引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...
- 将 mp3 等音乐资源以资源形式嵌入 exe 文件中
引用:http://www.easyx.cn/skills/View.aspx?id=6 本文讲解怎样将 mp3 等音乐资源以资源形式嵌入 exe 文件中,并通过 mciSendString 调用.嵌 ...
- VC播放mp3的方法
1.使用msi库 #include <mmsystem.h> #pragma comment(lib,"winmm.lib") ....... //打开文件 MCI_O ...
- Ubuntu终端命令行播放音乐(mp3)
有很多在终端命令行播放mp3的工具,有的甚至可以生成播放列表.也只有命令行重度使用者有这个需求,下面我们来看一看这些工具. Sox Sox(Sound eXchange)是操作声音文件的瑞士军刀,它可 ...
- 嵌入式mp3播放器
分四部分:按键驱动,声卡驱动,Madplay播放器移植,MP3主播放器处理 按键1:播放,按键2:停止,按键3:上一曲,按键4:下一曲 UA1341内核自带声卡驱动 .解压内核: tar zxvf l ...
- Java NIO ByteBuffer 的使用与源码研究
一.结论 ByteBuffer 是Java NIO体系中的基础类,所有与Channel进行数据交互操作的都是以ByteBuffer作为数据的载体(即缓冲区).ByteBuffer的底层是byte数组, ...
- git的初步研究1
工作中很多项目再往git上迁移,所以打算研究下git git是个版本控制系统 理解git工作区.暂存区.版本库的概念 工作区:在电脑中能看到的目录 暂存区:index即索引 即首先add加入暂存区 c ...
随机推荐
- linux每天一小步---rm命令详解
1 命令功能 rm命令用于删除文件或者目录,值得注意的是linux下的删除不弯曲等同于windows系统下的删除操作,linux系统下一旦删除了文件或者目录那么它将消失,而windows系统下我们还可 ...
- 打开窗口进行选择文件(txt文件),打开所选文件,读入文件
用mfc编写项目的时候往往需要调用窗口,允许用户通过窗口进行选择文件操作 TCHAR szBuffer[MAX_PATH] = { 0 }; OPENFILENAME ofn = { 0 }; ofn ...
- loadrunner - 问题汇总及解决方案(持续更新)
1.在此界面点击Run Load Tests提示: ”Can not save the license information because access to the registry is de ...
- Get Current LOV Query SQL
--3 click the lov object activing last query address. SELECT T.SQL_TEXT FROM V$SQLTEXT_WITH_NEWL ...
- [20190423]oradebug peek测试脚本.txt
[20190423]oradebug peek测试脚本.txt --//工作测试需要写一个oradebug peek测试脚本,不断看某个区域内存地址的值. 1.环境: SCOTT@book> @ ...
- ASP.NET MVC验证DateTime的问题
今天碰到一个Bug,在IE8中,日期验证失效,输入正确的日期格式也会验证失败,提示:xxx必须是日期格式(the field xxx must be a date) 最终找到问题所在:jquery.v ...
- Centos 安装 erlang 环境
系统 Centos 6.5 64位 Erlang 18.3.4 安装依赖组件 yum install -y gcc gcc-g++ unixODBC unixODBC-devel wxBase wxG ...
- nova 宿主机重启自动恢复虚拟机运行状态
1. 宿主机重启自动恢复虚拟机运行状态 1.1 参数描述与默认值 # Whether to start guests that were running before the host reboote ...
- 删除标注关联仿dda命令DIMDISASSOCIATE
static void sk_ARXTestXDatamydimassoc(void) { // Add your code for command sk_ARXTestXData.mydimasso ...
- BruteXSS(汉化版)
BruteXSS是一个非常强大和快速的跨站点脚本暴力注入.它用于暴力注入一个参数.该BruteXSS从指定的词库加载多种有效载荷进行注入并且使用指定的载荷和扫描检查这些参数很容易受到XSS漏洞.得益于 ...