Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound/sound/sound_control.cc @ 3020

Last change on this file since 3020 was 3020, checked in by simon, 19 years ago

/branches/sound/sound, branches/sound/hud: Made a few changes like a version which no longer compiles… Ah life is so hard on me. If it would compile, you would need to make a folder Data in hud and add a tga file named Font.tga with the letters in it.

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