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 */
030package org.openimaj.audio;
031
032import java.util.ArrayList;
033import java.util.List;
034
035import org.openimaj.audio.analysis.FourierTransform;
036import org.openimaj.audio.processor.AudioProcessor;
037import org.openimaj.util.pair.IndependentPair;
038import org.openimaj.util.pair.Pair;
039
040/**
041 * {@link AudioProcessor} that provides frequency information.
042 * 
043 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
044 *
045 */
046public class FrequencyAudioSource extends AudioProcessor implements Runnable {
047        
048        /**
049         * Interface for classes that listen to the frequency information
050         * extracted by the {@link FrequencyAudioSource}.
051         * 
052         * @author David Dupplaw (dpd@ecs.soton.ac.uk)
053         *
054         */
055        public static interface Listener {
056
057                /**
058                 * Called when new frequency data is available.
059                 * 
060                 * @param fftReal real fft values
061                 * @param fftImag imaginary fft values
062                 * @param low
063                 * @param high
064                 */
065                public void consumeFrequency(float[] fftReal, float[] fftImag, int low, int high);
066                
067        }
068
069        private final FourierTransform fftProc;
070        private final List<IndependentPair<Listener, Pair<Integer>>> listeners;
071        private float[] fftReal;
072        private float[] fftImag;
073        
074        
075        /**
076         * Construct on top of given stream
077         * @param stream the stream
078         */
079        public FrequencyAudioSource(final AudioStream stream) {
080                super(stream);
081                this.fftProc = new FourierTransform();
082                this.listeners = new ArrayList<IndependentPair<Listener, Pair<Integer>>>();
083                new Thread(this).start();
084        }
085        
086        @Override
087        public SampleChunk process(final SampleChunk sample) throws Exception {
088                this.fftProc.process(sample);
089                final float[] fft = this.fftProc.getLastFFT()[0];
090                this.fireFrequencyEvent(fft,sample);
091                return sample;
092        }
093
094        private void fireFrequencyEvent(final float[] fft,final SampleChunk sample) {
095                final double binSize = (sample.getFormat().getSampleRateKHz()*1000) / (fft.length/2);
096                if(this.fftReal == null || fft.length/4 != this.fftReal.length){
097                        this.fftReal = new float[fft.length/4];
098                        this.fftImag = new float[fft.length/4];
099                }
100                // Extract the spectra
101                for( int i = 0; i < fft.length/4; i++ )
102                {
103                        final float re = fft[i*2];
104                        final float im = fft[i*2+1];
105                        this.fftReal[i] = re;
106                        this.fftImag[i] = im;
107                }
108                for(final IndependentPair<Listener,Pair<Integer>> l : this.listeners){
109                        final Pair<Integer> range = l.secondObject();
110                        final int low = (int) (range.firstObject()/binSize);
111                        final int high = (int) (range.secondObject()/binSize);
112                        l.firstObject().consumeFrequency(this.fftReal,this.fftImag,low,high);
113                }
114        }
115
116        @Override
117        public void run() {
118//              while(true){
119                        try
120                {
121                        Thread.sleep( 500 );
122                        SampleChunk s = null;
123                        while( (s = this.nextSampleChunk()) != null ) {
124                                this.process( s );
125                        }
126                }
127                catch( final InterruptedException e )
128                {
129                        e.printStackTrace();
130                } 
131                catch (final Exception e) 
132                {
133                        e.printStackTrace();
134                }
135                
136//              }
137        }
138
139        /**
140         * Add a listener
141         * @param l the listener
142         */
143        public void addFrequencyListener(final Listener l) {
144                final Pair<Integer> range = null;
145                this.listeners.add(IndependentPair.pair(l,range));
146        }
147        
148        /**
149         * Add a listener
150         * @param l the listener
151         * @param requestFrequencyRange the range
152         */
153        public void addFrequencyListener(final Listener l, final Pair<Integer> requestFrequencyRange) {
154                this.listeners.add(IndependentPair.pair(l,requestFrequencyRange));
155        }
156}