Ticket #8919: itepatch

File itepatch, 4.5 KB (added by agent-q, 16 years ago)

Inherit the Earth: Memory reduction in SndRes::load()

Line 
1Index: sndres.cpp
2===================================================================
3--- sndres.cpp (revision 34211)
4+++ sndres.cpp (working copy)
5@@ -169,7 +169,6 @@
6 }
7
8 bool SndRes::load(ResourceContext *context, uint32 resourceId, SoundBuffer &buffer, bool onlyHeader) {
9- byte *soundResource;
10 Audio::AudioStream *voxStream;
11 size_t soundResourceLength;
12 bool result = false;
13@@ -180,13 +179,13 @@
14 byte flags;
15 size_t voxSize;
16 const GameSoundInfo *soundInfo;
17+ Common::File* file;
18
19 if (resourceId == (uint32)-1) {
20 return false;
21 }
22
23 if (_vm->getGameType() == GType_IHNM && _vm->isMacResources()) {
24- Common::File soundFile;
25 char soundFileName[40];
26 int dirIndex = resourceId / 64;
27
28@@ -199,15 +198,23 @@
29 } else {
30 sprintf(soundFileName, "SFX/SFX%d/SFX%03x", dirIndex, resourceId);
31 }
32- soundFile.open(soundFileName);
33- soundResourceLength = soundFile.size();
34- soundResource = new byte[soundResourceLength];
35- soundFile.read(soundResource, soundResourceLength);
36- soundFile.close();
37+
38+ file = new Common::File();
39+
40+ file->open(soundFileName);
41+ soundResourceLength = file->size();
42 } else {
43- _vm->_resource->loadResource(context, resourceId, soundResource, soundResourceLength);
44+
45+ ResourceData* resourceData = _vm->_resource->getResourceData(context, resourceId);
46+ file = context->getFile(resourceData);
47+
48+ file->seek(resourceData->offset);
49+ soundResourceLength = resourceData->size;
50+
51 }
52
53+ Common::SeekableReadStream& readS = *file;
54+
55 if ((context->fileType & GAME_VOICEFILE) != 0) {
56 soundInfo = _vm->getVoiceInfo();
57 } else {
58@@ -220,16 +227,20 @@
59 context->table[resourceId].fillSoundPatch(soundInfo);
60 }
61
62- MemoryReadStream readS(soundResource, soundResourceLength);
63
64 resourceType = soundInfo->resourceType;
65
66 if (soundResourceLength >= 8) {
67- if (!memcmp(soundResource, "Creative", 8)) {
68+ byte header[8];
69+
70+ readS.read(&header, 8);
71+ readS.seek(readS.pos() - 8);
72+
73+ if (!memcmp(header, "Creative", 8)) {
74 resourceType = kSoundVOC;
75- } else if (!memcmp(soundResource, "RIFF", 4) != 0) {
76+ } else if (!memcmp(header, "RIFF", 4) != 0) {
77 resourceType = kSoundWAV;
78- } else if (!memcmp(soundResource, "FORM", 4) != 0) {
79+ } else if (!memcmp(header, "FORM", 4) != 0) {
80 resourceType = kSoundAIFF;
81 }
82
83@@ -244,11 +255,11 @@
84 uncompressedSound = true;
85
86 if ((_vm->getFeatures() & GF_COMPRESSED_SOUNDS) && !uncompressedSound) {
87- if (soundResource[0] == char(0)) {
88+ if (header[0] == char(0)) {
89 resourceType = kSoundMP3;
90- } else if (soundResource[0] == char(1)) {
91+ } else if (header[0] == char(1)) {
92 resourceType = kSoundOGG;
93- } else if (soundResource[0] == char(2)) {
94+ } else if (header[0] == char(2)) {
95 resourceType = kSoundFLAC;
96 }
97 }
98@@ -268,9 +279,9 @@
99 buffer.stereo = false;
100 if (onlyHeader) {
101 buffer.buffer = NULL;
102- free(soundResource);
103 } else {
104- buffer.buffer = soundResource;
105+ buffer.buffer = (byte *) malloc(soundResourceLength);
106+ readS.read(buffer.buffer, soundResourceLength);
107 }
108 result = true;
109 break;
110@@ -284,9 +295,10 @@
111 buffer.buffer = NULL;
112 } else {
113 buffer.buffer = (byte *)malloc(buffer.size);
114- memcpy(buffer.buffer, soundResource + 36, buffer.size);
115+
116+ readS.seek(readS.pos() + 36);
117+ readS.read(buffer.buffer, buffer.size);
118 }
119- free(soundResource);
120 result = true;
121 break;
122 case kSoundVOX:
123@@ -297,7 +309,6 @@
124 buffer.size = soundResourceLength * 4;
125 if (onlyHeader) {
126 buffer.buffer = NULL;
127- free(soundResource);
128 } else {
129 voxStream = Audio::makeADPCMStream(&readS, false, soundResourceLength, Audio::kADPCMOki);
130 buffer.buffer = (byte *)malloc(buffer.size);
131@@ -325,7 +336,6 @@
132 }
133 result = true;
134 }
135- free(soundResource);
136 break;
137 case kSoundWAV:
138 if (Audio::loadWAVFromStream(readS, size, rate, flags)) {
139@@ -342,7 +352,6 @@
140 }
141 result = true;
142 }
143- free(soundResource);
144 break;
145 case kSoundAIFF:
146 if (Audio::loadAIFFFromStream(readS, size, rate, flags)) {
147@@ -359,7 +368,6 @@
148 }
149 result = true;
150 }
151- free(soundResource);
152 break;
153 case kSoundMP3:
154 case kSoundOGG:
155@@ -382,12 +390,17 @@
156 buffer.buffer = NULL;
157
158 result = true;
159- free(soundResource);
160 break;
161 default:
162 error("SndRes::load Unknown sound type");
163 }
164
165+
166+ if (_vm->getGameType() == GType_IHNM && _vm->isMacResources()) {
167+ delete file;
168+ }
169+
170+
171 // In ITE CD De some voices are absent and contain just 5 bytes header
172 // Round it to even number so soundmanager will not crash.
173 // See bug #1256701