public static void ADPCM2Wav(byte[] data, int sampleRate, Stream output) { RiffTags riff = new RiffTags() { FileType = "WAVE", }; byte[] adpcm = new byte[data.Length]; short[] wavSamples = new short[data.Length * 2]; Buffer.BlockCopy(data, 0, adpcm, 0, data.Length); ADPCM2PCM2(wavSamples, 0, adpcm, 0); /*Deinterleave(adpcm); * ADPCM2PCM(wavSamples, 0, adpcm, 0, adpcm.Length / 2); * ADPCM2PCM(wavSamples, wavSamples.Length / 2, adpcm, adpcm.Length / 2, adpcm.Length / 2); * Interleave(wavSamples);*/ byte[] wavData = new byte[wavSamples.Length * sizeof(short)]; Buffer.BlockCopy(wavSamples, 0, wavData, 0, wavData.Length); WaveFmt fmt = new WaveFmt { Format = 1, Channels = 2, SampleRate = sampleRate * 1, BitsPerSample = 16, }; fmt.UpdateValues(); riff["fmt "] = new RiffTag("fmt ", fmt.ToBytes()); riff["data"] = new RiffTag("data", wavData); riff.Save(output); }
public static void ADPCM2Wav(Stream input, Stream output) { RiffTags riff = new RiffTags(input); // Remove unnecissary tags foreach (string key in riff.Keys.ToArray()) { if (key != "fmt " && key != "data") { riff.Remove(key); } } if (riff.FileType != "WAVE") { throw new Exception("Input is not a WAVE file!"); } WaveFmt fmt = WaveFmt.Read(riff["fmt "].Data); if (fmt.Format != 20) { throw new Exception("Input is not ADPCM format!"); } if (fmt.Channels != 2) { throw new Exception("Input does not have 2 channels!"); } if (fmt.BitsPerSample != 4) { throw new Exception("Input does not have 4 bits per sample!"); } byte[] data = riff["data"].Data; byte[] adpcm = new byte[data.Length]; short[] wavSamples = new short[data.Length * 2]; Buffer.BlockCopy(data, 0, adpcm, 0, data.Length); ADPCM2PCM2(wavSamples, 0, adpcm, 0); ADPCM2PCM(wavSamples, 0, adpcm, 0, adpcm.Length / 2); ADPCM2PCM(wavSamples, wavSamples.Length / 2, adpcm, adpcm.Length / 2, adpcm.Length / 2); //Interleave(wavSamples); byte[] wavData = new byte[wavSamples.Length * sizeof(short)]; Buffer.BlockCopy(wavSamples, 0, wavData, 0, wavData.Length); fmt.Format = 1; fmt.BitsPerSample = 16; fmt.UpdateValues(); riff["fmt "] = new RiffTag("fmt ", fmt.ToBytes()); riff["data"] = new RiffTag("data", wavData); riff.Save(output); }
public static bool MaximizeWavVolume(string inputFile, out string outputFile) { RiffTags riff = new RiffTags(inputFile); // Remove unnecissary tags foreach (string key in riff.Keys.ToArray()) { if (key != "fmt " && key != "data") { riff.Remove(key); } } if (riff.FileType != "WAVE") { throw new Exception("Input is not a WAVE file!"); } WaveFmt fmt = WaveFmt.Read(riff["fmt "].Data); if (fmt.Format != 1) { throw new Exception("Input is not PCM format!"); } if (fmt.Channels != 2) { throw new Exception("Input does not have 2 channels!"); } if (fmt.BitsPerSample != 16) { throw new Exception("Input does not have 16 bits per sample!"); } if (fmt.SampleRate > 5000) { throw new Exception("Sample rate must be 5000Hz or less!"); } byte[] data = riff["data"].Data; short[] wavSamples = new short[data.Length / 2]; Buffer.BlockCopy(data, 0, wavSamples, 0, data.Length); short max = 0; short min = 0; for (int i = 0; i < wavSamples.Length; i++) { max = Math.Max(max, wavSamples[i]); min = Math.Min(min, wavSamples[i]); } // Silent File if (min == 0 && max == 0) { outputFile = inputFile; return(false); } // Lazy maximum volume checking const short range = 32; if (min <= (short.MinValue + range) || max >= (short.MaxValue - range)) { outputFile = inputFile; return(false); } double maxScale = (double)short.MaxValue / max; double minScale = (double)short.MinValue / min; double scale = Math.Min(maxScale, minScale); for (int i = 0; i < wavSamples.Length; i++) { wavSamples[i] = (short)(wavSamples[i] * scale); } byte[] wavData = new byte[wavSamples.Length * sizeof(short)]; Buffer.BlockCopy(wavSamples, 0, wavData, 0, wavData.Length); outputFile = Path.Combine(Directory.GetCurrentDirectory(), "wiimote.maximized.wav"); riff["data"] = new RiffTag("data", wavData); riff.Save(outputFile); return(false); }