Home | History | Annotate | Download | only in audio
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.jme3.audio;
     34 
     35 import com.jme3.util.NativeObject;
     36 
     37 /**
     38  * <code>AudioData</code> is an abstract representation
     39  * of audio data. There are two ways to handle audio data, short audio files
     40  * are to be stored entirely in memory, while long audio files (music) are
     41  * streamed from the hard drive as they are played.
     42  *
     43  * @author Kirill Vainer
     44  */
     45 public abstract class AudioData extends NativeObject {
     46 
     47     protected int sampleRate;
     48     protected int channels;
     49     protected int bitsPerSample;
     50 
     51     public enum DataType {
     52         Buffer,
     53         Stream
     54     }
     55 
     56     public AudioData(){
     57         super(AudioData.class);
     58     }
     59 
     60     protected AudioData(int id){
     61         super(AudioData.class, id);
     62     }
     63 
     64     /**
     65      * @return The data type, either <code>Buffer</code> or <code>Stream</code>.
     66      */
     67     public abstract DataType getDataType();
     68 
     69     /**
     70      * @return the duration in seconds of the audio clip.
     71      */
     72     public abstract float getDuration();
     73 
     74     /**
     75      * @return Bits per single sample from a channel.
     76      */
     77     public int getBitsPerSample() {
     78         return bitsPerSample;
     79     }
     80 
     81     /**
     82      * @return Number of channels. 1 for mono, 2 for stereo, etc.
     83      */
     84     public int getChannels() {
     85         return channels;
     86     }
     87 
     88     /**
     89      * @return The sample rate, or how many samples per second.
     90      */
     91     public int getSampleRate() {
     92         return sampleRate;
     93     }
     94 
     95     /**
     96      * Setup the format of the audio data.
     97      * @param channels # of channels, 1 = mono, 2 = stereo
     98      * @param bitsPerSample Bits per sample, e.g 8 bits, 16 bits.
     99      * @param sampleRate Sample rate, 44100, 22050, etc.
    100      */
    101     public void setupFormat(int channels, int bitsPerSample, int sampleRate){
    102         if (id != -1)
    103             throw new IllegalStateException("Already set up");
    104 
    105         this.channels = channels;
    106         this.bitsPerSample = bitsPerSample;
    107         this.sampleRate = sampleRate;
    108     }
    109 
    110 }
    111