use of javax.sound.midi.InvalidMidiDataException in project jdk8u_jdk by JetBrains.
the class SoftMidiAudioFileReader method getAudioInputStream.
public AudioInputStream getAudioInputStream(InputStream inputstream) throws UnsupportedAudioFileException, IOException {
inputstream.mark(200);
Sequence seq;
try {
seq = MidiSystem.getSequence(inputstream);
} catch (InvalidMidiDataException e) {
inputstream.reset();
throw new UnsupportedAudioFileException();
} catch (IOException e) {
inputstream.reset();
throw new UnsupportedAudioFileException();
}
return getAudioInputStream(seq);
}
use of javax.sound.midi.InvalidMidiDataException in project jdk8u_jdk by JetBrains.
the class SMFParser method getMidiFileFormatFromStream.
// $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat()
// returns format having invalid length
private MidiFileFormat getMidiFileFormatFromStream(InputStream stream, int fileLength, SMFParser smfParser) throws InvalidMidiDataException, IOException {
int maxReadLength = 16;
int duration = MidiFileFormat.UNKNOWN_LENGTH;
DataInputStream dis;
if (stream instanceof DataInputStream) {
dis = (DataInputStream) stream;
} else {
dis = new DataInputStream(stream);
}
if (smfParser == null) {
dis.mark(maxReadLength);
} else {
smfParser.stream = dis;
}
int type;
int numtracks;
float divisionType;
int resolution;
try {
int magic = dis.readInt();
if (!(magic == MThd_MAGIC)) {
// not MIDI
throw new InvalidMidiDataException("not a valid MIDI file");
}
// read header length
int bytesRemaining = dis.readInt() - 6;
type = dis.readShort();
numtracks = dis.readShort();
int timing = dis.readShort();
// decipher the timing code
if (timing > 0) {
// tempo based timing. value is ticks per beat.
divisionType = Sequence.PPQ;
resolution = timing;
} else {
// SMPTE based timing. first decipher the frame code.
int frameCode = -1 * (timing >> 8);
switch(frameCode) {
case 24:
divisionType = Sequence.SMPTE_24;
break;
case 25:
divisionType = Sequence.SMPTE_25;
break;
case 29:
divisionType = Sequence.SMPTE_30DROP;
break;
case 30:
divisionType = Sequence.SMPTE_30;
break;
default:
throw new InvalidMidiDataException("Unknown frame code: " + frameCode);
}
// now determine the timing resolution in ticks per frame.
resolution = timing & 0xFF;
}
if (smfParser != null) {
// remainder of this chunk
dis.skip(bytesRemaining);
smfParser.tracks = numtracks;
}
} finally {
// if only reading the file format, reset the stream
if (smfParser == null) {
dis.reset();
}
}
MidiFileFormat format = new MidiFileFormat(type, divisionType, resolution, fileLength, duration);
return format;
}
use of javax.sound.midi.InvalidMidiDataException in project jdk8u_jdk by JetBrains.
the class StandardMidiFileWriter method getFileStream.
//=================================================================================
private InputStream getFileStream(int type, Sequence sequence) throws IOException {
Track[] tracks = sequence.getTracks();
int bytesBuilt = 0;
int headerLength = 14;
int length = 0;
int timeFormat;
float divtype;
PipedOutputStream hpos = null;
DataOutputStream hdos = null;
PipedInputStream headerStream = null;
InputStream[] trackStreams = null;
InputStream trackStream = null;
InputStream fStream = null;
// Determine the filetype to write
if (type == MIDI_TYPE_0) {
if (tracks.length != 1) {
return null;
}
} else if (type == MIDI_TYPE_1) {
if (tracks.length < 1) {
// $$jb: 05.31.99: we _can_ write TYPE_1 if tracks.length==1
return null;
}
} else {
if (tracks.length == 1) {
type = MIDI_TYPE_0;
} else if (tracks.length > 1) {
type = MIDI_TYPE_1;
} else {
return null;
}
}
// Now build the file one track at a time
// Note that above we made sure that MIDI_TYPE_0 only happens
// if tracks.length==1
trackStreams = new InputStream[tracks.length];
int trackCount = 0;
for (int i = 0; i < tracks.length; i++) {
try {
trackStreams[trackCount] = writeTrack(tracks[i], type);
trackCount++;
} catch (InvalidMidiDataException e) {
if (Printer.err)
Printer.err("Exception in write: " + e.getMessage());
}
//bytesBuilt += trackStreams[i].getLength();
}
// Now seqence the track streams
if (trackCount == 1) {
trackStream = trackStreams[0];
} else if (trackCount > 1) {
trackStream = trackStreams[0];
for (int i = 1; i < tracks.length; i++) {
// don't include failed track streams
if (trackStreams[i] != null) {
trackStream = new SequenceInputStream(trackStream, trackStreams[i]);
}
}
} else {
throw new IllegalArgumentException("invalid MIDI data in sequence");
}
// Now build the header...
hpos = new PipedOutputStream();
hdos = new DataOutputStream(hpos);
headerStream = new PipedInputStream(hpos);
// Write the magic number
hdos.writeInt(MThd_MAGIC);
// Write the header length
hdos.writeInt(headerLength - 8);
// Write the filetype
if (type == MIDI_TYPE_0) {
hdos.writeShort(0);
} else {
// MIDI_TYPE_1
hdos.writeShort(1);
}
// Write the number of tracks
hdos.writeShort((short) trackCount);
// Determine and write the timing format
divtype = sequence.getDivisionType();
if (divtype == Sequence.PPQ) {
timeFormat = sequence.getResolution();
} else if (divtype == Sequence.SMPTE_24) {
timeFormat = (24 << 8) * -1;
timeFormat += (sequence.getResolution() & 0xFF);
} else if (divtype == Sequence.SMPTE_25) {
timeFormat = (25 << 8) * -1;
timeFormat += (sequence.getResolution() & 0xFF);
} else if (divtype == Sequence.SMPTE_30DROP) {
timeFormat = (29 << 8) * -1;
timeFormat += (sequence.getResolution() & 0xFF);
} else if (divtype == Sequence.SMPTE_30) {
timeFormat = (30 << 8) * -1;
timeFormat += (sequence.getResolution() & 0xFF);
} else {
// $$jb: 04.08.99: What to really do here?
return null;
}
hdos.writeShort(timeFormat);
// now construct an InputStream to become the FileStream
fStream = new SequenceInputStream(headerStream, trackStream);
hdos.close();
length = bytesBuilt + headerLength;
return fStream;
}
use of javax.sound.midi.InvalidMidiDataException in project jdk8u_jdk by JetBrains.
the class SMFParser method getSequence.
public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException {
SMFParser smfParser = new SMFParser();
MidiFileFormat format = getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, smfParser);
// must be MIDI Type 0 or Type 1
if ((format.getType() != 0) && (format.getType() != 1)) {
throw new InvalidMidiDataException("Invalid or unsupported file type: " + format.getType());
}
// construct the sequence object
Sequence sequence = new Sequence(format.getDivisionType(), format.getResolution());
// for each track, go to the beginning and read the track events
for (int i = 0; i < smfParser.tracks; i++) {
if (smfParser.nextTrack()) {
smfParser.readTrack(sequence.createTrack());
} else {
break;
}
}
return sequence;
}
use of javax.sound.midi.InvalidMidiDataException in project tika by apache.
the class MidiParser method parse.
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
metadata.set(Metadata.CONTENT_TYPE, "audio/midi");
XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
xhtml.startDocument();
// MidiSystem expects the stream to support the mark feature
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
try {
Sequence sequence = MidiSystem.getSequence(stream);
Track[] tracks = sequence.getTracks();
metadata.set("tracks", String.valueOf(tracks.length));
// TODO: Use XMPDM.TRACKS?
Patch[] patches = sequence.getPatchList();
metadata.set("patches", String.valueOf(patches.length));
float type = sequence.getDivisionType();
if (type == Sequence.PPQ) {
metadata.set("divisionType", "PPQ");
} else if (type == Sequence.SMPTE_24) {
metadata.set("divisionType", "SMPTE_24");
} else if (type == Sequence.SMPTE_25) {
metadata.set("divisionType", "SMPTE_25");
} else if (type == Sequence.SMPTE_30) {
metadata.set("divisionType", "SMPTE_30");
} else if (type == Sequence.SMPTE_30DROP) {
metadata.set("divisionType", "SMPTE_30DROP");
} else if (type == Sequence.SMPTE_24) {
metadata.set("divisionType", String.valueOf(type));
}
for (Track track : tracks) {
xhtml.startElement("p");
for (int i = 0; i < track.size(); i++) {
MidiMessage message = track.get(i).getMessage();
if (message instanceof MetaMessage) {
MetaMessage meta = (MetaMessage) message;
// Types 1-15 are reserved for text events
if (meta.getType() >= 1 && meta.getType() <= 15) {
// FIXME: What's the encoding?
xhtml.characters(new String(meta.getData(), ISO_8859_1));
}
}
}
xhtml.endElement("p");
}
} catch (InvalidMidiDataException ignore) {
// There is no way to know whether this exception was
// caused by the document being corrupted or by the format
// just being unsupported. So we do nothing.
}
xhtml.endDocument();
}
Aggregations