Ejemplo n.º 1
0
        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);
        }