-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDLAudio.cpp
221 lines (184 loc) · 6.43 KB
/
SDLAudio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// File.cpp
// Tetris
//
// Created by Fielding Johnston on 4/1/13.
// Copyright (c) 2013 Fielding Johnston. All rights reserved.
//
#include <iostream>
#include <cmath>
#include "SDLAudio.h"
//#include "AssetCache.h"
//
// Public Methods
//
SDLAudio::SDLAudio() : currentMusicName("") // Constructor
{
if ( Mix_OpenAudio( 44100, AUDIO_S16, 2, 4096) == -1 )
{
std::cerr << "ERROR: " << Mix_GetError << std::endl;
}
Mix_AllocateChannels(MAX_SOUND_CHANNELS);
}
SDLAudio::~SDLAudio() // Destructor
{
stopEverything();
Mix_CloseAudio();
}
void SDLAudio::playSound( std::string filename, int volume, int looping ) // Play a sound, at specified volume as a percent (default: uses the setting last set for the sound, or 100 if not changed since sound struct was constructed )
{ // and loop specified amount of times (-1 infinite, otherwise integer is literal; default: 0)
int availChannel = -1;
// let's find a channel that isn't currently playing anything
for ( int i = 0; i < MAX_SOUND_CHANNELS; i++ )
{
if (Mix_Playing( i ) == 0 )
{
availChannel = i;
break;
}
}
// if we found a free channel (availChannel should be set to something other than -1)
if ( availChannel != -1 )
{
try
{
currentSounds[availChannel] = assetCache.getSound(filename);
setSoundVolume( currentSounds[availChannel], volume );
Mix_PlayChannel( availChannel, currentSounds[availChannel], 0 );
}
catch( NotFoundException& ex)
{
// Error, file was not found
std::cerr<<ex.what()<<std::endl;
}
}
}
void SDLAudio::playMusic( std::string filename, int volume, int looping ) // Play music, at specified volume as a percent (default: uses the setting last set for the sound, or 100 if not changed since sound struct was constructed )
{ // and loop specified amount of times (-1 infinite, otherweise integer is literal; default: 0)
Mix_Music* current;
// attempt to load the file in to the cache, or retrieve a pointer to it if it exists
try
{
current = assetCache.getMusic( filename );
}
catch( NotFoundException& ex)
{
// We couldn't find the selected song, report that and then exit
std::cerr<<ex.what()<<std::endl;
return;
}
// See if a song is still playing, if so, stop it
if (currentMusicName != "")
{
try
{
if ( Mix_PlayingMusic() == true )
{
Mix_HaltMusic();
}
}
catch( NotFoundException& )
{
// Couldn't stop previous song
}
}
// set currentMusicName to the name of the music that is now playing
currentMusicName = filename;
// play the song!
Mix_PlayMusic(current, looping);
}
void SDLAudio::stopAllSounds() // Stop all sounds being played on any channel (does not stop the music)
{
for( int i = 0; i < MAX_SOUND_CHANNELS; i++ )
{
Mix_HaltChannel(i);
}
}
void SDLAudio::stopMusic() // Stop just the music that is currently playing (does not stop any sounds)
{
Mix_HaltMusic();
}
void SDLAudio::stopEverything() // Stops both sound and music being played.
{
for( int i = 0; i < MAX_SOUND_CHANNELS; i++ )
{
Mix_HaltChannel(i);
}
Mix_HaltMusic();
}
void SDLAudio::setSoundVolume ( std::string filename, int volAsPercent ) // Public version of setSoundVolume that takes filename as parameter and the volume as a percent
{
Mix_Chunk* sound = assetCache.getSound( filename );
setSoundVolume(sound, volAsPercent); // call the private version of this function using the pointer to the sound struct
}
int SDLAudio::getSoundVolume ( std::string filename ) // Public version of getSoundVolume that takes filename as parameter and returns the volume as a percent
{
Mix_Chunk* sound = assetCache.getSound( filename );
return getSoundVolume(sound);
}
void SDLAudio::setMusicVolume(int volAsPercent) // Set the music volume by percent
{
Mix_VolumeMusic( ceil( MIX_MAX_VOLUME * ( volAsPercent / 100.f ) ) ); // set the music volume based on the percent passed to the method
}
int SDLAudio::getMusicVolume() // Return the current music volume as a percent
{
return floor( ( Mix_VolumeMusic(-1) / 128.f ) * 100 ); // return the current music volume as a percent
}
void SDLAudio::setChannelVolume(int channel, int volAsPercent) // set the sound volume by percent
{
Mix_Volume(channel, ceil( MIX_MAX_VOLUME * ( volAsPercent / 100.f ) ) ); // set the music volume based on the percent passed to the method
}
int SDLAudio::getChannelVolume(int channel) // return the current sound volume as a percent
{
return floor( ( Mix_Volume(channel, -1 ) / 128.f ) * 100 ); // return the current music volume as a percent
}
void SDLAudio::toggleMusic() // returns true or false based on if the music was currently playing ( true if it was, false if it was muted );
{
if ( Mix_PausedMusic() == 1 )
{
Mix_ResumeMusic();
}
else
{
Mix_PauseMusic();
}
}
bool SDLAudio::isSoundPlaying( std::string filename ) // Checks if a sound is playing based on it's filename
{
for ( int i = 0; i < MAX_SOUND_CHANNELS; i++ )
{
if ( assetCache.getSound(filename) == currentSounds[i] )
{
if ( Mix_Playing( i ) )
{
return true;
}
else
{
return false;
}
}
}
return false;
}
bool SDLAudio::isMusicPlaying() // Checks to see if music is playing
{
if ( Mix_PlayingMusic() ) return true;
return false;
}
bool SDLAudio::isMusicPaused() // Checks to see if the music is currently paused.
{
if ( Mix_PausedMusic() == 1 ) return true;
return false;
}
//
// Private Methods
//
void SDLAudio::setSoundVolume(Mix_Chunk* sound, int volAsPercent ) // Private version of setSoundVolume that takes a pointer to the sound struct as parameter and the volume as a percent
{
Mix_VolumeChunk(sound, ceil( MIX_MAX_VOLUME * ( volAsPercent / 100.f) ) );
}
int SDLAudio::getSoundVolume( Mix_Chunk* sound) // Private version of getSoundVolume that takes a pointer to the sound struct as parameter and returns the volume as a percent
{
return floor( ( Mix_VolumeChunk(sound, -1) / 128.f ) * 100 ); // return the current sound volume as a percent
}