Home  | My Account  |  Membership
WorldDeveloper.org
Forum Details
Topics
Posts
Categories
 
577 member(s)
718,905 hit(s)
14 categories
51 topics
26 messages
0 online
Forums: Code Post
MIDI Converter, Type 1 to Type 0
hostj2me
43 topic(s)
23 post(s)

September 14, 2007 7:46 PM


/*
* Copyright (C) 2006 Astrient Labs, LLC 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.
*
* Astrient Labs, LLC
* www.astrientlabs.com
* rashid@astrientlabs.com
* Rashid Mayes 2005
*/
package com.astrientlabs.audio;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiFileFormat;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Track;

public class MidiConverter
{
public static boolean isType1(byte[] data) throws InvalidMidiDataException, IOException
{
return isType1(new ByteArrayInputStream(data));
}

public static boolean isType1(InputStream is) throws InvalidMidiDataException, IOException
{
MidiFileFormat format = MidiSystem.getMidiFileFormat(is);
return (format.getType() == 1);
}

public static byte[] convertToType(byte[] data, int maxTracks) throws InvalidMidiDataException, IOException
{
return convertToType0(new ByteArrayInputStream(data), maxTracks);
}

public static byte[] convertToType0(InputStream is, int maxTracks) throws InvalidMidiDataException, IOException
{
Sequence sequence = MidiSystem.getSequence(is);

Track[] tracks = sequence.getTracks();
Track first = tracks[0];
Track track;

for (int i = 1; i tracks.length; i++)
{
track = tracks[i];
if (i maxTracks)
for (int j = 0, stop = track.size(); j stop; first.add(track.get(j++)))
;
sequence.deleteTrack(track);
}

ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
MidiSystem.write(sequence, 0, os);

return os.toByteArray();
}

public static void main(String[] args)
{
try
{
RandomAccessFile raf = new RandomAccessFile(new File("G:/in.mid"), "r");
byte[] data = new byte[(int) raf.length()];
raf.readFully(data);

ByteArrayInputStream bais = new ByteArrayInputStream(data);

if (isType1(bais))
{
bais.reset();
data = convertToType0(bais, 8);

FileOutputStream fos = new FileOutputStream(new File("g:out.mid"));
fos.write(data);
fos.close(); // finally:)
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
}


©2008 WorldDeveloper.org - PO Box 310575 Miami, FL 33231

WorldDeveloper.org is a managed social network for application developers around the world.