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)的更多相关文章

  1. 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 数据文 ...

  2. 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 ...

  3. 【分享】利用Apache的Htaccess Files命令限制訪问文件类型,Files正则

    假设你在你的模板目录中有非常多PSD HTML模板,那么用接下来这个htaccess文件能够保护限制訪问: 文件D:\WebSite\ZBPHP.COM\www\Tpl\.htaccess 所有源代码 ...

  4. Python教程大纲

    缘起:最近想在部门推Python语言,写这个blog主要就是个教程大纲,之前先列出一些资源:Python历史:http://www.docin.com/p-53019548.html          ...

  5. The Python Standard Library

    The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...

  6. 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 ...

  7. [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 ...

  8. 详解 Too many open files

    运行在Linux系统上的Java程序可能会出现"Too many open files"的异常情况,且常见于高并发访问文件系统,多线程网络连接等场景. 程序经常访问的文件.sock ...

  9. 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 ...

随机推荐

  1. 解决git报ssh variant 'simple' does not support setting port

    解决办法 在git bash中输入命令 git config --global ssh.variant ssh

  2. C++Primer笔记-----day06

    ================================================================day06=============================== ...

  3. partial分部类

    意义 1.源代码控制 2.将一个类或结构分成不同的逻辑单元 3.代码拆分

  4. 压力测试工具--Siege

    Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行.s ...

  5. lnline Hook初试

    简单解释下hook: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理 ...

  6. Nginx源码完全注释(5)core/ngx_cpuinfo.c

    /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ...

  7. iOS中wkwebview加载本地html的要点

    项目中有些页面,我采用了html页面开发,然后用wkwebview加载的设计.在加载过程中遇见了一些问题,在这里进行一些记载和讨论.如有不同意见欢迎进行评论沟通. 问题时候这样的: 在webview的 ...

  8. jdk8中LocalDateTime,LocalDate,LocalTime等日期时间类

    package com.zy.time; import org.junit.Test; import java.time.*; import java.time.format.DateTimeForm ...

  9. adf笔记

    1>jsf页面js调试,手动添加debugger调试 方案:在页面中添加debugger,然后打开“开发者工具”(必须打开),直接运行页面自动跳转到debugger处. 2>jdevelo ...

  10. code1047 邮票面值设计

    dfs+dp dfs枚举每种情况,每层递归确定第k个数i:i = a[k-1]+1 to a[k-1]*n+1 当枚举完一个序列时,使用check()测试它能达到的max 使用dp.设dp[i]为凑成 ...