summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_SoundSystem.cpp
blob: 01afc6d475a917419591f4709817961251def3fe (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
#include "nw4r/snd/snd_SoundSystem.h"

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

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

#include <decomp.h>
#include "common.h"

#include "nw4r/snd/snd_AxVoiceManager.h"
#include "nw4r/snd/snd_AxManager.h"
#include "nw4r/snd/snd_Channel.h" // ChannelManager
#include "nw4r/snd/snd_global.h"
#include "nw4r/snd/snd_SeqPlayer.h"
#include "nw4r/snd/snd_SoundThread.h"
#include "nw4r/snd/snd_TaskManager.h"
#include "nw4r/snd/snd_TaskThread.h"
#include "nw4r/snd/snd_VoiceManager.h"

#include <rvl/OS/OS.h> // OSRegisterVersion
#include <rvl/AX/AXVPB.h> // AXGetMaxVoices
#include <rvl/SC/scsystem.h>
#include <rvl/SC/scapi.h>

#include "nw4r/NW4RAssert.hpp"

/*******************************************************************************
 * macros
 */

#define BUILDSTAMP_LIB_GROUP_NAME	"NW4R   "
#define BUILDSTAMP_LIB_NAME			"SND"

// You probably want to change these
#define BUILDSTAMP_BUILD_TYPE		"release"
#define BUILDSTAMP_DATE				__DATE__
#define BUILDSTAMP_TIME				__TIME__
#define BUILDSTAMP_CW_MAJOR_REV		STR(__CWCC__)
#define BUILDSTAMP_CW_MINOR_REV		STR(__CWBUILD__)

#define BUILDSTAMP_STRING	\
	"<< " \
	BUILDSTAMP_LIB_GROUP_NAME " - " BUILDSTAMP_LIB_NAME " \t" \
	BUILDSTAMP_BUILD_TYPE " build: " \
	BUILDSTAMP_DATE " " BUILDSTAMP_TIME \
	" (" BUILDSTAMP_CW_MAJOR_REV "_" BUILDSTAMP_CW_MINOR_REV ")" \
	" >>"

/*******************************************************************************
 * variables
 */

namespace nw4r { namespace snd
{
	// .data, .sdata
	extern "C" char const *NW4R_SND_Version_ = BUILDSTAMP_STRING;

	// .bss
	detail::TaskThread SoundSystem::sTaskThread;

	// .sbss
	namespace
	{
		bool sInitialized;
	} // unnamed namespace

	int SoundSystem::sMaxVoices;
}} // namespace nw4r::snd

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

namespace nw4r { namespace snd {

u32 SoundSystem::GetRequiredMemSize(SoundSystemParam const &param)
{
	// could have just used align assert? idk
	NW4RAssert_Line(106, param.soundThreadStackSize % 8 == 0);
	NW4RAssert_Line(107, param.dvdThreadStackSize % 8 == 0);

	int maxVoices = AXGetMaxVoices();

	return param.soundThreadStackSize + param.dvdThreadStackSize
	     + detail::AxVoiceManager::GetInstance().GetRequiredMemSize(maxVoices)
	     + detail::VoiceManager::GetInstance().GetRequiredMemSize(maxVoices)
	     + detail::ChannelManager::GetInstance().GetRequiredMemSize(maxVoices);
}

void SoundSystem::InitSoundSystem(SoundSystemParam const &param, void *workMem,
                                  u32 workMemSize)
{
	bool result; // presumably up here

	NW4RAssertAligned_Line(144, workMem, 32);
	NW4RAssert_Line(145, workMemSize >= GetRequiredMemSize( param ));

	if (sInitialized)
		return;

	sInitialized = true;

	OSRegisterVersion(NW4R_SND_Version_);

	detail::AxManager::GetInstance().Init();

	SCInit();

	SCStatus initStatus;
	do
		initStatus = SCCheckStatus();
	while (initStatus == SC_STATUS_BUSY);

	NW4RAssert_Line(171, initStatus == SC_STATUS_OK);

	SCSoundMode soundMode = SCGetSoundMode();
	switch (soundMode)
	{
	case SC_SND_MONO:
		detail::AxManager::GetInstance().SetOutputMode(OUTPUT_MODE_MONO);
		break;

	case SC_SND_STEREO:
		detail::AxManager::GetInstance().SetOutputMode(OUTPUT_MODE_STEREO);
		break;

	case SC_SND_SURROUND:
		detail::AxManager::GetInstance().SetOutputMode(OUTPUT_MODE_DPL2);
		break;

	default:
		detail::AxManager::GetInstance().SetOutputMode(OUTPUT_MODE_STEREO);
		break;
	}

	byte_t *ptr = static_cast<byte_t *>(workMem);

	void *dvdThreadStack = ptr;
	ptr += param.dvdThreadStackSize;

	void *soundThreadStack = ptr;
	ptr += param.soundThreadStackSize;

	sMaxVoices = AXGetMaxVoices();

	void *axVoiceWork = ptr;
	ptr += detail::AxVoiceManager::GetInstance().GetRequiredMemSize(
		sMaxVoices);

	detail::AxVoiceManager::GetInstance().Setup(
		axVoiceWork,
		detail::AxVoiceManager::GetInstance().GetRequiredMemSize(
			sMaxVoices));

	void *voiceWork = ptr;
	ptr += detail::VoiceManager::GetInstance().GetRequiredMemSize(sMaxVoices);

	detail::VoiceManager::GetInstance().Setup(
		voiceWork,
		detail::VoiceManager::GetInstance().GetRequiredMemSize(sMaxVoices));

	void *channelWork = ptr;
	ptr += detail::ChannelManager::GetInstance().GetRequiredMemSize(
		sMaxVoices);

	detail::ChannelManager::GetInstance().Setup(
		channelWork,
		detail::ChannelManager::GetInstance().GetRequiredMemSize(
			sMaxVoices));

	detail::SeqPlayer::InitSeqPlayer();

	result = sTaskThread.Create(param.dvdThreadPriority, dvdThreadStack,
	                   param.dvdThreadStackSize);
	NW4RAssert_Line(247, result);

	result = detail::SoundThread::GetInstance().Create(
		param.soundThreadPriority, soundThreadStack,
		param.soundThreadStackSize);
	NW4RAssert_Line(255, result);

	NW4RAssert_Line(257, ptr <= reinterpret_cast<u8*>( workMem ) + workMemSize);
}

void SoundSystem::ShutdownSoundSystem()
{
	if (!sInitialized)
		return;

	detail::SoundThread::GetInstance().Shutdown();

	detail::TaskManager::GetInstance().CancelAllTask();
	sTaskThread.Destroy();

	detail::ChannelManager::GetInstance().Shutdown();
	detail::VoiceManager::GetInstance().Shutdown();
	detail::AxVoiceManager::GetInstance().Shutdown();
	detail::AxManager::GetInstance().Shutdown();

	sInitialized = false;
}

bool SoundSystem::IsInitializedSoundSystem()
{
	return sInitialized;
}

// SoundSystem::WaitForResetReady ([R89JEL]:/bin/RVL/Debug/mainD.MAP:14493)
DECOMP_FORCE("SoundSystem::WaitForResetReady is TIME OUT.\n");

}} // namespace nw4r::snd