/// <summary> /// Adjusts the volume level of a WAV file, saving the adjusted file as a separate file. /// </summary> /// <param name="pSrcFilename">The name of the WAV file to adjust</param> /// <param name="pDestFilename">The name to use for the volume-adjusted WAV file</param> /// <param name="pMultiplier">The value by which to multiply the audio samples</param> public static void AdjustVolume_Copy(String pSrcFilename, String pDestFilename, double pMultiplier) { // If an empty source or destination file were passed in, then throw an exception. if (pSrcFilename == "") throw new WAVFileReadException("Blank filename specified.", "WAVFile.AdjustVolume_Copy()"); if (pDestFilename == "") throw new WAVFileWriteException("Blank filename specified.", "WAVFile.AdjustVolume_Copy()"); // Open the srouce file WAVFile srcFile = new WAVFile(); String retval = srcFile.Open(pSrcFilename, WAVFileMode.READ); if (retval == "") { // Check to make sure the input file has a supported number of bits/sample and sample rate. If // not, then throw an exception. if (!SupportedBitsPerSample(srcFile.BitsPerSample)) { WAVFileBitsPerSampleException exc = new WAVFileBitsPerSampleException(pSrcFilename + " has unsupported bits/sample (" + srcFile.BitsPerSample.ToString() + ")", "WAVFile.AdjustVolume_Copy()", srcFile.BitsPerSample); srcFile.Close(); throw exc; } // Open the destination file and start copying the adjusted audio data to it. WAVFile destFile = new WAVFile(); destFile.Create(pDestFilename, srcFile.IsStereo, srcFile.SampleRateHz, srcFile.BitsPerSample); if (srcFile.BitsPerSample == 8) { byte sample = 0; for (int i = 0; i < srcFile.NumSamples; ++i) { // Note: Only multiply the sample if pMultiplier is not 1.0 (if the multiplier is // 1.0, then it would be good to avoid any binary roundoff error). sample = srcFile.GetNextSample_8bit(); if (pMultiplier != 1.0) sample = (byte)((double)sample * pMultiplier); destFile.AddSample_8bit(sample); } } else if (srcFile.BitsPerSample == 16) { short sample = 0; for (int i = 0; i < srcFile.NumSamples; ++i) { // Note: Only multiply the sample if pMultiplier is not 1.0 (if the multiplier is // 1.0, then it would be good to avoid any binary roundoff error). sample = srcFile.GetNextSample_16bit(); if (pMultiplier != 1.0) sample = (short)((double)sample * pMultiplier); destFile.AddSample_16bit(sample); } } srcFile.Close(); destFile.Close(); } else throw new WAVFileReadException(retval, "WAVFile.AdjustVolume_Copy()"); }
/// <summary> /// For 8-bit WAV files: Adjusts the volume level and converts it to a 16-bit audio file. /// The converted data is saved to a separate file. /// </summary> /// <param name="pSrcFilename">The name of the WAV file to convert</param> /// <param name="pDestFilename">The name to use for the converted WAV file</param> /// <param name="pMultiplier">The volume multiplier</param> public static void AdjustVolume_Copy_8BitTo16Bit(String pSrcFilename, String pDestFilename, double pMultiplier) { // If an empty source or destination file were passed in, then throw an exception. if (pSrcFilename == "") throw new WAVFileReadException("Blank filename specified.", "WAVFile.AdjustVolume_Copy_8BitTo16Bit()"); if (pDestFilename == "") throw new WAVFileWriteException("Blank filename specified.", "WAVFile.AdjustVolume_Copy_8BitTo16Bit()"); // Open the srouce file WAVFile srcFile = new WAVFile(); String retval = srcFile.Open(pSrcFilename, WAVFileMode.READ); if (retval == "") { // Check to make sure the input file has 8 bits per sample. If not, then throw an exception. if (srcFile.BitsPerSample != 8) { WAVFileBitsPerSampleException exc = new WAVFileBitsPerSampleException(pSrcFilename + ": 8 bits per sample required, and the file has " + srcFile.BitsPerSample.ToString() + " bits per sample.", "WAVFile.AdjustVolume_Copy_8BitTo16Bit()", srcFile.BitsPerSample); srcFile.Close(); throw exc; } // Open the destination file WAVFile destFile = new WAVFile(); destFile.Create(pDestFilename, srcFile.IsStereo, srcFile.SampleRateHz, 16, true); // Copy the data short sample_16bit = 0; while (srcFile.NumSamplesRemaining > 0) { // Scale the sample from 8-bit to 16 bits sample_16bit = ScaleByteToShort(srcFile.GetNextSample_8bit()); // Now, apply pMultiplier if it is not 1.0 if (pMultiplier != 1.0) sample_16bit = (short)((double)sample_16bit * pMultiplier); // Save the sample to the destination file destFile.AddSample_16bit(sample_16bit); } srcFile.Close(); destFile.Close(); } else throw new WAVFileReadException(retval, "WAVFile.AdjustVolume_Copy_8BitTo16Bit()"); }