Beispiel #1
0
        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!
                    }
                }
            }
        }
Beispiel #2
0
        private void 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);
            }
        }