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.features;
034
035import org.openimaj.audio.AudioStream;
036import org.openimaj.audio.SampleChunk;
037import org.openimaj.audio.processor.AudioProcessor;
038import org.openimaj.audio.samples.SampleBuffer;
039import org.openimaj.feature.DoubleFV;
040import org.openimaj.feature.FeatureExtractor;
041import org.openimaj.util.array.ArrayUtils;
042
043/**
044 *      This class provides an OpenIMAJ wrapper for the JAudio library of feature extractors.
045 *      It provides the marshalling of data from the OpenIMAJ audio streams into
046 *      the data structures necessary for the jAudio FeatureExtractor interface.
047 *
048 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
049 *  @created 23 May 2013
050 *      @version $Author$, $Revision$, $Date$
051 */
052public abstract class JAudioFeatureExtractor extends AudioProcessor
053        implements FeatureExtractor<DoubleFV, SampleChunk>
054{
055        /** The jaudio feature extractor */
056        protected jAudioFeatureExtractor.AudioFeatures.FeatureExtractor featureExtractor;
057
058        /** The feature that was last calculated */
059        private double[][] lastCalculatedFeature;
060
061        /**
062         *      Default constructor for ad-hoc processing.
063         */
064        public JAudioFeatureExtractor()
065        {
066        }
067
068        /**
069         *      Chainable constructor
070         *      @param as The audio stream to chain to.
071         */
072        public JAudioFeatureExtractor( final AudioStream as )
073        {
074                super( as );
075        }
076
077        /**
078         *      Process the given sample buffer.
079         *      @param sb The sample buffer
080         *      @return The sample buffer
081         */
082        public SampleBuffer process( final SampleBuffer sb )
083        {
084                final double[][] chanSamples = sb.asDoubleChannelArray();
085                this.lastCalculatedFeature = new double[chanSamples.length][];
086                for( int c = 0; c < sb.getFormat().getNumChannels(); c++ )
087                        this.lastCalculatedFeature[c] = this.process( chanSamples[c], sb.getFormat().getSampleRateKHz()*1000d );
088                return sb;
089        }
090
091        /**
092         *      Process the given sample data.
093         *      @param samples The samples
094         *      @param sampleRate The sample rate of the data
095         *      @return The features
096         */
097        public double[][] process( final double[][] samples, final double sampleRate )
098        {
099                final double[][] featureVectors = new double[samples.length][];
100                for( int i = 0; i < samples.length; i++ )
101                        featureVectors[i] = this.process( samples[i], sampleRate );
102                return featureVectors;
103        }
104
105        /**
106         *      Process the given sample array for a single channel
107         *      @param samples The samples for a single channel
108         *      @param sampleRate The sample rate of the data
109         *      @return The feature for the single channel
110         */
111        public double[] process( final double[] samples, final double sampleRate )
112        {
113                // Process the feature
114                try
115                {
116                        final double[] f = this.featureExtractor.extractFeature( samples, sampleRate,
117                                        this.getExtraInputs( samples, sampleRate ) );
118                        return f;
119                }
120                catch( final Exception e )
121                {
122                        e.printStackTrace();
123                }
124
125                // If an exception occurs we return null
126                return null;
127        }
128
129        /**
130         *      Returns the extra inputs required by a specific feature extractor
131         *      @param samples The samples for a single channel
132         *      @param sampleRate The sample rate of the data
133         *      @return The extra input
134         */
135        public abstract double[][] getExtraInputs( double[] samples, double sampleRate );
136
137        /**
138         *      {@inheritDoc}
139         *      @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk)
140         */
141        @Override
142        public SampleChunk process( final SampleChunk sample ) throws Exception
143        {
144                this.process( sample.getSampleBuffer() );
145                return sample;
146        }
147
148        /**
149         *      Calculates the feature for each channel, then flattens the channel arrays
150         *      into a single {@link DoubleFV}.
151         *
152         *      {@inheritDoc}
153         *      @see org.openimaj.feature.FeatureExtractor#extractFeature(java.lang.Object)
154         */
155        @Override
156        public DoubleFV extractFeature( final SampleChunk sc )
157        {
158                // Calculate the feature vector for this frame.
159                this.process( sc.getSampleBuffer() );
160                return new DoubleFV( ArrayUtils.reshape( this.lastCalculatedFeature ) );
161        }
162
163        /**
164         *      @return the lastCalculatedFeature
165         */
166        public double[][] getLastCalculatedFeature()
167        {
168                return this.lastCalculatedFeature;
169        }
170
171        /**
172         *      @param lastCalculatedFeature the lastCalculatedFeature to set
173         */
174        public void setLastCalculatedFeature( final double[][] lastCalculatedFeature )
175        {
176                this.lastCalculatedFeature = lastCalculatedFeature;
177        }
178
179}