Load1.java revision 8729:0242fce0f717
133965Sjdp/*
278828Sobrien * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3218822Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
433965Sjdp *
533965Sjdp * This code is free software; you can redistribute it and/or modify it
633965Sjdp * under the terms of the GNU General Public License version 2 only, as
733965Sjdp * published by the Free Software Foundation.
833965Sjdp *
933965Sjdp * This code is distributed in the hope that it will be useful, but WITHOUT
1033965Sjdp * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1133965Sjdp * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1233965Sjdp * version 2 for more details (a copy is included in the LICENSE file that
1333965Sjdp * accompanied this code).
1433965Sjdp *
1533965Sjdp * You should have received a copy of the GNU General Public License version
1633965Sjdp * 2 along with this work; if not, write to the Free Software Foundation,
1733965Sjdp * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1833965Sjdp *
1933965Sjdp * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20218822Sdim * or visit www.oracle.com if you need additional information or have any
21218822Sdim * questions.
2233965Sjdp */
23130561Sobrien
24218822Sdim/* @test
25218822Sdim   @summary Test SoftTuning load method */
26218822Sdim
27218822Sdimimport java.io.UnsupportedEncodingException;
2833965Sjdp
29218822Sdimimport javax.sound.midi.MidiUnavailableException;
3033965Sjdpimport javax.sound.midi.Patch;
3133965Sjdpimport javax.sound.sampled.*;
3233965Sjdp
3333965Sjdpimport com.sun.media.sound.*;
3433965Sjdp
3533965Sjdppublic class Load1 {
3633965Sjdp
3760484Sobrien    private static void assertEquals(Object a, Object b) throws Exception
3833965Sjdp    {
3933965Sjdp        if(!a.equals(b))
4033965Sjdp            throw new RuntimeException("assertEquals fails!");
4133965Sjdp    }
4233965Sjdp
4333965Sjdp    private static void assertTrue(boolean value) throws Exception
4433965Sjdp    {
4533965Sjdp        if(!value)
46104834Sobrien            throw new RuntimeException("assertTrue fails!");
47104834Sobrien    }
48104834Sobrien
4933965Sjdp    public static void main(String[] args) throws Exception {
5060484Sobrien        // http://www.midi.org/about-midi/tuning.shtml
5177298Sobrien        // 0x01 BULK TUNING DUMP
5260484Sobrien        SoftTuning tuning = new SoftTuning();
5360484Sobrien        byte[] name;
5460484Sobrien        name = "Testing123      ".getBytes("ascii");
5560484Sobrien
5660484Sobrien        int[] msg = new int[24+3*128];
5733965Sjdp        int[] head = {0xf0,0x7e,0x7f,0x08,0x01,0x00};
5833965Sjdp        int ox = 0;
5933965Sjdp        for (int i = 0; i < head.length; i++)
6033965Sjdp            msg[ox++] = head[i];
6133965Sjdp        for (int i = 0; i < name.length; i++)
6233965Sjdp            msg[ox++] = name[i];
6333965Sjdp        for (int i = 0; i < 128; i++) {
6433965Sjdp            msg[ox++] = i;
6533965Sjdp            msg[ox++] = 64;
6633965Sjdp            msg[ox++] = 0;
6733965Sjdp        }
6833965Sjdp
6933965Sjdp        // Calc checksum
7033965Sjdp        int x = msg[1] & 0xFF; // 7E
7138889Sjdp        x = x ^ (msg[2] & 0xFF); // <device ID>
7238889Sjdp        x = x ^ (msg[4] & 0xFF); // nn
7338889Sjdp        x = x ^ (msg[5] & 0xFF); // tt
7438889Sjdp        for (int i = 22; i < msg.length - 2; i++)
7560484Sobrien            x = x ^ (msg[i] & 0xFF);
7660484Sobrien        msg[ox++] = (x & 127);
7760484Sobrien
7838889Sjdp        msg[ox++] = 0xf7;
7938889Sjdp        byte[] bmsg = new byte[msg.length];
8089857Sobrien        for (int i = 0; i < bmsg.length; i++)
8133965Sjdp            bmsg[i] = (byte)msg[i];
8233965Sjdp
8333965Sjdp        tuning.load(bmsg);
8489857Sobrien        assertEquals(tuning.getName(), "Testing123      ");
8533965Sjdp        double[] tunings = tuning.getTuning();
8633965Sjdp        for (int i = 0; i < tunings.length; i++)
8733965Sjdp            assertTrue(Math.abs(tunings[i]-(i*100 + 50)) < 0.00001);
8833965Sjdp
89130561Sobrien        // Check if tuning fails if checksum is wrong
9033965Sjdp        /*
9133965Sjdp        msg[msg.length - 2] += 10;
9233965Sjdp        for (int i = 0; i < bmsg.length; i++)
9377298Sobrien            bmsg[i] = (byte)msg[i];
9433965Sjdp        tuning = new SoftTuning();
9577298Sobrien        tuning.load(bmsg);
96130561Sobrien        assertTrue(!tuning.getName().equals("Testing123      "));
9733965Sjdp        */
9833965Sjdp    }
9960484Sobrien}
10033965Sjdp