Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

branches/sound: made SoundControl singleton. Must be built via getObject()

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