Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7306 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Apr 16, 2006, 10:12:11 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: Play Stop Rewind and Pause work perfectly: you can test this in this revision

Location:
trunk/src/lib/sound
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/sound/ogg_player.cc

    r7305 r7306  
    4949}
    5050
     51/**
     52 * @brief deletes the OggPlayer
     53 */
    5154OggPlayer::~OggPlayer()
    5255{
     
    127130  SoundEngine::checkError("OggPlayer::open()::SetSourceProperties", __LINE__);
    128131
     132  // Set to State Stopped
     133  this->state |= OggPlayer::Stopped;
    129134  return true;
    130135}
     
    139144  if (!(this->state & FileOpened))
    140145    return false;
     146
     147  this->state &= ~(OggPlayer::Stopped | OggPlayer::Paused);
    141148  if (this->musicThread == NULL)
    142149    return ((this->musicThread = SDL_CreateThread(OggPlayer::createAudioThread, (void*)this)) != NULL);
     
    146153
    147154void OggPlayer::stop()
    148 {}
     155{
     156  this->state &= ~(OggPlayer::Playing | OggPlayer::Paused);
     157  this->state |= OggPlayer::Stopped;
     158
     159  this->suspend();
     160  this->rewind();
     161}
    149162
    150163void OggPlayer::pause()
    151 {}
    152 
     164{
     165  this->state &= ~OggPlayer::Playing;
     166
     167  if (!(this->state & OggPlayer::Stopped))
     168    this->state |= OggPlayer::Paused;
     169
     170  this->suspend();
     171}
     172
     173/**
     174 * @brief rewind to the beginning, and play (if already playing)
     175 */
    153176void OggPlayer::rewind()
    154 {}
     177{
     178  this->jumpTo(0.0f);
     179}
    155180
    156181/**
     
    160185void OggPlayer::jumpTo(float timeCode)
    161186{
    162   if (this->state & FileOpened)
     187  if (this->state & OggPlayer::FileOpened)
    163188    ov_time_seek(&this->oggStream, timeCode);
    164189}
     190
     191/**
     192 * @returns the Length of the Music in Seconds
     193 */
     194float OggPlayer::length()
     195{
     196  if (this->state & OggPlayer::FileOpened)
     197    return ov_time_total(&this->oggStream, -1);
     198  else
     199    return 0.0f;
     200}
     201
    165202
    166203/**
     
    183220// INTERNAL FUNCTIONS //
    184221////////////////////////
     222/**
     223 * @brief creates a Thread for Playing back the music even if the rest of the Engine is slow
     224 * @param oggPlayer the OggPlayer to play back
     225 * @returns -1 on error.
     226 */
    185227int OggPlayer::createAudioThread(void* oggPlayer)
    186228{
     
    190232  PRINTF(4)("STARTIG AUDIO THREAD\n");
    191233
    192   ogg->playback();
    193 
    194   while (ogg->state & Playing)
     234  if (!ogg->playback())
     235    return -1;
     236
     237  while (ogg->state & OggPlayer::Playing)
    195238  {
    196239    ogg->update();
     
    200243}
    201244
     245
    202246/**
    203247 * @brief plays back the sound
     
    206250bool OggPlayer::playback()
    207251{
    208   if (!(this->state & FileOpened))
    209     return false;
    210 
    211   if(this->isPlaying())
     252  if (!(this->state & OggPlayer::FileOpened))
     253    return false;
     254
     255  if(this->state & OggPlayer::Playing)
    212256    return true;
    213   this->state |= Playing;
     257  this->state |= OggPlayer::Playing;
    214258
    215259  if(!this->stream(this->buffers[0]) || !this->stream(this->buffers[1]))
     
    233277
    234278/**
    235  * updates the stream, this has to be done every few parts of a second, for sound-consistency
     279 * @brief waits for the AudioThread to be finished.
     280 */
     281void OggPlayer::suspend()
     282{
     283  if (this->musicThread != NULL)
     284  {
     285    assert (!(this->state & Playing));
     286    SDL_WaitThread(this->musicThread, NULL);
     287    this->musicThread = NULL;
     288  }
     289  if (this->state & OggPlayer::SourceAllocated)
     290  {
     291    alSourceStop(this->source);
     292    alSourcei(this->source, AL_BUFFER, 0);
     293  }
     294}
     295
     296/**
     297 * @brief updates the stream, this has to be done every few parts of a second, for sound-consistency
    236298 * @returns true, if the Sound is playing flawlessly
    237299 */
     
    267329
    268330/**
    269  * gets a new Stream from buffer
     331 * @brief gets a new Stream from buffer
    270332 * @param buffer the buffer to get the stream from
    271333 * @return true, if everything worked as planed
     
    311373  {
    312374    assert(alIsSource(this->source));
    313     if (this->state & Playing);
     375    if (this->state & OggPlayer::Playing);
    314376    {
    315       alSourceStop(source);
     377      this->state &= ~OggPlayer::Playing;
     378      // Kill the Music Thread.
     379      if (this->musicThread != NULL)
     380        this->suspend();
     381
    316382      SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__);
    317       this->state & !Playing;
    318383    }
    319384    empty();
     
    321386    SoundEngine::getInstance()->pushALSource(this->source);
    322387    this->source = 0;
    323     this->state &= !SourceAllocated;
     388    this->state &= ~SourceAllocated;
    324389  }
    325390  if (this->state & BuffersAllocated)
     
    330395    this->buffers[0] = 0;
    331396    this->buffers[1] = 0;
    332     this->state &= !BuffersAllocated;
     397    this->state &= ~BuffersAllocated;
    333398  }
    334399
     
    336401  {
    337402    ov_clear(&oggStream);
    338     this->state &= ! FileOpened;
    339   }
    340 
    341   // Kill the Music Thread.
    342   if (this->musicThread != NULL)
    343   {
    344     assert (!(this->state & Playing));
    345     SDL_WaitThread(this->musicThread, NULL);
    346     this->musicThread = NULL;
    347   }
     403    this->state &= ~FileOpened;
     404  }
     405
    348406}
    349407
  • trunk/src/lib/sound/ogg_player.h

    r7305 r7306  
    2020
    2121
    22 #define OGG_PLAYER_BUFFER_SIZE (8096 * 2)
     22#define OGG_PLAYER_BUFFER_SIZE (8096 * 4)
    2323
    2424
     
    3333   */
    3434  typedef enum {
    35     None                   = 0x000,
    36     FileOpened             = 0x100,
    37     SourceAllocated        = 0x200,
    38     BuffersAllocated       = 0x400,
    39     Stopped                = 0x010,
    40     Playing                = 0x020,
    41     Paused                 = 0x040,
    42     Error                  = 0x001,
     35    None                   = 0x000,   //!< Initialized
     36    FileOpened             = 0x100,   //!< File is Opened
     37    SourceAllocated        = 0x200,   //!< Source is Allocated.
     38    BuffersAllocated       = 0x400,   //!< 2 Buffers are Allocated.
     39    Stopped                = 0x010,   //!< OggPlayer is stopped.
     40    Playing                = 0x020,   //!< OggPlayer is Playing.
     41    Paused                 = 0x040,   //!< OggPlayer is Paused.
     42    Error                  = 0x001,   //!< An Error has occured.
    4343  } State;
    4444
     
    5656  void jumpTo(float timeCode);
    5757
     58  float length();
    5859  bool isPlaying();
    5960  bool getState() { return this->state; };
     
    6768  static int createAudioThread(void* oggPlayer);
    6869  bool playback();
     70  void suspend();
    6971  bool update();
    7072
Note: See TracChangeset for help on using the changeset viewer.