Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/sound_engine: sound gets enabled

File size: 3.1 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
23using namespace std;
24
25/**
26   \brief standard constructor
27*/
28SoundEngine::SoundEngine () 
29{
30   this->setClassName ("SoundEngine");
31
32   // preInit
33   this->isInit = false;
34   this->frequency = SOUND_DEFAULT_FREQUENCY;
35   this->format = MIX_DEFAULT_FORMAT;
36   this->channels = SOUND_DEFAULT_CHANNELS;
37   this->buffers = SOUND_DEFAULT_BUFIZE;
38
39   this->enableSound();
40}
41
42/**
43   \brief the singleton reference to this class
44*/
45SoundEngine* SoundEngine::singletonRef = NULL;
46
47/**
48   \returns a Pointer to this Class
49*/
50SoundEngine* SoundEngine::getInstance(void)
51{
52  if (!SoundEngine::singletonRef)
53    SoundEngine::singletonRef = new SoundEngine();
54  return SoundEngine::singletonRef;
55}
56
57/**
58   \brief standard deconstructor
59
60*/
61SoundEngine::~SoundEngine () 
62{
63  SoundEngine::singletonRef = NULL;
64 
65  this->disableSound();
66}
67
68
69/**
70   \brief enables the SDL_mixer library
71*/
72void SoundEngine::enableSound(void)
73{
74  if (!this->isInit)
75    {
76      if (SDL_Init(SDL_INIT_AUDIO) == -1)
77        {
78          PRINTF(1)("SDL-sound could not be initialized\nSDL_Init: %s\n", SDL_GetError());
79          return;
80        }
81      else
82        {
83          this->isInit = true;
84         
85          // checking if the mode is supported
86          Mix_QuerySpec(&this->frequency, &this->format, &this->channels);
87         
88          this->bits = this->format & 0xFF;
89          PRINTF(3)("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n",
90                    this->frequency, this->bits, this->channels > 1 ? "stereo" : "mono", this->buffers); 
91          Mix_VolumeMusic(this->volume);
92         
93          if(Mix_OpenAudio(this->frequency, this->format, this->channels, this->buffers) == -1)
94            PRINTF(1)("Mix_OpenAudio: %s\n", Mix_GetError());
95        }
96 
97
98    }
99}
100
101/**
102   \brief disabled SDL_mixer
103*/
104void SoundEngine::disableSound(void)
105{
106  if (this->isInit)
107    {
108      Mix_CloseAudio();
109    }
110  else
111    PRINTF(4)("SDL_mixer was not initialized.\n");
112}
113
114/**
115   \brief checks if the compiled version and the local version of SDL_mixer match.
116   \returns true if match, false otherwise
117*/
118bool SoundEngine::checkVersion(void)
119{
120  SDL_version compile_version;
121  SDL_version link_version;
122  MIX_VERSION(&compile_version);
123  link_version = *Mix_Linked_Version();
124
125  if (compile_version.major == link_version.major &&
126      compile_version.minor == link_version.minor &&
127      compile_version.patch == link_version.patch)
128    {
129      return true;
130    }
131  else
132    {
133      PRINTF(2)("compiled with SDL_mixer version: %d.%d.%d\n", 
134                compile_version.major,
135                compile_version.minor,
136                compile_version.patch);
137     
138      PRINTF(2)("running with SDL_mixer version: %d.%d.%d\n", 
139                link_version.major,
140                link_version.minor,
141                link_version.patch);
142      return false;
143    }
144}
Note: See TracBrowser for help on using the repository browser.