Example #1
0
        public void ReadTest()
        {
            Assert.AreEqual(-1L, pipe.Length, "CUETools.Codecs.WAVReader.Length did not return the expected value.");
            AudioBuffer buff   = new AudioBuffer(pipe, 54);
            int         actual = pipe.Read(buff, -1);

            Assert.AreEqual(53, actual, "CUETools.Codecs.WAVReader.Read did not return the expected value.");
            Assert.AreEqual(actual, buff.Length, "CUETools.Codecs.WAVReader.Read did not return the expected value.");
            CollectionAssert.AreEqual(pipeSamples, buff.Samples, "AudioBuffer.Samples was not set correctly.");
            actual = pipe.Read(buff, -1);
            Assert.AreEqual(0, actual, "CUETools.Codecs.WAVReader.Read did not return the expected value.");
        }
Example #2
0
        private static Hashtable Initialize(string fileName)
        {
            string _wav  = _dir + "\\files\\" + fileName + ".wav";
            string _flac = _dir + "\\files\\temporary.flac";

            int          sampleRate  = 0;
            IAudioSource audioSource = new WAVReader(_wav, null);
            AudioBuffer  buff        = new AudioBuffer(audioSource, 0x10000);

            FlakeWriter flakewriter = new FlakeWriter(_flac, audioSource.PCM);

            sampleRate = audioSource.PCM.SampleRate;

            while (audioSource.Read(buff, -1) != 0)
            {
                flakewriter.Write(buff);
            }
            flakewriter.Close();
            int _sample = sampleRate;

            Hashtable Response = new Hashtable();

            Response.Add("wav_name", _wav);
            Response.Add("flac_name", _flac);
            Response.Add("sample_rate", _sample);

            return(Response);
        }
Example #3
0
        private byte[] Wav2FlacBuffConverter(byte[] Buffer)
        {
            Stream         OutWavStream  = new MemoryStream();
            Stream         OutFlacStream = new MemoryStream();
            AudioPCMConfig pcmconf       = new AudioPCMConfig(SAMPLE_ENCODING, CHANELS, RATE);
            WAVWriter      wr            = new WAVWriter(null, OutWavStream, pcmconf);

            wr.Write(new AudioBuffer(pcmconf, Buffer, Buffer.Length / 2));
            OutWavStream.Seek(0, SeekOrigin.Begin);
            WAVReader audioSource = new WAVReader(null, OutWavStream);

            if (audioSource.PCM.SampleRate != RATE)
            {
                return(null);
            }
            AudioBuffer buff        = new AudioBuffer(audioSource, 0x10000);
            FlakeWriter flakeWriter = new FlakeWriter(null, OutFlacStream, audioSource.PCM);

            flakeWriter.CompressionLevel = 8;
            while (audioSource.Read(buff, -1) != 0)
            {
                flakeWriter.Write(buff);
            }
            OutFlacStream.Seek(0, SeekOrigin.Begin);
            byte[] barr = new byte[OutFlacStream.Length];
            OutFlacStream.Read(barr, 0, (int)OutFlacStream.Length);
            return(barr);
        }
Example #4
0
File: Form1.cs Project: olekbb/KCK
        private void ConvertToFlac(Stream sourceStream)
        {
            var audioSource = new WAVReader(null, sourceStream);

            try
            {
                if (audioSource.PCM.SampleRate != 44100)
                {
                    throw new InvalidOperationException("Incorrect frequency - WAV file must be at 44.1 KHz.");
                }
                var buff        = new AudioBuffer(audioSource, 0x10000);
                var flakeWriter = new FlakeWriter(@"lol.flac", audioSource.PCM);
                flakeWriter.CompressionLevel = 8;
                while (audioSource.Read(buff, -1) != 0)
                {
                    flakeWriter.Write(buff);
                }
                flakeWriter.Close();
            }
            finally
            {
                audioSource.Close();
                //richTextBox1.Text += "Koniec Konwersji\n";
            }
        }
