summaryrefslogtreecommitdiff
path: root/osframework/source/SexyAppFramework/DSoundInstance.cpp
blob: 305c83b46cdf48e3a9e0a2a3ba11f7d39501e3ae (plain)
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
#include "DSoundInstance.h"
#include "DSoundManager.h"

using namespace Sexy;

DSoundInstance::DSoundInstance(DSoundManager* theSoundManager, LPDIRECTSOUNDBUFFER theSourceSound)
{
	mSoundManagerP = theSoundManager;
	mReleased = false;
	mAutoRelease = false;
	mHasPlayed = false;
	mSourceSoundBuffer = theSourceSound;
	mSoundBuffer = NULL;

	mBaseVolume = 1.0;
	mBasePan = 0;

	mVolume = 1.0;
	mPan = 0;

	mDefaultFrequency = 44100;

	HRESULT hr;

	if (mSourceSoundBuffer != NULL)
	{
		hr=mSoundManagerP->mDirectSound->DuplicateSoundBuffer(mSourceSoundBuffer, &mSoundBuffer);
		if (hr!=DS_OK)
		{
			switch (hr)
			{
			case DSERR_ALLOCATED: MessageBoxA(0,"DSERR_ALLOCATED","Hey",MB_OK);break;
			case DSERR_INVALIDCALL: MessageBoxA(0,"DSERR_INVALIDCALL","Hey",MB_OK);break;
			case DSERR_INVALIDPARAM: MessageBoxA(0,"DSERR_INVALIDPARAM","Hey",MB_OK);break;
			case DSERR_OUTOFMEMORY: MessageBoxA(0,"DSERR_OUTOFMEMORY","Hey",MB_OK);break;
			case DSERR_UNINITIALIZED: MessageBoxA(0,"DSERR_UNINITIALIZED","Hey",MB_OK);break;
			}
			exit(0);
		}

		mSoundBuffer->GetFrequency(&mDefaultFrequency);
	}

	RehupVolume();
}

DSoundInstance::~DSoundInstance()
{
	if (mSoundBuffer != NULL)
		mSoundBuffer->Release();
}

void DSoundInstance::RehupVolume()
{
	if (mSoundBuffer != NULL)
		mSoundBuffer->SetVolume(mSoundManagerP->VolumeToDB(mBaseVolume * mVolume * mSoundManagerP->mMasterVolume));
}

void DSoundInstance::RehupPan()
{
	if (mSoundBuffer != NULL)
		mSoundBuffer->SetPan(mBasePan + mPan);
}

void DSoundInstance::Release()
{
	Stop();
	mReleased = true;			
}

void DSoundInstance::SetVolume(double theVolume) // 0 = max
{
	mVolume = theVolume;
	RehupVolume();	
}

void DSoundInstance::SetPan(int thePosition) //-db to =db = left to right
{
	mPan = thePosition;
	RehupPan();	
}

void DSoundInstance::SetBaseVolume(double theBaseVolume)
{
	mBaseVolume = theBaseVolume;
	RehupVolume();
}

void DSoundInstance::SetBasePan(int theBasePan)
{
	mBasePan = theBasePan;
	RehupPan();
}

bool DSoundInstance::Play(bool looping, bool autoRelease)
{
	Stop();

	mHasPlayed = true;	
	mAutoRelease = autoRelease;	

	if (mSoundBuffer == NULL)
	{
		return false;
	}

	if (looping)
	{
		if (mSoundBuffer->Play(0, 0, DSBPLAY_LOOPING) != DS_OK)
			return false;
	}
	else
	{
		if (mSoundBuffer->Play(0, 0, 0) != DS_OK)
		{
			return false;
		}
	}

	return true;
}

void DSoundInstance::Stop()
{
	if (mSoundBuffer != NULL)
	{
		mSoundBuffer->Stop();
		mSoundBuffer->SetCurrentPosition(0);
		mAutoRelease = false;
	}
}

#include "DirectXErrorString.h"
void DSoundInstance::AdjustPitch(double theNumSteps)
{
	if (mSoundBuffer != NULL)
	{
		double aFrequencyMult = pow(1.0594630943592952645618252949463, theNumSteps);
		double aNewFrequency = mDefaultFrequency*aFrequencyMult;
		if (aNewFrequency < DSBFREQUENCY_MIN)
			aNewFrequency = DSBFREQUENCY_MIN;
		if (aNewFrequency > DSBFREQUENCY_MAX)
			aNewFrequency = DSBFREQUENCY_MAX;

		mSoundBuffer->SetFrequency((DWORD)aNewFrequency);
	}
}

bool DSoundInstance::IsPlaying()
{
	if (!mHasPlayed)
		return false;

	if (mSoundBuffer == NULL)
		return false;

	DWORD aStatus;
	if (mSoundBuffer->GetStatus(&aStatus) == DS_OK)
		// Has the sound stopped?
		return ((aStatus & DSBSTATUS_PLAYING) != 0);
	else
		return false;
}

bool DSoundInstance::IsReleased()
{
	if ((!mReleased) && (mAutoRelease) && (mHasPlayed) && (!IsPlaying()))	
		Release();	

	return mReleased;
}

double DSoundInstance::GetVolume()
{
	return mVolume; 
}