Beispiel #1
0
        /// <summary>
        /// Returns the highest sample value in a WAV file, as a 16-bit value, regardless of
        /// whether the file contains 8-bit or 16-bit audio.  If the sample is coming from
        /// an 8-bit audio file, the sample will be scaled up from 8-bit to 16-bit.
        /// </summary>
        /// <param name="pFilename">The audio file name</param>
        /// <returns>The highest sample value from the file, as a 16-bit value</returns>
        public static short HighestSampleValueAs16Bit(String pFilename)
        {
            short highestSample = 0;

            try
            {
                WAVFile audioFile = new WAVFile();
                if (audioFile.Open(pFilename, WAVFileMode.READ) == "")
                {
                    if (audioFile.BitsPerSample == 8)
                    {
                        short sample = 0;
                        for (int i = 0; i < audioFile.NumSamples; ++i)
                        {
                            sample = ScaleByteToShort(audioFile.GetNextSample_8bit());
                            if (sample > highestSample)
                                highestSample = sample;
                        }
                    }
                    else if (audioFile.BitsPerSample == 16)
                    {
                        short sample = 0;
                        for (int i = 0; i < audioFile.NumSamples; ++i)
                        {
                            sample = audioFile.GetNextSample_16bit();
                            if (sample > highestSample)
                                highestSample = sample;
                        }
                    }

                    audioFile.Close();
                }
            }
            catch (Exception)
            {
            }

            return highestSample;
        }
Beispiel #2
0
        /// <summary>
        /// Returns the highest sample value in a WAV audio file.
        /// The return value is a byte array and will contain one
        /// byte if the file contains 8-bit audio or 2 bytes if the file
        /// contains  16-bit audio.  The return value will be null if
        /// the file cannot be opened.  If it is known that the audio
        /// file contains 16-bit samples, the byte array can be converted
        /// to a 16-bit integer using BitConverter.ToInt16().
        /// </summary>
        /// <param name="pFilename">The name of the WAV file</param>
        /// <param name="pBitsPerSample">This will contain the number of bits per sample, or 0 if the file wasn't loaded.</param>
        /// <returns>A byte array containing the highest audio sample, or null if the file wasn't loaded.</returns>
        public static byte[] HighestSampleValue(String pFilename, out short pBitsPerSample)
        {
            pBitsPerSample = 0;
            byte[] highestSampleValue = null;

            WAVFile audioFile = new WAVFile();
            try
            {
                if (audioFile.Open(pFilename, WAVFileMode.READ) == "")
                {
                    pBitsPerSample = audioFile.mBitsPerSample;

                    if (audioFile.mBitsPerSample == 8)
                    {
                        byte sample = 0;
                        byte highestSample = 0;
                        for (int i = 0; i < audioFile.NumSamples; ++i)
                        {
                            sample = audioFile.GetNextSample_8bit();
                            if (sample > highestSample)
                                highestSample = sample;
                        }

                        highestSampleValue = new byte[1];
                        highestSampleValue[0] = highestSample;
                    }
                    else if (audioFile.mBitsPerSample == 16)
                    {
                        short sample = 0;
                        short highestSample = 0;
                        for (int i = 0; i < audioFile.NumSamples; ++i)
                        {
                            sample = audioFile.GetNextSample_16bit();
                            if (sample > highestSample)
                                highestSample = sample;
                        }

                        highestSampleValue = BitConverter.GetBytes(highestSample);
                        if (!BitConverter.IsLittleEndian)
                            Array.Reverse(highestSampleValue);
                    }

                    audioFile.Close();
                }
            }
            catch (Exception)
            {
            }

            return (highestSampleValue);
        }
Beispiel #3
0
        /// <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();
        }
Beispiel #4
0
        /// <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()");
        }
Beispiel #5
0
        /// <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()");
        }
Beispiel #6
0
        /// <summary>
        /// Changes the volume of a WAV file.
        /// </summary>
        /// <param name="pFilename">The name of the WAV file to adjust</param>
        /// <param name="pMultiplier">The volume multiplier</param>
        public static void AdjustVolumeInPlace(String pFilename, double pMultiplier)
        {
            // If pMultiplier is 1, then we don't need to do anything.
            if (pMultiplier == 1.0)
                return;

            // Open the file
            WAVFile audioFile = new WAVFile();
            String retval = audioFile.Open(pFilename, WAVFileMode.READ_WRITE);
            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(audioFile.BitsPerSample))
                {
                    short bitsPerSample = audioFile.BitsPerSample;
                    audioFile.Close();
                    throw new WAVFileBitsPerSampleException(pFilename + " has unsupported bits/sample ("
                                                            + bitsPerSample.ToString() + ")",
                                                            "WAVFile.AdjustVolumeInPlace()", bitsPerSample);
                }
                if (!SupportedSampleRate(audioFile.SampleRateHz))
                {
                    int sampleRate = audioFile.SampleRateHz;
                    audioFile.Close();
                    throw new WAVFileSampleRateException(pFilename + " has unsupported sample rate ("
                                                         + sampleRate.ToString() + ")",
                                                         "WAVFile.AdjustVolumeInPlace()", sampleRate);
                }

                // Adjust the file volume
                if (audioFile.BitsPerSample == 8)
                {
                    byte sample = 0;
                    for (int sampleNum = 0; sampleNum < audioFile.NumSamples; ++sampleNum)
                    {
                        sample = (byte)((double)audioFile.GetNextSample_8bit() * pMultiplier);
                        audioFile.SeekToAudioSample(sampleNum);
                        audioFile.AddSample_8bit(sample);
                    }
                }
                else if (audioFile.BitsPerSample == 16)
                {
                    short sample = 0;
                    for (int sampleNum = 0; sampleNum < audioFile.NumSamples; ++sampleNum)
                    {
                        sample = (short)((double)audioFile.GetNextSample_16bit() * pMultiplier);
                        audioFile.SeekToAudioSample(sampleNum);
                        audioFile.AddSample_16bit(sample);
                    }
                }

                audioFile.Close();
            }
            else
                throw new WAVFileReadException(retval, "WAVFile.AdjustVolumeInPlace()");
        }