-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMsInputEngine.java
112 lines (95 loc) · 3.43 KB
/
MsInputEngine.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
/*
* Copyright (c) 2011 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
package vavi.sound.adpcm.ms;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.nio.ByteOrder;
import vavi.io.InputEngine;
import vavi.io.LittleEndianDataInputStream;
import vavix.io.IOStreamInputEngine;
import static java.lang.System.getLogger;
/**
* MsInputEngine.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (umjammer)
* @version 0.00 2011/10/18 umjammer initial version <br>
*/
class MsInputEngine implements InputEngine {
private static final Logger logger = getLogger(MsInputEngine.class.getName());
private final OutputStream out;
private final Ms encoder = new Ms();
private InputStream in;
private final int samplesPerBlock;
private final int channels;
private final ByteOrder byteOrder;
/** */
public MsInputEngine(OutputStream out,
int samplesPerBlock,
int channels,
ByteOrder byteOrder) {
this.out = out;
this.samplesPerBlock = samplesPerBlock;
this.channels = channels;
this.byteOrder = byteOrder;
logger.log(Level.DEBUG, "byteOrder: " + this.byteOrder);
}
@Override
public void initialize(InputStream in) throws IOException {
if (this.in != null) {
throw new IOException("Already initialized");
} else {
this.in = in;
}
}
/** */
private final int[] steps = new int[16];
/**
* because {@link #in} reads only {@link #samplesPerBlock} * 2 bytes, bufferSize for
* {@link IOStreamInputEngine#IOStreamInputEngine(OutputStream, vavix.io.IOStreamInputEngine.InputStreamFactory, int)}
* of {@link IOStreamInputEngine} must be set {@link #samplesPerBlock} * 2.
*/
@Override
public void execute() throws IOException {
if (in == null) {
throw new IOException("Not yet initialized");
} else {
int bytesPerBlock = Ms.getBytesPerBlock(channels, samplesPerBlock);
//logger.log(Level.TRACE, "bytesPerBlock: " + bytesPerBlock + ", samplesPerBlock: " + samplesPerBlock);
byte[] buffer = new byte[samplesPerBlock * 2];
int l = 0;
while (l < buffer.length) {
int r = in.read(buffer, l, buffer.length - l);
if (r < 0) {
break;
}
l += r;
}
//logger.log(Level.TRACE, StringUtil.getDump(buffer, 128));
if (l > 0) {
byte[] adpcm = new byte[bytesPerBlock];
int[] pcm = new int[l / 2];
//logger.log(Level.TRACE, "adpcm: " + bytesPerBlock + ", pcm: " + pcm.length + ", " + l);
LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(new ByteArrayInputStream(buffer));
for (int i = 0; i < pcm.length; i++) {
pcm[i] = ledis.readShort();
}
ledis.close();
encoder.encodeBlock(1, pcm, pcm.length, steps, adpcm, bytesPerBlock);
//logger.log(Level.TRACE, StringUtil.getDump(adpcm, 128));
out.write(adpcm);
}
}
}
@Override
public void finish() throws IOException {
out.flush();
out.close();
}
}