Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/lib/sound/ogg_player.cc @ 7168

Last change on this file since 7168 was 6987, checked in by bensch, 19 years ago

trunk: some minor fixes

File size: 6.5 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 char* fileName)
34{
35  this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer");
36
37  this->trackLoaded = false;
38  this->source = 0;
39  this->buffers[0] = 0;
40  this->buffers[1] = 0;
41
42  if (fileName != NULL)
43  {
44    if (this->open(fileName))
45      this->setName(fileName);
46  }
47}
48
49OggPlayer::~OggPlayer()
50{
51  //this->release();
52}
53
54/**
55 * opens a file for playback
56 * @param fileName the file to open
57 */
58bool OggPlayer::open(const char* fileName)
59{
60  if (this->buffers[0] == 0)
61    alGenBuffers(2, this->buffers);
62  SoundEngine::checkError("Allocating Buffers", __LINE__);
63
64  if (this->source == 0)
65    SoundEngine::getInstance()->popALSource(this->source);
66  if (this->source == 0)
67  {
68    this->trackLoaded = false;
69    return false;
70  }
71
72  int result;
73
74  if(!(oggFile = fopen(fileName, "rb")))
75  {
76    PRINTF(2)("Could not open Ogg file.");
77    return false;
78  }
79
80  if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
81  {
82    PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
83    fclose(oggFile);
84    return false;
85  }
86
87  vorbisInfo = ov_info(&oggStream, -1);
88  vorbisComment = ov_comment(&oggStream, -1);
89
90  if(vorbisInfo->channels == 1)
91    format = AL_FORMAT_MONO16;
92  else
93    format = AL_FORMAT_STEREO16;
94
95  alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
96  alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
97  alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
98  alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
99  alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE      );
100  alSourcef (source, AL_GAIN,            SoundEngine::getInstance()->getMusicVolume());
101
102  this->trackLoaded = true;
103  return true;
104}
105
106/**
107 * releases a stream
108 */
109void OggPlayer::release()
110{
111  if (!this->trackLoaded)
112    return;
113  alSourceStop(source);
114  empty();
115  SoundEngine::getInstance()->pushALSource(source);
116  this->source = 0;
117  check();
118  alDeleteBuffers(2, buffers);
119  this->buffers[0] = 0;
120  this->buffers[1] = 0;
121  check();
122
123  ov_clear(&oggStream);
124  this->trackLoaded = false;
125}
126
127
128/**
129 * plays back the sound
130 * @return true if running, false otherwise
131 */
132bool OggPlayer::playback()
133{
134  if (!this->trackLoaded)
135    return false;
136
137  if(playing())
138    return true;
139
140  if(!stream(buffers[0]))
141    return false;
142
143  if(!stream(buffers[1]))
144    return false;
145
146  alSourceQueueBuffers(source, 2, buffers);
147  alSourcePlay(source);
148
149  return true;
150}
151
152/**
153 *
154 * @returns true if the file is playing
155 */
156bool OggPlayer::playing()
157{
158  if (!this->trackLoaded)
159    return false;
160  ALenum state;
161
162  alGetSourcei(this->source, AL_SOURCE_STATE, &state);
163
164  return (state == AL_PLAYING);
165}
166
167/**
168 * updates the stream, this has to be done every few parts of a second, for sound-consistency
169 * @returns true, if the Sound is playing flawlessly
170 */
171bool OggPlayer::update()
172{
173  if (unlikely(!this->trackLoaded))
174    return false;
175
176  int processed;
177  bool active = true;
178
179  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
180
181  while(processed--)
182  {
183    ALuint buffer;
184
185    alSourceUnqueueBuffers(source, 1, &buffer);
186    check();
187
188    active = stream(buffer);
189
190    alSourceQueueBuffers(source, 1, &buffer);
191    check();
192  }
193
194  return active;
195}
196
197/**
198 * gets a new Stream from buffer
199 * @param buffer the buffer to get the stream from
200 * @return true, if everything worked as planed
201 */
202bool OggPlayer::stream(ALuint buffer)
203{
204  if (!this->trackLoaded)
205    return false;
206  char pcm[BUFFER_SIZE];
207  int  size = 0;
208  int  section;
209  int  result;
210
211  while(size < BUFFER_SIZE)
212  {
213    result = ov_read(&oggStream, pcm + size, BUFFER_SIZE - size, 0, 2, 1, &section);
214
215    if(result > 0)
216      size += result;
217    else
218      if(result < 0)
219        throw errorString(result);
220      else
221        break;
222  }
223
224  if(size == 0)
225    return false;
226
227  alBufferData(buffer, format, pcm, size, vorbisInfo->rate);
228  check();
229
230  return true;
231}
232
233
234/**
235 * empties the buffers
236 */
237void OggPlayer::empty()
238{
239  int queued;
240
241  alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
242
243  while(queued--)
244  {
245    ALuint buffer;
246
247    alSourceUnqueueBuffers(source, 1, &buffer);
248    check();
249  }
250}
251
252
253/**
254 * displays some info about the ogg-file
255 */
256void OggPlayer::debug()
257{
258  cout
259  << "version         " << vorbisInfo->version         << "\n"
260  << "channels        " << vorbisInfo->channels        << "\n"
261  << "rate (hz)       " << vorbisInfo->rate            << "\n"
262  << "bitrate upper   " << vorbisInfo->bitrate_upper   << "\n"
263  << "bitrate nominal " << vorbisInfo->bitrate_nominal << "\n"
264  << "bitrate lower   " << vorbisInfo->bitrate_lower   << "\n"
265  << "bitrate window  " << vorbisInfo->bitrate_window  << "\n"
266  << "\n"
267  << "vendor " << vorbisComment->vendor << "\n";
268
269  for(int i = 0; i < vorbisComment->comments; i++)
270    cout << "   " << vorbisComment->user_comments[i] << "\n";
271
272  cout << endl;
273}
274
275/**
276 * checks for errors
277 */
278void OggPlayer::check()
279{
280  int error = alGetError();
281  if(error != AL_NO_ERROR)
282    PRINTF(2)("OpenAL error was raised.");
283}
284
285/**
286 * returns errors
287 * @param code the error-code
288 * @return the error as a String
289 */
290const char* OggPlayer::errorString(int code)
291{
292  switch(code)
293  {
294    case OV_EREAD:
295      return ("Read from media.");
296    case OV_ENOTVORBIS:
297      return ("Not Vorbis data.");
298    case OV_EVERSION:
299      return ("Vorbis version mismatch.");
300    case OV_EBADHEADER:
301      return ("Invalid Vorbis header.");
302    case OV_EFAULT:
303      return ("Internal logic fault (bug or heap/stack corruption.");
304    default:
305      return ("Unknown Ogg error.");
306  }
307}
Note: See TracBrowser for help on using the repository browser.