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.demos.sandbox.audio;
034
035import java.util.ArrayList;
036import java.util.List;
037import java.util.Set;
038
039import org.openimaj.audio.AudioStream;
040import org.openimaj.audio.SampleChunk;
041import org.openimaj.audio.samples.SampleBuffer;
042import org.openimaj.feature.FeatureVector;
043import org.openimaj.ml.annotation.AbstractAnnotator;
044import org.openimaj.ml.annotation.Annotated;
045import org.openimaj.ml.annotation.AnnotatedObject;
046import org.openimaj.ml.annotation.BatchAnnotator;
047import org.openimaj.ml.annotation.ScoredAnnotation;
048import org.openimaj.util.pair.IndependentPair;
049
050/**
051 * A general audio classifier class that can use any annotator and any dataset.
052 * The {@link AudioClassifier} requires an annotator to be passed in, which in
053 * turn requires feature extractor. The feature extractor must be able to
054 * extract a {@link FeatureVector} from a {@link SampleBuffer}.
055 *
056 * <p>
057 * The {@link #train(List)} method takes a list of pairs, where the pairs are
058 * streams mapped to annotations. Each stream represents a set of
059 * {@link SampleBuffer}s for that annotation. They are extracted into
060 * {@link AnnotatedObject}s before being passed into the annotator for feature
061 * extraction.
062 *
063 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
064 * @created 7 May 2013
065 * @version $Author$, $Revision$, $Date$
066 * @param <ANNOTATION>
067 *            The annotation type
068 */
069public class AudioClassifier<ANNOTATION> extends AbstractAnnotator<SampleBuffer, ANNOTATION>
070{
071        /** The annotator to use */
072        private final BatchAnnotator<SampleBuffer, ANNOTATION> annotator;
073
074        /**
075         * Constructor that takes the actual annotator type to use.
076         *
077         * @param annotator
078         *            The annotator
079         */
080        public AudioClassifier(final BatchAnnotator<SampleBuffer, ANNOTATION> annotator)
081        {
082                this.annotator = annotator;
083        }
084
085        /**
086         * Train the annotator on the given streams. The streams are annotated with
087         * the appropriate annotation, and sample chunks (and therefore buffers) are
088         * gathered from the streams into batches to train the annotator.
089         *
090         * @param streams
091         *            The annotated streams
092         */
093        public void train(final List<IndependentPair<AudioStream, ANNOTATION>> streams)
094        {
095                // Convert all the incoming streams into AnnotatedObject instances
096                // where the sample buffer for each
097                final List<Annotated<SampleBuffer, ANNOTATION>> list = new ArrayList<Annotated<SampleBuffer, ANNOTATION>>();
098                for (final IndependentPair<AudioStream, ANNOTATION> stream : streams)
099                {
100                        SampleChunk sc = null;
101                        while ((sc = stream.firstObject().nextSampleChunk()) != null)
102                        {
103                                final SampleBuffer sb = sc.getSampleBuffer();
104                                final AnnotatedObject<SampleBuffer, ANNOTATION> a = AnnotatedObject.create(sb, stream.secondObject());
105                                list.add(a);
106                        }
107                }
108
109                // Train the annotator for the streams
110                this.annotator.train(list);
111        }
112
113        /**
114         * {@inheritDoc}
115         *
116         * @see org.openimaj.ml.annotation.Annotator#getAnnotations()
117         */
118        @Override
119        public Set<ANNOTATION> getAnnotations()
120        {
121                return this.annotator.getAnnotations();
122        }
123
124        /**
125         * {@inheritDoc}
126         *
127         * @see org.openimaj.ml.annotation.Annotator#annotate(java.lang.Object)
128         */
129        @Override
130        public List<ScoredAnnotation<ANNOTATION>> annotate(final SampleBuffer object)
131        {
132                return this.annotator.annotate(object);
133        }
134}