1/*
2 * Copyright (c) 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.media.sound;
27
28import javax.sound.sampled.AudioFileFormat;
29import javax.sound.sampled.AudioFormat;
30import javax.sound.sampled.AudioSystem;
31
32/**
33 * An instance of the {@code StandardFileFormat} describes the file's length in
34 * bytes and the length in sample frames as longs. This will provide an
35 * additional precision unlike the {@code AudioFileFormat}.
36 */
37class StandardFileFormat extends AudioFileFormat {
38
39    /**
40     * File length in bytes stored as long.
41     */
42    private final long byteLength;
43
44    /**
45     * Audio data length in sample frames stored as long.
46     */
47    private final long frameLength;
48
49    /**
50     * Constructs {@code StandardFileFormat} object.
51     *
52     * @param  type the type of the audio file
53     * @param  format the format of the audio data contained in the file
54     * @param  frameLength the audio data length in sample frames, or
55     *         {@code AudioSystem.NOT_SPECIFIED}
56     */
57    StandardFileFormat(final Type type, final AudioFormat format,
58                       final long frameLength) {
59        this(type, AudioSystem.NOT_SPECIFIED, format, frameLength);
60    }
61
62    /**
63     * Constructs {@code StandardFileFormat} object.
64     *
65     * @param  type the type of the audio file
66     * @param  byteLength the length of the file in bytes, or
67     *         {@code AudioSystem.NOT_SPECIFIED}
68     * @param  format the format of the audio data contained in the file
69     * @param  frameLength the audio data length in sample frames, or
70     *         {@code AudioSystem.NOT_SPECIFIED}
71     */
72    StandardFileFormat(final Type type, final long byteLength,
73                       final AudioFormat format, final long frameLength) {
74        super(type, clip(byteLength), format, clip(frameLength));
75        this.byteLength = byteLength;
76        this.frameLength = frameLength;
77    }
78
79    /**
80     * Replaces the passed value to {@code AudioSystem.NOT_SPECIFIED} if the
81     * value is greater than {@code Integer.MAX_VALUE}.
82     *
83     * @param  value which should be clipped
84     * @return the clipped value
85     */
86    private static int clip(final long value) {
87        if (value > Integer.MAX_VALUE) {
88            return AudioSystem.NOT_SPECIFIED;
89        }
90        return (int) value;
91    }
92
93    /**
94     * Obtains the length of the audio data contained in the file, expressed in
95     * sample frames. The long precision is used.
96     *
97     * @return the number of sample frames of audio data in the file
98     * @see AudioSystem#NOT_SPECIFIED
99     */
100    public final long getLongFrameLength() {
101        return frameLength;
102    }
103
104    /**
105     * Obtains the size in bytes of the entire audio file (not just its audio
106     * data). The long precision is used.
107     *
108     * @return the audio file length in bytes
109     * @see AudioSystem#NOT_SPECIFIED
110     */
111    public final long getLongByteLength() {
112        return byteLength;
113    }
114}
115