mewkiz / flac

Package flac provides access to FLAC (Free Lossless Audio Codec) streams.
The Unlicense
318 stars 44 forks source link

Audio Compression #37

Open trekhopton opened 5 years ago

trekhopton commented 5 years ago

Hi, I'm interested to know if this package compresses audio when encoding it to flac. I have tried out the wav2flac program in the cmd folder however when given a 5 second stereo wav file sampled at 48kHz, the flac file produced is larger by 240kb.

Since wav is uncompressed and flac is supposed to be encoded using lossless compression, I was expecting a smaller file size. Am I missing something? Thanks, -Trek

mewmew commented 5 years ago

Hi Trek!

I'm interested to know if this package compresses audio when encoding it to flac.

The quick answer is, it does not. At least not currently. This is tracked by https://github.com/mewkiz/flac/issues/35 which covers the implementation of LPC encoding of audio samples.

I have tried out the wav2flac program in the cmd folder however when given a 5 second stereo wav file sampled at 48kHz, the flac file produced is larger by 240kb.

Since wav is uncompressed and flac is supposed to be encoded using lossless compression, I was expecting a smaller file size. Am I missing something?

That sounds about right. The audio samples are currently encoded using the verbatim (i.e. uncompressed) method, or the constant method (when all audio samples of a frame are the same). However, as noted above, LPC encoding is yet to be implemented, and it is only once this has been implemented that you will see a compression of audio streams.

My brother @karlek and I are coding on the flac project when we get the feeling to, and usually that is during summer hackathons :)

If you are interested in audio encoding and would be interested in contributing to the project, feel most welcome to take a stab at implementing LPC encoding :)

Take a look at encode_subframe.go for the LPC encoding method yet to be implemented.

    // Encode audio samples.
    switch subframe.Pred {
    case frame.PredConstant:
        if err := encodeConstantSamples(bw, hdr.BitsPerSample, subframe.Samples); err != nil {
            return errutil.Err(err)
        }
    case frame.PredVerbatim:
        if err := encodeVerbatimSamples(bw, hdr, subframe.Samples); err != nil {
            return errutil.Err(err)
        }
    //case frame.PredFixed:
    //  if err := encodeFixedSamples(bw, hdr, subframe.Samples, subframe.Order); err != nil {
    //      return errutil.Err(err)
    //  }
    //case frame.PredFIR:
    //  if err := encodeFIRSamples(bw, hdr, subframe.Samples, subframe.Order); err != nil {
    //      return errutil.Err(err)
    //  }

Cheers, Robin

severincognito commented 1 year ago

Hello, are there any plans now to move this issue forward? Thanks for this very well made library!

mewmew commented 1 year ago

Hi @severincognito!

Neither Henry nor I are looking into implementing compression of audio samples for the foreseeable time in the future. That being said, anyone in the community who feel up to the task is warmly invited to take up the challenge.

As noted above in https://github.com/mewkiz/flac/issues/37#issuecomment-456588829 the missing component is support LPC encoding (as tracked by issue #35).

Do you have experience with audio codes @severincognito? Or even better, feel like learning something new? : ) Should you feel up for the challenge @karlek and I can try to guide you if you get stuck in any part of the mewkiz/flac code base.

Wish you a most lovely Spring.

Warm regards, Robin & Henry

ilyassBZ commented 11 months ago

can i work on this ? i saw the comm #35 and u already did imp encodeFIRSamples and fixedS ..... so what the problem now is it u wanna implement wav2flac or what u wanna do exactly ....

wader commented 11 months ago

can i work on this ? i saw the comm #35 and u already did imp encodeFIRSamples and fixedS ..... so what the problem now is it u wanna implement wav2flac or what u wanna do exactly ....

As i understand it #35 only adds support for encoding the LPC sample format but does not include any code for doing linear prediction. So if you want to implement a wav2flac that should compress, instead of just use verbatim frames, you would have to implement something that takes raw samples and produce Subframe:s that are filled in using some kind of linear prediction calculation and then encode those (and probably only if it makes sense compression-wise).

I don't know much about linear prediction but in the #35 description there is link to https://github.com/mewkiz/flac/pull/32#issuecomment-420835961 with some pointers.

ilyassBZ commented 11 months ago

i will work on it in this weekend then im also don't know much about linear prediction but i will learn and work on it in the same time what the worse thing could hpn lol

mewmew commented 11 months ago

i will work on it in this weekend then im also don't know much about linear prediction but i will learn and work on it in the same time what the worse thing could hpn lol

That's the spirit!

And, precisely like @wader pointed out. The #35 PR implemented encoding of LPC audio samples. The remaining part now is to do the linear prediction, i.e. to find good coefficients for storing the audio samples such that they take less space.

Wish you happy coding @ilyassBZ and a good learning adventure : )

Cheerful regards from Chile, Robin