Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6828 in orxonox.OLD


Ignore:
Timestamp:
Jan 29, 2006, 2:31:48 PM (18 years ago)
Author:
bensch
Message:

trunk: Music plays

Location:
trunk/src
Files:
4 edited

Legend:

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

    r6827 r6828  
    5050void OggPlayer::open(const char* fileName)
    5151{
    52     int result;
    53 
    54     if(!(oggFile = fopen(fileName, "rb")))
    55         PRINTF(2)("Could not open Ogg file.");
    56 
    57     if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
    58     {
    59         fclose(oggFile);
    60 
    61         PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
    62     }
    63 
    64     vorbisInfo = ov_info(&oggStream, -1);
    65     vorbisComment = ov_comment(&oggStream, -1);
    66 
    67     if(vorbisInfo->channels == 1)
    68         format = AL_FORMAT_MONO16;
    69     else
    70         format = AL_FORMAT_STEREO16;
    71 
    72 
    73     alGenBuffers(2, buffers);
    74     check();
    75     SoundEngine::getInstance()->popALSource(this->source);
    76     check();
    77 
    78     alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
    79     alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
    80     alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
    81     alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
    82     alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE      );
    83     alSourcef (source, AL_GAIN,            SoundEngine::getInstance()->getMusicVolume());
     52  int result;
     53
     54  if(!(oggFile = fopen(fileName, "rb")))
     55    PRINTF(2)("Could not open Ogg file.");
     56
     57  if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
     58  {
     59    fclose(oggFile);
     60
     61    PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
     62  }
     63
     64  vorbisInfo = ov_info(&oggStream, -1);
     65  vorbisComment = ov_comment(&oggStream, -1);
     66
     67  if(vorbisInfo->channels == 1)
     68    format = AL_FORMAT_MONO16;
     69  else
     70    format = AL_FORMAT_STEREO16;
     71
     72
     73  alGenBuffers(2, buffers);
     74  check();
     75  SoundEngine::getInstance()->popALSource(this->source);
     76  check();
     77
     78  alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
     79  alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
     80  alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
     81  alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
     82  alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE      );
     83  alSourcef (source, AL_GAIN,            SoundEngine::getInstance()->getMusicVolume());
    8484}
    8585
     
    8989void OggPlayer::release()
    9090{
    91     alSourceStop(source);
    92     empty();
    93     SoundEngine::getInstance()->pushALSource(source);
    94     check();
    95     alDeleteBuffers(1, buffers);
    96     check();
    97 
    98     ov_clear(&oggStream);
    99 }
    100 
    101 /**
    102  * displays some info about the ogg-file
    103  */
    104 void OggPlayer::display()
    105 {
    106     cout
    107         << "version         " << vorbisInfo->version         << "\n"
    108         << "channels        " << vorbisInfo->channels        << "\n"
    109         << "rate (hz)       " << vorbisInfo->rate            << "\n"
    110         << "bitrate upper   " << vorbisInfo->bitrate_upper   << "\n"
    111         << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n"
    112         << "bitrate lower   " << vorbisInfo->bitrate_lower   << "\n"
    113         << "bitrate window  " << vorbisInfo->bitrate_window  << "\n"
    114         << "\n"
    115         << "vendor " << vorbisComment->vendor << "\n";
    116 
    117     for(int i = 0; i < vorbisComment->comments; i++)
    118         cout << "   " << vorbisComment->user_comments[i] << "\n";
    119 
    120     cout << endl;
     91  alSourceStop(source);
     92  empty();
     93  SoundEngine::getInstance()->pushALSource(source);
     94  check();
     95  alDeleteBuffers(1, buffers);
     96  check();
     97
     98  ov_clear(&oggStream);
    12199}
    122100
     
    128106bool OggPlayer::playback()
    129107{
    130     if(playing())
    131         return true;
    132 
    133     if(!stream(buffers[0]))
    134         return false;
    135 
    136     if(!stream(buffers[1]))
    137         return false;
    138 
    139     alSourceQueueBuffers(source, 2, buffers);
    140     alSourcePlay(source);
    141 
     108  if(playing())
    142109    return true;
     110
     111  if(!stream(buffers[0]))
     112    return false;
     113
     114  if(!stream(buffers[1]))
     115    return false;
     116
     117  alSourceQueueBuffers(source, 2, buffers);
     118  alSourcePlay(source);
     119
     120  return true;
    143121}
    144122
     
    149127bool OggPlayer::playing()
    150128{
    151     ALenum state;
    152 
    153     alGetSourcei(source, AL_SOURCE_STATE, &state);
    154 
    155     return (state == AL_PLAYING);
     129  ALenum state;
     130
     131  alGetSourcei(this->source, AL_SOURCE_STATE, &state);
     132
     133  return (state == AL_PLAYING);
    156134}
    157135
     
    162140bool OggPlayer::update()
    163141{
    164     int processed;
    165     bool active = true;
    166 
    167     alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
    168 
    169     while(processed--)
    170     {
    171         ALuint buffer;
    172 
    173         alSourceUnqueueBuffers(source, 1, &buffer);
    174         check();
    175 
    176         active = stream(buffer);
    177 
    178         alSourceQueueBuffers(source, 1, &buffer);
    179         check();
    180     }
    181 
    182     return active;
     142  int processed;
     143  bool active = true;
     144
     145  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
     146
     147  while(processed--)
     148  {
     149    ALuint buffer;
     150
     151    alSourceUnqueueBuffers(source, 1, &buffer);
     152    check();
     153
     154    active = stream(buffer);
     155
     156    alSourceQueueBuffers(source, 1, &buffer);
     157    check();
     158  }
     159
     160  return active;
    183161}
    184162
     
    190168bool OggPlayer::stream(ALuint buffer)
    191169{
    192     char pcm[BUFFER_SIZE];
    193     int  size = 0;
    194     int  section;
    195     int  result;
    196 
    197     while(size < BUFFER_SIZE)
    198     {
    199         result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
    200 
    201         if(result > 0)
    202             size += result;
    203         else
    204             if(result < 0)
    205                 throw errorString(result);
    206             else
    207                 break;
    208     }
    209 
    210     if(size == 0)
    211         return false;
    212 
    213     alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
     170  char pcm[BUFFER_SIZE];
     171  int  size = 0;
     172  int  section;
     173  int  result;
     174
     175  while(size < BUFFER_SIZE)
     176  {
     177    result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
     178
     179    if(result > 0)
     180      size += result;
     181    else
     182      if(result < 0)
     183        throw errorString(result);
     184      else
     185        break;
     186  }
     187
     188  if(size == 0)
     189    return false;
     190
     191  alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
     192  check();
     193
     194  return true;
     195}
     196
     197
     198/**
     199 * empties the buffers
     200 */
     201void OggPlayer::empty()
     202{
     203  int queued;
     204
     205  alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
     206
     207  while(queued--)
     208  {
     209    ALuint buffer;
     210
     211    alSourceUnqueueBuffers(source, 1, &buffer);
    214212    check();
    215 
    216     return true;
    217 }
    218 
    219 
    220 /**
    221  * empties the buffers
    222  */
    223 void OggPlayer::empty()
    224 {
    225     int queued;
    226 
    227     alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
    228 
    229     while(queued--)
    230     {
    231         ALuint buffer;
    232 
    233         alSourceUnqueueBuffers(source, 1, &buffer);
    234         check();
    235     }
     213  }
    236214}
    237215
     
    241219void OggPlayer::check()
    242220{
    243         int error = alGetError();
    244 
    245         if(error != AL_NO_ERROR)
    246                 PRINTF(2)("OpenAL error was raised.");
    247 }
     221  int error = alGetError();
     222
     223  if(error != AL_NO_ERROR)
     224    PRINTF(2)("OpenAL error was raised.");
     225}
     226
     227
     228
     229/**
     230 * displays some info about the ogg-file
     231 */
     232void OggPlayer::debug()
     233{
     234  cout
     235      << "version         " << vorbisInfo->version         << "\n"
     236      << "channels        " << vorbisInfo->channels        << "\n"
     237      << "rate (hz)       " << vorbisInfo->rate            << "\n"
     238      << "bitrate upper   " << vorbisInfo->bitrate_upper   << "\n"
     239      << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n"
     240      << "bitrate lower   " << vorbisInfo->bitrate_lower   << "\n"
     241      << "bitrate window  " << vorbisInfo->bitrate_window  << "\n"
     242      << "\n"
     243      << "vendor " << vorbisComment->vendor << "\n";
     244
     245  for(int i = 0; i < vorbisComment->comments; i++)
     246    cout << "   " << vorbisComment->user_comments[i] << "\n";
     247
     248  cout << endl;
     249}
     250
    248251
    249252/**
     
    254257const char* OggPlayer::errorString(int code)
    255258{
    256      switch(code)
    257      {
    258        case OV_EREAD:
    259          return ("Read from media.");
    260        case OV_ENOTVORBIS:
    261          return ("Not Vorbis data.");
    262        case OV_EVERSION:
    263          return ("Vorbis version mismatch.");
    264        case OV_EBADHEADER:
    265          return ("Invalid Vorbis header.");
    266        case OV_EFAULT:
    267          return ("Internal logic fault (bug or heap/stack corruption.");
    268        default:
    269          return ("Unknown Ogg error.");
    270      }
    271 }
     259  switch(code)
     260  {
     261    case OV_EREAD:
     262      return ("Read from media.");
     263    case OV_ENOTVORBIS:
     264      return ("Not Vorbis data.");
     265    case OV_EVERSION:
     266      return ("Vorbis version mismatch.");
     267    case OV_EBADHEADER:
     268      return ("Invalid Vorbis header.");
     269    case OV_EFAULT:
     270      return ("Internal logic fault (bug or heap/stack corruption.");
     271    default:
     272      return ("Unknown Ogg error.");
     273  }
     274}
  • trunk/src/lib/sound/ogg_player.h

    r5282 r6828  
    3131    void open(const char* fileName);
    3232    void release();
    33     void display();
     33    void debug();
    3434    bool playback();
    3535    bool playing();
  • trunk/src/orxonox.cc

    r6695 r6828  
    249249  // SDL_InitSubSystem(SDL_INIT_AUDIO);
    250250  SoundEngine::getInstance()->initAudio();
    251   SoundEngine::getInstance()->allocateSources(1);
     251  SoundEngine::getInstance()->allocateSources(32);
    252252
    253253  SoundEngine::getInstance()->loadSettings(this->iniParser);
  • trunk/src/story_entities/game_world_data.cc

    r6827 r6828  
    318318  PRINTF(3)("Setting Sound Track to %s\n", name);
    319319  this->music = (OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
    320 }
    321 
    322 
     320  if (this->music)
     321    this->music->debug();
     322}
     323
     324
Note: See TracChangeset for help on using the changeset viewer.