-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDecoderTest.java
More file actions
251 lines (225 loc) Β· 9.17 KB
/
Copy pathDecoderTest.java
File metadata and controls
251 lines (225 loc) Β· 9.17 KB
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
/*
* Copyright (c) 2012 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.spi.AudioFileReader;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import vavi.util.Debug;
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static vavi.sound.SoundUtil.volume;
import static vavix.util.DelayedWorker.later;
/**
* DecoderTest.
*
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
* @version 0.00 2012/06/11 umjammer initial version <br>
*/
@PropsEntity(url = "file://${user.dir}/local.properties")
class DecoderTest {
static boolean localPropertiesExists() {
return Files.exists(Paths.get("local.properties"));
}
@Property
String inFile = "src/test/resources/test.mp3";
/** */
public static void main(String[] args) throws Exception {
for (AudioFileFormat.Type type : AudioSystem.getAudioFileTypes()) {
System.err.println(type);
}
DecoderTest app = new DecoderTest();
app.setup();
app.test2();
}
/** play time limit in milliseconds */
static final long time = System.getProperty("vavi.test", "").equals("ide") ? 600 * 1000 : 3 * 1000;
@Property(name = "vavi.test.volume")
double volume = 0.2;
@BeforeEach
void setup() throws Exception {
if (localPropertiesExists()) {
PropsEntity.Util.bind(this);
}
Debug.print("time: " + time);
Debug.print("volume: " + volume);
}
@Test
@DisplayName("just play")
void test2() throws Exception {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(Paths.get(inFile).toFile());
play(audioInputStream);
}
/** */
void play(AudioInputStream originalAudioInputStream) throws Exception {
AudioFormat originalAudioFormat = originalAudioInputStream.getFormat();
Debug.println(originalAudioFormat);
AudioFormat targetAudioFormat = new AudioFormat( //PCM
originalAudioFormat.getSampleRate(),
16,
originalAudioFormat.getChannels(),
true,
false);
Debug.println(targetAudioFormat);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(targetAudioFormat, originalAudioInputStream);
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.addLineListener(event -> {
if (event.getType().equals(LineEvent.Type.START)) {
Debug.println("play");
}
if (event.getType().equals(LineEvent.Type.STOP)) {
Debug.println("done");
}
});
byte[] buf = new byte[8192];
line.open(audioFormat, buf.length);
volume(line, volume);
line.start();
int r = 0;
while (!later(time).come()) {
r = audioInputStream.read(buf, 0, buf.length);
if (r < 0) {
break;
}
line.write(buf, 0, r);
}
line.drain();
line.close();
}
@Test
@DisplayName("https://github.com/umjammer/mp3spi/issues/5")
void test3() throws Exception {
assertThrows(UnsupportedAudioFileException.class, () -> {
Path in = Paths.get(DecoderTest.class.getResource("/test.caf").toURI());
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(Files.newInputStream(in)));
Debug.println(ais);
}, "spi consumes all bytes, and eof make stream unresettable");
}
@Test
@DisplayName("spi settings")
@SuppressWarnings({ "unchecked", "rawtypes", "restriction" })
void test4() throws Exception {
List<AudioFileReader> providers = (List) com.sun.media.sound.JDK13Services.getProviders(AudioFileReader.class);
providers.forEach(System.err::println);
assertTrue(providers.stream().map(o -> o.getClass().getName()).anyMatch(s -> s.contains("javazoom.spi.mpeg.sampled.file.MpegAudioFileReader")));
}
@Test
void test5() throws Exception {
String file = "src/test/resources/test2.mp3";
InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(file)));
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
play(audioInputStream);
}
@Test
@DisplayName("i18n")
void test6() throws Exception {
String file = "src/test/resources/test2.mp3";
InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(file)));
AudioFileFormat format = AudioSystem.getAudioFileFormat(is);
format.properties().forEach((k, v) -> System.err.println(k + ": " + v));
String genre = (String) format.properties().get("mp3.id3tag.genre");
Debug.println("genre: " + genre);
assertEquals("Pop", genre);
assertEquals("ζ₯ζ¬θͺ", format.properties().get("title"));
assertEquals("γ’γ«γγ ", format.properties().get("album"));
assertEquals("γ«γ»γγ", format.properties().get("author"));
assertEquals("γ³γ‘γ³γ", format.properties().get("comment"));
}
@Test
@Disabled
@DisplayName("test all mp3s in your Music.app music")
void test() throws IOException {
Path root = Paths.get(System.getProperty("user.home"), "Music", "Media.localized", "Music");
Debug.println("ROOT: " + Files.exists(root));
AtomicInteger count = new AtomicInteger();
AtomicInteger error = new AtomicInteger();
Files.walkFileTree(root, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
if (file.getFileName().toString().toLowerCase().endsWith(".mp3")) {
//Debug.println(file);
count.incrementAndGet();
try {
AudioSystem.getAudioInputStream(file.toFile());
} catch (Exception e) {
try {
Debug.println("ERROR: " + file + ", " + Files.size(file));
} catch (Exception ignored) {
}
error.incrementAndGet();
}
}
return FileVisitResult.CONTINUE;
}
});
Debug.println("RESULT: " + error + "/" + count);
}
@Test
@EnabledIf("localPropertiesExists")
void testIssue16() throws Exception {
String file = "tmp/pcm-48k_24.wav";
Debug.println(file);
InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(file)));
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
play(audioInputStream);
}
// TODO noise
// $ play -v 0.02 src/test/resources/with-junk.wav got
// ... play WARN wav: Premature EOF on .wav input file
@Test
@DisplayName("wave reader spi will catch at first")
void testIssue16_1() throws Exception {
String in = "src/test/resources/with-junk.wav";
InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(in)));
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
assertEquals(AudioFormat.Encoding.PCM_SIGNED, audioInputStream.getFormat().getEncoding());
//play(audioInputStream);
}
@Test
@DisplayName("reader direct cause unhandled")
void testIssue16_2() throws Exception {
String in = "src/test/resources/with-junk.wav";
InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(in)));
Exception e = assertThrows(UnsupportedAudioFileException.class, () -> {
new MpegAudioFileReader().getAudioInputStream(is);
});
Debug.println(e);
}
@Test
@DisplayName("mp3 in wav")
void testIssue16_3() throws Exception {
String in = "src/test/resources/mp3.wav";
InputStream is = new BufferedInputStream(Files.newInputStream(Paths.get(in)));
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(is);
play(audioInputStream);
}
}