[Vorbis-dev] How to loop a Vobis sound ?

steve steve at astrocorp.com.tw
Tue Jan 17 17:51:03 PST 2006


Hi:

I have tried to loop a vobis sound with OpenAL. But I got an error message from OpenAL.
The error message shows that the decoded data stream is wrong.

I get the data stream start position when the sound file is open, then rewind the sound stream when it
needs to be replayed.

Thank you, very much.


Here is my souce code:

  bool OggStream::LoadFromMemory (FileInMemory *pMemFile)
  {
    m_tOggMemoryFile = *pMemFile;

    // Open the file from memory.  We need to pass it a pointer to our data  (in this case our FileInMemory structure),
    // a pointer to our ogg stream  (which the vorbis libs will fill up for us), and our s_callbacks
    if  (ov_open_callbacks (&m_tOggMemoryFile, &m_tOggStream, NULL, 0, s_callbacks) != 0)
      throw string ("Could not read Ogg file from memory");

    m_iStreamStart = ov_pcm_tell (&m_tOggStream);
    m_bReleaseStream = true;
    return true;
  }

  bool OggStream::Rewind ()
  {
    if (!ov_seekable (&m_tOggStream))
      return false;

    int result = ov_pcm_seek (&m_tOggStream, m_iStreamStart);
    if (result < 0)
      throw OvErrorString  (result);
    return true;
  }

	//  OpenAL Souce Object

  bool Source::Update ()
  {
    if (!m_pBuf->IsDoubleBuffering ()) return true;

    ALint  error;
    char   szErr[1024];

    // OpenAL Loop function doesn't work for double buffering, we have to loop manually
    // todo: loop ogg doen't work, try to solve the problem
    if (!IsPlaying ())
    {
      if (m_iRepeats > 0)
      {
	    if (m_pOggStream)
	      return m_pOggStream->Rewind ();
	Play ();
	--m_iRepeats;
      }
      else if (m_iRepeats < 0)
      {
	    if (m_pOggStream)
	      return m_pOggStream->Rewind ();

	Play ();
      }
      return true;
    }

    bool  bActive = true;
    int	  processed;

    alGetSourcei (m_id, AL_BUFFERS_PROCESSED, &processed);

    while (processed--)
    {
      ALuint buffer;

      alSourceUnqueueBuffers (m_id, 1, &buffer);
      CHECK_ERROR ("Update () : ");

      bActive = m_pBuf->Stream (buffer);
      if (bActive)
      {
	alSourceQueueBuffers (m_id, 1, &buffer);
	CHECK_ERROR ("Update () : ");
      }
    }

    return bActive;
  }




More information about the Vorbis-dev mailing list