Skip to content

Commit e7cb407

Browse files
committed
#368 GBA sound samples and music playback optimization
1 parent ffff0d1 commit e7cb407

File tree

8 files changed

+300
-122
lines changed

8 files changed

+300
-122
lines changed

src/fixed/game.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,6 @@ struct Game
130130
rooms[roomIndex].add(lara);
131131
}
132132

133-
int32 getAmbientTrack() // TODO
134-
{
135-
extern int32 gLevelID;
136-
if (gLevelID == 0)
137-
return -1;
138-
return 5;
139-
}
140-
141133
void updateItems()
142134
{
143135
ItemObj* item = ItemObj::sFirstActive;

src/fixed/level.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,12 @@ void updateLevel(int32 frames)
170170
}
171171
}
172172

173+
int32 getAmbientTrack()
174+
{
175+
extern int32 gLevelID;
176+
if (gLevelID == 0)
177+
return -1;
178+
return 5;
179+
}
180+
173181
#endif

src/platform/gba/packer/main.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,6 +3079,35 @@ struct LevelPC
30793079

30803080
//f.writeArray(demoData, demoDataSize);
30813081

3082+
for (int32 i = 0; i < soundOffsetsCount; i++)
3083+
{
3084+
uint8* ptr = soundData + soundOffsets[i];
3085+
int32 size = *(int32*)(ptr + 40);
3086+
uint8* src = ptr + 44;
3087+
uint8* dst = ptr;
3088+
3089+
while ((dst - soundData) % 4 != 0) {
3090+
dst++;
3091+
}
3092+
dst += 4;
3093+
3094+
for (int32 j = 0; j < size; j++)
3095+
{
3096+
dst[j] = src[j];
3097+
}
3098+
3099+
while ((size % 4) != 0)
3100+
{
3101+
dst[size] = dst[size - 1];
3102+
size++;
3103+
}
3104+
3105+
dst -= 4;
3106+
*(int32*)dst = size;
3107+
3108+
soundOffsets[i] = dst - soundData;
3109+
}
3110+
30823111
header.soundMap = f.align4();
30833112
f.write(soundMap, 256);
30843113

src/platform/gba/render.iwram.cpp

Lines changed: 19 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,19 +1039,25 @@ void renderShadow(int32 x, int32 z, int32 sx, int32 sz)
10391039
return;
10401040
}
10411041

1042-
int32 sx2 = sx << 1;
1043-
int32 sz2 = sz << 1;
1044-
1045-
MeshVertex v[8] = {
1046-
{ x - sx, 0, z + sz2 }, // 0
1047-
{ x + sx, 0, z + sz2 }, // 1
1048-
{ x + sx2, 0, z + sz }, // 2
1049-
{ x + sx2, 0, z - sz }, // 3
1050-
{ x + sx, 0, z - sz2 }, // 4
1051-
{ x - sx, 0, z - sz2 }, // 5
1052-
{ x - sx2, 0, z - sz }, // 6
1053-
{ x - sx2, 0, z + sz } // 7
1054-
};
1042+
int16 xns1 = x - sx;
1043+
int16 xps1 = x + sx;
1044+
int16 xns2 = xns1 - sx;
1045+
int16 xps2 = xps1 + sx;
1046+
1047+
int16 zns1 = z - sz;
1048+
int16 zps1 = z + sz;
1049+
int16 zns2 = zns1 - sz;
1050+
int16 zps2 = zps1 + sz;
1051+
1052+
MeshVertex v[8];
1053+
v[0].x = xns1; v[0].y = 0; v[0].z = zps2;
1054+
v[1].x = xps1; v[1].y = 0; v[1].z = zps2;
1055+
v[2].x = xps2; v[2].y = 0; v[2].z = zps1;
1056+
v[3].x = xps2; v[3].y = 0; v[3].z = zns1;
1057+
v[4].x = xps1; v[4].y = 0; v[4].z = zns2;
1058+
v[5].x = xns1; v[5].y = 0; v[5].z = zns2;
1059+
v[6].x = xns2; v[6].y = 0; v[6].z = zns1;
1060+
v[7].x = xns2; v[7].y = 0; v[7].z = zps1;
10551061

