Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3300 was 3179, checked in by bensch, 20 years ago

orxonox/branches/sound: added files to source again, made it compile for widnows again.
Simon, if you arrive at a point where it is doing something usefull, we inject it into the trunk.

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