summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_AxVoiceManager.cpp
blob: b28623cae7808429118e68aaf16081b7e37848d6 (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
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
222
223
224
225
226
227
228
229
230
231
#include "nw4r/snd/snd_AxVoiceManager.h"

/* Original source:
 * kiwi515/ogws
 * src/nw4r/snd/snd_AxVoiceManager.cpp
 */

/*******************************************************************************
 * headers
 */

#include <new>

#include "common.h"

#include "nw4r/snd/snd_AxVoice.h"

#include "nw4r/ut/ut_Lock.h" // ut::AutoInterruptLock

#include <rvl/AX/AXAlloc.h>
#include <rvl/AX/AXVPB.h>

#include "nw4r/NW4RAssert.hpp"

/*******************************************************************************
 * functions
 */

namespace nw4r { namespace snd { namespace detail {

AxVoiceManager &AxVoiceManager::GetInstance()
{
	static AxVoiceManager instance;

	return instance;
}

AxVoiceManager::AxVoiceManager() :
	mInitialized	(false)
{
}

u32 AxVoiceManager::GetRequiredMemSize(int axVoiceCount)
{
	return sizeof(AxVoice) * (VOICE_COUNT_MARGIN + axVoiceCount);
}

void AxVoiceManager::Setup(void *mem, u32 memSize)
{
	if (mInitialized)
		return;

	mVoiceCount = memSize / sizeof(AxVoice);
	byte_t *ptr = static_cast<byte_t *>(mem);

	for (u32 i = 0; i < mVoiceCount; i++)
	{
		mFreeVoiceList.PushBack(new (ptr) AxVoice);

		ptr += sizeof(AxVoice);
	}

	NW4RAssert_Line(65, ptr <= reinterpret_cast<u8*>( mem ) + memSize);

	mInitialized = true;
}

void AxVoiceManager::Shutdown()
{
	if (!mInitialized)
		return;

	while (!mActiveVoiceList.IsEmpty())
	{
		AxVoice &voice = mActiveVoiceList.GetFront();
		mActiveVoiceList.PopFront();

		if (voice.mVpb.IsAvailable())
		{
			voice.Stop();

			if (voice.mCallback)
			{
				(*voice.mCallback)(&voice, AxVoice::CALLBACK_STATUS_CANCEL,
				                   voice.mCallbackData);
			}

			FreeAxVoice(&voice);
		}
	}

	while (!mFreeReservedVoiceList.IsEmpty())
	{
		AxVoice &voice = mFreeReservedVoiceList.GetFront();
		// ERRATUM: Pop from wrong list
		mActiveVoiceList.PopFront();

		if (voice.mVpb.IsAvailable())
		{
			voice.Stop();

			if (voice.mCallback)
			{
				(*voice.mCallback)(&voice, AxVoice::CALLBACK_STATUS_CANCEL,
				                   voice.mCallbackData);
			}

			FreeAxVoice(&voice);
		}
	}

	while (!mFreeVoiceList.IsEmpty())
	{
		AxVoice &voice = mFreeVoiceList.GetFront();
		mFreeVoiceList.PopFront();

		voice.~AxVoice();
	}

	mInitialized = false;
}

AxVoice *AxVoiceManager::Alloc()
{
	ut::AutoInterruptLock lock;

	FreeAllReservedAxVoice();

	if (mFreeVoiceList.IsEmpty())
		return nullptr;

	AxVoice *ptr = &mFreeVoiceList.GetFront();
	mFreeVoiceList.PopFront();

	AxVoice *voice = new (ptr) AxVoice();
	mActiveVoiceList.PushBack(ptr);

	return voice;
}

void AxVoiceManager::Free(AxVoice *voice)
{
	NW4RAssert_Line(154, voice);

	voice->~AxVoice();

	ut::AutoInterruptLock lock;

	if (voice->mReserveForFreeFlag)
		mFreeReservedVoiceList.Erase(voice);
	else
		mActiveVoiceList.Erase(voice);

	mFreeVoiceList.PushBack(voice);
}

void AxVoiceManager::ReserveForFree(AxVoice *voice)
{
	NW4RAssert_Line(169, voice);

	ut::AutoInterruptLock lock;

	mActiveVoiceList.Erase(voice);
	mFreeReservedVoiceList.PushBack(voice);
}

AxVoice *AxVoiceManager::AcquireAxVoice(u32 priority,
                                        AxVoice::Callback *callback,
                                        void *callbackData)
{
	ut::AutoInterruptLock lock;

	AxVoice *voice = Alloc();
	if (!voice)
		return nullptr;

	AXVPB *vpb = AXAcquireVoice(priority, &AxVoice::VoiceCallback,
	                            reinterpret_cast<u32>(voice));
	if (!vpb)
	{
		Free(voice);
		return nullptr;
	}

	voice->mVpb.Set(vpb);
	voice->mCallback = callback;
	voice->mCallbackData = callbackData;

	return voice;
}

void AxVoiceManager::FreeAxVoice(AxVoice *voice)
{
	ut::AutoInterruptLock lock;

	NW4RAssertPointerNonnull_Line(212, voice);

	if (voice->mVpb.IsAvailable())
		AXFreeVoice(voice->mVpb);

	Free(voice);
}

void AxVoiceManager::ReserveForFreeAxVoice(AxVoice *voice)
{
	ut::AutoInterruptLock lock;

	NW4RAssert_Line(226, voice);

	voice->mReserveForFreeFlag = true;
	ReserveForFree(voice);
}

void AxVoiceManager::FreeAllReservedAxVoice()
{
	while (!mFreeReservedVoiceList.IsEmpty())
	{
		AxVoice &voice = mFreeReservedVoiceList.GetFront();

		if (voice.mCallback)
		{
			(*voice.mCallback)(&voice, AxVoice::CALLBACK_STATUS_DROP_DSP,
			                voice.mCallbackData);
		}

		// NOTE: unnecessary call to GetInstance from instance-method
		GetInstance().FreeAxVoice(&voice);
	}
}

}}} // namespace nw4r::snd::detail