Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

/branches/sound: finally a running version. To test, you can switch the parameters in the test class sound_test. A .ogg file named music.ogg from the directory . is played. The test class is really ugly, I know.

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