public Memory <byte> DecodeFrame(Stream stream, int blockSize) { var frame = FlacFrame.FromStream(stream, new FlacMetadataStreamInfo { SampleRate = 44100, Channels = 2, BitsPerSample = 16 }); Memory <byte> buffer = null; frame.NextFrame(); frame.GetBuffer(ref buffer); return(buffer); }
private WriteStatus Write(IntPtr context, IntPtr frame, IntPtr buffer, IntPtr userData) { FlacFrame f = (FlacFrame)Marshal.PtrToStructure(frame, typeof(FlacFrame)); int samplesPerChannel = f.Header.BlockSize; inputBitDepth = f.Header.BitsPerSample; inputChannels = f.Header.Channels; inputSampleRate = f.Header.SampleRate; if (!writer.HasHeader) { writer.WriteHeader(inputSampleRate, inputBitDepth, inputChannels); } if (totalSamples < 0) { totalSamples = FLAC__stream_decoder_get_total_samples(context); } if (samples == null) { samples = new int[samplesPerChannel * inputChannels]; } if (samplesChannel == null) { samplesChannel = new float[inputChannels]; } for (int i = 0; i < inputChannels; i++) { IntPtr pChannelBits = Marshal.ReadIntPtr(buffer, i * IntPtr.Size); Marshal.Copy(pChannelBits, samples, i * samplesPerChannel, samplesPerChannel); } // For each channel, there are BlockSize number of samples, so let's process these. for (int i = 0; i < samplesPerChannel; i++) { for (int c = 0; c < inputChannels; c++) { int v = samples[i + c * samplesPerChannel]; switch (inputBitDepth / 8) { case sizeof(short): // 16-bit writer.WriteInt16(v); break; case sizeof(int) - sizeof(byte): // 24-bit writer.WriteInt24(v); break; default: throw new NotSupportedException("Input FLAC bit depth is not supported!"); } } processedSamples += 1; } // Show progress //if (totalSamples > 0) // ConsoleProgress.Update(processedSamples, totalSamples); return(WriteStatus.Continue); }