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
|
/* *************************************
* Includes
* *************************************/
#include "Sfx.h"
/* *************************************
* Defines
* *************************************/
#define MAX_VOLUME SPU_MAXVOL
#define NUMBER_OF_VOICES 24
/* *************************************
* Local Prototypes
* *************************************/
/* *************************************
* Local Variables
* *************************************/
static uint16_t SfxGlobalVolumeReduction;
static bool usedVoices[NUMBER_OF_VOICES];
#ifndef NO_CDDA
static uint16_t SfxCddaVolumeReduction;
#endif // NO_CDDA
void SfxPlaySound(SsVag* sound)
{
if (sound->data_size != 0)
{
SsPlayVag(sound, sound->cur_voice, MAX_VOLUME - SfxGlobalVolumeReduction, MAX_VOLUME - SfxGlobalVolumeReduction);
}
}
bool SfxUploadSound_Ex(const char* file_path, SsVag* vag, uint8_t voiceIndex)
{
#ifdef PSXSDK_DEBUG
static size_t SPUBytesUsed;
#endif // PSXSDK_DEBUG
if (voiceIndex >= NUMBER_OF_VOICES)
{
Serial_printf( "Invalid input voice index %d. Only indexes [%d-%d] are allowed.\n",
voiceIndex,
0,
NUMBER_OF_VOICES - 1 );
return false;
}
if (usedVoices[voiceIndex])
{
Serial_printf("Voice number %d is already being used.\n", voiceIndex);
return false;
}
if (SystemLoadFile(file_path) == false)
{
return false;
}
SsReadVag(vag, SystemGetBufferAddress());
SsUploadVag(vag);
vag->cur_voice = voiceIndex;
usedVoices[voiceIndex] = true;
#ifdef PSXSDK_DEBUG
SPUBytesUsed += vag->data_size;
if (SPUBytesUsed != 0)
{
enum
{
SPU_MAX_ALLOWED_BYTES = 512 * 1024 // 512 KBytes
};
uint16_t percentage = SPUBytesUsed * 100 / SPU_MAX_ALLOWED_BYTES;
Serial_printf("SPU usage: %d%%\n", percentage);
}
#endif // PSXSDK_DEBUG
return true;
}
bool SfxUploadSound(const char* file_path, SsVag* vag)
{
bool success = false;
uint8_t i;
for (i = 0; i < NUMBER_OF_VOICES; i++)
{
if (usedVoices[i] == false)
{
success = true;
break;
}
}
if (success == false)
{
Serial_printf("Could not find any free SPU slot.\n");
return false;
}
return SfxUploadSound_Ex(file_path, vag, i);
}
void SfxPlayTrack(MUSIC_TRACKS track)
{
#ifndef NO_CDDA
enum
{
CD_MAX_VOLUME = (uint16_t)0x7FFF
};
SsCdVol(CD_MAX_VOLUME - SfxCddaVolumeReduction,
CD_MAX_VOLUME - SfxCddaVolumeReduction);
SsEnableCd();
CdPlayTrack(track);
Serial_printf("Track number %d playing...\n",track);
#endif // NO_CDDA
}
void SfxStopMusic(void)
{
#ifndef NO_CDDA
uint16_t CDVol = 0x7FFF;
while (CDVol > 0x003F)
{
CDVol >>= 1;
SsCdVol(CDVol, CDVol);
}
CdSendCommand(CdlMute,0);
#endif // NO_CDDA
}
|