Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/sound/sound_source.cc @ 7341

Last change on this file since 7341 was 7326, checked in by bensch, 19 years ago

orxonox/trunk: limiting the maximum rotation speed of the turbine_hover, as proposed by the network-guys

File size: 6.0 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5386]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[5386]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
[1853]17
[5386]18#include "sound_source.h"
19#include "sound_engine.h"
[5917]20
[5386]21#include "alincl.h"
22#include "compiler.h"
[1853]23
[1856]24using namespace std;
[1853]25
[5386]26/**
[7297]27 * @brief creates a SoundSource at position sourceNode with the SoundBuffer buffer
[5386]28 */
29SoundSource::SoundSource(const PNode* sourceNode, const SoundBuffer* buffer)
30{
31  this->setClassID(CL_SOUND_SOURCE, "SoundSource");
[1856]32
[5386]33  // adding the Source to the SourcesList of the SoundEngine
34  this->buffer = buffer;
35  this->sourceNode = sourceNode;
[7317]36  this->resident = false;
[5386]37
[5930]38  this->sourceID = 0;
39  this->bPlay = false;
[5386]40}
41
[7317]42
[3245]43/**
[7299]44 * @brief construct a SoundSource out of the another soundSource
45 * @param source the Source to create this source from.
46 *
47 * Copies the buffer from source to this Source.
48 * Acquires a new SourceID if source is Playing.
49 */
50SoundSource::SoundSource(const SoundSource& source)
51{
52  this->setClassID(CL_SOUND_SOURCE, "SoundSource");
53
54  // adding the Source to the SourcesList of the SoundEngine
55  this->buffer = source.buffer;
56  this->sourceNode = source.sourceNode;
[7317]57  this->resident = source.resident;
[7299]58
59  this->sourceID = 0;
60  if (source.bPlay == true)
61  {
62    this->bPlay = true;
63    SoundEngine::getInstance()->popALSource(this->sourceID);
64  }
65  else
66    this->bPlay = false;
67}
68
[7317]69
[7299]70/**
71 * @brief paste a copy of the source into this Source.
72 * @param source the SoundSource to paste into this one.
73 * @returns a Reference to this Source.
74 *
75 */
76SoundSource& SoundSource::operator=(const SoundSource& source)
77{
78  this->buffer = source.buffer;
79  this->sourceNode = sourceNode;
[7317]80  this->resident = source.resident;
[7299]81
82  if (source.bPlay)
83    this->play();
84  else
85    this->stop();
86}
87
[7317]88
[7299]89/**
90 * @brief compares two Sources with each other.
91 * @param source the Source to compare against this One.
92 * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
93 * The alSource must not match, because no two Sources can have the same alSource.
94 */
95bool SoundSource::operator==(const SoundSource& source)
96{
97  return (this->buffer == source.buffer &&
98          this->bPlay == source.bPlay &&
99          this->sourceNode == source.sourceNode);
100}
101
[7317]102
[7299]103/**
[7297]104 * @brief deletes a SoundSource
[5386]105 */
106SoundSource::~SoundSource()
[3365]107{
[7291]108  this->stop();
[7317]109  if (this->sourceID != 0)
110    SoundEngine::getInstance()->pushALSource(this->sourceID);
[5386]111}
[4320]112
[7317]113
[5386]114/**
[7297]115 * @brief Plays back a SoundSource
[5386]116 */
117void SoundSource::play()
118{
[7318]119  if (this->buffer && this->retrieveSource())
[7317]120  {
[7318]121    if (this->bPlay)
122      alSourceStop(this->sourceID);
123    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
124      alSourcePlay(this->sourceID);
125
[7317]126    if (DEBUG >= 3)
127      SoundEngine::checkError("Play Source", __LINE__);
128    this->bPlay = true;
129  }
[5386]130}
[4320]131
[7317]132
[5386]133/**
[7317]134 * @brief Plays back buffer on this Source
[5386]135 * @param buffer the buffer to play back on this Source
136 */
137void SoundSource::play(const SoundBuffer* buffer)
138{
[7317]139  if (!this->retrieveSource())
[7291]140  {
[7317]141    PRINTF(2)("No more Free sources (You might consider raising the Source-Count).\n");
142    return;
[7291]143  }
[5930]144
[5386]145  alSourceStop(this->sourceID);
146  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
147  alSourcePlay(this->sourceID);
148
149  if (unlikely(this->buffer != NULL))
150    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
[5930]151  this->bPlay = true;
[7290]152
153  if (DEBUG >= 3)
154    SoundEngine::checkError("Play Source", __LINE__);
[3365]155}
[1853]156
[7317]157
[5386]158/**
[7297]159 * @brief Stops playback of a SoundSource
[5386]160 */
161void SoundSource::stop()
162{
[5930]163  this->bPlay = false;
[7291]164  if (this->sourceID != 0)
165  {
166    alSourceStop(this->sourceID);
167    if (DEBUG >= 3)
168      SoundEngine::checkError("StopSource", __LINE__);
[7297]169    alSourcei(this->sourceID, AL_BUFFER, 0);
[7317]170    if (!this->resident)
171      SoundEngine::getInstance()->pushALSource(this->sourceID);
[7291]172    this->sourceID = 0;
173  }
[5386]174}
[1853]175
[7317]176
[3245]177/**
[7297]178 * @brief Pauses Playback of a SoundSource
[5386]179 */
180void SoundSource::pause()
[3543]181{
[5386]182  alSourcePause(this->sourceID);
[7290]183  if (DEBUG >= 3)
184    SoundEngine::checkError("Pause Source", __LINE__);
[3543]185}
[5386]186
[7317]187
[5386]188/**
[7297]189 * @brief Rewinds Playback of a SoundSource
[5386]190 */
191void SoundSource::rewind()
192{
193  alSourceRewind(this->sourceID);
[7290]194
195  if (DEBUG >= 3)
196    SoundEngine::checkError("Rewind Source", __LINE__);
[5386]197}
198
[7317]199
[5386]200/**
[7297]201 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
[5386]202 * @param rolloffFactor The Factor described
[7297]203 *
204 * this tells openAL how fast the Sounds decay outward from the Source
[5386]205 */
206void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
207{
208  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
[7290]209
210  if (DEBUG >= 3)
211    SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
[5386]212}
213
[7317]214
215/**
216 * @brief sets the Positional this Source should be attached to.
217 * @param sourceNode the Source this is attached to.
218 * If sourceNode == NULL then the Source will be centered, and Audio will be played on all channels.
219 */
220void SoundSource::setSourceNode(const PNode* sourceNode)
221{
222  this->sourceNode = sourceNode;
223}
224
225/**
226 * @brief retrieve a Source.
227 */
228bool SoundSource::retrieveSource()
229{
230  if (this->sourceID != 0)
231    return true;
232  else
233  {
234    SoundEngine::getInstance()->popALSource(this->sourceID);
235    if (this->sourceID != 0)
236    {
237      if (unlikely(this->sourceNode == NULL))
238        resetSource(this->sourceID);
239      return true;
240    }
241  }
242  return false;
243}
244
245
246/**
247 * @brief reset an alSource to its default Values.
248 */
249void SoundSource::resetSource(ALuint sourceID)
250{
251  alSource3f(sourceID, AL_POSITION,        0.0, 0.0, 0.0);
252  alSource3f(sourceID, AL_VELOCITY,        0.0, 0.0, 0.0);
253  alSource3f(sourceID, AL_DIRECTION,       0.0, 0.0, 0.0);
254  alSourcef (sourceID, AL_ROLLOFF_FACTOR,  0.0          );
[7326]255  //alSourcei (sourceID, AL_SOURCE_RELATIVE, AL_TRUE      );
[7317]256  alSourcef (sourceID, AL_GAIN,            SoundEngine::getInstance()->getEffectsVolume());
257}
Note: See TracBrowser for help on using the repository browser.