Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7308 in orxonox.OLD for trunk


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

orxonox/trunk: added a mutex, that should prevent the SoundEngine from failing, when jumping around in the file

Location:
trunk/src
Files:
3 edited

Legend:

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

    r7307 r7308  
    3535  this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer");
    3636
    37   this->state = None;
     37  this->state = OggPlayer::None;
    3838
    3939  this->source = 0;
    4040  this->buffers[0] = 0;
    4141  this->buffers[1] = 0;
    42   this->musicThread = NULL;
     42  this->musicThreadID = NULL;
     43  this->musicMutex = SDL_CreateMutex();
    4344
    4445  if (!fileName.empty())
     
    4748      this->setName(fileName);
    4849  }
     50
    4951}
    5052
     
    5557{
    5658  this->release();
     59  SDL_DestroyMutex(this->musicMutex);
    5760}
    5861
     
    6770{
    6871  // release old Ogg-File (if loaded)
    69   if (this->state & FileOpened)
     72  if (this->state & OggPlayer::FileOpened)
    7073    this->release();
    7174
     
    7578  SoundEngine::checkError("Allocating Buffers", __LINE__);
    7679  if (this->buffers[0] != 0 && this->buffers[1] != 0)
    77     state |= BuffersAllocated;
     80    state |= OggPlayer::BuffersAllocated;
    7881  else
    7982  {
     
    8689    SoundEngine::getInstance()->popALSource(this->source);
    8790  if (this->source != 0)
    88     state |= SourceAllocated;
     91    state |= OggPlayer::SourceAllocated;
    8992  else
    9093  {
     
    110113    return false;
    111114  }
    112   this->state |= FileOpened;
     115  this->state |= OggPlayer::FileOpened;
    113116
    114117  // acquiring the vorbis-properties.
     
    142145bool OggPlayer::play()
    143146{
    144   if (!(this->state & FileOpened))
     147  if (!(this->state & OggPlayer::FileOpened))
    145148    return false;
    146149
     
    150153    return false;
    151154
    152   if (this->musicThread == NULL)
    153     return ((this->musicThread = SDL_CreateThread(OggPlayer::createAudioThread, (void*)this)) != NULL);
     155  if (this->musicThreadID == NULL)
     156    return ((this->musicThreadID = SDL_CreateThread(OggPlayer::musicThread, (void*)this)) != NULL);
    154157  return true;
    155158}
     
    189192void OggPlayer::jumpTo(float timeCode)
    190193{
     194
    191195  if (this->state & OggPlayer::FileOpened)
     196  {
     197    SDL_mutexP(this->musicMutex);
    192198    ov_time_seek(&this->oggStream, timeCode);
     199    SDL_mutexV(this->musicMutex);
     200  }
    193201}
    194202
     
    210218bool OggPlayer::isPlaying()
    211219{
    212   if (!(this->state & FileOpened))
     220  if (!(this->state & OggPlayer::FileOpened))
    213221    return false;
    214222  ALenum state;
     
    229237 * @returns -1 on error.
    230238 */
    231 int OggPlayer::createAudioThread(void* oggPlayer)
     239int OggPlayer::musicThread(void* oggPlayer)
    232240{
    233241  if (oggPlayer == NULL)
    234242    return -1;
    235243  OggPlayer* ogg = (OggPlayer*)oggPlayer;
     244
    236245  PRINTF(4)("STARTIG AUDIO THREAD\n");
    237 
    238246  while (ogg->state & OggPlayer::Playing)
    239247  {
     248    SDL_mutexP(ogg->musicMutex);
    240249    ogg->update();
     250    SDL_mutexV(ogg->musicMutex);
    241251    SDL_Delay(1);
    242252  }
     
    264274  if (DEBUG >= 3)
    265275    SoundEngine::checkError("OggPlayer::playback()::alSourceQueueBuffers", __LINE__);
    266   if (!alIsBuffer(this->buffers[0])) printf("AHA0\n");
    267   if (!alIsBuffer(this->buffers[1])) printf("AHA1\n");
    268 
    269   if (!alIsSource(this->source)) printf("AHA2\n");
    270   SoundEngine::checkError("SKJFLKSDJF",__LINE__);
     276
    271277
    272278  alSourcePlay(this->source);
     
    282288void OggPlayer::suspend()
    283289{
    284   if (this->musicThread != NULL)
     290  if (this->musicThreadID != NULL)
    285291  {
    286292    assert (!(this->state & Playing));
    287293    this->printState();
    288     SDL_WaitThread(this->musicThread, NULL);
    289     this->musicThread = NULL;
     294    SDL_WaitThread(this->musicThreadID, NULL);
     295    this->musicThreadID = NULL;
    290296  }
    291297  if (this->state & OggPlayer::SourceAllocated)
     
    302308bool OggPlayer::update()
    303309{
    304   if (unlikely(!(this->state & Playing)))
    305     return false;
    306 
    307   int processed;
     310  int processed = 0;
    308311  bool active = true;
    309312
     
    372375void OggPlayer::release()
    373376{
    374   if (this->state & SourceAllocated)
     377  if (this->state & OggPlayer::SourceAllocated)
    375378  {
    376379    assert(alIsSource(this->source));
     
    379382      this->state &= ~OggPlayer::Playing;
    380383      // Kill the Music Thread.
    381       if (this->musicThread != NULL)
     384      if (this->musicThreadID != NULL)
    382385        this->suspend();
    383386
     
    390393    this->state &= ~SourceAllocated;
    391394  }
    392   if (this->state & BuffersAllocated)
     395  if (this->state & OggPlayer::BuffersAllocated)
    393396  {
    394397    assert (this->buffers[0] != 0 && this->buffers[1] != 0);
     
    397400    this->buffers[0] = 0;
    398401    this->buffers[1] = 0;
    399     this->state &= ~BuffersAllocated;
    400   }
    401 
    402   if (this->state & FileOpened)
     402    this->state &= ~OggPlayer::BuffersAllocated;
     403  }
     404
     405  if (this->state & OggPlayer::FileOpened)
    403406  {
    404407    ov_clear(&oggStream);
    405     this->state &= ~FileOpened;
     408    this->state &= ~OggPlayer::FileOpened;
    406409  }
    407410
     
    457460{
    458461  PRINTF(0)("OggPlayer is in the following States: ");
    459   if (this->state & FileOpened)
     462  if (this->state & OggPlayer::FileOpened)
    460463    PRINT(0)("FileOpened ");
    461   if (this->state & SourceAllocated)
     464  if (this->state & OggPlayer::SourceAllocated)
    462465    PRINT(0)("SourceAllocated ");
    463   if (this->state & BuffersAllocated)
     466  if (this->state & OggPlayer::BuffersAllocated)
    464467    PRINT(0)("BuffersAllocated ");
    465   if (this->state & Stopped)
     468  if (this->state & OggPlayer::Stopped)
    466469    PRINT(0)("Stopped ");
    467   if (this->state & Playing)
     470  if (this->state & OggPlayer::Playing)
    468471    PRINT(0)("Playing ");
    469   if (this->state & Paused)
     472  if (this->state & OggPlayer::Paused)
    470473    PRINT(0)("Paused ");
    471   if (this->state & Error)
     474  if (this->state & OggPlayer::Error)
    472475    PRINT(0)("Error ");
    473476  PRINT(0)("\n");
  • trunk/src/lib/sound/ogg_player.h

    r7307 r7308  
    2020
    2121
    22 #define OGG_PLAYER_BUFFER_SIZE (8096 * 4)
     22#define OGG_PLAYER_BUFFER_SIZE (8096 * 2)
    2323
    2424
     
    6666
    6767private:
    68   static int createAudioThread(void* oggPlayer);
     68  static int musicThread(void* oggPlayer);
    6969  bool playback();
    7070  void suspend();
     
    8787  unsigned int        state;                //!< The States the OggPlayer is in (this can be multiple entries from OggPlayer::State).
    8888
    89   SDL_Thread*         musicThread;          //!< The Thread in which music is Played back.
     89  SDL_Thread*         musicThreadID;        //!< The Thread in which music is Played back.
     90  SDL_mutex*          musicMutex;           //!< A Mutex so that the two threads do not interfere.
    9091};
    9192
  • trunk/src/story_entities/game_world.cc

    r7307 r7308  
    274274    this->display ();
    275275
     276    /// TODO REMOVE THIS CRAP
    276277    if (this->dataTank->music)
    277278    {
    278       if (this->cycle%100 == 0)
     279/*      if (this->cycle%100 == 0)
    279280      {
    280281        printf("Cycle %d  ", this->cycle);
    281282        this->dataTank->music->printState();
    282283      }
     284      if (this->cycle %10)
     285        this->dataTank->music->jumpTo((float)this->cycle*(float)rand()/(float)RAND_MAX);
    283286      if (this->cycle % 4 == 0)
    284287        this->dataTank->music->pause();
     
    286289        this->dataTank->music->play();
    287290      if (this->cycle % 11 == 0)
    288         this->dataTank->music->stop();
     291      this->dataTank->music->stop(); */
    289292    }
    290293  }
Note: See TracChangeset for help on using the changeset viewer.