Subversion Repositories svn.mios

Rev

Rev 504 | Rev 666 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)SIDV2librarian.java  beta1   2008/01/21
3
 *
4
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.sidlibr;
22
 
628 adamjking 23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
213 tk 25
import java.util.Observable;
26
import java.util.Observer;
27
 
628 adamjking 28
public class SIDLibController extends Observable implements Observer,
29
        ActionListener {
30
    private Object copyPasteBuffer;
31
    private FileHandler fileHandler = new FileHandler();
213 tk 32
 
33
    private int currentPatchNumber = 0;
34
    private int currentBankNumber = 0;
293 rutgerv 35
    private int[] requestBankIndices;
213 tk 36
    private Bank[] patchBanks = new Bank[7];
37
    private EnsBank ensBank;
628 adamjking 38
 
213 tk 39
    private Boolean openEditor = false;
628 adamjking 40
    private int coresHardware = 15; // AND mask for selected cores --- we
41
    // assume, that all cores are available by
42
    // default
293 rutgerv 43
    private int coresSelected = 1;
628 adamjking 44
 
213 tk 45
    private SysExController sysexController;
628 adamjking 46
 
213 tk 47
    public SIDLibController(SysExController sysexController) {
48
        this.sysexController = sysexController;
49
        sysexController.addObserver(this);
628 adamjking 50
        // ensBank = new EnsBank(sysexController.getReceiver());
51
        for (int i = 0; i < patchBanks.length; i++) {
213 tk 52
            patchBanks[i] = new Bank(sysexController.getReceiver());
53
        }
293 rutgerv 54
        requestBankIndices = new int[128];
628 adamjking 55
        for (int i = 0; i < requestBankIndices.length; i++) {
56
            requestBankIndices[i] = i;
57
        }
213 tk 58
    }
628 adamjking 59
 
60
    public void scanHardware() {
294 rutgerv 61
        sysexController.scanHardware();
62
    }
628 adamjking 63
 
293 rutgerv 64
    public void setCores(int i) {
628 adamjking 65
        coresSelected = i;
213 tk 66
    }
628 adamjking 67
 
293 rutgerv 68
    public int getCores() {
628 adamjking 69
        return coresSelected & coresHardware;
213 tk 70
    }
628 adamjking 71
 
72
    public void editCurrentPatch() {
73
        if (coresHardware == 0 | coresSelected == 0) {
213 tk 74
            // Open directly (offline mode)
75
            setChanged();
76
            notifyObservers("Edit");
77
            clearChanged();
78
        } else {
79
            if (sysexController.isDone()) {
80
                // Sends notification to SIDV2librarianGUI when done...
81
                openEditor = true;
628 adamjking 82
                sysexController.dumpPatchToBuffer(patchBanks[currentBankNumber]
83
                        .getPatchAt(currentPatchNumber), getCores());
213 tk 84
            }
85
        }
86
    }
628 adamjking 87
 
213 tk 88
    public Patch getCurrentPatch() {
89
        return patchBanks[currentBankNumber].getPatchAt(currentPatchNumber);
90
    }
628 adamjking 91
 
213 tk 92
    public Bank getBank(int bankNumber) {
93
        return patchBanks[bankNumber];
94
    }
628 adamjking 95
 
213 tk 96
    public EnsBank getEnsBank() {
97
        return ensBank;
98
    }
628 adamjking 99
 
213 tk 100
    public void setCurrentBankNumber(int i) {
101
        currentBankNumber = i;
102
    }
628 adamjking 103
 
213 tk 104
    public int getCurrentBankNumber() {
105
        return currentBankNumber;
106
    }
628 adamjking 107
 
213 tk 108
    public void setCurrentPatchNumber(int i) {
109
        currentPatchNumber = i;
110
    }
628 adamjking 111
 
213 tk 112
    public int getCurrentPatchNumber() {
113
        return currentPatchNumber;
114
    }
628 adamjking 115
 
213 tk 116
    public void setPatchAt(Patch p, int patchNumber, int bankNumber) {
628 adamjking 117
        patchBanks[bankNumber].setPatchAt(patchNumber, p);
213 tk 118
    }
119
 
120
    // ****************** Edit functions ***********************
121
    public void editCopy() {
628 adamjking 122
        copyPasteBuffer = patchBanks[currentBankNumber]
123
                .getPatchAt(currentPatchNumber);
213 tk 124
    }
628 adamjking 125
 
213 tk 126
    public void editPaste() {
628 adamjking 127
        if ((copyPasteBuffer.getClass() == Patch.class)
128
                && (patchBanks[currentBankNumber]
129
                        .getPatchAt(currentPatchNumber).getClass() == Patch.class)) {
130
            patchBanks[currentBankNumber].setPatchAt(currentPatchNumber,
131
                    ((Patch) copyPasteBuffer).clone());
132
        }
213 tk 133
        setChanged();
134
        notifyObservers("Data changed");
135
        clearChanged();
136
    }
628 adamjking 137
 
213 tk 138
    public void editSwap(int a, int b) {
628 adamjking 139
        Patch temp_a = patchBanks[currentBankNumber].getPatchAt(a);
140
        Patch temp_b = patchBanks[currentBankNumber].getPatchAt(b);
141
        patchBanks[currentBankNumber].setPatchAt(a, temp_b);
142
        patchBanks[currentBankNumber].setPatchAt(b, temp_a);
143
        setChanged();
144
        notifyObservers("Data changed");
145
        clearChanged();
213 tk 146
    }
628 adamjking 147
 
213 tk 148
    public void editCut() {
628 adamjking 149
        copyPasteBuffer = patchBanks[currentBankNumber]
150
                .getPatchAt(currentPatchNumber);
151
        patchBanks[currentBankNumber].setPatchAt(currentPatchNumber, new Patch(
152
                sysexController.getReceiver()));
213 tk 153
        setChanged();
154
        notifyObservers("Data changed");
155
        clearChanged();
156
    }
628 adamjking 157
 
213 tk 158
    public void editClear() {
628 adamjking 159
        patchBanks[currentBankNumber].setPatchAt(currentPatchNumber, new Patch(
160
                sysexController.getReceiver()));
213 tk 161
        setChanged();
162
        notifyObservers("Data changed");
163
        clearChanged();
164
    }
628 adamjking 165
 
213 tk 166
    public void editRename() {
167
        setChanged();
168
        notifyObservers("Rename");
169
        clearChanged();
170
    }
628 adamjking 171
 
213 tk 172
    public void initCurrentBank() {
173
        patchBanks[currentBankNumber] = new Bank(sysexController.getReceiver());
174
        setChanged();
175
        notifyObservers("Data changed");
176
        clearChanged();
177
    }
628 adamjking 178
 
213 tk 179
    public void initCurrentPatch(Object object) {
628 adamjking 180
        patchBanks[currentBankNumber].setPatchAt(currentPatchNumber, new Patch(
181
                sysexController.getReceiver()));
182
        patchBanks[currentBankNumber].getPatchAt(currentPatchNumber).setEngine(
183
                object);
213 tk 184
        setChanged();
628 adamjking 185
        notifyObservers("Data changed");
213 tk 186
        clearChanged();
187
    }
628 adamjking 188
 
213 tk 189
    // ****************** Event handling functions ***********************
190
    public void update(Observable observable, Object object) {
628 adamjking 191
        if (object == "Hardware scan") {
192
            coresHardware = (Integer) sysexController.pickMeUp;
504 rutgerv 193
            System.out.println(coresHardware);
628 adamjking 194
        } else if (object == "Patch ready") {
195
            patchBanks[sysexController.requestBank].setPatchAt(
196
                    sysexController.requestPatch[sysexController.requestCount],
197
                    (Patch) sysexController.pickMeUp);
213 tk 198
            setChanged();
199
            notifyObservers("Data changed");
200
            clearChanged();
628 adamjking 201
        } else if (object == "Ensemble ready") {
202
            // ensBanks[currentBankNumber].setEnsAt(currentPatchNumber,
203
            // (Ensemble)sysexController.pickMeUp);
213 tk 204
            setChanged();
205
            notifyObservers("Data changed");
628 adamjking 206
            clearChanged();
207
        } else if ((object == "Dump completed") && (openEditor)) {
213 tk 208
            openEditor = false;
209
            setChanged();
210
            notifyObservers("Edit");
628 adamjking 211
            clearChanged();
213 tk 212
        }
213
    }
628 adamjking 214
 
213 tk 215
    public void actionPerformed(ActionEvent ae) {
216
        if (ae.getActionCommand().equals("Load bank")) {
628 adamjking 217
            Bank tempBank = fileHandler.loadPatchBank(sysexController
218
                    .getReceiver());
219
            if (tempBank != null) {
213 tk 220
                patchBanks[currentBankNumber] = tempBank;
221
                setChanged();
222
                notifyObservers("Data changed");
223
                clearChanged();
224
            }
225
        } else if (ae.getActionCommand().equals("Save bank")) {
628 adamjking 226
            fileHandler.savePatchBank(patchBanks[currentBankNumber],
227
                    currentBankNumber);
213 tk 228
        } else if (ae.getActionCommand().equals("Load patch")) {
628 adamjking 229
            Patch tempPatch = fileHandler.loadPatch(sysexController
230
                    .getReceiver());
231
            if (tempPatch != null) {
232
                patchBanks[currentBankNumber].setPatchAt(currentPatchNumber,
233
                        tempPatch);
213 tk 234
                setChanged();
235
                notifyObservers("Data changed");
236
                clearChanged();
237
            }
238
        } else if (ae.getActionCommand().equals("Save patch")) {
628 adamjking 239
            fileHandler.savePatch(patchBanks[currentBankNumber]
240
                    .getPatchAt(currentPatchNumber), currentBankNumber,
241
                    currentPatchNumber);
213 tk 242
        } else if (ae.getActionCommand().equals("Edit")) {
243
            editCurrentPatch();
244
        } else if (ae.getActionCommand().equals("Rename")) {
245
            editRename();
246
        } else if (ae.getActionCommand().equals("Cut")) {
247
            editCut();
248
        } else if (ae.getActionCommand().equals("Copy")) {
249
            editCopy();
250
        } else if (ae.getActionCommand().equals("Paste")) {
251
            editPaste();
252
        } else if (ae.getActionCommand().equals("Clear")) {
253
            editClear();
254
        } else if (ae.getActionCommand().equals("Init LEAD patch")) {
255
            initCurrentPatch(Patch.LEAD);
256
        } else if (ae.getActionCommand().equals("Init BASSLINE patch")) {
257
            initCurrentPatch(Patch.BASSLINE);
258
        } else if (ae.getActionCommand().equals("Init DRUM patch")) {
259
            initCurrentPatch(Patch.DRUM);
260
        } else if (ae.getActionCommand().equals("Init MULTI patch")) {
261
            initCurrentPatch(Patch.MULTI);
262
        } else if (ae.getActionCommand().equals("Init current bank")) {
263
            initCurrentBank();
293 rutgerv 264
        } else if (ae.getActionCommand().equals("Scan hardware")) {
265
            sysexController.scanHardware();
213 tk 266
        } else if (ae.getActionCommand().equals("Transmit patch to buffer")) {
628 adamjking 267
            sysexController.dumpPatchToBuffer(patchBanks[currentBankNumber]
268
                    .getPatchAt(currentPatchNumber), getCores());
213 tk 269
        } else if (ae.getActionCommand().equals("Transmit patch to memory")) {
628 adamjking 270
            sysexController.dumpPatch(
271
                    new Patch[] { patchBanks[currentBankNumber]
272
                            .getPatchAt(currentPatchNumber) },
273
                    new int[] { currentPatchNumber }, currentBankNumber);
213 tk 274
        } else if (ae.getActionCommand().equals("Transmit bank to memory")) {
628 adamjking 275
            sysexController.dumpPatchBank(patchBanks[currentBankNumber],
276
                    currentBankNumber);
213 tk 277
        } else if (ae.getActionCommand().equals("Receive patch from buffer")) {
628 adamjking 278
            sysexController.requestPatchBuffer(0, currentPatchNumber,
279
                    currentBankNumber);
280
        } else if (ae.getActionCommand().equals("Receive patch from memory")) {
281
            sysexController.requestPatch(new int[] { currentPatchNumber },
282
                    currentBankNumber);
213 tk 283
        } else if (ae.getActionCommand().equals("Receive bank from memory")) {
628 adamjking 284
            sysexController.requestPatch(requestBankIndices, currentBankNumber);
213 tk 285
        }
286
    }
287
}