-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMfiMidiFileReader.java
79 lines (66 loc) · 2.48 KB
/
MfiMidiFileReader.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
/*
* Copyright (c) 2003 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/
package vavi.sound.midi.mfi;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.Arrays;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.Sequence;
import vavi.sound.mfi.InvalidMfiDataException;
import vavi.sound.mfi.MfiSystem;
import vavi.sound.mfi.MfiUnavailableException;
import vavi.sound.midi.BasicMidiFileReader;
import static java.lang.System.getLogger;
/**
* MfiMidiFileReader implemented by vavi.sound.mfi.vavi package
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 030817 nsano initial version <br>
*/
public class MfiMidiFileReader extends BasicMidiFileReader {
private static final Logger logger = getLogger(MfiMidiFileReader.class.getName());
/** Gets a MIDI Sequence converted from MFi */
@Override
public Sequence getSequence(InputStream is) throws InvalidMidiDataException, IOException {
boolean reset = false;
try {
if (!is.markSupported()) {
throw new IOException("mark not supported: " + is);
}
is.mark(4); // 4 means MfiSystem#getSequence is able to determine it 4 bytes
DataInputStream dis = new DataInputStream(is);
byte[] b = new byte[4];
dis.readFully(b);
if (!Arrays.equals(b, "melo".getBytes())) {
throw new InvalidMidiDataException("not MFi signature");
} else {
is.reset();
reset = true;
}
vavi.sound.mfi.Sequence mfiSequence = MfiSystem.getSequence(is);
//logger.log(Level.TRACE, mfiSequence);
return MfiSystem.toMidiSequence(mfiSequence);
} catch (InvalidMfiDataException e) {
logger.log(Level.DEBUG, e);
logger.log(Level.TRACE, e.getMessage(), e);
throw (InvalidMidiDataException) new InvalidMidiDataException().initCause(e);
} catch (MfiUnavailableException e) {
logger.log(Level.ERROR, e.getMessage(), e);
throw new IOException(e);
} finally {
try {
if (!reset) {
is.reset();
}
} catch (IOException e) {
logger.log(Level.DEBUG, e);
}
}
}
}