Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound_engine/src/lib/sound/sound_engine.cc @ 3890

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

orxonox/branches/sound_engine: loading unloading should work

File size: 5.9 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   Some code was taken from Simon's first implementation of the SoundEngine.
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SOUND
19
20#include "sound_engine.h"
21#include <SDL_mixer.h>
22#include "compiler.h"
23
24using namespace std;
25
26
27/*----------------
28  SOUND AND MUSIC
29  ---------------*/
30
31Sound::Sound(const char* fileName, SOUND_TYPE soundType)
32{
33  // checking if the sound-format is known
34  if (soundType == SOUND_NONE)
35    {
36      if (likely(!strncmp(fileName+(strlen(fileName)-4), ".wav", 4)) ||
37          unlikely(!strncmp(fileName+(strlen(fileName)-4), ".voc", 4)) ||
38          unlikely(!strncmp(fileName+(strlen(fileName)-5), ".aiff", 5)) ||
39          unlikely(!strncmp(fileName+(strlen(fileName)-5), ".riff", 5)))
40        this->soundType = SOUND_WAV;
41      else if (likely(!strncmp(fileName+(strlen(fileName)-4), ".ogg", 4)))
42        this->soundType = SOUND_OGG;
43      else if (likely(!strncmp(fileName+(strlen(fileName)-4), ".mp3", 4)))
44        this->soundType = SOUND_MP3;
45      else if (unlikely(!strncmp(fileName+(strlen(fileName)-4), ".mod", 4)) ||
46               unlikely(!strncmp(fileName+(strlen(fileName)-4), ".s3m", 4)) ||
47               unlikely(!strncmp(fileName+(strlen(fileName)-3), ".it", 3)) ||
48               unlikely(!strncmp(fileName+(strlen(fileName)-3), ".xm", 3)))
49        this->soundType = SOUND_MOD;
50      else if (likely(!strncmp(fileName+(strlen(fileName)-4), ".mid", 4)))
51        this->soundType = SOUND_MID;
52    }
53}
54
55Sound::~Sound()
56{
57
58}
59
60SoundEffect::SoundEffect(const char* fileName, SOUND_TYPE soundType) : Sound(fileName, soundType)
61{
62  // checking how to load
63  this->effect = NULL;
64  if (likely(SoundEngine::isInit))
65    {
66      if (this->soundType == SOUND_WAV ||
67          this->soundType == SOUND_MOD ||
68          this->soundType == SOUND_OGG)
69        this->effect = Mix_LoadWAV(fileName);
70      else
71        PRINTF(2)("%s cannot be loaded as SoundEffect. Wrong Type\n", fileName);
72    }
73  else
74    PRINTF(2)("SoundEngine not yet started. not loading SoundEffect %s\n", fileName);
75}
76
77SoundEffect::~SoundEffect(void)
78{
79  // deleting the effects-chunk
80  Mix_FreeChunk(this->effect);
81}
82
83Music::Music(const char* fileName, SOUND_TYPE soundType) : Sound(fileName, soundType)
84{
85  this->music = NULL;
86  if (likely(SoundEngine::isInit))
87    {
88      if (this->soundType == SOUND_WAV ||
89          this->soundType == SOUND_MOD ||
90          this->soundType == SOUND_OGG ||
91          this->soundType == SOUND_MP3 ||
92          this->soundType == SOUND_MID )
93        this->music = Mix_LoadMUS(fileName);
94      else
95        PRINTF(2)("%s cannot be loaded as Music. Wrong Type\n", fileName);
96    }
97  else
98    PRINTF(2)("SoundEngine not yet started. not loading Music %s\n", fileName);
99}
100
101Music::~Music()
102{
103  Mix_FreeMusic(this->music);
104}
105
106/*----------------------
107 STARTING AND UNLOADING
108  ----------------------*/
109/**
110   \brief standard constructor
111*/
112SoundEngine::SoundEngine () 
113{
114   this->setClassName ("SoundEngine");
115
116   // preInit
117   this->frequency = SOUND_DEFAULT_FREQUENCY;
118   this->format = MIX_DEFAULT_FORMAT;
119   this->channels = SOUND_DEFAULT_CHANNELS;
120   this->buffers = SOUND_DEFAULT_BUFIZE;
121
122   this->enableSound();
123}
124
125/**
126   \brief the singleton reference to this class
127*/
128SoundEngine* SoundEngine::singletonRef = NULL;
129
130/**
131   \returns a Pointer to this Class
132*/
133SoundEngine* SoundEngine::getInstance(void)
134{
135  if (!SoundEngine::singletonRef)
136    SoundEngine::singletonRef = new SoundEngine();
137  return SoundEngine::singletonRef;
138}
139
140/**
141   \brief standard deconstructor
142
143*/
144SoundEngine::~SoundEngine () 
145{
146  SoundEngine::singletonRef = NULL;
147 
148  this->disableSound();
149}
150
151bool SoundEngine::isInit = false;
152bool SoundEngine::enabled = false;
153
154
155
156/**
157   \brief enables the SDL_mixer library
158*/
159void SoundEngine::enableSound(void)
160{
161  if (!this->isInit)
162    {
163      if (SDL_Init(SDL_INIT_AUDIO) == -1)
164        {
165          PRINTF(1)("SDL-sound could not be initialized\nSDL_Init: %s\n", SDL_GetError());
166          return;
167        }
168      else
169        {
170          this->isInit = true;
171         
172          // checking if the mode is supported
173          Mix_QuerySpec(&this->frequency, &this->format, &this->channels);
174         
175          this->bits = this->format & 0xFF;
176          PRINTF(3)("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n",
177                    this->frequency, this->bits, this->channels > 1 ? "stereo" : "mono", this->buffers); 
178          Mix_VolumeMusic(this->volume);
179         
180          if(Mix_OpenAudio(this->frequency, this->format, this->channels, this->buffers) == -1)
181            PRINTF(1)("Mix_OpenAudio: %s\n", Mix_GetError());
182        }
183    }
184}
185
186/**
187   \brief disabled SDL_mixer
188*/
189void SoundEngine::disableSound(void)
190{
191  if (this->isInit)
192    {
193      Mix_CloseAudio();
194    }
195  else
196    PRINTF(4)("SDL_mixer was not initialized.\n");
197}
198
199/**
200   \brief checks if the compiled version and the local version of SDL_mixer match.
201   \returns true if match, false otherwise
202*/
203bool SoundEngine::checkVersion(void)
204{
205  SDL_version compile_version;
206  SDL_version link_version;
207  MIX_VERSION(&compile_version);
208  link_version = *Mix_Linked_Version();
209
210  if (compile_version.major == link_version.major &&
211      compile_version.minor == link_version.minor &&
212      compile_version.patch == link_version.patch)
213    {
214      return true;
215    }
216  else
217    {
218      PRINTF(2)("compiled with SDL_mixer version: %d.%d.%d\n", 
219                compile_version.major,
220                compile_version.minor,
221                compile_version.patch);
222     
223      PRINTF(2)("running with SDL_mixer version: %d.%d.%d\n", 
224                link_version.major,
225                link_version.minor,
226                link_version.patch);
227      return false;
228    }
229}
230
231
232/*-------------
233  FUNCTIONALITY
234  -------------*/
235
236/**
237   \param numberOfChannels sets the number of Channels to use.
238*/
239void SoundEngine::setNumberOfChannels(unsigned int numberOfChannels) 
240{
241  this->numberOfChannels = numberOfChannels;
242  Mix_AllocateChannels(numberOfChannels);
243}
Note: See TracBrowser for help on using the repository browser.