Example #5
0
        private void convert()
        {
            using (var reader = new WaveFileReader(path + rawFile))
            {
                var newFormat = new NAudio.Wave.WaveFormat(16000, 16, 1);
                using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
                {
                    WaveFileWriter.CreateWaveFile(path + wavFile, conversionStream);
                }
            }

            if (!File.Exists(path + wavFile))
            {
                Console.WriteLine("wav file no!");
            }
            else
            {
                using (FileStream sourceStream = new FileStream(path + wavFile, FileMode.Open))
                {
                    WAVReader audioSource = new WAVReader(path + wavFile, sourceStream);

                    AudioBuffer buff        = new AudioBuffer(audioSource, 0x10000);
                    FlakeWriter flakeWriter = new FlakeWriter(path + flacFile, audioSource.PCM);

                    flakeWriter.CompressionLevel = 8;
                    while (audioSource.Read(buff, -1) != 0)
                    {
                        flakeWriter.Write(buff);
                    }

                    flakeWriter.Close();
                    audioSource.Close();
                }
            }
        }
        private static Tuple <int, string> WavToFlacHelper(WAVReader audioSource, string targetFlacPath)
        {
            int                 sampleRate;
            AudioBuffer         buffer   = new AudioBuffer(audioSource, 0x10000);
            FlakeWriterSettings settings = new FlakeWriterSettings();

            settings.PCM = audioSource.PCM;
            FlakeWriter audioDestination = new FlakeWriter(targetFlacPath, settings);

            while (audioSource.Read(buffer, -1) != 0)
            {
                audioDestination.Write(buffer);
            }
            sampleRate = settings.PCM.SampleRate;
            audioDestination.Close();
            audioSource.Close();
            return(new Tuple <int, string>(sampleRate, targetFlacPath));
        }
        /// <summary> Конвертирование wav-файла во flac </summary>
        /// <returns>Частота дискретизации</returns>
        public static int Wav2Flac(Stream wavStream, Stream flacStream)
        {
            int sampleRate = 0;

            IAudioSource audioSource = new WAVReader(null, wavStream);
            AudioBuffer  buff        = new AudioBuffer(audioSource, 0x10000);

            FlakeWriter flakewriter = new FlakeWriter(null, flacStream, audioSource.PCM);

            sampleRate = audioSource.PCM.SampleRate;

            FlakeWriter audioDest = flakewriter;

            while (audioSource.Read(buff, -1) != 0)
            {
                audioDest.Write(buff);
            }
            return(sampleRate);
        }
        /// <summary>Конвертирование wav-файла во flac</summary>
        /// <returns>Частота дискретизации</returns>
        private static void _Wav2Flac(String wavName, string flacName)
        {
            Debug.Assert(wavName != null);
            Debug.Assert(!string.IsNullOrEmpty(flacName));

            IAudioSource audioSource = new WAVReader(wavName, null);
            AudioBuffer  buff        = new AudioBuffer(audioSource, 0x10000);

            FlakeWriter flakewriter = new FlakeWriter(flacName, audioSource.PCM);

            FlakeWriter audioDest = flakewriter;

            while (audioSource.Read(buff, -1) != 0)
            {
                audioDest.Write(buff);
            }

            audioDest.Close();
            audioSource.Close();
        }
        public static string WavStreamToGoogle(Stream stream)
        {
            FlakeWriter  audioDest   = null;
            IAudioSource audioSource = null;
            string       answer;

            try
            {
                var outStream = new MemoryStream();

                stream.Position = 0;

                audioSource = new WAVReader("", stream);

                var buff = new AudioBuffer(audioSource, 0x10000);

                audioDest = new FlakeWriter("", outStream, audioSource.PCM);

                var sampleRate = audioSource.PCM.SampleRate;

                while (audioSource.Read(buff, -1) != 0)
                {
                    audioDest.Write(buff);
                }

                answer = GoogleRequest(outStream, sampleRate);
            }
            finally
            {
                if (audioDest != null)
                {
                    audioDest.Close();
                }
                if (audioSource != null)
                {
                    audioSource.Close();
                }
            }
            return(answer);
        }
Example #10
0
        private void ConvertToFlac(Stream sourceStream, Stream destinationStream)
        {
            var audioSource = new WAVReader(null, sourceStream);

            try {
                if (audioSource.PCM.SampleRate != 16000)
                {
                    throw new InvalidOperationException("Incorrect frequency - WAV file must be at 16 KHz.");
                }
                var buff        = new AudioBuffer(audioSource, 0x10000);
                var flakeWriter = new FlakeWriter(null, destinationStream, audioSource.PCM);
                //flakeWriter.CompressionLevel = 8;
                while (audioSource.Read(buff, -1) != 0)
                {
                    flakeWriter.Write(buff);
                }
                flakeWriter.Close();
            }
            finally {
                audioSource.Close();
            }
        }
Example #11
0
        /// <summary> Конвертирование wav-файла во flac </summary>
        /// <returns>Частота дискретизации</returns>
        public static int Wav2Flac(String wavName, string flacName)
        {
            int sampleRate = 0;

            IAudioSource audioSource = new WAVReader(wavName, null);
            AudioBuffer  buff        = new AudioBuffer(audioSource, 0x10000);

            FlakeWriter flakewriter = new FlakeWriter(flacName, audioSource.PCM);

            sampleRate = audioSource.PCM.SampleRate;

            FlakeWriter audioDest = flakewriter;

            while (audioSource.Read(buff, -1) != 0)
            {
                audioDest.Write(buff);
            }
            audioDest.Close();

            audioDest.Close();

            return(sampleRate);
        }
