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 ...
随机推荐
- ZSTU4266 回文 2017-03-22 14:25 55人阅读 评论(0) 收藏
4266: 回文 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1636 Solved: 504 Description 小王想知道一个字符串是否为 ...
- Centos 7 安装 mysql5.7
1.需要下载mysql 下载地址:http://dev.mysql.com/downloads/mysql/ 2.将下载的rpm包上传到centos 7上(我是放在根下面的opt目录) 3. 安装my ...
- OpenglEs开篇
1.,但博客有接近一年没有写了.虽然有学到东西,但没有记录感觉是是空空的,最近在学习Opengles, 现在开始重操旧业(写博客了).
- 使用Emit实现给实体赋值
Dapper.net的速度很快,最近看源码,原来他orm的实现是通过编写大量IL代码实现的. 使用DynamicMethod,自己编织一个给实体赋值的方法.这种写法效率很高,接近直接对属性赋值.比使用 ...
- WPF透明窗体不支持缩放解决方案
方案一 WPF中的无边框透明窗体,由于没有边并且透明,窗体无法进行缩放操作,今天来讲解如何解决这个问题. 先说一下思路,我们先手为该窗体添加4个边,4个角用于缩放操作,然后再为他们写事件,完成拖放操作 ...
- 部署网络存储ISCSI
1.什么是ISCSIInternet Small Computer System Interface 互联网小型计算机接口技术,是一种将SCS存储与以太网技术相结合,可以用来在互联网中传输SCSI接口 ...
- Android 载入 HTML
Android 中载入 HTML 有两种方式: 1. 用 TextView.setText(Html.fromHtml("<html></html>")); ...
- 磁盘 blk_update_request: I/O error
1.尝试1: 解决 blk_update_request: I/O error, dev fd0, sector 0 错误 参考文档: https://bbs.archlinux.org/viewto ...
- java学习笔记—实现一个类MyInputStream(28)
1 实现一个类MyInputStream读取文件,且不能抛出异常 public class TestDemo { public static void main(String[] args) thro ...
- redis5.0.4-cluster集群搭建及jedis客户端操作
一.去官网下载redis5.0 https://redis.io/download 然后解压安装 $ .tar.gz $ cd redis- $ make 二.准备配置文件 打开redis-5.0.4 ...