10561062
transformMesh(v, 8, 0);
10571063
faceAddMeshQuads(gShadowQuads, 3);
@@ -1153,79 +1159,3 @@ void renderGlyph(int32 vx, int32 vy, int32 index)
11531159
{
11541160
//
11551161
}
1156-
1157-
extern int16 IMA_STEP[89];
1158-
1159-
#define DECODE_IMA_4(n)\
1160-
step = IMA_STEP[idx];\
1161-
index = n & 7;\
1162-
step += index * step << 1;\
1163-
if (index < 4) {\
1164-
idx = X_MAX(idx - 1, 0);\
1165-
} else {\
1166-
idx = X_MIN(idx + ((index - 3) << 1), X_COUNT(IMA_STEP) - 1);\
1167-
}\
1168-
if (n & 8) {\
1169-
smp -= step >> 3;\
1170-
} else {\
1171-
smp += step >> 3;\
1172-
}\
1173-
*buffer++ = smp >> (16 - (8 + SND_VOL_SHIFT));
1174-
1175-
void decodeIMA(IMA_STATE &state, const uint8* data, int32* buffer, int32 size)
1176-
{
1177-
uint32 step, index;
1178-
1179-
int32 idx = state.idx;
1180-
int32 smp = state.smp;
1181-
1182-
for (int32 i = 0; i < size; i++)
1183-
{
1184-
uint32 n = *data++;
1185-
DECODE_IMA_4(n);
1186-
n >>= 4;
1187-
DECODE_IMA_4(n);
1188-
}
1189-
1190-
state.idx = idx;
1191-
state.smp = smp;
1192-
}
1193-
1194-
/* TODO OUT OF IWRAM!
1195-
#define DECODE_IMA_4_sample(n)\
1196-
step = IMA_STEP[idx];\
1197-
index = n & 7;\
1198-
step += index * step << 1;\
1199-
if (index < 4) {\
1200-
idx = X_MAX(idx - 1, 0);\
1201-
} else {\
1202-
idx = X_MIN(idx + ((index - 3) << 1), X_COUNT(IMA_STEP) - 1);\
1203-
}\
1204-
if (n & 8) {\
1205-
smp -= step >> 3;\
1206-
} else {\
1207-
smp += step >> 3;\
1208-
}\
1209-
*buffer++ += smp * volume >> (16 - (8 + SND_VOL_SHIFT));
1210-
1211-
void decodeIMA_sample(IMA_STATE &state, const uint8* data, int32* buffer, int32 size, int32 inc, int32 volume)
1212-
{
1213-
uint32 step, index;
1214-
1215-
int32 idx = state.idx;
1216-
int32 smp = state.smp;
1217-
1218-
for (int32 i = 0; i < size; i++)
1219-
{
1220-
uint32 n = *data;
1221-
DECODE_IMA_4_sample(n);
1222-
n >>= 4;
1223-
DECODE_IMA_4_sample(n);
1224-
1225-
data += inc;
1226-
}
1227-
1228-
state.idx = idx;
1229-
state.smp = smp;
1230-
}
1231-
*/

