Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7305 in orxonox.OLD for trunk


Ignore:
Timestamp:
Apr 16, 2006, 8:46:38 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: cleanup of the SoundEngine. man, this was necessary :)

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

Legend:

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

    r7304 r7305  
    5454}
    5555
    56 /**
    57  * @brief jump to Second timeCode in the MusicFile
    58  * @param timeCode where to jump to.
    59  */
    60 void OggPlayer::jumpTo(float timeCode)
    61 {
    62   if (this->state & FileOpened)
    63     ov_time_seek(&this->oggStream, timeCode);
    64 }
    65 
    66 /**
    67  * opens a file for playback
     56///////////////
     57// INTERFACE //
     58///////////////
     59/**
     60 * @brief opens a file for playback
    6861 * @param fileName the file to open
    6962 */
    7063bool OggPlayer::open(const std::string& fileName)
    7164{
    72   // release old Ogg-File
     65  // release old Ogg-File (if loaded)
    7366  if (this->state & FileOpened)
    7467    this->release();
     
    109102  if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
    110103  {
    111     PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
     104    PRINTF(2)("Could not open Ogg stream. %s", getVorbisError(result));
    112105    fclose(oggFile);
    113106    this->release();
     
    138131
    139132
    140 
     133/**
     134 * @brief start Playing Music.
     135 * @returns true on success false otherwise.
     136 */
     137bool OggPlayer::play()
     138{
     139  if (!(this->state & FileOpened))
     140    return false;
     141  if (this->musicThread == NULL)
     142    return ((this->musicThread = SDL_CreateThread(OggPlayer::createAudioThread, (void*)this)) != NULL);
     143  return true;
     144}
     145
     146
     147void OggPlayer::stop()
     148{}
     149
     150void OggPlayer::pause()
     151{}
     152
     153void OggPlayer::rewind()
     154{}
     155
     156/**
     157 * @brief jump to Second timeCode in the MusicFile
     158 * @param timeCode where to jump to.
     159 */
     160void OggPlayer::jumpTo(float timeCode)
     161{
     162  if (this->state & FileOpened)
     163    ov_time_seek(&this->oggStream, timeCode);
     164}
     165
     166/**
     167 * @returns true if the file is playing
     168 */
     169bool OggPlayer::isPlaying()
     170{
     171  if (!(this->state & FileOpened))
     172    return false;
     173  ALenum state;
     174
     175  alGetSourcei(this->source, AL_SOURCE_STATE, &state);
     176
     177  return (state == AL_PLAYING);
     178}
     179
     180
     181
     182////////////////////////
     183// INTERNAL FUNCTIONS //
     184////////////////////////
    141185int OggPlayer::createAudioThread(void* oggPlayer)
    142186{
     
    154198  }
    155199  PRINTF(4)("End the AudioThread\n");
     200}
     201
     202/**
     203 * @brief plays back the sound
     204 * @return true if running, false otherwise
     205 */
     206bool OggPlayer::playback()
     207{
     208  if (!(this->state & FileOpened))
     209    return false;
     210
     211  if(this->isPlaying())
     212    return true;
     213  this->state |= Playing;
     214
     215  if(!this->stream(this->buffers[0]) || !this->stream(this->buffers[1]))
     216    return false;
     217
     218  alSourceQueueBuffers(this->source, 2, this->buffers);
     219  if (DEBUG >= 3)
     220    SoundEngine::checkError("OggPlayer::playback()::alSourceQueueBuffers", __LINE__);
     221  if (!alIsBuffer(this->buffers[0])) printf("AHA0\n");
     222  if (!alIsBuffer(this->buffers[1])) printf("AHA1\n");
     223
     224  if (!alIsSource(this->source)) printf("AHA2\n");
     225  SoundEngine::checkError("SKJFLKSDJF",__LINE__);
     226
     227  alSourcePlay(this->source);
     228  if (DEBUG >= 3)
     229    SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__);
     230  return true;
     231}
     232
     233
     234/**
     235 * updates the stream, this has to be done every few parts of a second, for sound-consistency
     236 * @returns true, if the Sound is playing flawlessly
     237 */
     238bool OggPlayer::update()
     239{
     240  if (unlikely(!(this->state & Playing)))
     241    return false;
     242
     243  int processed;
     244  bool active = true;
     245
     246  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
     247  if (DEBUG >= 3)
     248    SoundEngine::checkError("OggPlayer::update()::alGetSourceI", __LINE__);
     249
     250  while(processed--)
     251  {
     252    ALuint buffer;
     253
     254    alSourceUnqueueBuffers(source, 1, &buffer);
     255    if (DEBUG >= 3)
     256      SoundEngine::checkError("OggPlayer::update()::unqueue", __LINE__);
     257
     258    active = stream(buffer);
     259
     260    alSourceQueueBuffers(source, 1, &buffer);
     261    if (DEBUG >= 3)
     262      SoundEngine::checkError("OggPlayer::update()::queue", __LINE__);
     263  }
     264
     265  return active;
     266}
     267
     268/**
     269 * gets a new Stream from buffer
     270 * @param buffer the buffer to get the stream from
     271 * @return true, if everything worked as planed
     272 */
     273bool OggPlayer::stream(ALuint buffer)
     274{
     275  if (unlikely(!(this->state & Playing)))
     276    return false;
     277  char pcm[OGG_PLAYER_BUFFER_SIZE];
     278  int  size = 0;
     279  int  section;
     280  int  result;
     281
     282  while(size < OGG_PLAYER_BUFFER_SIZE)
     283  {
     284    result = ov_read(&this->oggStream, pcm + size, OGG_PLAYER_BUFFER_SIZE - size, 0, 2, 1, &section);
     285
     286    if(result > 0)
     287      size += result;
     288    else if(result < 0)
     289      throw getVorbisError(result);
     290    else /* eof */
     291      ov_time_seek(&this->oggStream, 0.0);
     292  }
     293
     294  if(size == 0)
     295    return false;
     296
     297  alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
     298  if (DEBUG >= 3)
     299    SoundEngine::checkError("OggPlayer::stream()::BUFFER", __LINE__);
     300
     301  return true;
    156302}
    157303
     
    203349
    204350
    205 bool OggPlayer::play()
    206 {
    207   if (this->musicThread == NULL)
    208     this->musicThread = SDL_CreateThread(OggPlayer::createAudioThread, (void*)this);
    209 }
    210 
    211 /**
    212  * plays back the sound
    213  * @return true if running, false otherwise
    214  */
    215 bool OggPlayer::playback()
    216 {
    217   if (!(this->state & FileOpened))
    218     return false;
    219 
    220   if(playing())
    221     return true;
    222   this->state |= Playing;
    223 
    224   if(!this->stream(this->buffers[0]) || !this->stream(this->buffers[1]))
    225     return false;
    226 
    227   alSourceQueueBuffers(this->source, 2, this->buffers);
    228   if (DEBUG >= 3)
    229     SoundEngine::checkError("OggPlayer::playback()::alSourceQueueBuffers", __LINE__);
    230   if (!alIsBuffer(this->buffers[0])) printf("AHA0\n");
    231   if (!alIsBuffer(this->buffers[1])) printf("AHA1\n");
    232 
    233   if (!alIsSource(this->source)) printf("AHA2\n");
    234   SoundEngine::checkError("SKJFLKSDJF",__LINE__);
    235 
    236   alSourcePlay(this->source);
    237   if (DEBUG >= 3)
    238     SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__);
    239   return true;
    240 }
    241 
    242 /**
    243  *
    244  * @returns true if the file is playing
    245  */
    246 bool OggPlayer::playing()
    247 {
    248   if (!(this->state & FileOpened))
    249     return false;
    250   ALenum state;
    251 
    252   alGetSourcei(this->source, AL_SOURCE_STATE, &state);
    253 
    254   return (state == AL_PLAYING);
    255 }
    256 
    257 /**
    258  * updates the stream, this has to be done every few parts of a second, for sound-consistency
    259  * @returns true, if the Sound is playing flawlessly
    260  */
    261 bool OggPlayer::update()
    262 {
    263   if (unlikely(!(this->state & Playing)))
    264     return false;
    265 
    266   int processed;
    267   bool active = true;
    268 
    269   alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
    270   if (DEBUG >= 3)
    271     SoundEngine::checkError("OggPlayer::update()::alGetSourceI", __LINE__);
    272 
    273   while(processed--)
    274   {
    275     ALuint buffer;
    276 
    277     alSourceUnqueueBuffers(source, 1, &buffer);
    278     if (DEBUG >= 3)
    279       SoundEngine::checkError("OggPlayer::update()::unqueue", __LINE__);
    280 
    281     active = stream(buffer);
    282 
    283     alSourceQueueBuffers(source, 1, &buffer);
    284     if (DEBUG >= 3)
    285       SoundEngine::checkError("OggPlayer::update()::queue", __LINE__);
    286   }
    287 
    288   return active;
    289 }
    290 
    291 /**
    292  * gets a new Stream from buffer
    293  * @param buffer the buffer to get the stream from
    294  * @return true, if everything worked as planed
    295  */
    296 bool OggPlayer::stream(ALuint buffer)
    297 {
    298   if (unlikely(!(this->state & Playing)))
    299     return false;
    300   char pcm[OGG_PLAYER_BUFFER_SIZE];
    301   int  size = 0;
    302   int  section;
    303   int  result;
    304 
    305   while(size < OGG_PLAYER_BUFFER_SIZE)
    306   {
    307     result = ov_read(&this->oggStream, pcm + size, OGG_PLAYER_BUFFER_SIZE - size, 0, 2, 1, &section);
    308 
    309     if(result > 0)
    310       size += result;
    311     else if(result < 0)
    312       throw errorString(result);
    313     else /* eof */
    314       ov_time_seek(&this->oggStream, 0.0);
    315   }
    316 
    317   if(size == 0)
    318     return false;
    319 
    320   alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
    321   if (DEBUG >= 3)
    322     SoundEngine::checkError("OggPlayer::stream()::BUFFER", __LINE__);
    323 
    324   return true;
    325 }
    326 
    327 
    328 /**
    329  * empties the buffers
     351/**
     352 * @brief empties the buffers
    330353 */
    331354void OggPlayer::empty()
     
    345368
    346369
     370/////////////////////
     371// DEBUG FUNCTIONS //
     372/////////////////////
    347373/**
    348374 * displays some info about the ogg-file
    349375 */
    350 void OggPlayer::debug()
     376void OggPlayer::debug() const
    351377{
    352378  cout
     
    368394
    369395
    370 void OggPlayer::printState()
     396void OggPlayer::printState() const
    371397{
    372398  PRINTF(0)("OggPlayer is in the following States: ");
     
    393419 * @return the error as a String
    394420 */
    395 const char* OggPlayer::errorString(int code)
     421const char* OggPlayer::getVorbisError(int code)
    396422{
    397423  switch(code)
  • trunk/src/lib/sound/ogg_player.h

    r7304 r7305  
    2727{
    2828public:
    29   OggPlayer(const std::string& fileName = "");
    30   virtual ~OggPlayer();
    31 
    32   bool open(const std::string& fileName);
    33   void release();
    34   bool play();
    35   bool playing();
    36   bool update();
    37 
    38   void jumpTo(float timeCode);
    39 
    40   void debug();
    41   void printState();
    42 
    43   static int createAudioThread(void* oggPlayer);
    44 
    45 
    46 protected:
    47   bool stream(ALuint buffer);
    48   void empty();
    49   const char* errorString(int code);
    50 
    51 private:
    52   bool playback();
    53 
    54 
    55 public:
     29  /**
     30   * An enumerator defining in what State the OggPlayer is.
     31   * The OggPlayer can be in multiple States listed here.
     32   * It can as an example be in FileOpened and Stopped.
     33   */
    5634  typedef enum {
    5735    None                   = 0x000,
     
    6543  } State;
    6644
     45  OggPlayer(const std::string& fileName = "");
     46  virtual ~OggPlayer();
     47  /** @param state compare this State with the internal State */
     48  bool operator==(OggPlayer::State state) { return this->state & state; };
     49
     50  bool open(const std::string& fileName = "");
     51
     52  bool play();
     53  void stop();
     54  void pause();
     55  void rewind(); // convenience
     56  void jumpTo(float timeCode);
     57
     58  bool isPlaying();
     59  bool getState() { return this->state; };
     60
     61  void debug() const;
     62  void printState() const;
     63  const char* getVorbisError(int code);
     64
     65
     66private:
     67  static int createAudioThread(void* oggPlayer);
     68  bool playback();
     69  bool update();
     70
     71  void release();
     72
     73  bool stream(ALuint buffer);
     74  void empty();
     75
     76public:
     77
    6778private:
    6879  FILE*               oggFile;              //!< general file-handler, to open the sound-file
     
    7485  ALuint              source;               //!< The source we play back on
    7586  ALenum              format;               //!< The format we play back
    76   unsigned int        state;
     87  unsigned int        state;                //!< The States the OggPlayer is in (this can be multiple entries from OggPlayer::State).
    7788
    7889  SDL_Thread*         musicThread;          //!< The Thread in which music is Played back.
Note: See TracChangeset for help on using the changeset viewer.