-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Wav.java
173 lines (148 loc) · 5.25 KB
/
Wav.java
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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.badlogic.gdx.backends.lwjgl3.audio;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.StreamUtils;
public class Wav {
static public class Music extends OpenALMusic {
private WavInputStream input;
public Music (OpenALLwjgl3Audio audio, FileHandle file) {
super(audio, file);
input = new WavInputStream(file);
if (audio.noDevice) return;
setup(input.channels, input.sampleRate);
}
public int read (byte[] buffer) {
if (input == null) {
input = new WavInputStream(file);
setup(input.channels, input.sampleRate);
}
try {
return input.read(buffer);
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
}
}
public void reset () {
StreamUtils.closeQuietly(input);
input = null;
}
}
static public class Sound extends OpenALSound {
public Sound (OpenALLwjgl3Audio audio, FileHandle file) {
super(audio);
if (audio.noDevice) return;
WavInputStream input = null;
try {
input = new WavInputStream(file);
setup(StreamUtils.copyStreamToByteArray(input, input.dataRemaining), input.channels, input.sampleRate);
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
} finally {
StreamUtils.closeQuietly(input);
}
}
}
/** @author Nathan Sweet */
static public class WavInputStream extends FilterInputStream {
public int channels, sampleRate, dataRemaining;
public WavInputStream (FileHandle file) {
super(file.read());
try {
if (read() != 'R' || read() != 'I' || read() != 'F' || read() != 'F')
throw new GdxRuntimeException("RIFF header not found: " + file);
skipFully(4);
if (read() != 'W' || read() != 'A' || read() != 'V' || read() != 'E')
throw new GdxRuntimeException("Invalid wave file header: " + file);
int fmtChunkLength = seekToChunk('f', 'm', 't', ' ');
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
// http://soundfile.sapp.org/doc/WaveFormat/
int type = read() & 0xff | (read() & 0xff) << 8;
if (type != 1) {
String name;
switch (type) {
case 0x0002:
name = "ADPCM";
break;
case 0x0003:
name = "IEEE float";
break;
case 0x0006:
name = "8-bit ITU-T G.711 A-law";
break;
case 0x0007:
name = "8-bit ITU-T G.711 u-law";
break;
case 0xFFFE:
name = "Extensible";
break;
default:
name = "Unknown";
}
throw new GdxRuntimeException("WAV files must be PCM, unsupported format: " + name + " (" + type + ")");
}
channels = read() & 0xff | (read() & 0xff) << 8;
if (channels != 1 && channels != 2) throw new GdxRuntimeException("WAV files must have 1 or 2 channels: " + channels);
sampleRate = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24;
skipFully(6);
int bitsPerSample = read() & 0xff | (read() & 0xff) << 8;
if (bitsPerSample != 16) throw new GdxRuntimeException("WAV files must have 16 bits per sample: " + bitsPerSample);
skipFully(fmtChunkLength - 16);
dataRemaining = seekToChunk('d', 'a', 't', 'a');
} catch (Throwable ex) {
StreamUtils.closeQuietly(this);
throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
}
}
private int seekToChunk (char c1, char c2, char c3, char c4) throws IOException {
while (true) {
boolean found = read() == c1;
found &= read() == c2;
found &= read() == c3;
found &= read() == c4;
int chunkLength = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24;
if (chunkLength == -1) throw new IOException("Chunk not found: " + c1 + c2 + c3 + c4);
if (found) return chunkLength;
skipFully(chunkLength);
}
}
private void skipFully (int count) throws IOException {
while (count > 0) {
long skipped = in.skip(count);
if (skipped <= 0) throw new EOFException("Unable to skip.");
count -= skipped;
}
}
public int read (byte[] buffer) throws IOException {
if (dataRemaining == 0) return -1;
int offset = 0;
do {
int length = Math.min(super.read(buffer, offset, buffer.length - offset), dataRemaining);
if (length == -1) {
if (offset > 0) return offset;
return -1;
}
offset += length;
dataRemaining -= length;
} while (offset < buffer.length);
return offset;
}
}
}