1/*
2 * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24import javax.sound.midi.MidiDevice;
25import javax.sound.midi.MidiSystem;
26import javax.sound.midi.MidiUnavailableException;
27import javax.sound.midi.Sequencer;
28
29/**
30 * @test
31 * @bug 4716740
32 * @summary default sequencer does not set the tempo factor
33 */
34public class SequencerCacheValues {
35
36    static boolean failed = false;
37
38    public static void main(String args[]) throws Exception {
39        Sequencer seq = null;
40        int totalNumberOfSequencers = 0;
41
42        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
43        for (int device=0; device<infos.length; device++) {
44            //seq = MidiSystem.getSequencer();
45            MidiDevice dev = MidiSystem.getMidiDevice(infos[device]);
46            if (dev instanceof Sequencer) {
47                seq = (Sequencer) dev;
48                totalNumberOfSequencers++;
49                System.out.println("Opening sequencer "+infos[device]);
50                try {
51                    seq.open();
52                    try {
53                        doTest(seq);
54                    } finally {
55                        if (seq != null) {
56                            seq.close();
57                            seq = null;
58                        }
59                    }
60                } catch (MidiUnavailableException mue) {
61                    System.err.println("MidiUnavailableException was thrown: " + mue);
62                    System.err.println("could not test this sequencer.");
63                }
64            }
65        }
66        if (totalNumberOfSequencers == 0) {
67            System.out.println("No sequencers installed!");
68            failed = true;
69        }
70        if (failed) {
71            throw new Exception("FAILED");
72        } else {
73            System.out.println("test OK");
74        }
75    }
76
77    public static boolean equalsFloat(float f1, float f2) {
78        return (f1-f2<0.0001) && (f2-f1<0.0001);
79    }
80
81    public static void doTest(Sequencer seq) throws Exception {
82        seq.setTempoInMPQ(3.0f);
83        System.out.println("Setting tempo in MPQ to "+3.0f);
84        if (!equalsFloat(seq.getTempoInMPQ(), 3.0f)) {
85            System.err.println("getTempoInMPQ() returns wrong value : "
86                + seq.getTempoInMPQ());
87            failed = true;
88        }
89
90        System.out.println("Setting tempo factor to "+2.0f);
91        seq.setTempoFactor(2.0f);
92        if (!equalsFloat(seq.getTempoFactor(), 2.0f)) {
93            System.err.println("getTempoFactor() returns: " + seq.getTempoFactor());
94            failed = true;
95        }
96
97        float bpmTempo = 120.0f;
98        System.out.println("Setting tempo to "+120.0f+"bpm");
99        seq.setTempoInBPM(bpmTempo);
100        if (!equalsFloat(seq.getTempoInMPQ(), (60000000.0f/seq.getTempoInBPM()))) {
101            System.err.println("getTempoInMPQ() returns: " + seq.getTempoInMPQ());
102            System.err.println("getTempoInBPM() returns: " + seq.getTempoInBPM());
103            failed = true;
104        }
105    }
106}
107