001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 *
032 */
033package org.openimaj.audio.samples;
034
035import org.openimaj.audio.AudioFormat;
036import org.openimaj.audio.SampleChunk;
037import org.openimaj.audio.timecode.AudioTimecode;
038
039/**
040 *      This class provides a consistent API for access samples of different
041 *      sizes (e.g. 8-bit and 16-bit). Subclasses implement get and set methods
042 *      for samples in a sample byte buffer. The getters and setters return and
043 *      take floats in all cases, so it is up to the implementing class to decide
044 *      how best to convert the incoming value to an appropriate value for the
045 *      sample buffer. The values given and expected should be normalised between
046 *      {@link Integer#MAX_VALUE} and {@link Integer#MIN_VALUE}. Despite using
047 *      floats, this allows us to detect clipping. Return values are signed such
048 *      that an empty signal has sample value 0. If the audio has multiple channels
049 *      they will occur in the same order as the original file (likely to be
050 *      multiplexed - so <c>get(0)</c> will be channel 1 and <c>get(1)</c>
051 *      will be channel 2, etc.).
052 *
053 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
054 *      @created 23rd November 2011
055 */
056public interface SampleBuffer extends Iterable<Float>
057{
058        /**
059         *      Get the sample at the given index. The sample will be a float and have
060         *      a value between {@link Integer#MAX_VALUE} and {@link Integer#MIN_VALUE}.
061         *      That sample will be signed.
062         *
063         *      @param index The index of the sample to retrieve
064         *      @return The sample value as an float
065         */
066        public float get( int index );
067
068        /**
069         *      Set the sample value at the given index. The sample value should be
070         *      scaled between {@link Integer#MAX_VALUE} and {@link Integer#MIN_VALUE}
071         *      (i.e. it should be signed).
072         *
073         *      @param index The index of the sample to set.
074         *      @param sample The sample value to set.
075         */
076        public void set( int index, float sample );
077
078        /**
079         *      Returns the size of this buffer. Divide by the number of channels
080         *      to get the number of samples per channel.
081         *      @return the size of this buffer.
082         */
083        public int size();
084
085        /**
086         *      Returns the audio format for this set of samples.
087         *      @return The {@link AudioFormat} for this set of samples.
088         */
089        public AudioFormat getFormat();
090
091        /**
092         *      Reset the audio format for the samples in this buffer.
093         *      @param af The {@link AudioFormat}
094         */
095        public void setFormat( AudioFormat af );
096
097        /**
098         *      Return a sample chunk that contains the data from
099         *      this sample buffer. Note that any timestamps will be
100         *      unset in the new sample chunk.
101         *
102         *      @return A {@link SampleChunk} containing data in this buffer.
103         */
104        public SampleChunk getSampleChunk();
105
106        /**
107         *      Return a sample chunk that contains the data from
108         *      a specific channel in this sample buffer. The channel is
109         *      numbered from 0 (so mono audio streams will only have 1 channel
110         *      accessed with getSampleChunk(0)).
111         *
112         *      Note that any timestamps will be unset in the new sample chunk.
113         *
114         *  @param channel The channel
115         *
116         *      @return A {@link SampleChunk} containing data in this buffer.
117         */
118        public SampleChunk getSampleChunk( int channel );
119
120        /**
121         *      Returns the normalised (0..1) sample buffer data as a double array.
122         *      @return A double array containing the normalised samples
123         */
124        public double[] asDoubleArray();
125
126        /**
127         *      Returns the samples in their channels as normalise (0..1) sample buffer
128         *      data.
129         *      @return The array where the first dimension is the channels.
130         */
131        public double[][] asDoubleChannelArray();
132
133        /**
134         *      Returns the sample value at the given index without scaling - that is,
135         *      at it's original sample value. This can be useful for debugging, or
136         *      if the sample buffer is being used for non-audio applications.
137         *
138         *  @param index The index to retrieve
139         *  @return The value unscaled
140         */
141        public float getUnscaled( int index );
142
143        /**
144         *      Returns the timecode of the start of this sample buffer. May return null if
145         *      the timecode is unknown or has no meaning in this context.
146         *      @return The timecode a the start of this buffer.
147         */
148        public AudioTimecode getStartTimecode();
149}