Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/soundEngine/src/sound_control.cc @ 3508

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

orxonox/branche/soundManager: added sound_control and it compiles

  • 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
20int sfx_channel1 = -1;
21int sfx_channel2 = -1;
22int finished = 0;
23SoundControl* SoundControl::sound = SoundControl::getInstance();
24SoundControl* SoundControl::singletonRef = 0;
25int volume = SDL_MIX_MAXVOLUME;
26int track_number = 1;
27Mix_Music* music = NULL;
28int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS, 
29audio_buffers = 16384, bits = 0; 
30Uint16 audio_format = MIX_DEFAULT_FORMAT;
31SDL_Event event;
32
33
34/**
35    \brief standard constructor
36    This constructor builds a SoundControl Object and initialises it .
37    All sound output is handled by this singleton object.
38*/ 
39SoundControl::SoundControl() { 
40  if(SDL_Init(SDL_INIT_AUDIO)<0) {
41    printf("SDL_Init: INIT_AUDIO error.\n"); 
42  } 
43  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { 
44    printf("Mix_OpenAudio: Failed to open audio!\n"); 
45  } 
46  initialise();
47}
48
49/**
50    \brief Default destructor
51*/ 
52SoundControl::~SoundControl() {
53}
54
55/**
56   \brief Returns a reference to the SoundControl singleton
57*/
58SoundControl* SoundControl::getInstance() {
59  if (!SoundControl::singletonRef) 
60    return singletonRef;
61  else
62    return singletonRef = new SoundControl;
63}
64
65/**
66    \brief Is called by SoundControl object to initiate all values and to output some text
67*/ 
68void SoundControl::initialise() { 
69  Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 
70  bits=audio_format&0xFF; 
71  printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); 
72  Mix_VolumeMusic(volume);
73} 
74
75/**
76    \brief Sets the number of output Channels
77*/ 
78void SoundControl::setNumberOfChannels(int number_of_channels) { 
79  Mix_AllocateChannels(number_of_channels);
80} 
81
82/**
83    \brief Static function to play a .xm file
84    \param filename: self-explanatory
85*/ 
86void SoundControl::playMod(char* fileName) {
87  Mix_Chunk* chunk = NULL; 
88  chunk = Mix_LoadWAV(fileName); 
89  if(Mix_PlayChannel(-1, chunk, 0) == -1) {
90    printf("Mix_PlayChannel: %s\n", Mix_GetError());
91  }
92}
93
94/**
95    \brief Static function to play a .wav file
96    \param filename: self-explanatory
97*/ 
98void SoundControl::playWav(char* fileName) {
99  Mix_Chunk* chunk = NULL; 
100  chunk = Mix_LoadWAV(fileName); 
101  if(Mix_PlayChannel(-1, chunk, 0) == -1) {
102    printf("Mix_PlayChannel: %s\n", Mix_GetError());
103  }
104} 
105
106/**
107    \brief Static function to play an .ogg file
108    \param filename: self-explanatory
109*/ 
110void SoundControl::playOgg(char* fileName) {
111  Mix_Music* music = NULL;
112  music = Mix_LoadMUS(fileName);
113  if(Mix_PlayMusic(music, 1) == -1) {
114    printf("Mix_PlayMusic: %s\n",Mix_GetError());
115  }
116  Mix_HookMusicFinished(musicDone);
117} 
118
119/**
120    \brief Heightens the overall volume of output
121*/ 
122void SoundControl::volumeUp() { 
123  volume = (volume + 1) << 1; 
124  if(volume > SDL_MIX_MAXVOLUME) 
125    volume = SDL_MIX_MAXVOLUME; 
126  Mix_VolumeMusic(volume); 
127} 
128
129
130/**
131    \brief Lowers the overall volume of output
132*/ 
133void SoundControl::volumeDown() { 
134  volume >>= 1; 
135  Mix_VolumeMusic(volume); 
136} 
137
138/**
139    \brief Rewinds music to the beginning
140*/ 
141void SoundControl::trackRewind() { 
142  Mix_RewindMusic(); 
143} 
144
145/**
146    \brief Rewinds the music 5 seconds
147*/ 
148void SoundControl::forwardMusic() { 
149  Mix_SetMusicPosition(+5); 
150} 
151
152/**
153    \brief Forwards the music 5 seconds
154*/ 
155void SoundControl::rewindMusic () { 
156  Mix_SetMusicPosition(-5); 
157} 
158
159/**
160    \brief Pauses music output
161*/ 
162void SoundControl::pauseMusic() {
163  Mix_PauseMusic();
164} 
165
166/**
167    \brief Pauses music output
168*/ 
169void SoundControl::resumeMusic() {
170  Mix_ResumeMusic();
171} 
172
173/**
174    \brief Fades in music
175*/ 
176void fadeInMusic(int time) {
177
178}
179
180/**
181    \brief Fades out music
182*/ 
183void SoundControl::fadeOutMusic(int time) {
184
185}
186
187/**
188    \brief Hooked by playOgg at end of .ogg playback
189*/ 
190void SoundControl::musicDone() {
191  Mix_HaltMusic();
192  Mix_FreeMusic(music);
193  music = NULL;
194} 
195
196/**
197    \brief Handles input events
198*/ 
199void SoundControl::handleKey(SDL_KeyboardEvent key) {
200  SoundControl* sound = SoundControl::getInstance();
201
202  switch(key.keysym.sym) {
203  case SDLK_a:
204    if(key.type == SDL_KEYDOWN) {
205      if(sound->sfx_channel1 < 0) {
206        sound->sfx_channel1 = 1;
207        sound->playWav("sound1.wav");
208      }
209    } else {
210      Mix_HaltChannel(sound->sfx_channel1);
211      sound->sfx_channel1 = -1;
212    }
213    break;
214  case SDLK_s:
215    if(key.type == SDL_KEYDOWN) {
216      if(sound->sfx_channel2 < 0) {
217        sound->sfx_channel2 = 1;
218        sound->playWav("sound2.wav");
219      }
220    } else {
221      Mix_HaltChannel(sound->sfx_channel2);
222      sound->sfx_channel2 = -1;
223    }
224    break;
225  case SDLK_m:
226    if(key.state == SDL_PRESSED) {
227      sound->playOgg("music.ogg");
228    }
229    break;
230  case SDLK_q:
231    sound->finished = 1;
232    break;
233  default:
234    break;
235  }
236}
237
238int main(int argc, char* argv[]) {
239  SDL_Surface* screen;
240  SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); 
241  screen = SDL_SetVideoMode(320, 240, 0, 0);
242  while(!finished) {
243    while(SDL_PollEvent(&event)) {
244      switch(event.type) {
245      case SDL_QUIT:
246        finished = 1;
247        break;
248      case SDL_KEYDOWN:
249      case SDL_KEYUP:
250        SoundControl::handleKey(event.key);
251        break;
252      default:
253        break;
254      }
255    }
256    SDL_Delay(50);
257  }
258  delete SoundControl::getInstance();
259  SDL_Quit();
260  return 0;
261}
Note: See TracBrowser for help on using the repository browser.