1 /*
2  * Copyright (c) 2007, 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.  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 
26 package com.sun.media.sound;
27 
28 import java.io.InputStream;
29 import java.util.Arrays;
30 
31 import javax.sound.midi.Soundbank;
32 import javax.sound.midi.SoundbankResource;
33 import javax.sound.sampled.AudioFormat;
34 import javax.sound.sampled.AudioInputStream;
35 
36 /**
37  * This class is used to store the sample data itself.
38  * A sample is encoded as PCM audio stream
39  * and in DLS Level 1 files it is always a mono 8/16 bit stream.
40  * They are stored just like RIFF WAVE files are stored.
41  * It is stored inside a "wave" List Chunk inside DLS files.
42  *
43  * @author Karl Helgason
44  */
45 public final class DLSSample extends SoundbankResource {
46 
47     byte[] guid = null;
48     DLSInfo info = new DLSInfo();
49     DLSSampleOptions sampleoptions;
50     ModelByteBuffer data;
51     AudioFormat format;
52 
DLSSample(Soundbank soundBank)53     public DLSSample(Soundbank soundBank) {
54         super(soundBank, null, AudioInputStream.class);
55     }
56 
DLSSample()57     public DLSSample() {
58         super(null, null, AudioInputStream.class);
59     }
60 
getInfo()61     public DLSInfo getInfo() {
62         return info;
63     }
64 
65     @Override
getData()66     public Object getData() {
67         AudioFormat format = getFormat();
68 
69         InputStream is = data.getInputStream();
70         if (is == null)
71             return null;
72         return new AudioInputStream(is, format, data.capacity());
73     }
74 
getDataBuffer()75     public ModelByteBuffer getDataBuffer() {
76         return data;
77     }
78 
getFormat()79     public AudioFormat getFormat() {
80         return format;
81     }
82 
setFormat(AudioFormat format)83     public void setFormat(AudioFormat format) {
84         this.format = format;
85     }
86 
setData(ModelByteBuffer data)87     public void setData(ModelByteBuffer data) {
88         this.data = data;
89     }
90 
setData(byte[] data)91     public void setData(byte[] data) {
92         this.data = new ModelByteBuffer(data);
93     }
94 
setData(byte[] data, int offset, int length)95     public void setData(byte[] data, int offset, int length) {
96         this.data = new ModelByteBuffer(data, offset, length);
97     }
98 
99     @Override
getName()100     public String getName() {
101         return info.name;
102     }
103 
setName(String name)104     public void setName(String name) {
105         info.name = name;
106     }
107 
getSampleoptions()108     public DLSSampleOptions getSampleoptions() {
109         return sampleoptions;
110     }
111 
setSampleoptions(DLSSampleOptions sampleOptions)112     public void setSampleoptions(DLSSampleOptions sampleOptions) {
113         this.sampleoptions = sampleOptions;
114     }
115 
116     @Override
toString()117     public String toString() {
118         return "Sample: " + info.name;
119     }
120 
getGuid()121     public byte[] getGuid() {
122         return guid == null ? null : Arrays.copyOf(guid, guid.length);
123     }
124 
setGuid(byte[] guid)125     public void setGuid(byte[] guid) {
126         this.guid = guid == null ? null : Arrays.copyOf(guid, guid.length);
127     }
128 }
129