Example #12
0
        /// <summary>
        /// Converts .wav to .flac and returns sample rate via CUETools.
        /// </summary>
        /// <param name="wavFilePath">Full path to the .wav file.</param>
        /// <param name="flacFilePath">Full path to the .flac file.</param>
        /// <returns>Returns sample rate of the .flac file.</returns>
        public static int ConvertWavToFlac(String wavFilePath, String flacFilePath)
        {
            int sampleRate = 0;

            IAudioSource audioSource = new WAVReader(wavFilePath, null);
            AudioBuffer  buff        = new AudioBuffer(audioSource, 0x10000);

            FlakeWriter flakewriter = new FlakeWriter(flacFilePath, audioSource.PCM);

            sampleRate = audioSource.PCM.SampleRate;

            FlakeWriter audioDest = flakewriter;

            while (audioSource.Read(buff, -1) != 0)
            {
                audioDest.Write(buff);
            }
            audioDest.Close(); //TODO: Check - maybe it should be flakewriter.Close();

            audioDest.Close();

            return(sampleRate);
        }
Example #13
0
        private byte[] Wav2FlacBuffConverter(byte[] Buffer)
        {
            AudioPCMConfig pcmconf = new AudioPCMConfig(16, 1, 16000);

            if (flakeWriter == null)
            {
                OutFlacStream       = new MemoryStream();
                flakeWriter         = new FlakeWriter(null, OutFlacStream, pcmconf);
                flakeWriter.Padding = 0;
            }

            Stream OutWavStream = new MemoryStream();

            WAVWriter wr = new WAVWriter(null, OutWavStream, pcmconf);

            wr.Write(new AudioBuffer(pcmconf, Buffer, Buffer.Length / 2));
            OutWavStream.Seek(0, SeekOrigin.Begin);

            WAVReader audioSource = new WAVReader(null, OutWavStream);

            if (audioSource.PCM.SampleRate != 16000)
            {
                return(null);
            }
            AudioBuffer buff = new AudioBuffer(audioSource, Buffer.Length);

            flakeWriter.CompressionLevel = 8;
            while (audioSource.Read(buff, -1) != 0)
            {
                flakeWriter.Write(buff);
            }
            OutFlacStream.Seek(0, SeekOrigin.Begin);
            byte[] barr = new byte[OutFlacStream.Length];
            OutFlacStream.Read(barr, 0, (int)OutFlacStream.Length);
            return(barr);
        }
Example #14
0
        /// <summary>
        /// Convert .wav file to .flac file with the same name
        /// </summary>
        /// <param name="WavName">path to .wav file</param>
        /// <returns>Sample Rate of converted .flac</returns>
        public static int Wav2Flac(string WavName)
        {
            int sampleRate;
            var flacName = Path.ChangeExtension(WavName, "flac");

            FlakeWriter  audioDest   = null;
            IAudioSource audioSource = null;

            try
            {
                audioSource = new WAVReader(WavName, null);

                AudioBuffer buff = new AudioBuffer(audioSource, 0x10000);

                audioDest = new FlakeWriter(flacName, audioSource.PCM);

                sampleRate = audioSource.PCM.SampleRate;

                while (audioSource.Read(buff, -1) != 0)
                {
                    audioDest.Write(buff);
                }
            }
            finally
            {
                if (audioDest != null)
                {
                    audioDest.Close();
                }
                if (audioSource != null)
                {
                    audioSource.Close();
                }
            }
            return(sampleRate);
        }
