Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

minor

File size: 8.1 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  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  }
160}
161
162
163/**
164 * plays back the sound
165 * @return true if running, false otherwise
166 */
167bool OggPlayer::playback()
168{
169  if (!(this->state & FileOpened))
170    return false;
171
172  if(playing())
173    return true;
174  this->state |= Playing;
175
176  if(!stream(buffers[0]) || !stream(buffers[1]))
177    return false;
178
179  alSourceQueueBuffers(this->source, 2, this->buffers);
180  if (DEBUG >= 3)
181    SoundEngine::checkError("OggPlayer::playback()::alSourceQueueBuffers", __LINE__);
182  alSourcePlay(this->source);
183  if (DEBUG >= 3)
184    SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__);
185  return true;
186}
187
188/**
189 *
190 * @returns true if the file is playing
191 */
192bool OggPlayer::playing()
193{
194  if (!(this->state & FileOpened))
195    return false;
196  ALenum state;
197
198  alGetSourcei(this->source, AL_SOURCE_STATE, &state);
199
200  return (state == AL_PLAYING);
201}
202
203/**
204 * updates the stream, this has to be done every few parts of a second, for sound-consistency
205 * @returns true, if the Sound is playing flawlessly
206 */
207bool OggPlayer::update()
208{
209  if (unlikely(!(this->state & Playing)))
210    return false;
211
212  int processed;
213  bool active = true;
214
215  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
216  if (DEBUG >= 3)
217    SoundEngine::checkError("OggPlayer::update()::alGetSourceI", __LINE__);
218
219  while(processed--)
220  {
221    ALuint buffer;
222
223    alSourceUnqueueBuffers(source, 1, &buffer);
224    if (DEBUG >= 3)
225      SoundEngine::checkError("OggPlayer::update()::unqueue", __LINE__);
226
227    active = stream(buffer);
228
229    alSourceQueueBuffers(source, 1, &buffer);
230    if (DEBUG >= 3)
231      SoundEngine::checkError("OggPlayer::update()::queue", __LINE__);
232  }
233
234  return active;
235}
236
237/**
238 * gets a new Stream from buffer
239 * @param buffer the buffer to get the stream from
240 * @return true, if everything worked as planed
241 */
242bool OggPlayer::stream(ALuint buffer)
243{
244  if (unlikely(!(this->state & Playing)))
245    return false;
246  char pcm[BUFFER_SIZE];
247  int  size = 0;
248  int  section;
249  int  result;
250
251  while(size < BUFFER_SIZE)
252  {
253    result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
254
255    if(result > 0)
256      size += result;
257    else
258      if(result < 0)
259        throw errorString(result);
260      else
261        break;
262  }
263
264  if(size == 0)
265    return false;
266
267  alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
268  if (DEBUG >= 3)
269    SoundEngine::checkError("OggPlayer::playback()::BUFFER", __LINE__);
270
271  return true;
272}
273
274
275/**
276 * empties the buffers
277 */
278void OggPlayer::empty()
279{
280  int queued;
281
282  alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
283
284  while(queued--)
285  {
286    ALuint buffer;
287
288    alSourceUnqueueBuffers(source, 1, &buffer);
289    SoundEngine::checkError("OggPlayer::empty()::unqueue Buffers", __LINE__);
290  }
291}
292
293
294/**
295 * displays some info about the ogg-file
296 */
297void OggPlayer::debug()
298{
299  cout
300  << "version         " << vorbisInfo->version         << "\n"
301  << "channels        " << vorbisInfo->channels        << "\n"
302  << "rate (hz)       " << vorbisInfo->rate            << "\n"
303  << "bitrate upper   " << vorbisInfo->bitrate_upper   << "\n"
304  << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n"
305  << "bitrate lower   " << vorbisInfo->bitrate_lower   << "\n"
306  << "bitrate window  " << vorbisInfo->bitrate_window  << "\n"
307  << "\n"
308  << "vendor " << vorbisComment->vendor << "\n";
309
310  for(int i = 0; i < vorbisComment->comments; i++)
311    cout << "   " << vorbisComment->user_comments[i] << "\n";
312
313  cout << endl;
314}
315
316
317/**
318 * returns errors
319 * @param code the error-code
320 * @return the error as a String
321 */
322const char* OggPlayer::errorString(int code)
323{
324  switch(code)
325  {
326    case OV_EREAD:
327      return ("Read from media.");
328    case OV_ENOTVORBIS:
329      return ("Not Vorbis data.");
330    case OV_EVERSION:
331      return ("Vorbis version mismatch.");
332    case OV_EBADHEADER:
333      return ("Invalid Vorbis header.");
334    case OV_EFAULT:
335      return ("Internal logic fault (bug or heap/stack corruption.");
336    default:
337      return ("Unknown Ogg error.");
338  }
339}
Note: See TracBrowser for help on using the repository browser.