Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/soundEngine: moved unrelevant stuff out of sound_control.

  • Property svn:executable set to *
File size: 4.3 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
20SoundControl* SoundControl::sound = SoundControl::getInstance();
21SoundControl* SoundControl::singletonRef = NULL;
22
23
24/**
25    \brief standard constructor
26    This constructor builds a SoundControl Object and initialises it .
27    All sound output is handled by this singleton object.
28*/ 
29SoundControl::SoundControl() {
30  if(SDL_Init(SDL_INIT_AUDIO)<0) {
31    printf("SDL_Init: INIT_AUDIO error.\n"); 
32  } 
33  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { 
34    printf("Mix_OpenAudio: Failed to open audio!\n"); 
35  } 
36  init();
37}
38
39/**
40    \brief Default destructor
41*/ 
42SoundControl::~SoundControl() 
43{
44  singletonRef = NULL;
45}
46
47/**
48   \brief Returns a reference to the SoundControl singleton
49*/
50SoundControl* SoundControl::getInstance() {
51  if (SoundControl::singletonRef) 
52    return singletonRef;
53  else
54    return singletonRef = new SoundControl;
55}
56
57/**
58    \brief Is called by SoundControl object to initiate all values and to output some text
59*/ 
60void SoundControl::init(void) 
61{ 
62  this->sfx_channel1 = -1;
63  this->sfx_channel2 = -1;
64  this->finished = 0;
65  this->volume = SDL_MIX_MAXVOLUME;
66  this->track_number = 1;
67  this->music = NULL;
68  this->audio_rate = 44100;
69  this->audio_channels = MIX_DEFAULT_CHANNELS;
70  this->audio_buffers = 16384;
71  this->bits = 0; 
72  this->audio_format = MIX_DEFAULT_FORMAT;
73 
74 
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 Static function to play a .xm file
90    \param filename: self-explanatory
91*/ 
92void SoundControl::playMod(char* fileName) {
93  Mix_Chunk* chunk = Mix_LoadWAV(fileName); 
94  if(Mix_PlayChannel(-1, chunk, 0) == -1) {
95    printf("Mix_PlayChannel: %s\n", Mix_GetError());
96  }
97}
98
99/**
100    \brief Static function to play a .wav file
101    \param filename: self-explanatory
102*/ 
103void SoundControl::playWav(char* fileName) {
104  Mix_Chunk* chunk = Mix_LoadWAV(fileName); 
105  if(Mix_PlayChannel(-1, chunk, 0) == -1) {
106    printf("Mix_PlayChannel: %s\n", Mix_GetError());
107  }
108} 
109
110/**
111    \brief Static function to play an .ogg file
112    \param filename: self-explanatory
113*/ 
114void SoundControl::playOgg(char* fileName) {
115  Mix_Music* music = NULL;
116  music = Mix_LoadMUS(fileName);
117  if(Mix_PlayMusic(music, 1) == -1) {
118    printf("Mix_PlayMusic: %s\n",Mix_GetError());
119  }
120  Mix_HookMusicFinished(musicDone);
121} 
122
123/**
124    \brief Heightens the overall volume of output
125*/ 
126void SoundControl::volumeUp() { 
127  volume = (volume + 1) << 1; 
128  if(volume > SDL_MIX_MAXVOLUME) 
129    volume = SDL_MIX_MAXVOLUME; 
130  Mix_VolumeMusic(volume); 
131} 
132
133
134/**
135    \brief Lowers the overall volume of output
136*/ 
137void SoundControl::volumeDown() { 
138  volume >>= 1; 
139  Mix_VolumeMusic(volume); 
140} 
141
142/**
143    \brief Rewinds music to the beginning
144*/ 
145void SoundControl::trackRewind() { 
146  Mix_RewindMusic(); 
147} 
148
149/**
150    \brief Rewinds the music 5 seconds
151*/ 
152void SoundControl::forwardMusic() { 
153  Mix_SetMusicPosition(+5); 
154} 
155
156/**
157    \brief Forwards the music 5 seconds
158*/ 
159void SoundControl::rewindMusic () { 
160  Mix_SetMusicPosition(-5); 
161} 
162
163/**
164    \brief Pauses music output
165*/ 
166void SoundControl::pauseMusic() {
167  Mix_PauseMusic();
168} 
169
170/**
171    \brief Pauses music output
172*/ 
173void SoundControl::resumeMusic() {
174  Mix_ResumeMusic();
175} 
176
177/**
178    \brief Fades in music
179*/ 
180void fadeInMusic(int time) {
181
182}
183
184/**
185    \brief Fades out music
186*/ 
187void SoundControl::fadeOutMusic(int time) {
188
189}
190
191/**
192    \brief Hooked by playOgg at end of .ogg playback
193*/ 
194void SoundControl::musicDone()
195{
196  Mix_Music* music = SoundControl::getInstance()->music;
197  Mix_HaltMusic();
198  Mix_FreeMusic(music);
199  music = NULL;
200} 
201
Note: See TracBrowser for help on using the repository browser.