Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound/src/sound_control.cc @ 2838

Last change on this file since 2838 was 2838, checked in by simon, 20 years ago

branches/sound: instance can now be freed via static deleteInstance()

  • Property svn:executable set to *
File size: 5.7 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: Simon Hofmann
13   co-programmer:
14*/
15
16#include "sound_control.h"
17#include <string.h> 
18#include "SDL_mixer.h"
19
20using namespace std;
21
22// global variables
23
24SoundControl* SoundControl::instance = 0; // singleton reference
25int volume = SDL_MIX_MAXVOLUME; // volume
26int done = 0; //
27int track_number = 1;
28Mix_Music* music = NULL; 
29
30/**
31    \brief standard constructor
32   
33    This constructor builds a SoundControl Object, which waits for callers.
34    All sound output is handled by this singleton object.
35*/ 
36SoundControl::SoundControl () { 
37 
38  //setup parameters
39  int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 8192, bits = 0; 
40  Uint16 audio_format = MIX_DEFAULT_FORMAT; 
41
42  //initializing sound and calling Mix_OpenAudio
43  if(SDL_Init(SDL_INIT_AUDIO)<0){ 
44    printf("SDL_Init: \n"); 
45    exit(1); 
46  } 
47 
48  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ 
49    printf("Mix_OpenAudio: \n"); 
50    exit(1); 
51  } 
52 
53  initialise();
54}
55
56
57/**
58    \brief Destructor for the singleton instance
59*/ 
60SoundControl::~SoundControl () { 
61  delete instance;
62  instance = 0;
63}
64
65/**
66   \brief Returns a reference to the singleton
67*/
68SoundControl* SoundControl::getInstance() {
69  if (instance == 0) {
70    instance = new SoundControl;
71  }
72  return instance;
73}
74
75void SoundControl::deleteInstance() {
76  ~SoundControl();
77}
78
79/**
80    \brief Is called by SoundControl object to initiate all values
81*/ 
82void SoundControl::initialise() { 
83 
84  // Print some info
85  Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 
86  bits=audio_format&0xFF; 
87  printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); 
88  Mix_VolumeMusic(volume);
89} 
90
91
92/**
93    \brief Sets the number of output Channels (should not be used)
94*/ 
95void SoundControl::setNumberOfChannels (int number_of_channels) { 
96  Mix_AllocateChannels(number_of_channels);
97} 
98
99
100/**
101    \brief May be called from any WorldEntity to play a .xm file
102    \param filename: self-explanatory
103*/ 
104int SoundControl::playMod (char* fileName) {
105  Mix_Chunk* chunk = NULL; 
106  chunk = Mix_LoadWAV(fileName); 
107  if(Mix_PlayChannel(-1, chunk, 0) == -1) {
108    printf("Mix_PlayChannel: %s\n", Mix_GetError());
109  }
110}
111
112
113/**
114    \brief May be called from any WorldEntity to play a .wav file
115    \param filename: self-explanatory
116*/ 
117int SoundControl::playWav (char* fileName) {
118  Mix_Chunk* chunk = NULL; 
119  chunk = Mix_LoadWAV(fileName); 
120  if(Mix_PlayChannel(-1, chunk, 0) == -1) {
121    printf("Mix_PlayChannel: %s\n", Mix_GetError());
122  }
123} 
124
125
126/**
127    \brief May be called from any WorldEntity to play a .ogg file
128    \param filename: self-explanatory
129*/ 
130int SoundControl::playOgg (char* fileName) {
131  Mix_Music* music = NULL;
132  music = Mix_LoadMUS(fileName);
133  if(Mix_PlayMusic(music, 1) == -1){
134    printf("Mix_PlayMusic: %s\n",Mix_GetError());
135  }
136  Mix_HookMusicFinished(musicDone);
137} 
138
139
140/**
141    \brief Heightens the overall volume of output
142*/ 
143void SoundControl::volumeUp () { 
144  volume = (volume + 1) << 1; 
145  if(volume > SDL_MIX_MAXVOLUME) 
146    volume = SDL_MIX_MAXVOLUME; 
147  Mix_VolumeMusic(volume); 
148} 
149
150
151/**
152    \brief Lowers the overall volume of output
153*/ 
154void SoundControl::volumeDown () { 
155  volume >>= 1; 
156  Mix_VolumeMusic(volume); 
157} 
158
159
160/**
161    \brief Rewinds music to the beginning
162*/ 
163void SoundControl::trackRewind () { 
164  Mix_RewindMusic(); 
165} 
166
167
168/**
169    \brief Rewinds the music 5 seconds
170*/ 
171void SoundControl::forwardMusic () { 
172  Mix_SetMusicPosition(+5); 
173} 
174
175
176/**
177    \brief Forwards the music 5 seconds
178*/ 
179void SoundControl::rewindMusic () { 
180  Mix_SetMusicPosition(-5); 
181} 
182
183
184/**
185    \brief Pauses music output
186*/ 
187void SoundControl::pauseMusic () {
188  Mix_PauseMusic()
189} 
190
191
192/**
193    \brief this function pauses music output
194*/ 
195void SoundControl::resumeMusic () {
196  Mix_ResumeMusic()
197} 
198
199/**
200    \brief Selects the track of all orxonox tracks
201*/
202void SoundControl::trackSelect () { 
203  switch (track_number) { 
204  case 1:
205    music = Mix_LoadMUS("luke_grey_orxonox1.ogg"); 
206    if(Mix_PlayMusic(music, 1) == -1){
207      printf("Mix_PlayMusic: %s\n",Mix_GetError());
208    }
209    Mix_HookMusicFinished(musicDone);
210    break; 
211  case 2: 
212    music = Mix_LoadMUS("luke_grey_orxonox2.ogg");
213    if(Mix_PlayMusic(music, 1) == -1){
214      printf("Mix_PlayMusic: %s\n",Mix_GetError());
215    }
216    Mix_HookMusicFinished(musicDone);
217    break; 
218  case 3: 
219    music = Mix_LoadMUS("luke_grey_orxonox3.ogg"); 
220    if(Mix_PlayMusic(music, 1) == -1){
221    printf("Mix_PlayMusic: %s\n",Mix_GetError());
222  }
223  Mix_HookMusicFinished(musicDone);   
224  break; 
225  case 4: 
226    music = Mix_LoadMUS("luke_grey_and_aquarius_orxonox.ogg"); 
227    if(Mix_PlayMusic(music, 1) == -1){
228      printf("Mix_PlayMusic: %s\n",Mix_GetError());
229    }
230    Mix_HookMusicFinished(musicDone);   
231    break; 
232  case 5: 
233    music = Mix_LoadMUS("nomenes_orxonox.ogg"); 
234    if(Mix_PlayMusic(music, 1) == -1){
235      printf("Mix_PlayMusic: %s\n",Mix_GetError());
236    }
237    Mix_HookMusicFinished(musicDone);   
238    break; 
239  case 6:
240    music = Mix_LoadMUS("nomenes_funkadudu.ogg");
241  if(Mix_PlayMusic(music, 1) == -1){
242    printf("Mix_PlayMusic: %s\n",Mix_GetError());
243  }
244  Mix_HookMusicFinished(musicDone);   
245break;
246  } 
247}
248
249
250/**
251    \brief Hooked by playOgg at end of .ogg playback
252*/ 
253void SoundControl::musicDone () {
254  track_number++; 
255  Mix_HaltMusic();
256  Mix_FreeMusic(music);
257  music = NULL;
258} 
Note: See TracBrowser for help on using the repository browser.