/// <summary> /// Converts a WAV file's bits/sample and number of channels to a separate WAV file. /// </summary> /// <param name="pSrcFilename">The name of the file to convert</param> /// <param name="pDestFilename">The destination file name</param> /// <param name="pBitsPerSample">The destination's number of bits/sample</param> /// <param name="pStereo">Whether or not the destination should be stereo</param> /// <param name="pVolumeMultiplier">A multiplier that can be used to adjust the volume of the output audio file</param> public static void CopyAndConvert(String pSrcFilename, String pDestFilename, short pBitsPerSample, bool pStereo, double pVolumeMultiplier) { WAVFile srcFile = new WAVFile(); String retval = srcFile.Open(pSrcFilename, WAVFileMode.READ); if (retval != "") throw new WAVFileException(retval, "WAVFile.Convert_Copy()"); WAVFile destFile = new WAVFile(); destFile.Create(pDestFilename, pStereo, srcFile.SampleRateHz, pBitsPerSample); if ((srcFile.BitsPerSample == 8) && (pBitsPerSample == 8)) { byte sample = 0; if (srcFile.IsStereo && !pStereo) { // 8-bit to 8-bit, stereo to mono: Average each 2 samples while (srcFile.NumSamplesRemaining > 0) { sample = (byte)((short)((short)srcFile.GetNextSample_8bit() + (short)srcFile.GetNextSample_8bit()) / 2); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 8-bit to 8-bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_8bit(); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 8-bit to 8-bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_8bit(); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); destFile.AddSample_8bit(sample); } } } else if ((srcFile.BitsPerSample == 8) && (pBitsPerSample == 16)) { short sample = 0; if (srcFile.IsStereo && !pStereo) { // 8-bit to 16 bit, stereo to mono: Average each 2 samples while (srcFile.NumSamplesRemaining > 0) { sample = (short)((int)((int)srcFile.GetNextSampleAs16Bit() + (int)srcFile.GetNextSampleAs16Bit()) / 2); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 8-bit to 16 bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSampleAs16Bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 8-bit to 16 bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSampleAs16Bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); destFile.AddSample_16bit(sample); } } } else if ((srcFile.BitsPerSample == 16) && (pBitsPerSample == 8)) { byte sample = 0; if (srcFile.IsStereo && !pStereo) { // 16-bit to 8-bit, stereo to mono: Average each 2 samples short sample_16bit = 0; while (srcFile.NumSamplesRemaining > 0) { sample_16bit = (short)((int)srcFile.GetNextSample_16bit() + (int)srcFile.GetNextSample_16bit() / 2); if (pVolumeMultiplier != 1.0) sample_16bit = (short)((double)sample_16bit * pVolumeMultiplier); sample = ScaleShortToByte(sample_16bit); destFile.AddSample_8bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 16-bit to 8-bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = ScaleShortToByte(srcFile.GetNextSample_16bit()); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 16-bit to 8-bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = ScaleShortToByte(srcFile.GetNextSample_16bit()); if (pVolumeMultiplier != 1.0) sample = (byte)((double)sample * pVolumeMultiplier); destFile.AddSample_8bit(sample); destFile.AddSample_8bit(sample); } } } else if ((srcFile.BitsPerSample == 16) && (pBitsPerSample == 16)) { short sample = 0; if (srcFile.IsStereo && !pStereo) { // 16-bit to 16-bit, stereo to mono: Average each 2 samples while (srcFile.NumSamplesRemaining > 0) { sample = (short)((int)((int)srcFile.GetNextSample_16bit() + (int)srcFile.GetNextSample_16bit()) / 2); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if ((srcFile.IsStereo && pStereo) || (!srcFile.IsStereo && !pStereo)) { // 16-bit to 16-bit, stereo to stereo or mono to mono while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_16bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); } } else if (!srcFile.IsStereo && pStereo) { // 16-bit to 16-bit, mono to stereo: Write each sample twice while (srcFile.NumSamplesRemaining > 0) { sample = srcFile.GetNextSample_16bit(); if (pVolumeMultiplier != 1.0) sample = (short)((double)sample * pVolumeMultiplier); destFile.AddSample_16bit(sample); destFile.AddSample_16bit(sample); } } } destFile.Close(); srcFile.Close(); }