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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
* SND_MEM.java
* Copyright (C) 2004
*
* $Id: WaveLoader.java,v 1.4 2005-04-26 22:16:34 cawe Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package jake2.sound;
import jake2.Defines;
import jake2.qcommon.Com;
import jake2.qcommon.FS;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteOrder;
import javax.sound.sampled.*;
/**
* SND_MEM
*/
public class WaveLoader {
private static AudioFormat sampleFormat;
static {
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
sampleFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 22050, 16, 1, 2, 22050, false);
} else {
sampleFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 22050, 16, 1, 2, 22050, true);
}
}
/*
==============
S_LoadSound
==============
*/
public static sfxcache_t LoadSound(sfx_t s) {
if (s.name.charAt(0) == '*')
return null;
// see if still in memory
sfxcache_t sc = s.cache;
if (sc != null)
return sc;
String name;
// load it in
if (s.truename != null)
name = s.truename;
else
name = s.name;
String namebuffer;
if (name.charAt(0) == '#')
namebuffer = name.substring(1);
else
namebuffer = "sound/" + name;
byte[] data = FS.LoadFile(namebuffer);
if (data == null) {
Com.DPrintf("Couldn't load " + namebuffer + "\n");
return null;
}
int size = data.length;
wavinfo_t info = GetWavinfo(s.name, data, size);
AudioInputStream in = null;
AudioInputStream out = null;
try {
in = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data));
if (in.getFormat().getSampleSizeInBits() == 8) {
in = convertTo16bit(in);
}
out = AudioSystem.getAudioInputStream(sampleFormat, in);
int l = (int)out.getFrameLength();
sc = s.cache = new sfxcache_t(l*2);
sc.length = l;
int c = out.read(sc.data, 0, l * 2);
out.close();
in.close();
} catch (Exception e) {
Com.Printf("Couldn't load " + namebuffer + "\n");
return null;
}
sc.loopstart = info.loopstart * ((int)sampleFormat.getSampleRate() / info.rate);
sc.speed = (int)sampleFormat.getSampleRate();
sc.width = sampleFormat.getSampleSizeInBits() / 8;
sc.stereo = 0;
data = null;
return sc;
}
static AudioInputStream convertTo16bit(AudioInputStream in) throws IOException {
AudioFormat format = in.getFormat();
int length = (int)in.getFrameLength();
byte[] samples = new byte[2*length];
for (int i = 0; i < length; i++) {
in.read(samples, 2*i+1, 1);
samples[2*i+1] -= 128;
}
in.close();
AudioFormat newformat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), 16, format.getChannels(), 2, format.getFrameRate(), false);
return new AudioInputStream(new ByteArrayInputStream(samples), newformat, length);
}
/*
===============================================================================
WAV loading
===============================================================================
*/
static byte[] data_b;
static int data_p;
static int iff_end;
static int last_chunk;
static int iff_data;
static int iff_chunk_len;
static short GetLittleShort() {
int val = 0;
val = data_b[data_p] & 0xFF;
data_p++;
val |= ((data_b[data_p] & 0xFF) << 8);
data_p++;
return (short)val;
}
static int GetLittleLong() {
int val = 0;
val = data_b[data_p] & 0xFF;
data_p++;
val |= ((data_b[data_p] & 0xFF) << 8);
data_p++;
val |= ((data_b[data_p] & 0xFF) << 16);
data_p++;
val |= ((data_b[data_p] & 0xFF) << 24);
data_p++;
return val;
}
static void FindNextChunk(String name) {
while (true) {
data_p = last_chunk;
if (data_p >= iff_end) { // didn't find the chunk
data_p = 0;
return;
}
data_p += 4;
iff_chunk_len = GetLittleLong();
if (iff_chunk_len < 0) {
data_p = 0;
return;
}
if (iff_chunk_len > 1024*1024) {
Com.Println(" Warning: FindNextChunk: length is past the 1 meg sanity limit");
}
data_p -= 8;
last_chunk = data_p + 8 + ((iff_chunk_len + 1) & ~1);
String s = new String(data_b, data_p, 4);
if (s.equals(name))
return;
}
}
static void FindChunk(String name) {
last_chunk = iff_data;
FindNextChunk(name);
}
/*
============
GetWavinfo
============
*/
static wavinfo_t GetWavinfo(String name, byte[] wav, int wavlength) {
wavinfo_t info = new wavinfo_t();
int i;
int format;
int samples;
if (wav == null)
return info;
iff_data = 0;
iff_end = wavlength;
data_b = wav;
// find "RIFF" chunk
FindChunk("RIFF");
String s = new String(data_b, data_p + 8, 4);
if (!s.equals("WAVE")) {
Com.Printf("Missing RIFF/WAVE chunks\n");
return info;
}
// get "fmt " chunk
iff_data = data_p + 12;
// DumpChunks ();
FindChunk("fmt ");
if (data_p == 0) {
Com.Printf("Missing fmt chunk\n");
return info;
}
data_p += 8;
format = GetLittleShort();
if (format != 1) {
Com.Printf("Microsoft PCM format only\n");
return info;
}
info.channels = GetLittleShort();
info.rate = GetLittleLong();
data_p += 4 + 2;
info.width = GetLittleShort() / 8;
// get cue chunk
FindChunk("cue ");
if (data_p != 0) {
data_p += 32;
info.loopstart = GetLittleLong();
// Com_Printf("loopstart=%d\n", sfx->loopstart);
// if the next chunk is a LIST chunk, look for a cue length marker
FindNextChunk("LIST");
if (data_p != 0) {
s = new String(data_b, data_p + 28, 4);
if (s.equals("MARK")) { // this is not a proper parse, but it works with cooledit...
data_p += 24;
i = GetLittleLong(); // samples in loop
info.samples = info.loopstart + i;
// Com_Printf("looped length: %i\n", i);
}
}
} else
info.loopstart = -1;
// find data chunk
FindChunk("data");
if (data_p == 0) {
Com.Printf("Missing data chunk\n");
return info;
}
data_p += 4;
samples = GetLittleLong() / info.width;
if (info.samples != 0) {
if (samples < info.samples)
Com.Error(Defines.ERR_DROP, "Sound " + name + " has a bad loop length");
} else
info.samples = samples;
info.dataofs = data_p;
return info;
}
static class wavinfo_t {
int rate;
int width;
int channels;
int loopstart;
int samples;
int dataofs; // chunk starts this many bytes from file start
}
}
|