public FlacReader(string input, WavWriter output) { if (output == null) { throw new ArgumentNullException("WavWriter"); } stream = File.OpenRead(input); reader = new BinaryReader(stream); writer = output; context = FLAC__stream_decoder_new(); if (context == IntPtr.Zero) { throw new ApplicationException("FLAC: Could not initialize stream decoder!"); } write = new WriteCallback(Write); metadata = new MetadataCallback(Metadata); error = new ErrorCallback(Error); if (FLAC__stream_decoder_init_file(context, input, write, metadata, error, IntPtr.Zero) != 0) { throw new ApplicationException("FLAC: Could not open stream for reading!"); } }
static void Run(string input, string text) { ConsoleProgress.Reset(); bool isInputFlac = Path.GetExtension(input).ToUpperInvariant() == ".FLAC"; if (isInputFlac) { // FLAC -> WAV string inputFile = Path.Combine("flac", input); string outputFile = Path.Combine("wav", Path.ChangeExtension(input, ".wav")); if (!File.Exists(inputFile)) { throw new ApplicationException("Input file " + inputFile + " cannot be found!"); } using (WavWriter wav = new WavWriter(outputFile)) using (FlacReader flac = new FlacReader(inputFile, wav)) flac.Process(); } else { // WAV -> FLAC string inputFile = Path.Combine("wav", input); string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac")); if (!File.Exists(inputFile)) { throw new ApplicationException("Input file " + inputFile + " cannot be found!"); } using (WavReader wav = new WavReader(inputFile)) { using (FlacWriter flac = new FlacWriter(File.Create(outputFile), wav.BitDepth, wav.Channels, wav.SampleRate)) { // Buffer for 1 second's worth of audio data byte[] buffer = new byte[1 * wav.Bitrate / 8]; int bytesRead; do { ConsoleProgress.Update(wav.InputStream.Position, wav.InputStream.Length); bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length); flac.Write(buffer, 0, bytesRead); } while (bytesRead > 0); // Finished! } } } }
static void Run(string input, string text) { ConsoleProgress.Reset(); bool isInputFlac = Path.GetExtension(input).ToUpperInvariant() == ".FLAC"; if (isInputFlac) { // FLAC -> WAV string inputFile = Path.Combine("flac", input); string outputFile = Path.Combine("wav", Path.ChangeExtension(input, ".wav")); if (!File.Exists(inputFile)) throw new ApplicationException("Input file " + inputFile + " cannot be found!"); using (WavWriter wav = new WavWriter(outputFile)) using (FlacReader flac = new FlacReader(inputFile, wav)) flac.Process(); } else { // WAV -> FLAC string inputFile = Path.Combine("wav", input); string outputFile = Path.Combine("flac", Path.ChangeExtension(input, ".flac")); if (!File.Exists(inputFile)) throw new ApplicationException("Input file " + inputFile + " cannot be found!"); using (WavReader wav = new WavReader(inputFile)) { using (FlacWriter flac = new FlacWriter(File.Create(outputFile), wav.BitDepth, wav.Channels, wav.SampleRate)) { // Buffer for 1 second's worth of audio data byte[] buffer = new byte[1 * wav.Bitrate / 8]; int bytesRead; do { ConsoleProgress.Update(wav.InputStream.Position, wav.InputStream.Length); bytesRead = wav.InputStream.Read(buffer, 0, buffer.Length); flac.Write(buffer, 0, bytesRead); } while (bytesRead > 0); // Finished! } } } }
public FlacReader(string input, WavWriter output) { if (output == null) throw new ArgumentNullException("WavWriter"); stream = File.OpenRead(input); reader = new BinaryReader(stream); writer = output; context = FLAC__stream_decoder_new(); if (context == IntPtr.Zero) throw new ApplicationException("FLAC: Could not initialize stream decoder!"); write = new WriteCallback(Write); metadata = new MetadataCallback(Metadata); error = new ErrorCallback(Error); if (FLAC__stream_decoder_init_file(context, input, write, metadata, error, IntPtr.Zero) != 0) throw new ApplicationException("FLAC: Could not open stream for reading!"); }