Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some nice Class-Stuff

File size: 4.6 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;
36
[5930]37  this->sourceID = 0;
38  this->bPlay = false;
[5386]39}
40
[3245]41/**
[7299]42 * @brief construct a SoundSource out of the another soundSource
43 * @param source the Source to create this source from.
44 *
45 * Copies the buffer from source to this Source.
46 * Acquires a new SourceID if source is Playing.
47 */
48SoundSource::SoundSource(const SoundSource& source)
49{
50  this->setClassID(CL_SOUND_SOURCE, "SoundSource");
51
52  // adding the Source to the SourcesList of the SoundEngine
53  this->buffer = source.buffer;
54  this->sourceNode = source.sourceNode;
55
56  this->sourceID = 0;
57  if (source.bPlay == true)
58  {
59    this->bPlay = true;
60    SoundEngine::getInstance()->popALSource(this->sourceID);
61  }
62  else
63    this->bPlay = false;
64}
65
66/**
67 * @brief paste a copy of the source into this Source.
68 * @param source the SoundSource to paste into this one.
69 * @returns a Reference to this Source.
70 *
71 */
72SoundSource& SoundSource::operator=(const SoundSource& source)
73{
74  this->buffer = source.buffer;
75  this->sourceNode = sourceNode;
76
77  if (source.bPlay)
78    this->play();
79  else
80    this->stop();
81}
82
83/**
84 * @brief compares two Sources with each other.
85 * @param source the Source to compare against this One.
86 * Two Sources are the same, if the PNodes match, and the Sound Played are the same.
87 * The alSource must not match, because no two Sources can have the same alSource.
88 */
89bool SoundSource::operator==(const SoundSource& source)
90{
91  return (this->buffer == source.buffer &&
92          this->bPlay == source.bPlay &&
93          this->sourceNode == source.sourceNode);
94}
95
96/**
[7297]97 * @brief deletes a SoundSource
[5386]98 */
99SoundSource::~SoundSource()
[3365]100{
[7291]101  this->stop();
[5386]102}
[4320]103
[5386]104/**
[7297]105 * @brief Plays back a SoundSource
[5386]106 */
107void SoundSource::play()
108{
[5930]109  if (this->sourceID == 0)
110    SoundEngine::getInstance()->popALSource(this->sourceID);
[5386]111  alSourcePlay(this->sourceID);
[7290]112  if (DEBUG >= 3)
113    SoundEngine::checkError("Play Source", __LINE__);
[5930]114  this->bPlay = true;
[5386]115}
[4320]116
[5386]117/**
118 * Plays back buffer on this Source
119 * @param buffer the buffer to play back on this Source
120 */
121void SoundSource::play(const SoundBuffer* buffer)
122{
[5930]123  if (unlikely(this->sourceID == 0))
[7291]124  {
[5930]125    SoundEngine::getInstance()->popALSource(this->sourceID);
[7291]126    if (sourceID == 0)
127    {
128      PRINTF(2)("No more Free source\n");
129      return;
130    }
131  }
[7299]132  //  assert (this->sourceID != 0);
[5930]133
[5386]134  alSourceStop(this->sourceID);
135  alSourcei (this->sourceID, AL_BUFFER, buffer->getID());
136  alSourcePlay(this->sourceID);
137
138  if (unlikely(this->buffer != NULL))
139    alSourcei (this->sourceID, AL_BUFFER, this->buffer->getID());
[5930]140  this->bPlay = true;
[7290]141
142  if (DEBUG >= 3)
143    SoundEngine::checkError("Play Source", __LINE__);
[3365]144}
[1853]145
[5386]146/**
[7297]147 * @brief Stops playback of a SoundSource
[5386]148 */
149void SoundSource::stop()
150{
[5930]151  this->bPlay = false;
[7291]152  if (this->sourceID != 0)
153  {
154    alSourceStop(this->sourceID);
155    if (DEBUG >= 3)
156      SoundEngine::checkError("StopSource", __LINE__);
[7297]157    alSourcei(this->sourceID, AL_BUFFER, 0);
[7291]158    SoundEngine::getInstance()->pushALSource(this->sourceID);
159    this->sourceID = 0;
160  }
[5386]161}
[1853]162
[3245]163/**
[7297]164 * @brief Pauses Playback of a SoundSource
[5386]165 */
166void SoundSource::pause()
[3543]167{
[5386]168  alSourcePause(this->sourceID);
[7290]169  if (DEBUG >= 3)
170    SoundEngine::checkError("Pause Source", __LINE__);
[3543]171}
[5386]172
173/**
[7297]174 * @brief Rewinds Playback of a SoundSource
[5386]175 */
176void SoundSource::rewind()
177{
178  alSourceRewind(this->sourceID);
[7290]179
180  if (DEBUG >= 3)
181    SoundEngine::checkError("Rewind Source", __LINE__);
[5386]182}
183
184/**
[7297]185 * @brief sets the RolloffFactor of the Sound emitted from the SoundSource
[5386]186 * @param rolloffFactor The Factor described
[7297]187 *
188 * this tells openAL how fast the Sounds decay outward from the Source
[5386]189 */
190void SoundSource::setRolloffFactor(ALfloat rolloffFactor)
191{
192  alSourcef(this->sourceID, AL_ROLLOFF_FACTOR, rolloffFactor);
[7290]193
194  if (DEBUG >= 3)
195    SoundEngine::checkError("Set Source Rolloff-factor", __LINE__);
[5386]196}
197
Note: See TracBrowser for help on using the repository browser.