#398 Compression Problem

1.3.0
closed-invalid
Erik
None
5
2014-03-21
2013-05-14
Levi Butler
No

Hello,
Im encoding a wav to flac using FLAC__stream_encoder_init_stream
Please see below for the two file comparisons using spectro
(Im assuming the bitrate is higher due to the lower compression ratio.)
I have used the same compression level (6) for both files.

I may be possible that my code is missing something? - although ive read through all the documentation and it appears correct. Other than this the output file is perfect.

Thanks
Levi

1 Attachments

Discussion

  • Levi Butler
    Levi Butler
    2013-05-14

    Attachment 2

     
    Attachments
  • Levi Butler
    Levi Butler
    2013-05-14

    I am using the latest version of libflac.dll

    here are the methods i am using to replicate the problem

    public FlacWriter(Stream output, int bitDepth, int channels, int sampleRate)
    {
    stream = output;
    writer = new BinaryWriter(stream);

            inputBitDepth = bitDepth;
            inputChannels = channels;
            inputSampleRate = sampleRate;
    
            context = FLAC__stream_encoder_new();
    
            if (context == IntPtr.Zero)
                throw new ApplicationException("FLAC: Could not initialize stream encoder!");
    
            Check(FLAC__stream_encoder_set_channels(context, channels), "set channels");
            Check(FLAC__stream_encoder_set_bits_per_sample(context, inputChannels), "set bits per sample");
            Check(FLAC__stream_encoder_set_sample_rate(context, sampleRate), "set sample rate");
            Check(FLAC__stream_encoder_set_compression_level(context, 6), "set compression level");
    
            write = new WriteCallback(Write);
            seek = new SeekCallback(Seek);
            tell = new TellCallback(Tell);
    
            if (FLAC__stream_encoder_init_stream(context, write, seek, tell, null, IntPtr.Zero) != 0)
                throw new ApplicationException("FLAC: Could not open stream for writing!");
    
            //if (FLAC__stream_encoder_init_file(context, @"wav\miles_fisher-this_must_be_the_place-iphone2.flac", IntPtr.Zero, IntPtr.Zero) != 0)
            //    throw new ApplicationException("FLAC: Could not open stream for writing!");
        }
    
        public void Dispose()
        {
            if (context != IntPtr.Zero)
            {
                Check(FLAC__stream_encoder_finish(context), "finalize stream encoder");
                Check(FLAC__stream_encoder_delete(context), "dispose of stream encoder instance");
                context = IntPtr.Zero;
            }
        }
    
        public void Close()
        {
            Dispose();
        }
    
        unsafe
        public void Write(byte[] buffer, int offset, int uncompressedBytes)
        {
            if (context == IntPtr.Zero)
                throw new ApplicationException("FLAC: Stream encoder is not initialized!");
    
            int bytes = inputBitDepth / 8;
            int paddedSamples = uncompressedBytes / bytes;
            int samples = paddedSamples / inputChannels;
    
            if (padded == null || padded.Length < paddedSamples) // only needs to be assigned once - extra values arent sent on.
                padded = new int[paddedSamples];
    
           for (int i = 0; i < paddedSamples; i++)
                 padded[i] = buffer[i * bytes + 1] << 8 | buffer[i * bytes + 0];
    
            fixed (int* fixedInput = padded)
            {
                IntPtr input = new IntPtr(fixedInput);
    
                IntPtr t = Marshal.AllocHGlobal(padded.Length * sizeof(int));
                Marshal.Copy(padded, 0, t, padded.Length);
    
                Check(FLAC__stream_encoder_process_interleaved(context, input, paddedSamples), "process audio samples");
            }
        }
    
        private void Check(bool result, string operation)
        {
            if (!result)
            {
                int state = FLAC__stream_encoder_get_state(context);
                throw new ApplicationException(string.Format("FLAC: Could not {0}!", operation));
            }
        }
    

    Any help would be hugely appreciated
    Thanks
    Levi

     
  • This bug tracker isn't used currently, if you want you're probably better of at the flac-dev mailinglist. See the flac website for more information.

     
  • Erik
    Erik
    2014-03-21

    • status: open --> closed-invalid
    • assigned_to: Erik
     
  • Erik
    Erik
    2014-03-21

    Spectrograms are irrelevant. FLAC is loss-less so PCM data encoded to FLAC and then decoded back to PCM should be bit identical to the input. The current FLAC test suite seems to say that this is in fact the case.