Changeset 7293 in orxonox.OLD for trunk/src/lib/sound
- Timestamp:
- Apr 14, 2006, 12:19:55 PM (19 years ago)
- Location:
- trunk/src/lib/sound
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/sound/ogg_player.cc
r7292 r7293 35 35 this->setClassID(CL_SOUND_OGG_PLAYER, "OggPlayer"); 36 36 37 this->trackLoaded = false; 37 this->state = None; 38 38 39 this->source = 0; 39 40 this->buffers[0] = 0; … … 58 59 bool OggPlayer::open(const std::string& fileName) 59 60 { 61 // release old State 62 if (state & FileOpened) 63 release(); 64 65 // allocating Buffers 60 66 if (this->buffers[0] == 0) 61 67 alGenBuffers(2, this->buffers); 62 68 SoundEngine::checkError("Allocating Buffers", __LINE__); 63 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 64 77 if (this->source == 0) 65 78 SoundEngine::getInstance()->popALSource(this->source); 66 if (this->source == 0) 67 { 68 this->trackLoaded = false; 69 return false; 70 } 71 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; 72 89 int result; 73 74 90 if(!(oggFile = fopen(fileName.c_str(), "rb"))) 75 91 { 76 92 PRINTF(2)("Could not open Ogg file."); 77 return false; 78 } 79 93 this->release(); 94 return false; 95 } 96 // reading the Stream. 80 97 if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0) 81 98 { 82 99 PRINTF(2)("Could not open Ogg stream. %s", errorString(result)); 83 100 fclose(oggFile); 84 return false; 85 } 86 101 this->release(); 102 return false; 103 } 104 105 // acquiring the vorbis-properties. 87 106 vorbisInfo = ov_info(&oggStream, -1); 88 107 vorbisComment = ov_comment(&oggStream, -1); 89 108 this->state |= FileOpened; 90 109 if(vorbisInfo->channels == 1) 91 110 format = AL_FORMAT_MONO16; … … 93 112 format = AL_FORMAT_STEREO16; 94 113 114 // setting the Source Properties. 95 115 alSource3f(source, AL_POSITION, 0.0, 0.0, 0.0); 96 116 alSource3f(source, AL_VELOCITY, 0.0, 0.0, 0.0); … … 101 121 SoundEngine::checkError("OggPlayer::SetSourceProperties", __LINE__); 102 122 103 this->trackLoaded = true;104 123 return true; 105 124 } … … 110 129 void OggPlayer::release() 111 130 { 112 if (!this->trackLoaded) 113 return; 114 alSourceStop(source); 115 SoundEngine::checkError("OggPlayer::release()::alSourceStop", __LINE__); 116 empty(); 117 SoundEngine::getInstance()->pushALSource(source); 118 this->source = 0; 119 alDeleteBuffers(2, buffers); 120 SoundEngine::checkError("OggPlayer::release()::alDeleteBuffers", __LINE__); 121 this->buffers[0] = 0; 122 this->buffers[1] = 0; 123 124 ov_clear(&oggStream); 125 this->trackLoaded = false; 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 } 126 160 } 127 161 … … 133 167 bool OggPlayer::playback() 134 168 { 135 if (! this->trackLoaded)169 if (!(this->state & FileOpened)) 136 170 return false; 137 171 … … 148 182 if (DEBUG >= 3) 149 183 SoundEngine::checkError("OggPlayer::playback()::alSourcePlay", __LINE__); 150 printf("%d\n", this->source);184 this->state |= Playing; 151 185 return true; 152 186 } … … 158 192 bool OggPlayer::playing() 159 193 { 160 if (! this->trackLoaded)194 if (!(this->state & FileOpened)) 161 195 return false; 162 196 ALenum state; … … 173 207 bool OggPlayer::update() 174 208 { 175 if (unlikely(! this->trackLoaded))209 if (unlikely(!(this->state & Playing))) 176 210 return false; 177 211 … … 208 242 bool OggPlayer::stream(ALuint buffer) 209 243 { 210 if ( !this->trackLoaded)244 if (unlikely(!(this->state & Playing))) 211 245 return false; 212 246 char pcm[BUFFER_SIZE]; -
trunk/src/lib/sound/ogg_player.h
r7292 r7293 17 17 18 18 struct File; 19 20 19 21 20 … … 42 41 const char* errorString(int code); 43 42 43 public: 44 typedef enum { 45 None = 0x000, 46 FileOpened = 0x100, 47 SourceAllocated = 0x200, 48 BuffersAllocated = 0x400, 49 Stopped = 0x010, 50 Playing = 0x020, 51 Paused = 0x040, 52 Error = 0x001, 53 } State; 54 44 55 private: 45 56 FILE* oggFile; //!< general file-handler, to open the sound-file … … 51 62 ALuint source; //!< The source we play back on 52 63 ALenum format; //!< The format we play back 53 bool trackLoaded; //!< If a Track has been loaded. 64 unsigned int state; 65 //bool trackLoaded; //!< If a Track has been loaded. 54 66 }; 55 67
Note: See TracChangeset
for help on using the changeset viewer.