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