Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/ogg_player.cc @ 7295

Last change on this file since 7295 was 7295, checked in by bensch, 18 years ago

cool new debug function

File size: 8.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15
16   -------------------------------------------------------------------
17   The source of this file comes stright from http://www.devmaster.net
18   Thanks a lot for the nice work, and the easy portability to our Project.
19*/
20
21#include <iostream>
22
23#include "ogg_player.h"
24
25#include "sound_engine.h"
26
27#include "debug.h"
28
29/**
30 * initializes an Ogg-player from a file
31 * @param fileName the file to load
32 */
33OggPlayer::OggPlayer(const std::string& fileName)
34{
35  this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer");
36
37  this->state = None;
38
39  this->source = 0;
40  this->buffers[0] = 0;
41  this->buffers[1] = 0;
42
43  if (!fileName.empty())
44  {
45    if (this->open(fileName))
46      this->setName(fileName);
47  }
48}
49
50OggPlayer::~OggPlayer()
51{
52  this->release();
53}
54
55/**
56 * opens a file for playback
57 * @param fileName the file to open
58 */
59bool OggPlayer::open(const std::string& fileName)
60{
61  // release old State
62  if (state & FileOpened)
63    release();
64
65  // allocating Buffers
66  if (this->buffers[0] == 0)
67    alGenBuffers(2, this->buffers);
68  SoundEngine::checkError("Allocating Buffers", __LINE__);
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
77  if (this->source == 0)
78    SoundEngine::getInstance()->popALSource(this->source);
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;
89  int result;
90  if(!(oggFile = fopen(fileName.c_str(), "rb")))
91  {
92    PRINTF(2)("Could not open Ogg file.");
93    this->release();
94    return false;
95  }
96  // reading the Stream.
97  if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
98  {
99    PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
100    fclose(oggFile);
101    this->release();
102    return false;
103  }
104
105  // acquiring the vorbis-properties.
106  vorbisInfo = ov_info(&oggStream, -1);
107  vorbisComment = ov_comment(&oggStream, -1);
108  this->state |= FileOpened;
109  if(vorbisInfo->channels == 1)
110    format = AL_FORMAT_MONO16;
111  else
112    format = AL_FORMAT_STEREO16;
113
114  // setting the Source Properties.
115  alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
116  alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
117  alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
118  alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
119  alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE      );
120  alSourcef (source, AL_GAIN,            SoundEngine::getInstance()->getMusicVolume());
121  SoundEngine::checkError("OggPlayer::SetSourceProperties", __LINE__);
122
123  return true;
124}
125
126/**
127 * releases a stream
128 */
129void OggPlayer::release()
130{
131  this->printState();
132  if (this->state & SourceAllocated)
133  {
134    assert(alIsSource(this->source));
135    if (this->state & Playing);
136    {
137      alSourceStop(source);
138      SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__);
139      this->state & !Playing;
140    }
141    empty();
142    SoundEngine::getInstance()->pushALSource(source);
143    this->source = 0;
144    this->state &= !SourceAllocated;
145  }
146  if (this->state & BuffersAllocated)
147  {
148    assert (this->buffers[0] != 0 && this->buffers[1] != 0);
149    alDeleteBuffers(2, buffers);
150    SoundEngine::checkError("OggPlayer::release()::alDeleteBuffers", __LINE__);
151    this->buffers[0] = 0;
152    this->buffers[1] = 0;
153    this->state &= !BuffersAllocated;
154  }
155
156  if (this->state & FileOpened)
157  {
158    ov_clear(&oggStream);
159    this->state &= ! FileOpened;
160  }
161}
162
163
164/**
165 * plays back the sound
166 * @return true if running, false otherwise
167 */
168bool OggPlayer::playback()
169{
170  if (!(this->state & FileOpened))
171    return false;
172
173  if(playing())
174    return true;
175  this->state |= Playing;
176
177  if(!stream(buffers[0]) || !stream(buffers[1]))
178    return false;
179
180  alSourceQueueBuffers(this->source, 2, this->buffers);
181  if (DEBUG >= 3)
182    SoundEngine::checkError("OggPlayer::playback()::alSourceQueueBuffers", __LINE__);
183  alSourcePlay(this->source);
184  if (DEBUG >= 3)
185    SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__);
186  return true;
187}
188
189/**
190 *
191 * @returns true if the file is playing
192 */
193bool OggPlayer::playing()
194{
195  if (!(this->state & FileOpened))
196    return false;
197  ALenum state;
198
199  alGetSourcei(this->source, AL_SOURCE_STATE, &state);
200
201  return (state == AL_PLAYING);
202}
203
204/**
205 * updates the stream, this has to be done every few parts of a second, for sound-consistency
206 * @returns true, if the Sound is playing flawlessly
207 */
208bool OggPlayer::update()
209{
210  if (unlikely(!(this->state & Playing)))
211    return false;
212
213  int processed;
214  bool active = true;
215
216  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
217  if (DEBUG >= 3)
218    SoundEngine::checkError("OggPlayer::update()::alGetSourceI", __LINE__);
219
220  while(processed--)
221  {
222    ALuint buffer;
223
224    alSourceUnqueueBuffers(source, 1, &buffer);
225    if (DEBUG >= 3)
226      SoundEngine::checkError("OggPlayer::update()::unqueue", __LINE__);
227
228    active = stream(buffer);
229
230    alSourceQueueBuffers(source, 1, &buffer);
231    if (DEBUG >= 3)
232      SoundEngine::checkError("OggPlayer::update()::queue", __LINE__);
233  }
234
235  return active;
236}
237
238/**
239 * gets a new Stream from buffer
240 * @param buffer the buffer to get the stream from
241 * @return true, if everything worked as planed
242 */
243bool OggPlayer::stream(ALuint buffer)
244{
245  if (unlikely(!(this->state & Playing)))
246    return false;
247  char pcm[BUFFER_SIZE];
248  int  size = 0;
249  int  section;
250  int  result;
251
252  while(size < BUFFER_SIZE)
253  {
254    result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
255
256    if(result > 0)
257      size += result;
258    else
259      if(result < 0)
260        throw errorString(result);
261      else
262        break;
263  }
264
265  if(size == 0)
266    return false;
267
268  alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
269  if (DEBUG >= 3)
270    SoundEngine::checkError("OggPlayer::playback()::BUFFER", __LINE__);
271
272  return true;
273}
274
275
276/**
277 * empties the buffers
278 */
279void OggPlayer::empty()
280{
281  int queued;
282
283  alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
284
285  while(queued--)
286  {
287    ALuint buffer;
288
289    alSourceUnqueueBuffers(source, 1, &buffer);
290    SoundEngine::checkError("OggPlayer::empty()::unqueue Buffers", __LINE__);
291  }
292}
293
294
295/**
296 * displays some info about the ogg-file
297 */
298void OggPlayer::debug()
299{
300  cout
301  << "version         " << vorbisInfo->version         << "\n"
302  << "channels        " << vorbisInfo->channels        << "\n"
303  << "rate (hz)       " << vorbisInfo->rate            << "\n"
304  << "bitrate upper   " << vorbisInfo->bitrate_upper   << "\n"
305  << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n"
306  << "bitrate lower   " << vorbisInfo->bitrate_lower   << "\n"
307  << "bitrate window  " << vorbisInfo->bitrate_window  << "\n"
308  << "\n"
309  << "vendor " << vorbisComment->vendor << "\n";
310
311  for(int i = 0; i < vorbisComment->comments; i++)
312    cout << "   " << vorbisComment->user_comments[i] << "\n";
313
314  cout << endl;
315}
316
317
318void OggPlayer::printState()
319{
320  PRINTF(0)("OggPlayer is in the following States: ");
321  if (this->state & FileOpened)
322      PRINT(0)("FileOpened ");
323  if (this->state & SourceAllocated)
324    PRINT(0)("SourceAllocated ");
325  if (this->state & BuffersAllocated)
326    PRINT(0)("BuffersAllocated ");
327  if (this->state & Stopped)
328    PRINT(0)("Stopped ");
329  if (this->state & Playing)
330    PRINT(0)("Playing ");
331  if (this->state & Paused)
332    PRINT(0)("Paused ");
333  if (this->state & Error)
334    PRINT(0)("Error ");
335  PRINT(0)("\n");
336}
337
338/**
339 * returns errors
340 * @param code the error-code
341 * @return the error as a String
342 */
343const char* OggPlayer::errorString(int code)
344{
345  switch(code)
346  {
347    case OV_EREAD:
348      return ("Read from media.");
349    case OV_ENOTVORBIS:
350      return ("Not Vorbis data.");
351    case OV_EVERSION:
352      return ("Vorbis version mismatch.");
353    case OV_EBADHEADER:
354      return ("Invalid Vorbis header.");
355    case OV_EFAULT:
356      return ("Internal logic fault (bug or heap/stack corruption.");
357    default:
358      return ("Unknown Ogg error.");
359  }
360}
361
Note: See TracBrowser for help on using the repository browser.