如何转换音频数据格式1
2013-04-02 22:46
441 查看
原文在此:http://www.codeproject.com/Articles/501521/How-to-convert-between-most-audio-formats-in-NET
前面的音频处理背景知识就先跳过,需要的请自行脑补。
直接上干货。
这种方式其实也相当于把3个采样点,线性拟合变成了2个了。
=================== 占坑,以后讲原理====================
新采样率小于旧的,就会在现有的点上,等比例往后拉。
========== 留坑,讲重采样的测试==========
下一部分,音频文件格式的转换
前面的音频处理背景知识就先跳过,需要的请自行脑补。
直接上干货。
一、声道转换
1、单声道转立体声
原理,双声道的16位采样,每16位是一个声道,也就是两字节;下一个16位是另外一个声道,交错进行。private byte[] MonoToStereo(byte[] input) { byte[] output = new byte[input.Length * 2]; int outputIndex = 0; for (int n = 0; n < input.Length; n+=2) { // copy in the first 16 bit sample output[outputIndex++] = input ; output[outputIndex++] = input[n+1]; // now copy it in again output[outputIndex++] = input ; output[outputIndex++] = input[n+1]; } return output; }
2、立体声转单声道
原理,去掉一半的数据即可。private byte[] StereoToMono(byte[] input) { byte[] output = new byte[input.Length / 2]; int outputIndex = 0; for (int n = 0; n < input.Length; n+=4) { // copy in the first 16 bit sample output[outputIndex++] = input ; output[outputIndex++] = input[n+1]; } return output; }
3、混合立体声转单声道
如果是混合立体声,则可以把左右声道的数据求平均,得到单声道的值private byte[] MixStereoToMono(byte[] input) { byte[] output = new byte[input.Length / 2]; int outputIndex = 0; for (int n = 0; n < input.Length; n+=4) { int leftChannel = BitConverter.ToInt16(input,n); int rightChannel = BitConverter.ToInt16(input,n+2); int mixed = (leftChannel + rightChannel) / 2; byte[] outSample = BitConverter.GetBytes((short)mixed); // copy in the first 16 bit sample output[outputIndex++] = outSample[0]; output[outputIndex++] = outSample[1]; } return output; }
二、位宽转换
4、16位转32位float
相对简单,把每个16bit(两个byte,合成一个short)除以16位的最大值,得到一个相对的float值(介于0-1之间)。public float[] Convert16BitToFloat(byte[] input) { int inputSamples = input.Length / 2; // 16 bit input, so 2 bytes per sample float[] output = new float[inputSamples]; int outputIndex = 0; for(int n = 0; n < inputSamples; n++) { short sample = BitConverter.ToInt16(input,n*2); output[outputIndex++] = sample / 32768f; } return output; }
5、24位转32位float
这个就稍微麻烦了,从原数据中每次取24位,即3个byte,补上一个0,折合成一个int,然后除以3个byte组成的数据最大值,得到一个相对float值(介于0-1之间)。public float[] Convert24BitToFloat(byte[] input) { int inputSamples = input.Length / 3; // 24 bit input float[] output = new float[inputSamples]; int outputIndex = 0; var temp = new byte[4]; for(int n = 0; n < inputSamples; n++) { // copy 3 bytes in Array.Copy(input,n*3,temp,0,3); int sample = BitConverter.ToInt32(temp,0); output[outputIndex++] = sample / 16777216f; } return output; }
这种方式其实也相当于把3个采样点,线性拟合变成了2个了。
6、还原数据
两种方式还原的代码一样(后一种多的一个点信息已经丢失,还原也只有2个byte了):for (int sample = 0; sample < sourceSamples; sample++) { // adjust volume float sample32 = sourceBuffer[sample] * volume; // clip if (sample32 > 1.0f) sample32 = 1.0f; if (sample32 < -1.0f) sample32 = -1.0f; destBuffer[destOffset++] = (short)(sample32 * 32767); }
三、重采样
采样是这个文章中比较复杂的部分。=================== 占坑,以后讲原理====================
7、一个简单的重采样算法
原理就是,拉大或缩小采样点的间距。当然,明显的是,如果如果新采样率大于旧的,其实没有意义,造成很多点只会简单重复。新采样率小于旧的,就会在现有的点上,等比例往后拉。
// Just about worst resampling algorithm possible: private float[] ResampleNaive(float[] inBuffer, int inputSampleRate, int outputSampleRate) { var outBuffer = new List<float>(); double ratio = (double) inputSampleRate / outputSampleRate; int outSample = 0; while (true) { int inBufferIndex = (int)(outSample++ * ratio); if (inBufferIndex < read) writer.WriteSample(inBuffer[inBufferIndex]); else break; } return outBuffer.ToArray(); }
========== 留坑,讲重采样的测试==========
下一部分,音频文件格式的转换
相关文章推荐
- 如何转换音频数据格式1
- [sql技巧]int型的(毫)秒数据如何转换成字符型的hh:mm:ss时间格式
- 在ENVI进行的土地利用分类数据,需要做景观指数分析,如何将其转换成GRID格式呀
- 如何将gml格式的网络数据转换为可处理的格式
- 如何将C#/.NET 将json字符串格式数据转换成对象
- 如何在 Ubuntu 上转换图像、音频和视频格式
- 如何将对象转换成json格式,如何将json格式数据转换成js对象
- Json是什么?Json的数据格式如何转换
- 如何将一个List<Map<String,String>>类型的集合数据转换成json格式输出
- 如何将list类型的数据转换成json格式在服务器端
- 如何将matlab数据转换成libsvm格式文件
- 如何把数据转换成libsvm可以接受的数据格式
- 如何将语音文件转换为Asterisk可以播放的音频格式文件(.wav)
- jQuery、Ajax,DataTable数据如何转换成Json格式
- 如何将Pcm格式的音频文件转换成Wave格式的文件
- 如何将json数据转换为常见的数据格式
- java 代码转换音频数据格式(字节数组转换)
- 关于如何将下载的json数据转换为utf-8格式
- 如何将数据从DATASET写入XML文档,并转换成其他应用系统所需的格式!(如RSS阅读器XML文档格式)
- 如何在C#中进行音频格式转换