//-----------------------------------------------------------------------------
void MusicOggStream::update()
{

    if (m_pausedMusic || m_soundSource == ALuint(-1))
    {
        // nothing todo
        return;
    }

    int processed= 0;
    bool active= true;

    alGetSourcei(m_soundSource, AL_BUFFERS_PROCESSED, &processed);

    while(processed--)
    {
        ALuint buffer;

        alSourceUnqueueBuffers(m_soundSource, 1, &buffer);
        if(!check("alSourceUnqueueBuffers")) return;

        active = streamIntoBuffer(buffer);
        if(!active)
        {
            // no more data. Seek to beginning (causes the sound to loop)
            ov_time_seek(&m_oggStream, 0);
            active = streamIntoBuffer(buffer);//now there really should be data
        }

        alSourceQueueBuffers(m_soundSource, 1, &buffer);
        if (!check("alSourceQueueBuffers")) return;
    }

    if (active)
    {
        // For debugging
        SFXManager::checkError("before source state");
        // we have data, so we should be playing...
        ALenum state;
        alGetSourcei(m_soundSource, AL_SOURCE_STATE, &state);
        if (state != AL_PLAYING)
        {
            // Prevent flooding
            static int count = 0;
            count++;
            if (count<10)
                Log::warn("MusicOgg", "Music not playing when it should be. "
                          "Source state: %d", state);
            alGetSourcei(m_soundSource, AL_BUFFERS_PROCESSED, &processed);
            alSourcePlay(m_soundSource);
        }
    }
    else
    {
        Log::warn("MusicOgg", "Attempt to stream music into buffer failed "
                              "twice in a row.");
    }
}   // update
//-----------------------------------------------------------------------------
void MusicOggStream::update()
{

    if (m_pausedMusic || m_soundSource == ALuint(-1))
    {
        // nothing todo
        return;
    }

    int processed= 0;
    bool active= true;

    alGetSourcei(m_soundSource, AL_BUFFERS_PROCESSED, &processed);

    while(processed--)
    {
        ALuint buffer;

        alSourceUnqueueBuffers(m_soundSource, 1, &buffer);
        if(!check("alSourceUnqueueBuffers")) return;

        active = streamIntoBuffer(buffer);
        if(!active)
        {
            // no more data. Seek to beginning (causes the sound to loop)
            ov_time_seek(&m_oggStream, 0);
            active = streamIntoBuffer(buffer);//now there really should be data
            //fprintf(stdout,"Music buffer under-run.\n");
        }

        alSourceQueueBuffers(m_soundSource, 1, &buffer);
        if (!check("alSourceQueueBuffers")) return;
    }

    if (active)
    {
        // we have data, so we should be playing...
        ALenum state;
        alGetSourcei(m_soundSource, AL_SOURCE_STATE, &state);
        if (state != AL_PLAYING)
        {
            fprintf(stderr,"WARNING: Music not playing when it should be. Source state: %d\n", state);
            alGetSourcei(m_soundSource, AL_BUFFERS_PROCESSED, &processed);
            alSourcePlay(m_soundSource);
        }
    }
    else
    {
        fprintf(stderr,"WARNING: Attempt to stream music into buffer failed twice in a row.\n");
    }
}   // update
//-----------------------------------------------------------------------------
bool MusicOggStream::playMusic()
{
    if(isPlaying())
        return true;

    if(!streamIntoBuffer(m_soundBuffers[0]))
        return false;

    if(!streamIntoBuffer(m_soundBuffers[1]))
        return false;

    alSourceQueueBuffers(m_soundSource, 2, m_soundBuffers);

    alSourcePlay(m_soundSource);
    m_pausedMusic = false;
    m_playing = true;
    check("playMusic");
    return true;
}   // playMusic