Decoding VOX Files in C# (Converting VOX Files to WAV Files)
I wrote a C# class to decode VOX files into WAV files. It follows the Dialogic ADPCM specificationstrictly. If you read through that specification, the code below will become a lot clearer, otherwise you might think you’re reading another language altogether. The specification is really quite simple and nice once you boil it down. Note that the Dialogic ADPCM specification is different from the way NMS Communications libraries create VOX files as their file format is slightly different, and for files such as those, the code below will not work without some tweaks.
My implementation to decode from VOX to WAV files is as follows:
using System;
using System.IO; class VOXDecoder
{ static float signal = 0;
static int previousStepSizeIndex = 0;
static bool computedNextStepSizeOnce = false;
static int[] possibleStepSizes = new int[49] { 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552 }; public static void Decode(string inputFile, out string outputFile)
{
outputFile = String.Format("{0}\\{1}.wav", Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile));
using (FileStream inputStream = File.Open(inputFile, FileMode.Open))
using (BinaryReader reader = new BinaryReader(inputStream))
using (FileStream outputStream = File.Create(outputFile))
using (BinaryWriter writer = new BinaryWriter(outputStream))
{
// Note that 32-bit integer values always take up 4 bytes.
// Note that 16-bit integer values (shorts) always take up 2 bytes.
// Note that HEX values resolve as 32-bit integers unless casted as something else, such as short values.
// ChunkID: "RIFF"
writer.Write(0x46464952);
// ChunkSize: The size of the entire file in bytes minus 8 bytes for the two fields not included in this count: ChunkID and ChunkSize.
writer.Write((int)(reader.BaseStream.Length * 4) + 36);
// Format: "WAVE"
writer.Write(0x45564157);
// Subchunk1ID: "fmt " (with the space).
writer.Write(0x20746D66);
// Subchunk1Size: 16 for PCM.
writer.Write(16);
// AudioFormat: 1 for PCM.
writer.Write((short)1);
// NumChannels: 1 for Mono. 2 for Stereo.
writer.Write((short)1);
// SampleRate: 8000 is usually the default for VOX.
writer.Write(8000);
// ByteRate: SampleRate * NumChannels * BitsPerSample / 8.
writer.Write(12000);
// BlockAlign: NumChannels * BitsPerSample / 8. I rounded this up to 2. It sounds best this way.
writer.Write((short)2);
// BitsPerSample: I will set this as 12 (12 bits per raw output sample as per the VOX specification).
writer.Write((short)12);
// Subchunk2ID: "data"
writer.Write(0x61746164);
// Subchunk2Size: NumSamples * NumChannels * BitsPerSample / 8. You can also think of this as the size of the read of the subchunk following this number.
writer.Write((int)(reader.BaseStream.Length * 4));
// Write the data stream to the file in linear audio.
while (reader.BaseStream.Position != reader.BaseStream.Length)
{
byte b = reader.ReadByte();
float firstDifference = GetDifference((byte)(b / 16));
signal += firstDifference;
writer.Write(TruncateSignalIfNeeded());
float secondDifference = GetDifference((byte)(b % 16));
signal += secondDifference;
writer.Write(TruncateSignalIfNeeded());
}
}
} static short TruncateSignalIfNeeded()
{
// Keep signal truncated to 12 bits since, as per the VOX spec, each 4 bit input has 12 output bits.
// Note that 12 bits is 0b111111111111. That's 0xFFF in HEX. That's also 4095 in decimal.
// The sound wave is a signed signal, so factoring in 1 unused bit for the sign, that's 4095/2 rounded down to 2047.
if (signal > 2047)
{
signal = 2047;
}
if (signal < -2047)
{
signal = -2047;
}
return (short)signal;
} static float GetDifference(byte nibble)
{
int stepSize = GetNextStepSize(nibble);
float difference = ((stepSize * GetBit(nibble, 2)) + ((stepSize / 2) * GetBit(nibble, 1)) + (stepSize / 4 * GetBit(nibble, 0)) + (stepSize / 8));
if (GetBit(nibble, 3) == 1)
{
difference = -difference;
}
return difference;
} static byte GetBit(byte b, int zeroBasedBitNumber)
{
// Shift the bits to the right by the number of the bit you want to get and then logic AND it with 1 to clear bits trailing to the left of your desired bit.
return (byte)((b >> zeroBasedBitNumber) & 1);
} static int GetNextStepSize(byte nibble)
{
if (!computedNextStepSizeOnce)
{
computedNextStepSizeOnce = true;
return possibleStepSizes[0];
}
else
{
int magnitude = GetMagnitude(nibble);
if (previousStepSizeIndex + magnitude > 48)
{
previousStepSizeIndex = previousStepSizeIndex + magnitude;
return possibleStepSizes[48];
}
else if (previousStepSizeIndex + magnitude > 0)
{
previousStepSizeIndex = previousStepSizeIndex + magnitude;
return possibleStepSizes[previousStepSizeIndex];
}
else
{
return possibleStepSizes[0];
}
}
} static int GetMagnitude(byte nibble)
{
if (nibble == 15 || nibble == 7)
return 8;
else if (nibble == 14 || nibble == 6)
return 6;
else if (nibble == 13 || nibble == 5)
return 4;
else if (nibble == 12 || nibble == 4)
return 2;
else
return -1;
}
}
It is easily called through the following two lines:
string outputWAVFilePath;
VOXDecoder.Decode(pathToYourVOXFile, out outputWAVFilePath);
Give it a shot with this sample Dialogic ADPCM VOX audio fil
Decoding VOX Files in C# (Converting VOX Files to WAV Files)的更多相关文章
- 17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files
17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files 如果数据库是大的, 复制raw 数据文 ...
- reading words in your computer and changing to female voice, linux festival text2wave saving wav files
on a brand new linux PC, e.g. ubuntu 14.04 amd64 To hear voice sudo apt-get install festival -y then ...
- 【分享】利用Apache的Htaccess Files命令限制訪问文件类型,Files正则
假设你在你的模板目录中有非常多PSD HTML模板,那么用接下来这个htaccess文件能够保护限制訪问: 文件D:\WebSite\ZBPHP.COM\www\Tpl\.htaccess 所有源代码 ...
- Python教程大纲
缘起:最近想在部门推Python语言,写这个blog主要就是个教程大纲,之前先列出一些资源:Python历史:http://www.docin.com/p-53019548.html ...
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- Huge CSV and XML Files in Python, Error: field larger than field limit (131072)
Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...
- [ImportNew] Perforce - Restoring Mistakenly Deleted Files in Workspace
Shit happens when you accidentally delete some files in your workspace and you have no ideas which o ...
- 详解 Too many open files
运行在Linux系统上的Java程序可能会出现"Too many open files"的异常情况,且常见于高并发访问文件系统,多线程网络连接等场景. 程序经常访问的文件.sock ...
- Embed dll Files Within an exe (C# WinForms)—Winform 集成零散dll进exe的方法
A while back I was working on a small C# WinForms application in Visual Studio 2008. For the sake of ...
随机推荐
- 浅谈AVL树,红黑树,B树,B+树原理及应用(转)
出自:https://blog.csdn.net/whoamiyang/article/details/51926985 背景:这几天在看<高性能Mysql>,在看到创建高性能的索引,书上 ...
- java第三方类库实现图片等比缩放
public class ThumbnailTest { public static void main(String[] args) { InputStream is = null; try { / ...
- ELK-Stack 最后一次全篇文档
简介: ELK-Stack 日志收集系统.最后一次全篇记录的笔记,之后关于 ELK 的笔记都将是片段型.针对性的. 环境介绍: ELK-Stack:192.168.1.25 ( Redis.LogS ...
- kaptcha验证码组件使用简介
Kaptcha是一个基于SimpleCaptcha的验证码开源项目. 官网地址:http://code.google.com/p/kaptcha/ kaptcha的使用比较方便,只需添加jar包依赖之 ...
- jQuery——表单异步提交
如果不做任何处理,表单提交时会刷新页面,为了改善体验,可以使用jQuery做到异步提交表单:通过$("#form").serialize()将表单元素的数据转化为字符串,然后通过$ ...
- 学习ios一段过程后的思考
现在回想起来,学习ios也有一段时间了,大概三个月不到吧,本来是搞linux驱动,刚开始来公司就我一个人负责驱动的东西,主要就是一些bug的解决,后来系统基本上稳定了,我就闲下来了,公司又有些移动医疗 ...
- Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
在Apache的配置文件 httpd.conf 中开启 LoadModule headers_module modules/mod_headers.so 即可解决这个问题.
- Scala开发Hadoop示例
import org.apache.hadoop.conf.{Configuration, Configured}; import org.apache.hadoop.util.{ToolRunner ...
- 相机IMU融合四部曲(二):误差状态四元数详细解读
相机IMU融合四部曲(二):误差状态四元数详细解读 极品巧克力 前言 上一篇文章,<D-LG-EKF详细解读>中,讲了理论上的SE3上相机和IMU融合的思想.但是,还没有涉及到实际的操作, ...
- PXE
PXE 摘自:http://www.360doc.com/content/15/0226/08/17652659_450872586.shtml 一.简介 1.1 什么是PXE PXE(Pre ...