Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/old.chris/sound_test/sound_control.cc @ 3360

Last change on this file since 3360 was 3102, checked in by chris, 20 years ago

orxonox/branches/chris: quick doodle with simons sound test

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