Packging LEFT and RIGHT channel data

808 views Asked by At

I am decoding FLAC audio into memory, and passing the decoded audio data to the OpenAL: void alBufferData (ALuint bufferName, ALenum format, const ALvoid *data, ALsizei size, ALsizei frequency);

The data from the decoded audio goes into mine std::vector<FLAC__int32> data_;. Into which I am attempting to package the LEFT and RIGHT channels (AL_FORMAT_STEREO16). However, I don't understand how I am to store/align these channels within my data_ vector.

So I have the libFLAC virtual callback member function:

FLAC__StreamDecoderWriteStatus
Source::write_callback (
    FLAC__Frame const* _frame, FLAC__int32 const *const _buffer[])
{

    for(size_t i(0); i < _frame->header.blocksize; i++) {

        data_[index_] = _buffer[0][i]; // channel audio on the left
        ++index_;

        data_[index_] = _buffer[1][i]; // what about the right channel?

    } // jump

    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
} // main

At the moment, during audio playback, I am hearing only the LEFT channel. There is static sound after the sound has finished playing which I am assuming is the missing RIGHT channel data. How do i get the RIGHT channel to work also?

Also, this is the metadata callback signature as per libFLAC:

void
Source::metadata_callback (const ::FLAC__StreamMetadata *metadata)
{

    total_samples_ = metadata->data.stream_info.total_samples;
    rate_ = metadata->data.stream_info.sample_rate;
    channels_ = metadata->data.stream_info.channels;
    bps_ = metadata->data.stream_info.bits_per_sample;

    switch (bps_) {
        case 16 :

            if (channels_ > 1) {
                format_ = AL_FORMAT_STEREO16; } else { 
                format_ = AL_FORMAT_MONO16; }

            break;
        case 8 :

            if (channels_ > 1) {
                format_ = AL_FORMAT_STEREO8; } else { 
                format_ = AL_FORMAT_MONO8; }

            break;
        default:
            break;
    }

    size_ = (ALuint)(rate_ * channels_ * (bps_ / 8));
    data_.resize(total_samples_); index_ = 0;
} // main
1

There are 1 answers

0
8-bitButterfly On

A solution which works, was to assagned the below struct as the vector data type like so:

struct Data
{

    FLAC__int16 channelLeft_;
    FLAC__int16 channelRight_;

};

std::vector<Source::Data> data_;

than assign the size_ like so:

size_ = total_samples_ * sizeof(Source::Data);

finaly the data loop should now be:

for(size_t i(0); i < _frame->header.blocksize; i++) {

    data_[index_].channelLeft_ = _buffer[0][i];
    data_[index_].channelRight_ = _buffer[1][i];

    ++index_;
} // jump