Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Debug test
            //args = (@"E:\Mijn documenten\Visual Studio 2010\ADPCMEncoder\~24000HZTEST.wav|test").Split(Convert.ToChar("|"));
            //args = (@"E:\Mijn documenten\Visual Studio 2010\ADPCMEncoder\TestFiles\LoadingSNOW.wav|test").Split(Convert.ToChar("|"));

            //Print header
            Console.WriteLine("=================");
            Console.WriteLine("IMA ADPCM Encoder");
            Console.WriteLine(" by Flitskikker");
            Console.WriteLine("=================");
            Console.WriteLine("");

            //Check args
            if(args.Length == 0){
                //No args
                Console.WriteLine("ERROR: No command line arguments passed. Press any key to exit...");
                Console.ReadLine();
                Environment.Exit(1);
            }

            //Check file
            if(!File.Exists(args[0])){
                //File not found
                Console.WriteLine("ERROR: File \"" + args[0] + "\" not found. Press any key to exit...");
                Console.ReadLine();
                Environment.Exit(2);
            }

            //Print file
            Console.WriteLine("File: " + args[0]);
            Console.WriteLine("");

            //Load file
            WAVFile wav = new WAVFile();
            wav.Open(args[0], WAVFile.WAVFileMode.READ);

            //Show WAV info
            Console.WriteLine("WAV Info:");
            Console.WriteLine("\tBits per sample:\t" + wav.BitsPerSample.ToString() + " bits");
            Console.WriteLine("\tBytes per sample:\t" + wav.BytesPerSample.ToString());
            Console.WriteLine("\tBytes per second:\t" + wav.BytesPerSec.ToString());
            Console.WriteLine("\tData size:\t\t" + wav.DataSizeBytes.ToString() + " bytes");
            Console.WriteLine("\tDuration:\t\t" + Math.Round(Convert.ToDecimal((double)wav.NumSamples / (double)wav.SampleRateHz), 2).ToString() + " seconds (" + Utils.getDurationString(wav.NumSamples, wav.SampleRateHz) + ")");
            Console.WriteLine("\tEncoding type:\t\t" + wav.EncodingType.ToString());
            Console.WriteLine("\tFile size:\t\t" + wav.FileSizeBytes.ToString() + " bytes");
            Console.WriteLine("\t# of channels:\t\t" + wav.NumChannels.ToString());
            Console.WriteLine("\t# of samples:\t\t" + wav.NumSamples.ToString());
            Console.WriteLine("\tRIFF type:\t\t" + wav.RIFFTypeString);
            Console.WriteLine("\tSample Rate:\t\t" + wav.SampleRateHz.ToString() + " Hz");
            Console.WriteLine("\tWAV header:\t\t" + wav.WAVHeaderString);
            Console.WriteLine("");

            //Check bits
            if(wav.BitsPerSample != 16){
                //Not 16 bits
                Console.WriteLine("ERROR: WAV should have 16 bits per sample. Press any key to exit...");
                Console.ReadLine();
                Environment.Exit(1);
            }

            //Check bytes per sample
            if(wav.BytesPerSample / wav.NumChannels != 2){
                //Not 2 bytes
                Console.WriteLine("ERROR: WAV should have (wav.NumChannels * 2) bytes per sample. Press any key to exit...");
                Console.ReadLine();
                Environment.Exit(1);
            }

            //Print message
            Console.WriteLine("Ready to go! Press any key when ready...");

            //Wait for input
            Console.ReadLine();

            if(wav.NumChannels > 1){
                //Split channels
                Console.WriteLine("");
                Console.WriteLine("Saving separate channels...");
                Console.WriteLine("");

                List<WAVFile> wavs = new List<WAVFile>();

                for (int c = 0; c < wav.NumChannels; c++) {
                    wavs.Add(new WAVFile());
                    wavs[c].Create(Path.GetDirectoryName(args[0]) + "\\" + Path.GetFileNameWithoutExtension(args[0]) + "_" + (c + 1).ToString() + ".wav", false, wav.SampleRateHz, wav.BitsPerSample, true);
                }

                int numSamplesCorrected = ((wav.DataSizeBytes - 8) / wav.BytesPerSample);

                //for (long i = 0; i < wav.NumSamples / wav.NumChannels; i++) {
                for (long i = 0; i < numSamplesCorrected; i++) {
                    for (int c = 0; c < wav.NumChannels; c++) {
                        wavs[c].AddSample_16bit(wav.GetNextSampleAs16Bit());
                    }
                }

                for (int c = 0; c < wav.NumChannels; c++) {
                    wavs[c].Close();
                }
            }

            //Encode
            Console.WriteLine("");
            Console.WriteLine("Encoding file(s)...");
            Console.WriteLine("");

            IMAADPCM.ADPCMState state = new IMAADPCM.ADPCMState();
            WAVFile cwav = new WAVFile();
            int nc = wav.NumChannels;

            for (int c = 0; c < wav.NumChannels; c++) {
                Console.WriteLine("Encoding file: " + Path.GetDirectoryName(args[0]) + "\\" + Path.GetFileNameWithoutExtension(args[0]) + "_" + (c + 1).ToString() + ".bin");

                MemoryStream ms = new MemoryStream();
                cwav = new WAVFile();

                // Open splitted WAV
                if(wav.NumChannels > 1){
                    cwav.Open(Path.GetDirectoryName(args[0]) + "\\" + Path.GetFileNameWithoutExtension(args[0]) + "_" + (c + 1).ToString() + ".wav", WAVFile.WAVFileMode.READ_WRITE);
                }else{
                    cwav = wav;
                    //wav.Close();
                    //cwav.Open(args[0], WAVFile.WAVFileMode.READ_WRITE);
                }

                byte[] bytes = new byte[2];
                int loopValue = ((cwav.DataSizeBytes - 8) / cwav.BytesPerSample);

                //Actual encode
                for (long i = 0; i < loopValue / 2; i++) {
                    bytes[0] = IMAADPCM.encodeADPCM(cwav.GetNextSampleAs16Bit(), ref state);
                    bytes[1] = IMAADPCM.encodeADPCM(cwav.GetNextSampleAs16Bit(), ref state);
                    ms.Write(BitConverter.GetBytes(Convert.ToInt32(Utils.binaryString(bytes[1], 4) + Utils.binaryString(bytes[0], 4), 2)), 0, 1);
                }

                //Get WAV data
                byte[] dataWAV = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(dataWAV, 0, (int)ms.Length);
                ms.Close();

                //Create file
                FileStream fs = new FileStream(Path.GetDirectoryName(args[0]) + "\\" + Path.GetFileNameWithoutExtension(args[0]) + "_" + (c + 1).ToString() + ".bin", FileMode.Create, FileAccess.ReadWrite);

                //Write sample
                fs.Seek(0, SeekOrigin.Begin);
                fs.Write(dataWAV, 0, dataWAV.Length);

                //Close
                cwav.Close();
                ms.Close();
                fs.Close();

                if (nc == 1) break;
            }

            //Close WAV file
            wav.Close();

            //Print message
            Console.WriteLine("");
            Console.WriteLine("Done! Press any key to exit...");

            //Wait for input
            Console.ReadLine();

            //Exit
            Environment.Exit(0);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the highest number of bits per sample in a set of audio files.
        /// </summary>
        /// <param name="pFilenames">An array containing the audio file names</param>
        /// <returns>The highest number of bits per sample in the set of audio files</returns>
        public static short HighestBitsPerSample(String[] pFilenames)
        {
            short bitsPerSample = 0;

            if (pFilenames != null)
            {
                WAVFile audioFile = new WAVFile();
                String retval = "";
                foreach (String filename in pFilenames)
                {
                    try
                    {
                        retval = audioFile.Open(filename, WAVFileMode.READ);
                        if (retval == "")
                        {
                            if (audioFile.BitsPerSample > bitsPerSample)
                                bitsPerSample = audioFile.BitsPerSample;
                            audioFile.Close();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return bitsPerSample;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns the highest number of channels in a set of audio files.
        /// </summary>
        /// <param name="pFilenames">An array containing the audio file names</param>
        /// <returns>The highest number of channels in the set of audio files</returns>
        public static byte HighestNumChannels(String[] pFilenames)
        {
            byte numChannels = 0;

            if (pFilenames != null)
            {
                WAVFile audioFile = new WAVFile();
                String retval = "";
                foreach (String filename in pFilenames)
                {
                    try
                    {
                        retval = audioFile.Open(filename, WAVFileMode.READ);
                        if (retval == "")
                        {
                            if (audioFile.NumChannels > numChannels)
                                numChannels = audioFile.NumChannels;
                            audioFile.Close();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return numChannels;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns a WAVFormat struct containing audio format information
        /// (# channels, sample rate, and bits per sample) for a WAV file.
        /// </summary>
        /// <param name="pFilename">The name of the file about which to retrieve format information</param>
        /// <returns>A WAVFormat struct object containing the audio format information for the open file</returns>
        public static WAVFormat GetAudioFormat(String pFilename)
        {
            WAVFormat format = new WAVFormat();

            WAVFile audioFile = new WAVFile();
            if (audioFile.Open(pFilename, WAVFileMode.READ) == "")
            {
                format.BitsPerSample = audioFile.mBitsPerSample;
                format.NumChannels = audioFile.mNumChannels;
                format.SampleRateHz = audioFile.mSampleRateHz;

                audioFile.Close();
            }

            return (format);
        }
Ejemplo n.º 7
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();
        }
Ejemplo n.º 8
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()");
        }
Ejemplo n.º 9
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()");
        }
Ejemplo n.º 10
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()");
        }