Subversion Repositories svn.mios

Rev

Rev 690 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
688 rutgerv 1
package org.midibox.sidlibr;
2
 
3
import java.io.IOException;
689 adamjking 4
import java.io.InputStream;
688 rutgerv 5
 
6
import javax.swing.JOptionPane;
7
 
8
import org.midibox.midi.MidiUtils;
689 adamjking 9
import org.midibox.utils.ResourceLoader;
688 rutgerv 10
 
11
public class InitPatches {
12
    private int[] lead;
13
    private int[] bassline;
14
    private int[] drum;
15
    private int[] multi;
16
    private int[] ensemble;
738 adamjking 17
 
688 rutgerv 18
    public InitPatches() {
738 adamjking 19
        lead = loadPatch("lead.syx", 512);
20
        bassline = loadPatch("bassline.syx", 512);
21
        drum = loadPatch("drum.syx", 512);
22
        multi = loadPatch("multi.syx", 512);
23
        ensemble = loadPatch("ensemble.syx", 256);
688 rutgerv 24
    }
738 adamjking 25
 
688 rutgerv 26
    public int[] getInitLead() {
27
        return lead.clone();
28
    }
738 adamjking 29
 
688 rutgerv 30
    public int[] getInitBassline() {
31
        return bassline.clone();
32
    }
738 adamjking 33
 
688 rutgerv 34
    public int[] getInitDrum() {
35
        return drum.clone();
36
    }
738 adamjking 37
 
688 rutgerv 38
    public int[] getInitMulti() {
39
        return multi.clone();
40
    }
738 adamjking 41
 
688 rutgerv 42
    public int[] getInitEnsemble() {
43
        return ensemble.clone();
44
    }
738 adamjking 45
 
688 rutgerv 46
    public int[] loadPatch(String filename, int patchSize) {
47
        int[] patch = new int[patchSize];
48
        try {
49
            byte[] data = loadData(filename);
50
            if (data != null) {
738 adamjking 51
                String s = MidiUtils.getHexString(data).replace(" ", "");
688 rutgerv 52
                int dsi = 20; // Index where real patch data starts
53
                for (int i = 0; i < patchSize; i++) {
738 adamjking 54
                    String s1 = s.substring((4 * i) + dsi + 1, (4 * i) + dsi
55
                            + 2);
56
                    String s2 = s.substring((4 * i) + dsi + 3, (4 * i) + dsi
57
                            + 4);
688 rutgerv 58
                    int temp = Integer.parseInt(s1 + s2, 16);
59
                    patch[i] = temp;
60
                }
61
            }
62
        } catch (IOException e) {
738 adamjking 63
            JOptionPane.showMessageDialog(null,
64
                    "An error has occurred while reading built-in init patch!",
65
                    "Error", JOptionPane.ERROR_MESSAGE);
688 rutgerv 66
        }
67
        return patch;
68
    }
738 adamjking 69
 
688 rutgerv 70
    private byte[] loadData(String s) throws IOException {
71
        byte[] b = null;
690 tk 72
        b = new byte[65536];
73
        int numBytes = 0;
74
        InputStream in = null;
688 rutgerv 75
        try {
738 adamjking 76
            in = ResourceLoader.getResourceAsStream("patches/" + s);
77
            numBytes = in.read(b, 0, 65536);
690 tk 78
        } finally {
738 adamjking 79
            if (in != null) {
80
                in.close();
81
            }
688 rutgerv 82
        }
690 tk 83
        // TODO: we return an array with 65536 members...
738 adamjking 84
        // it would be better to extract [0:numBytes], but I don't know how to
85
        // do this w/o creating a new array...
688 rutgerv 86
        return b;
87
    }
88
}