Subversion Repositories svn.mios

Rev

Rev 738 | 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 {
738 adamjking 30
    private Patch[] copyPasteBuffer;
688 rutgerv 31
    private InitPatches initPatches = new InitPatches();
32
    private FileHandler fileHandler = new FileHandler(initPatches);
738 adamjking 33
    private int[] currentPatchNumber = new int[] { 0 };
698 rutgerv 34
    private int currentBankNumber = 1;
293 rutgerv 35
    private int[] requestBankIndices;
738 adamjking 36
    private Bank[] patchBanks = new Bank[8];
213 tk 37
    private Boolean openEditor = false;
738 adamjking 38
    private int coresHardware = 15; // AND mask for selected cores --- we
808 adamjking 39
    // assume, that all cores are available by
40
    // default
293 rutgerv 41
    private int coresSelected = 1;
628 adamjking 42
 
213 tk 43
    private SysExController sysexController;
628 adamjking 44
 
738 adamjking 45
    public SIDLibController(SysExController sysexController) {
213 tk 46
        this.sysexController = sysexController;
47
        sysexController.addObserver(this);
688 rutgerv 48
        sysexController.setInitPatches(initPatches);
738 adamjking 49
 
50
        patchBanks[0] = new Bank(sysexController.getReceiver(), true,
51
                initPatches);
666 rutgerv 52
        for (int i = 1; i < patchBanks.length; i++) {
738 adamjking 53
            patchBanks[i] = new Bank(sysexController.getReceiver(), false,
54
                    initPatches);
213 tk 55
        }
738 adamjking 56
 
293 rutgerv 57
        requestBankIndices = new int[128];
628 adamjking 58
        for (int i = 0; i < requestBankIndices.length; i++) {
59
            requestBankIndices[i] = i;
60
        }
213 tk 61
    }
628 adamjking 62
 
63
    public void scanHardware() {
294 rutgerv 64
        sysexController.scanHardware();
65
    }
628 adamjking 66
 
293 rutgerv 67
    public void setCores(int i) {
628 adamjking 68
        coresSelected = i;
698 rutgerv 69
        setChanged();
70
        notifyObservers("Cores changed");
71
        clearChanged();
213 tk 72
    }
628 adamjking 73
 
293 rutgerv 74
    public int getCores() {
628 adamjking 75
        return coresSelected & coresHardware;
213 tk 76
    }
738 adamjking 77
 
698 rutgerv 78
    public int getCoresHardware() {
79
        return coresHardware;
80
    }
628 adamjking 81
 
82
    public void editCurrentPatch() {
83
        if (coresHardware == 0 | coresSelected == 0) {
213 tk 84
            // Open directly (offline mode)
85
            setChanged();
86
            notifyObservers("Edit");
87
            clearChanged();
88
        } else {
89
            if (sysexController.isDone()) {
90
                // Sends notification to SIDV2librarianGUI when done...
91
                openEditor = true;
738 adamjking 92
                sysexController.dumpPatchToBuffer(patchBanks[currentBankNumber]
93
                        .getPatchAt(currentPatchNumber[0]), getCores());
213 tk 94
            }
95
        }
96
    }
628 adamjking 97
 
213 tk 98
    public Patch getCurrentPatch() {
738 adamjking 99
        return patchBanks[currentBankNumber].getPatchAt(currentPatchNumber[0]);
213 tk 100
    }
738 adamjking 101
 
213 tk 102
    public Bank getBank(int bankNumber) {
103
        return patchBanks[bankNumber];
104
    }
738 adamjking 105
 
213 tk 106
    public void setCurrentBankNumber(int i) {
738 adamjking 107
        currentBankNumber = i;
698 rutgerv 108
        setChanged();
109
        notifyObservers("Bank changed");
110
        clearChanged();
213 tk 111
    }
628 adamjking 112
 
213 tk 113
    public int getCurrentBankNumber() {
114
        return currentBankNumber;
115
    }
628 adamjking 116
 
666 rutgerv 117
    public void setCurrentPatchNumber(int[] iarray) {
118
        currentPatchNumber = iarray;
213 tk 119
    }
628 adamjking 120
 
213 tk 121
    public int getCurrentPatchNumber() {
666 rutgerv 122
        return currentPatchNumber[0];
213 tk 123
    }
628 adamjking 124
 
213 tk 125
    public void setPatchAt(Patch p, int patchNumber, int bankNumber) {
628 adamjking 126
        patchBanks[bankNumber].setPatchAt(patchNumber, p);
213 tk 127
    }
738 adamjking 128
 
666 rutgerv 129
    public boolean isEnsembleBank() {
738 adamjking 130
        return currentBankNumber == 0;
666 rutgerv 131
    }
213 tk 132
 
133
    // ****************** Edit functions ***********************
134
    public void editCopy() {
738 adamjking 135
        copyPasteBuffer = patchBanks[currentBankNumber]
136
                .getPatchesAt(currentPatchNumber);
213 tk 137
    }
628 adamjking 138
 
213 tk 139
    public void editPaste() {
738 adamjking 140
        if ((copyPasteBuffer != null)
141
                && patchBanks[currentBankNumber].isEnsembleBank() == copyPasteBuffer[0]
142
                        .isEnsemble()) {
143
            for (int i = 0; i < copyPasteBuffer.length; i++) {
144
                if (currentPatchNumber[0] + i > patchBanks[currentBankNumber].bankSize - 1) {
666 rutgerv 145
                    break;
146
                } else {
738 adamjking 147
                    patchBanks[currentBankNumber].setPatchAt(
148
                            currentPatchNumber[0] + i, copyPasteBuffer[i]
149
                                    .clone());
666 rutgerv 150
                }
151
            }
152
            setChanged();
153
            notifyObservers("Data changed");
154
            clearChanged();
628 adamjking 155
        }
213 tk 156
    }
628 adamjking 157
 
213 tk 158
    public void editCut() {
738 adamjking 159
        copyPasteBuffer = patchBanks[currentBankNumber]
160
                .getPatchesAt(currentPatchNumber);
666 rutgerv 161
        editClear();
213 tk 162
    }
628 adamjking 163
 
213 tk 164
    public void editClear() {
666 rutgerv 165
        int s;
166
        if (patchBanks[currentBankNumber].isEnsembleBank()) {
167
            s = 256;
168
        } else {
169
            s = 512;
170
        }
738 adamjking 171
        for (int i = 0; i < currentPatchNumber.length; i++) {
172
            patchBanks[currentBankNumber].setPatchAt(currentPatchNumber[i],
173
                    new Patch(sysexController.getReceiver(), s, initPatches));
666 rutgerv 174
        }
213 tk 175
        setChanged();
176
        notifyObservers("Data changed");
177
        clearChanged();
178
    }
628 adamjking 179
 
213 tk 180
    public void editRename() {
181
        setChanged();
182
        notifyObservers("Rename");
183
        clearChanged();
184
    }
628 adamjking 185
 
213 tk 186
    public void initCurrentBank() {
738 adamjking 187
        if (currentBankNumber == 0) {
188
            patchBanks[currentBankNumber] = new Bank(sysexController
189
                    .getReceiver(), true, initPatches);
666 rutgerv 190
        } else {
738 adamjking 191
            patchBanks[currentBankNumber] = new Bank(sysexController
192
                    .getReceiver(), false, initPatches);
193
        }
213 tk 194
        setChanged();
195
        notifyObservers("Data changed");
196
        clearChanged();
197
    }
628 adamjking 198
 
213 tk 199
    public void initCurrentPatch(Object object) {
666 rutgerv 200
        if (patchBanks[currentBankNumber].isEnsembleBank()) {
738 adamjking 201
            for (int i = 0; i < currentPatchNumber.length; i++) {
202
                patchBanks[currentBankNumber].setPatchAt(currentPatchNumber[i],
203
                        new Patch(sysexController.getReceiver(), 256,
204
                                initPatches));
666 rutgerv 205
            }
738 adamjking 206
        } else if (object != null) {
207
            for (int i = 0; i < currentPatchNumber.length; i++) {
208
                patchBanks[currentBankNumber].setPatchAt(currentPatchNumber[i],
209
                        new Patch(sysexController.getReceiver(), 512,
210
                                initPatches));
211
                patchBanks[currentBankNumber].getPatchAt(currentPatchNumber[i])
212
                        .setEngine(object);
666 rutgerv 213
            }
214
        }
213 tk 215
        setChanged();
628 adamjking 216
        notifyObservers("Data changed");
213 tk 217
        clearChanged();
218
    }
628 adamjking 219
 
213 tk 220
    // ****************** Event handling functions ***********************
221
    public void update(Observable observable, Object object) {
628 adamjking 222
        if (object == "Hardware scan") {
223
            coresHardware = (Integer) sysexController.pickMeUp;
698 rutgerv 224
            setChanged();
225
            notifyObservers("Cores changed");
738 adamjking 226
            clearChanged();
628 adamjking 227
        } else if (object == "Patch ready") {
738 adamjking 228
            patchBanks[sysexController.requestBank + 1].setPatchAt(
229
                    sysexController.requestPatch[sysexController.requestCount],
230
                    (Patch) sysexController.pickMeUp);
213 tk 231
            setChanged();
232
            notifyObservers("Data changed");
233
            clearChanged();
628 adamjking 234
        } else if ((object == "Dump completed") && (openEditor)) {
213 tk 235
            openEditor = false;
236
            setChanged();
237
            notifyObservers("Edit");
628 adamjking 238
            clearChanged();
213 tk 239
        }
240
    }
628 adamjking 241
 
213 tk 242
    public void actionPerformed(ActionEvent ae) {
243
        if (ae.getActionCommand().equals("Load bank")) {
738 adamjking 244
            Bank tempBank = fileHandler.loadPatchBank(sysexController
245
                    .getReceiver(), patchBanks[currentBankNumber]
246
                    .isEnsembleBank());
628 adamjking 247
            if (tempBank != null) {
213 tk 248
                patchBanks[currentBankNumber] = tempBank;
249
                setChanged();
250
                notifyObservers("Data changed");
251
                clearChanged();
252
            }
253
        } else if (ae.getActionCommand().equals("Save bank")) {
738 adamjking 254
            fileHandler.savePatchBank(patchBanks[currentBankNumber],
255
                    currentBankNumber - 1);
213 tk 256
        } else if (ae.getActionCommand().equals("Load patch")) {
738 adamjking 257
            Patch tempPatch = fileHandler.loadPatch(sysexController
258
                    .getReceiver(), patchBanks[currentBankNumber]
259
                    .isEnsembleBank());
628 adamjking 260
            if (tempPatch != null) {
738 adamjking 261
                patchBanks[currentBankNumber].setPatchAt(currentPatchNumber[0],
262
                        tempPatch);
213 tk 263
                setChanged();
264
                notifyObservers("Data changed");
265
                clearChanged();
266
            }
267
        } else if (ae.getActionCommand().equals("Save patch")) {
738 adamjking 268
            fileHandler.savePatch(patchBanks[currentBankNumber]
269
                    .getPatchAt(currentPatchNumber[0]), currentBankNumber - 1,
270
                    currentPatchNumber[0]);
213 tk 271
        } else if (ae.getActionCommand().equals("Edit")) {
272
            editCurrentPatch();
273
        } else if (ae.getActionCommand().equals("Rename")) {
274
            editRename();
275
        } else if (ae.getActionCommand().equals("Cut")) {
276
            editCut();
277
        } else if (ae.getActionCommand().equals("Copy")) {
278
            editCopy();
279
        } else if (ae.getActionCommand().equals("Paste")) {
280
            editPaste();
281
        } else if (ae.getActionCommand().equals("Clear")) {
282
            editClear();
283
        } else if (ae.getActionCommand().equals("Init LEAD patch")) {
284
            initCurrentPatch(Patch.LEAD);
285
        } else if (ae.getActionCommand().equals("Init BASSLINE patch")) {
286
            initCurrentPatch(Patch.BASSLINE);
287
        } else if (ae.getActionCommand().equals("Init DRUM patch")) {
288
            initCurrentPatch(Patch.DRUM);
289
        } else if (ae.getActionCommand().equals("Init MULTI patch")) {
290
            initCurrentPatch(Patch.MULTI);
688 rutgerv 291
        } else if (ae.getActionCommand().equals("Init ensemble")) {
292
            initCurrentPatch(null);
213 tk 293
        } else if (ae.getActionCommand().equals("Init current bank")) {
294
            initCurrentBank();
293 rutgerv 295
        } else if (ae.getActionCommand().equals("Scan hardware")) {
296
            sysexController.scanHardware();
213 tk 297
        } else if (ae.getActionCommand().equals("Transmit patch to buffer")) {
738 adamjking 298
            sysexController.dumpPatchToBuffer(patchBanks[currentBankNumber]
299
                    .getPatchAt(currentPatchNumber[0]), getCores());
213 tk 300
        } else if (ae.getActionCommand().equals("Transmit patch to memory")) {
666 rutgerv 301
            Patch[] p = new Patch[currentPatchNumber.length];
738 adamjking 302
            for (int i = 0; i < currentPatchNumber.length; i++) {
303
                p[i] = patchBanks[currentBankNumber]
304
                        .getPatchAt(currentPatchNumber[i]);
666 rutgerv 305
            }
738 adamjking 306
            sysexController.dumpPatch(p, currentPatchNumber,
307
                    currentBankNumber - 1);
213 tk 308
        } else if (ae.getActionCommand().equals("Transmit bank to memory")) {
738 adamjking 309
            sysexController.dumpPatchBank(patchBanks[currentBankNumber],
310
                    currentBankNumber - 1);
213 tk 311
        } else if (ae.getActionCommand().equals("Receive patch from buffer")) {
738 adamjking 312
            sysexController.requestPatchBuffer(0, currentPatchNumber[0],
313
                    currentBankNumber - 1);
628 adamjking 314
        } else if (ae.getActionCommand().equals("Receive patch from memory")) {
738 adamjking 315
            sysexController.requestPatch(currentPatchNumber,
316
                    currentBankNumber - 1);
213 tk 317
        } else if (ae.getActionCommand().equals("Receive bank from memory")) {
738 adamjking 318
            sysexController.requestPatch(requestBankIndices,
319
                    currentBankNumber - 1);
213 tk 320
        }
321
    }
322
}