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