-
Notifications
You must be signed in to change notification settings - Fork 26
/
soundmanager.cpp
72 lines (59 loc) · 1.82 KB
/
soundmanager.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
#include "soundmanager.h"
#include <SDL.h>
bool SoundManager::Create(SoundSample*& dest, const char *filename)
{
dest = new SoundSample(filename, samplefreq, bytespersample);
return dest->initOkay();
}
bool SoundManager::Initialize(int freq, int channels)
{
if ( SDL_InitSubSystem(SDL_INIT_AUDIO) < 0 ) {
return false;
}
//TODO: change parameters?
//TODO: close audio
//check http://www.koders.com/cpp/fidCCF9C0D295448E549DBADA9E72FF9A974D8ACBCD.aspx
//for examples
if ( Mix_OpenAudio(freq, AUDIO_S16SYS, channels, 1024) < 0 ) {
return false;
}
Mix_AllocateChannels(numchannels);
samplefreq = freq;
//bytespersample = 2;
bytespersample = 4; //for some reason
return true;
}
void SoundManager::playMusic(const char* filename){
currentmusic = Mix_LoadMUS(filename);
Mix_PlayMusic(currentmusic, -1);
}
void SoundManager::setMusicVolume(float volume){
Mix_VolumeMusic(volume*MIX_MAX_VOLUME);
}
SoundSample::SoundSample(const char *filename,int freq,int bytes){
//TODO: the delay seems solvable by using VC++ compiler/libs and DirectSound
chunk = Mix_LoadWAV(filename);
samplefreq = freq;
bytespersample = bytes;
}
SoundSample::~SoundSample(void) {
if (chunk) {
Mix_FreeChunk(chunk);
}
}
bool SoundSample::initOkay(void){
return chunk;
}
float SoundSample::GetSoundLength(void) {
return (float)(chunk->alen) / bytespersample / samplefreq * 1000;
}
void SoundSample::Play(float volume, float pan){
Mix_VolumeChunk(chunk,volume*MIX_MAX_VOLUME);
int channel = Mix_PlayChannel(-1, chunk,0);
if(channel == -1){
Mix_AllocateChannels(8 + Mix_AllocateChannels(-1));
channel = Mix_PlayChannel(-1, chunk,0);
}
if(channel != -1)
Mix_SetPanning(channel, 127 - (pan * 127) , 127 + (pan * 127));
}