Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7293 in orxonox.OLD for trunk/src


Ignore:
Timestamp:
Apr 14, 2006, 12:19:55 PM (19 years ago)
Author:
bensch
Message:

OggPlayer::state

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

Legend:

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

    r7292 r7293  
    3535  this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer");
    3636
    37   this->trackLoaded = false;
     37  this->state = None;
     38
    3839  this->source = 0;
    3940  this->buffers[0] = 0;
     
    5859bool OggPlayer::open(const std::string& fileName)
    5960{
     61  // release old State
     62  if (state & FileOpened)
     63    release();
     64
     65  // allocating Buffers
    6066  if (this->buffers[0] == 0)
    6167    alGenBuffers(2, this->buffers);
    6268  SoundEngine::checkError("Allocating Buffers", __LINE__);
    63 
     69  if (this->buffers[0] != 0 && this->buffers[1] != 0)
     70    state |= BuffersAllocated;
     71  else  {
     72    PRINTF(2)("Unable to allocate al-Buffers\n");
     73    this->release();
     74    return false;
     75  }
     76  // allocating source
    6477  if (this->source == 0)
    6578    SoundEngine::getInstance()->popALSource(this->source);
    66   if (this->source == 0)
    67   {
    68     this->trackLoaded = false;
    69     return false;
    70   }
    71 
     79  if (this->source != 0)
     80    state |= SourceAllocated;
     81  else
     82  {
     83    PRINTF(2)("No more Sources Availiable (maybe you should consider raising the source-count\n");
     84    this->release();
     85    return false;
     86  }
     87
     88  // opening the FILE;
    7289  int result;
    73 
    7490  if(!(oggFile = fopen(fileName.c_str(), "rb")))
    7591  {
    7692    PRINTF(2)("Could not open Ogg file.");
    77     return false;
    78   }
    79 
     93    this->release();
     94    return false;
     95  }
     96  // reading the Stream.
    8097  if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
    8198  {
    8299    PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
    83100    fclose(oggFile);
    84     return false;
    85   }
    86 
     101    this->release();
     102    return false;
     103  }
     104
     105  // acquiring the vorbis-properties.
    87106  vorbisInfo = ov_info(&oggStream, -1);
    88107  vorbisComment = ov_comment(&oggStream, -1);
    89 
     108  this->state |= FileOpened;
    90109  if(vorbisInfo->channels == 1)
    91110    format = AL_FORMAT_MONO16;
     
    93112    format = AL_FORMAT_STEREO16;
    94113
     114  // setting the Source Properties.
    95115  alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
    96116  alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
     
    101121  SoundEngine::checkError("OggPlayer::SetSourceProperties", __LINE__);
    102122
    103   this->trackLoaded = true;
    104123  return true;
    105124}
     
    110129void OggPlayer::release()
    111130{
    112   if (!this->trackLoaded)
    113     return;
    114   alSourceStop(source);
    115   SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__);
    116   empty();
    117   SoundEngine::getInstance()->pushALSource(source);
    118   this->source = 0;
    119   alDeleteBuffers(2, buffers);
    120   SoundEngine::checkError("OggPlayer::release()::alDeleteBuffers", __LINE__);
    121   this->buffers[0] = 0;
    122   this->buffers[1] = 0;
    123 
    124   ov_clear(&oggStream);
    125   this->trackLoaded = false;
     131  if (this->state & SourceAllocated)
     132  {
     133    assert(alIsSource(this->source));
     134    if (this->state & Playing);
     135    {
     136      alSourceStop(source);
     137      SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__);
     138      this->state & !Playing;
     139    }
     140    empty();
     141    SoundEngine::getInstance()->pushALSource(source);
     142    this->source = 0;
     143    this->state &= !SourceAllocated;
     144  }
     145  if (this->state & BuffersAllocated)
     146  {
     147    assert (this->buffers[0] != 0 && this->buffers[1] != 0);
     148    alDeleteBuffers(2, buffers);
     149    SoundEngine::checkError("OggPlayer::release()::alDeleteBuffers", __LINE__);
     150    this->buffers[0] = 0;
     151    this->buffers[1] = 0;
     152    this->state &= !BuffersAllocated;
     153  }
     154
     155  if (this->state & FileOpened)
     156  {
     157    ov_clear(&oggStream);
     158    this->state &= ! FileOpened;
     159  }
    126160}
    127161
     
    133167bool OggPlayer::playback()
    134168{
    135   if (!this->trackLoaded)
     169  if (!(this->state & FileOpened))
    136170    return false;
    137171
     
    148182  if (DEBUG >= 3)
    149183    SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__);
    150   printf("%d\n", this->source);
     184  this->state |= Playing;
    151185  return true;
    152186}
     
    158192bool OggPlayer::playing()
    159193{
    160   if (!this->trackLoaded)
     194  if (!(this->state & FileOpened))
    161195    return false;
    162196  ALenum state;
     
    173207bool OggPlayer::update()
    174208{
    175   if (unlikely(!this->trackLoaded))
     209  if (unlikely(!(this->state & Playing)))
    176210    return false;
    177211
     
    208242bool OggPlayer::stream(ALuint buffer)
    209243{
    210   if (!this->trackLoaded)
     244  if (unlikely(!(this->state & Playing)))
    211245    return false;
    212246  char pcm[BUFFER_SIZE];
  • trunk/src/lib/sound/ogg_player.h

    r7292 r7293  
    1717
    1818struct File;
    19 
    2019
    2120
     
    4241  const char* errorString(int code);
    4342
     43  public:
     44    typedef enum {
     45      None                   = 0x000,
     46      FileOpened             = 0x100,
     47      SourceAllocated        = 0x200,
     48      BuffersAllocated       = 0x400,
     49      Stopped                = 0x010,
     50      Playing                = 0x020,
     51      Paused                 = 0x040,
     52      Error                  = 0x001,
     53    } State;
     54
    4455private:
    4556  FILE*               oggFile;              //!< general file-handler, to open the sound-file
     
    5162  ALuint              source;               //!< The source we play back on
    5263  ALenum              format;               //!< The format we play back
    53   bool                trackLoaded;          //!< If a Track has been loaded.
     64  unsigned int        state;
     65  //bool                trackLoaded;          //!< If a Track has been loaded.
    5466};
    5567
Note: See TracChangeset for help on using the changeset viewer.