Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/current_cd/src/lib/sound/ogg_player.cc @ 7238

Last change on this file since 7238 was 6872, checked in by bensch, 19 years ago

trunk: oggPlayer

File size: 6.4 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    fclose(oggFile);
83
84    PRINTF(2)("Could not open Ogg stream. %s", errorString(result));
85    fclose(oggFile);
86    return false;
87  }
88
89  vorbisInfo = ov_info(&oggStream, -1);
90  vorbisComment = ov_comment(&oggStream, -1);
91
92  if(vorbisInfo->channels == 1)
93    format = AL_FORMAT_MONO16;
94  else
95    format = AL_FORMAT_STEREO16;
96
97  alSource3f(source, AL_POSITION,        0.0, 0.0, 0.0);
98  alSource3f(source, AL_VELOCITY,        0.0, 0.0, 0.0);
99  alSource3f(source, AL_DIRECTION,       0.0, 0.0, 0.0);
100  alSourcef (source, AL_ROLLOFF_FACTOR,  0.0          );
101  alSourcei (source, AL_SOURCE_RELATIVE, AL_TRUE      );
102  alSourcef (source, AL_GAIN,            SoundEngine::getInstance()->getMusicVolume());
103
104  this->trackLoaded = true;
105  return true;
106}
107
108/**
109 * releases a stream
110 */
111void OggPlayer::release()
112{
113  if (!this->trackLoaded)
114    return;
115  alSourceStop(source);
116  empty();
117  SoundEngine::getInstance()->pushALSource(source);
118  this->source = 0;
119  check();
120  alDeleteBuffers(2, buffers);
121  this->buffers[0] = 0;
122  this->buffers[1] = 0;
123  check();
124
125  ov_clear(&oggStream);
126  this->trackLoaded = false;
127}
128
129
130/**
131 * plays back the sound
132 * @return true if running, false otherwise
133 */
134bool OggPlayer::playback()
135{
136  if (!this->trackLoaded)
137    return false;
138
139  if(playing())
140    return true;
141
142  if(!stream(buffers[0]))
143    return false;
144
145  if(!stream(buffers[1]))
146    return false;
147
148  alSourceQueueBuffers(source, 2, buffers);
149  alSourcePlay(source);
150
151  return true;
152}
153
154/**
155 *
156 * @returns true if the file is playing
157 */
158bool OggPlayer::playing()
159{
160  if (!this->trackLoaded)
161    return false;
162  ALenum state;
163
164  alGetSourcei(this->source, AL_SOURCE_STATE, &state);
165
166  return (state == AL_PLAYING);
167}
168
169/**
170 * updates the stream, this has to be done every few parts of a second, for sound-consistency
171 * @returns true, if the Sound is playing flawlessly
172 */
173bool OggPlayer::update()
174{
175  if (unlikely(!this->trackLoaded))
176    return false;
177
178  int processed;
179  bool active = true;
180
181  alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
182
183  while(processed--)
184  {
185    ALuint buffer;
186
187    alSourceUnqueueBuffers(source, 1, &buffer);
188    check();
189
190    active = stream(buffer);
191
192    alSourceQueueBuffers(source, 1, &buffer);
193    check();
194  }
195
196  return active;
197}
198
199/**
200 * gets a new Stream from buffer
201 * @param buffer the buffer to get the stream from
202 * @return true, if everything worked as planed
203 */
204bool OggPlayer::stream(ALuint buffer)
205{
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.