src/platform/gba/sndIMA.s

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "common_asm.inc"
2+
3+
state .req r0
4+
buffer .req r1
5+
data .req r2
6+
size .req r3
7+
smp .req r4
8+
idx .req r5
9+
stepLUT .req r6
10+
step .req r7
11+
n .req r8
12+
index .req r9
13+
outA .req r12
14+
outB .req lr
15+
tmp .req outB
16+
17+
IMA_STEP_SIZE = 88
18+
19+
.macro decode4 n, out
20+
ldr step, [stepLUT, idx, lsl #2]
21+
22+
and index, \n, #7
23+
mov tmp, step, lsl #1
24+
mla step, index, tmp, step
25+
tst \n, #8
26+
subne smp, smp, step, lsr #3
27+
addeq smp, smp, step, lsr #3
28+
29+
subs index, #3
30+
suble idx, idx, #1
31+
bicle idx, idx, idx, asr #31
32+
addgt idx, idx, index, lsl #1
33+
cmpgt idx, #IMA_STEP_SIZE
34+
movgt idx, #IMA_STEP_SIZE
35+
36+
mov \out, smp, asr #2
37+
.endm
38+
39+
.global sndIMA_asm
40+
sndIMA_asm:
41+
stmfd sp!, {r4-r9, lr}
42+
43+
ldmia state, {smp, idx}
44+
45+
ldr stepLUT, =IMA_STEP
46+
47+
.loop:
48+
ldrb n, [data], #1
49+
50+
decode4 n, outA
51+
52+
mov n, n, lsr #4
53+
54+
decode4 n, outB
55+
56+
stmia buffer!, {outA, outB}
57+
58+
subs size, #1
59+
bne .loop
60+
61+
stmia state, {smp, idx}
62+
63+
ldmfd sp!, {r4-r9, lr}
64+
bx lr

src/platform/gba/sndPCM.s

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "common_asm.inc"
2+
3+
pos .req r0
4+
inc .req r1
5+
size .req r2
6+
volume .req r3
7+
8+
data .req r4
9+
buffer .req r5
10+
count .req r6
11+
ampA .req r7
12+
ampB .req r8
13+
outA .req r9
14+
outB .req r12
15+
last .req count
16+
tmp .req outB
17+
18+
.global sndPCM_asm
19+
sndPCM_asm:
20+
mov tmp, sp
21+
stmfd sp!, {r4-r9}
22+
23+
ldmia tmp, {data, buffer, count}
24+
25+
mla last, inc, count, pos
26+
cmp last, size
27+
movgt last, size
28+
29+
.loop:
30+
ldrb ampA, [data, pos, lsr #8]
31+
add pos, pos, inc
32+
ldrb ampB, [data, pos, lsr #8]
33+
add pos, pos, inc
34+
cmp pos, last
35+
36+
sub ampA, ampA, #128
37+
sub ampB, ampB, #128
38+
39+
ldmia buffer, {outA, outB}
40+
mla outA, volume, ampA, outA
41+
mla outB, volume, ampB, outB
42+
stmia buffer!, {outA, outB}
43+
44+
blt .loop
45+
46+
.done:
47+
ldmfd sp!, {r4-r9}
48+
bx lr

src/platform/gba/sndWrite.s

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "common_asm.inc"
2+
3+
buffer .req r0
4+
count .req r1
5+
data .req r2
6+
vA .req r3
7+
vB .req r4
8+
vC .req r5
9+
vD .req r12
10+
11+
SND_VOL_SHIFT = 6
12+
13+
.macro encode amp
14+
mov \amp, \amp, asr #SND_VOL_SHIFT
15+
cmp \amp, #-128
16+
movlt \amp, #-128
17+
cmp \amp, #127
18+
movgt \amp, #127
19+
.endm
20+
21+
.global sndWrite_asm
22+
sndWrite_asm:
23+
stmfd sp!, {r4-r5}
24+
.loop:
25+
ldmia data!, {vA, vB, vC, vD}
26+
27+
encode vA
28+
encode vB
29+
encode vC
30+
encode vD
31+
32+
and vA, vA, #0xFF
33+
and vB, vB, #0xFF
34+
and vC, vC, #0xFF
35+
orr vA, vA, vB, lsl #8
36+
orr vA, vA, vC, lsl #16
37+
orr vA, vA, vD, lsl #24
38+
str vA, [buffer], #4
39+
40+
subs count, #4
41+
bne .loop
42+
43+
ldmfd sp!, {r4-r5}
44+
bx lr

0 commit comments

Comments
 (0)