1/*
2 * Copyright (c) 2011, 2013, 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
24/**
25 * @test
26 * @bug 6835393
27 * @summary Tests that MidiFileReader correctly reads sequences with different division types
28 * @author Alex Menkov
29 */
30
31import java.io.ByteArrayInputStream;
32import java.io.ByteArrayOutputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import javax.sound.midi.InvalidMidiDataException;
36import javax.sound.midi.MidiSystem;
37import javax.sound.midi.Sequence;
38
39public class SMPTESequence {
40
41    static int failed = 0;
42
43    public static void main(String[] args) {
44        test(Sequence.PPQ);
45        test(Sequence.SMPTE_24);
46        test(Sequence.SMPTE_25);
47        test(Sequence.SMPTE_30);
48        test(Sequence.SMPTE_30DROP);
49
50        if (failed > 0) {
51            throw new RuntimeException("" + failed + " tests failed");
52        }
53    }
54
55    static boolean test(float divisionType) {
56        boolean result = false;
57        try {
58            log("Testing divisionType == " + divisionType);
59            Sequence sequence = new Sequence(divisionType, 16, 1);
60            float div1 = sequence.getDivisionType();
61
62            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
63            MidiSystem.write(sequence, 1, outStream);
64
65            InputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
66
67            sequence = MidiSystem.getSequence(inStream);
68            float div2 = sequence.getDivisionType();
69
70            log("After write/read got divisionType == " + div2);
71            if (Math.abs(div2 - div1) < 0.001f) {
72                result = true;
73            }
74        } catch (InvalidMidiDataException ex) {
75            log(ex);
76        } catch (IOException ex) {
77            log(ex);
78        } catch (IllegalArgumentException ex) {
79            log(ex);
80        }
81        if (result) {
82            log("OK");
83        } else {
84            log("FAIL");
85            failed++;
86        }
87        return result;
88    }
89
90    static void log(String s) {
91        System.out.println(s);
92    }
93
94    static void log(Exception ex) {
95        log("got exception (" + ex.getClass().getSimpleName() + "): " + ex.getMessage());
96    }
97
98}
99