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 
30 import javax.sound.midi.Soundbank;
31 import javax.sound.midi.SoundbankResource;
32 import javax.sound.sampled.AudioFormat;
33 import javax.sound.sampled.AudioInputStream;
34 
35 /**
36  * Soundfont sample storage.
37  *
38  * @author Karl Helgason
39  */
40 public final class SF2Sample extends SoundbankResource {
41 
42     String name = "";
43     long startLoop = 0;
44     long endLoop = 0;
45     long sampleRate = 44100;
46     int originalPitch = 60;
47     byte pitchCorrection = 0;
48     int sampleLink = 0;
49     int sampleType = 0;
50     ModelByteBuffer data;
51     ModelByteBuffer data24;
52 
SF2Sample(Soundbank soundBank)53     public SF2Sample(Soundbank soundBank) {
54         super(soundBank, null, AudioInputStream.class);
55     }
56 
SF2Sample()57     public SF2Sample() {
58         super(null, null, AudioInputStream.class);
59     }
60 
61     @Override
getData()62     public Object getData() {
63 
64         AudioFormat format = getFormat();
65         /*
66         if (sampleFile != null) {
67             FileInputStream fis;
68             try {
69                 fis = new FileInputStream(sampleFile);
70                 RIFFReader riff = new RIFFReader(fis);
71                 if (!riff.getFormat().equals("RIFF")) {
72                     throw new RIFFInvalidDataException(
73                         "Input stream is not a valid RIFF stream!");
74                 }
75                 if (!riff.getType().equals("sfbk")) {
76                     throw new RIFFInvalidDataException(
77                         "Input stream is not a valid SoundFont!");
78                 }
79                 while (riff.hasNextChunk()) {
80                     RIFFReader chunk = riff.nextChunk();
81                     if (chunk.getFormat().equals("LIST")) {
82                         if (chunk.getType().equals("sdta")) {
83                             while(chunk.hasNextChunk()) {
84                                 RIFFReader chunkchunk = chunk.nextChunk();
85                                 if(chunkchunk.getFormat().equals("smpl")) {
86                                     chunkchunk.skip(sampleOffset);
87                                     return new AudioInputStream(chunkchunk,
88                                             format, sampleLen);
89                                 }
90                             }
91                         }
92                     }
93                 }
94                 return null;
95             } catch (Exception e) {
96                 return new Throwable(e.toString());
97             }
98         }
99         */
100         InputStream is = data.getInputStream();
101         if (is == null)
102             return null;
103         return new AudioInputStream(is, format, data.capacity());
104     }
105 
getDataBuffer()106     public ModelByteBuffer getDataBuffer() {
107         return data;
108     }
109 
getData24Buffer()110     public ModelByteBuffer getData24Buffer() {
111         return data24;
112     }
113 
getFormat()114     public AudioFormat getFormat() {
115         return new AudioFormat(sampleRate, 16, 1, true, false);
116     }
117 
setData(ModelByteBuffer data)118     public void setData(ModelByteBuffer data) {
119         this.data = data;
120     }
121 
setData(byte[] data)122     public void setData(byte[] data) {
123         this.data = new ModelByteBuffer(data);
124     }
125 
setData(byte[] data, int offset, int length)126     public void setData(byte[] data, int offset, int length) {
127         this.data = new ModelByteBuffer(data, offset, length);
128     }
129 
setData24(ModelByteBuffer data24)130     public void setData24(ModelByteBuffer data24) {
131         this.data24 = data24;
132     }
133 
setData24(byte[] data24)134     public void setData24(byte[] data24) {
135         this.data24 = new ModelByteBuffer(data24);
136     }
137 
setData24(byte[] data24, int offset, int length)138     public void setData24(byte[] data24, int offset, int length) {
139         this.data24 = new ModelByteBuffer(data24, offset, length);
140     }
141 
142     /*
143     public void setData(File file, int offset, int length) {
144         this.data = null;
145         this.sampleFile = file;
146         this.sampleOffset = offset;
147         this.sampleLen = length;
148     }
149     */
150 
151     @Override
getName()152     public String getName() {
153         return name;
154     }
155 
setName(String name)156     public void setName(String name) {
157         this.name = name;
158     }
159 
getEndLoop()160     public long getEndLoop() {
161         return endLoop;
162     }
163 
setEndLoop(long endLoop)164     public void setEndLoop(long endLoop) {
165         this.endLoop = endLoop;
166     }
167 
getOriginalPitch()168     public int getOriginalPitch() {
169         return originalPitch;
170     }
171 
setOriginalPitch(int originalPitch)172     public void setOriginalPitch(int originalPitch) {
173         this.originalPitch = originalPitch;
174     }
175 
getPitchCorrection()176     public byte getPitchCorrection() {
177         return pitchCorrection;
178     }
179 
setPitchCorrection(byte pitchCorrection)180     public void setPitchCorrection(byte pitchCorrection) {
181         this.pitchCorrection = pitchCorrection;
182     }
183 
getSampleLink()184     public int getSampleLink() {
185         return sampleLink;
186     }
187 
setSampleLink(int sampleLink)188     public void setSampleLink(int sampleLink) {
189         this.sampleLink = sampleLink;
190     }
191 
getSampleRate()192     public long getSampleRate() {
193         return sampleRate;
194     }
195 
setSampleRate(long sampleRate)196     public void setSampleRate(long sampleRate) {
197         this.sampleRate = sampleRate;
198     }
199 
getSampleType()200     public int getSampleType() {
201         return sampleType;
202     }
203 
setSampleType(int sampleType)204     public void setSampleType(int sampleType) {
205         this.sampleType = sampleType;
206     }
207 
getStartLoop()208     public long getStartLoop() {
209         return startLoop;
210     }
211 
setStartLoop(long startLoop)212     public void setStartLoop(long startLoop) {
213         this.startLoop = startLoop;
214     }
215 
216     @Override
toString()217     public String toString() {
218         return "Sample: " + name;
219     }
220 }
221