Load5.java revision 8729:0242fce0f717
138774Snsouch/*
293023Snsouch * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
338774Snsouch * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
438774Snsouch *
538774Snsouch * This code is free software; you can redistribute it and/or modify it
638774Snsouch * under the terms of the GNU General Public License version 2 only, as
738774Snsouch * published by the Free Software Foundation.
838774Snsouch *
938774Snsouch * This code is distributed in the hope that it will be useful, but WITHOUT
1038774Snsouch * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1138774Snsouch * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1238774Snsouch * version 2 for more details (a copy is included in the LICENSE file that
1338774Snsouch * accompanied this code).
1438774Snsouch *
1538774Snsouch * You should have received a copy of the GNU General Public License version
1638774Snsouch * 2 along with this work; if not, write to the Free Software Foundation,
1738774Snsouch * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1838774Snsouch *
1938774Snsouch * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2038774Snsouch * or visit www.oracle.com if you need additional information or have any
2138774Snsouch * questions.
2238774Snsouch */
2338774Snsouch
2438774Snsouch/* @test
2538774Snsouch   @summary Test SoftTuning load method */
2638774Snsouch
27119418Sobrienimport java.io.UnsupportedEncodingException;
28119418Sobrien
29119418Sobrienimport javax.sound.midi.MidiUnavailableException;
3038774Snsouchimport javax.sound.midi.Patch;
3138774Snsouchimport javax.sound.sampled.*;
3238774Snsouch
3338774Snsouchimport com.sun.media.sound.*;
3438774Snsouch
3538774Snsouchpublic class Load5 {
3638774Snsouch
37181304Sjhb    private static void assertEquals(Object a, Object b) throws Exception
38167856Simp    {
3938774Snsouch        if(!a.equals(b))
40181304Sjhb            throw new RuntimeException("assertEquals fails!");
41282674Sloos    }
42274641Sian
43289656Sdumbbell    private static void assertTrue(boolean value) throws Exception
4438774Snsouch    {
4538774Snsouch        if(!value)
4638774Snsouch            throw new RuntimeException("assertTrue fails!");
4738774Snsouch    }
4838774Snsouch
4938774Snsouch    public static void main(String[] args) throws Exception {
50129152Sjoerg        // http://www.midi.org/about-midi/tuning_extens.shtml
51129152Sjoerg        // 0x05 SCALE/OCTAVE TUNING DUMP, 1 byte format
52129152Sjoerg        SoftTuning tuning = new SoftTuning();
5340782Snsouch
5440782Snsouch        byte[] name;
5540782Snsouch        name = "Testing123      ".getBytes("ascii");
56160372Simp        int[] msg = {0xf0,0x7e,0x7f,0x08,0x05,0,0,
5742442Snsouch                name[0],name[1],name[2],name[3],name[4],name[5],name[6],
58186833Snwhitehorn                name[7],name[8],name[9],name[10],name[11],name[12],name[13],
59186833Snwhitehorn                name[14],name[15],
60187457Snwhitehorn                5,10,15,20,25,30,35,40,45,50,51,52,0,
6140782Snsouch                0xf7};
6240782Snsouch        // Calc checksum
63129152Sjoerg        int x = msg[1] & 0xFF;
64289656Sdumbbell        for (int i = 2; i < msg.length - 2; i++)
6540782Snsouch            x = x ^ (msg[i] & 0xFF);
6640782Snsouch        msg[msg.length-2] = (x & 127);
6740782Snsouch
6840782Snsouch        int[] oct = {5,10,15,20,25,30,35,40,45,50,51,52};
6940782Snsouch        byte[] bmsg = new byte[msg.length];
7040782Snsouch        for (int i = 0; i < bmsg.length; i++)
7140782Snsouch            bmsg[i] = (byte)msg[i];
7240782Snsouch        tuning.load(bmsg);
7340782Snsouch        double[] tunings = tuning.getTuning();
7440782Snsouch        for (int i = 0; i < tunings.length; i++)
7540782Snsouch            assertTrue(Math.abs(tunings[i]-(i*100 + (oct[i%12]-64))) < 0.00001);
7640782Snsouch
7740782Snsouch        // Check if tuning fails if checksum is wrong
7840782Snsouch        msg[msg.length - 2] += 10;
7940782Snsouch        for (int i = 0; i < bmsg.length; i++)
8040782Snsouch            bmsg[i] = (byte)msg[i];
8140782Snsouch        tuning = new SoftTuning();
8240782Snsouch        tuning.load(bmsg);
8340782Snsouch        assertTrue(!tuning.getName().equals("Testing123      "));
8442442Snsouch    }
8540782Snsouch}
8638774Snsouch