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;
034
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 *      An abstract class for objects that are able to act as audio grabbers.
040 *      Audio grabbers should be able to provide streams of audio data, hence
041 *      they extend the {@link AudioStream} abstract class. Obviously, if using
042 *      an audio grabber in the context of an audio stream (for processing or
043 *      whatever), it cannot be guaranteed that the audio grabber will buffer
044 *      sampled between calls of {@link #nextSampleChunk()}.
045 * 
046 *  @author David Dupplaw (dpd@ecs.soton.ac.uk)
047 *      
048 *      @created 28 Oct 2011
049 */
050public abstract class AudioGrabber extends AudioStream implements Runnable
051{
052        /** A list of listeners for samples */
053        protected List<AudioGrabberListener> listeners = new ArrayList<AudioGrabberListener>();
054
055        /**
056         * Start the stream grabbing.
057         * 
058         * {@inheritDoc}
059         * @see java.lang.Runnable#run()
060         */
061        @Override
062        public abstract void run();
063        
064        /**
065         * Stop the stream grabbing
066         */
067        public abstract void stop();
068        
069        /**
070         * Whether the stream is stopped or not.
071         * @return TRUE if the stream is stopped; FALSE otherwise
072         */
073        public abstract boolean isStopped();
074        
075        /**
076         *      Fire the event and audio is now available
077         */
078        protected abstract void fireAudioAvailable();
079        
080        /**
081         * Add an audio grabber listener to this audio grabber.
082         * @param l The listener to add.
083         */
084        public void addAudioGrabberListener( AudioGrabberListener l )
085        {
086                listeners.add( l );
087        }
088
089        /**
090         * Remove an audio grabber listener from this audio grabber.
091         * @param l The audio grabber listener to remove.
092         */
093        public void removeAudioGrabberListener( AudioGrabberListener l )
094        {
095                listeners.remove( l );
096        }
097        
098        /**
099         *      Does nothing by default for an audio grabber.
100         *      @see org.openimaj.audio.AudioStream#reset()
101         */
102        @Override
103        public void reset()
104        {
105        }
106}