Write() public method

public Write ( AudioBuffer buff ) : void
buff AudioBuffer
return void
 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);
 }
Example #2
0
 private byte[] Wav2FlacBuffConverter(byte[] Buffer)
 {
     Stream OutWavStream = new MemoryStream();
     Stream OutFlacStream = new MemoryStream();
     AudioPCMConfig pcmconf = new AudioPCMConfig(16, 1, 16000);
     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, 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 #3
0
        public async Task TestWriting()
        {

            var wavStream =
                await
                (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///test.wav")))
                    .OpenStreamForReadAsync();

            var expectedBytes =
                ReadFully(await
               (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///flake.flac")))
                   .OpenStreamForReadAsync());


            var buff = WAVReader.ReadAllSamples(wavStream);
            FlakeWriter target;

            var outputStream = new MemoryStream();

            target = new FlakeWriter(outputStream, new FlakeWriterSettings { PCM = buff.PCM, EncoderMode = "7" });
            target.Settings.Padding = 1;
            target.DoSeekTable = false;
            target.Write(buff);
            target.Close();

            outputStream.Seek(0, SeekOrigin.Begin);

            var resultContent = outputStream.ToArray();
            var outStream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync("out.flac",
                                                                        CreationCollisionOption.ReplaceExisting);
            outStream.Write(resultContent, 0, resultContent.Length);
            outStream.Dispose();
            Debug.WriteLine(ApplicationData.Current.LocalFolder.Path);
            

            CollectionAssert.AreEqual(expectedBytes, outputStream.ToArray(), "result and expected doesn't match.");
        }
Example #4
0
        public void TestWriting()
        {
            AudioBuffer buff = WAVReader.ReadAllSamples("test.wav", null);
            FlakeWriter target;

            target = new FlakeWriter("flakewriter0.flac", null, new FlakeWriterSettings { PCM = buff.PCM, EncoderMode = "7" });
            target.Settings.Padding = 1;
            target.DoSeekTable = false;
            //target.Vendor = "CUETools";
            //target.CreationTime = DateTime.Parse("15 Aug 1976");
            target.FinalSampleCount = buff.Length;
            target.Write(buff);
            target.Close();
            //CollectionAssert.AreEqual(File.ReadAllBytes("flake.flac"), File.ReadAllBytes("flakewriter0.flac"), "flakewriter0.flac doesn't match.");

            target = new FlakeWriter("flakewriter1.flac", null, new FlakeWriterSettings { PCM = buff.PCM, EncoderMode = "7" });
            target.Settings.Padding = 1;
            target.DoSeekTable = false;
            //target.Vendor = "CUETools";
            //target.CreationTime = DateTime.Parse("15 Aug 1976");
            target.Write(buff);
            target.Close();
            CollectionAssert.AreEqual(File.ReadAllBytes("flake.flac"), File.ReadAllBytes("flakewriter1.flac"), "flakewriter1.flac doesn't match.");
        }
 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();
   }
 }
        /// <summary>
        /// Convert stream of wav to flac format and send it to google speech recognition service.
        /// </summary>
        /// <param name="stream">wav stream</param>
        /// <returns>recognized result</returns>
        /// // Step 1- Converts wav stream to flac 
        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;
        }
        /// <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 #8
0
        private static byte[] WaveSamplesToFlake(byte[] combinedChunks)
        {
            var audioBuffer = new AudioBuffer(new AudioPCMConfig(16, 1, 22050), combinedChunks, combinedChunks.Length/2);

              byte[] flakeBuffer;
              using (var flakeStream = new MemoryStream())
              {
            var flakeWriter = new FlakeWriter(null, flakeStream,
              new FlakeWriterSettings {PCM = audioBuffer.PCM, EncoderMode = "7"});
            flakeWriter.Settings.Padding = 1;
            flakeWriter.DoSeekTable = false;
            flakeWriter.FinalSampleCount = audioBuffer.Length;
            flakeWriter.Write(audioBuffer);

            flakeStream.Position = 0;
            using (var br = new BinaryReader(flakeStream))
            {
              flakeBuffer = new byte[flakeStream.Length];
              br.Read(flakeBuffer, 0, flakeBuffer.Length);
            }
              }
              return flakeBuffer;
        }