Example #15
0
        static void Main(string[] args)
        {
            Console.SetOut(Console.Error);
            Console.WriteLine("LossyWAV {0}, Copyright (C) 2007,2008 Nick Currie, Copyleft.", LossyWAVWriter.version_string);
            Console.WriteLine("C# port Copyright (C) 2008 Grigory Chudov.");
            Console.WriteLine("This is free software under the GNU GPLv3+ license; There is NO WARRANTY, to");
            Console.WriteLine("the extent permitted by law. <http://www.gnu.org/licenses/> for details.");
            if (args.Length < 1 || (args[0].StartsWith("-") && args[0] != "-"))
            {
                Usage();
                return;
            }
            string sourceFile       = args[0];
            string stdinName        = null;
            double quality          = 5.0;
            bool   createCorrection = false;
            bool   toStdout         = false;
            int    outputBPS        = 0;

            for (int arg = 1; arg < args.Length; arg++)
            {
                bool ok = true;
                if (args[arg] == "-I" || args[arg] == "--insane")
                {
                    quality = 10;
                }
                else if (args[arg] == "-E" || args[arg] == "--extreme")
                {
                    quality = 7.5;
                }
                else if (args[arg] == "-S" || args[arg] == "--standard")
                {
                    quality = 5.0;
                }
                else if (args[arg] == "-P" || args[arg] == "--portable")
                {
                    quality = 2.5;
                }
                else if (args[arg] == "-C" || args[arg] == "--correction")
                {
                    createCorrection = true;
                }
                else if (args[arg] == "--16")
                {
                    outputBPS = 16;
                }
                else if ((args[arg] == "-N" || args[arg] == "--stdinname") && ++arg < args.Length)
                {
                    stdinName = args[arg];
                }
                else if ((args[arg] == "-q" || args[arg] == "--quality") && ++arg < args.Length)
                {
                    ok = double.TryParse(args[arg], out quality);
                }
                else if (args[arg] == "--stdout")
                {
                    toStdout = true;
                }
                else
                {
                    ok = false;
                }
                if (!ok)
                {
                    Usage();
                    return;
                }
            }
            DateTime start     = DateTime.Now;
            TimeSpan lastPrint = TimeSpan.FromMilliseconds(0);

#if !DEBUG
            try
#endif
            {
                WAVReader audioSource = new WAVReader(sourceFile, (sourceFile == "-" ? Console.OpenStandardInput() : null));
                if (sourceFile == "-" && stdinName != null)
                {
                    sourceFile = stdinName;
                }
                AudioPCMConfig pcm       = outputBPS == 0 ? audioSource.PCM : new AudioPCMConfig(outputBPS, audioSource.PCM.ChannelCount, audioSource.PCM.SampleRate, audioSource.PCM.ChannelMask);
                WAVWriter      audioDest = new WAVWriter(Path.ChangeExtension(sourceFile, ".lossy.wav"), toStdout ? Console.OpenStandardOutput() : null, new WAVWriterSettings(pcm));
                WAVWriter      lwcdfDest = createCorrection ? new WAVWriter(Path.ChangeExtension(sourceFile, ".lwcdf.wav"), null, new WAVWriterSettings(audioSource.PCM)) : null;
                LossyWAVWriter lossyWAV  = new LossyWAVWriter(audioDest, lwcdfDest, quality, new AudioEncoderSettings(audioSource.PCM));
                AudioBuffer    buff      = new AudioBuffer(audioSource, 0x10000);

                Console.WriteLine("Filename  : {0}", sourceFile);
                Console.WriteLine("File Info : {0}kHz; {1} channel; {2} bit; {3}", audioSource.PCM.SampleRate, audioSource.PCM.ChannelCount, audioSource.PCM.BitsPerSample, TimeSpan.FromSeconds(audioSource.Length * 1.0 / audioSource.PCM.SampleRate));
                lossyWAV.FinalSampleCount = audioSource.Length;

                while (audioSource.Read(buff, -1) != 0)
                {
                    lossyWAV.Write(buff);
                    TimeSpan elapsed = DateTime.Now - start;
                    if ((elapsed - lastPrint).TotalMilliseconds > 60)
                    {
                        Console.Error.Write("\rProgress  : {0:00}%; {1:0.0000} bits; {2:0.00}x; {3}/{4}",
                                            100.0 * audioSource.Position / audioSource.Length,
                                            1.0 * lossyWAV.OverallBitsRemoved / audioSource.PCM.ChannelCount / lossyWAV.BlocksProcessed,
                                            lossyWAV.SamplesProcessed / elapsed.TotalSeconds / audioSource.PCM.SampleRate,
                                            elapsed,
                                            TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds / lossyWAV.SamplesProcessed * audioSource.Length)
                                            );
                        lastPrint = elapsed;
                    }
                }

                TimeSpan totalElapsed = DateTime.Now - start;
                Console.Error.Write("\r                                                                         \r");
                Console.WriteLine("Results   : {0:0.0000} bits; {1:0.00}x; {2}",
                                  (1.0 * lossyWAV.OverallBitsRemoved) / audioSource.PCM.ChannelCount / lossyWAV.BlocksProcessed,
                                  lossyWAV.SamplesProcessed / totalElapsed.TotalSeconds / audioSource.PCM.SampleRate,
                                  totalElapsed
                                  );
                audioSource.Close();
                lossyWAV.Close();
            }
#if !DEBUG
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine("Error: {0}", ex.Message);
                //Console.WriteLine("{0}", ex.StackTrace);
            }
#endif
        }