public FlacStream(Stream baseStream, bool keepOpen, int bitsPerSample) { this.baseStream = baseStream; this.keepOpen = keepOpen; this.bitsPerSample = bitsPerSample; reader = new FlacReader(baseStream, true); reader.RecordType = FlacBox.FlacRecordType.FrameFooter; writer = new FlacWriter(baseStream, true); }
/** * If a FlacWriter object currently exists, stops it and removes the * InProgress suffix from its file name. */ private void MaybeEndCurrentFlacWriter() { lock (flacLock) { if (flacWriter == null) { return; } flacWriter.EndStream(); flacStream.Close(); File.Move( flacFilePath, FileNaming.RemoveInProgressSuffix(flacFilePath)); flacFilePath = null; flacStream = null; flacWriter = null; } }
/** Creates a FlacWriter object if none currently exists. */ private void MaybeCreateFlacWriter() { if (flacWriter != null) { return; } flacFilePath = flacFilePath = FileNaming.AddInProgressSuffix( FileNaming.GetMicWavInFilePath(dataDir)); flacStream = File.Create(flacFilePath); flacWriter = new FlacWriter(flacStream); FlacStreaminfo streamInfo = new() { ChannelsCount = AUDIO_NUM_CHANNELS, BitsPerSample = AUDIO_BITS_PER_SAMPLE, SampleRate = AUDIO_SAMPLE_RATE_HZ, MaxBlockSize = AUDIO_SAMPLE_RATE_HZ, }; flacWriter.StartStream(streamInfo); }
public Stream ConvertToFLAC(string inputFile, ConvertOptions options) { // Convering to FLAC is a 2-way step. First we convert to WAV. var output = Convert(inputFile, ".wav", "-f wav -sample_fmt s16 " + options, null); if (output.Length == 0) { return(output); } FormatRepair.RepairWAVStream(output); // Offset the memory to temp folder. var temp = Path.GetTempFileName(); var fs = File.Open(temp, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); TemporaryFiles.Add(temp); // Next we encode using libFLAC's native encoder because FFmpeg unfortunately // does not support --no-padding and --no-seektable at the moment. *sigh* using (var reader = new WavReader(output)) using (var writer = new FlacWriter(fs, reader.BitDepth, reader.Channels, reader.SampleRate)) { // Buffer for 1 second's worth of audio data var buffer = new byte[1 * reader.Bitrate / 8]; int read; do { OnConversionProgressChanged(new ConversionProgressChangedEventArgs { SourceFormat = ".wav", TargetFormat = ".flac", Progress = (int)(reader.InputStream.Position * 100 / reader.InputStream.Length) }); read = reader.InputStream.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, read); } while (read > 0); } output.Close(); return(fs); }