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.analysis;
031
032import java.nio.ByteBuffer;
033import java.nio.ShortBuffer;
034
035import org.openimaj.audio.AudioStream;
036import org.openimaj.audio.SampleChunk;
037import org.openimaj.audio.processor.FixedSizeSampleAudioProcessor;
038
039/**
040 *      Calculate the effective sound pressure by calculating the RMS of samples over
041 *      a temporal window. This will sum across all channels in the sample chunk.
042 * 
043 *      @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 */
045public class EffectiveSoundPressure extends FixedSizeSampleAudioProcessor
046{
047        private double rms = 0;
048
049        /**
050         * Default constructor
051         */
052        public EffectiveSoundPressure()
053        {
054                super( 1 );
055        }
056
057        /**
058         *      Contstructor for ad-hoc processing. Note that the window and overlap
059         *      size is given in samples (not in milliseconds as the other constructor).
060         * 
061         *      @param windowSizeSamples Size of the processing window in samples
062         *      @param overlapSamples The overlap of the windows in samples
063         */
064        public EffectiveSoundPressure( final int windowSizeSamples, final int overlapSamples )
065        {
066                super( windowSizeSamples );
067                this.setWindowStep( overlapSamples );
068        }
069
070        /**
071         *      Construct with given stream and window parameters. Note that the window
072         *      parameters are given in milliseconds and converted into a number of
073         *      samples by the method.
074         * 
075         *      @param stream The audio stream
076         *      @param windowSizeMillis The window size in milliseconds
077         *      @param overlapMillis The overlap between windows in milliseconds
078         */
079        public EffectiveSoundPressure( final AudioStream stream, final int windowSizeMillis,
080                        final int overlapMillis )
081        {
082                super( stream, (int) (stream.getFormat().getSampleRateKHz()
083                                * windowSizeMillis * stream.getFormat().getNumChannels()) );
084                this.setWindowStep( (int) (stream.getFormat().getSampleRateKHz() * 
085                                overlapMillis * stream.getFormat().getNumChannels()) );
086        }
087
088        /**
089         *      {@inheritDoc}
090         *      @see org.openimaj.audio.processor.FixedSizeSampleAudioProcessor#process(org.openimaj.audio.SampleChunk)
091         */
092        @Override
093        public SampleChunk process( final SampleChunk sample ) throws Exception
094        {
095                long accum = 0;
096                final int size;
097
098                switch (sample.getFormat().getNBits())
099                {
100                        case 16:
101                        {
102                                final ShortBuffer b = sample.getSamplesAsByteBuffer().asShortBuffer();
103                                size = b.limit();
104                                for( int x = 0; x < size; x++ )
105                                        accum += b.get( x ) * b.get( x );
106                                break;
107                        }
108                        case 8:
109                        {
110                                final ByteBuffer b = sample.getSamplesAsByteBuffer();
111                                size = b.limit();
112                                for( int x = 0; x < size; x++ )
113                                        accum += b.get( x ) * b.get( x );
114                                break;
115                        }
116                        default:
117                                throw new Exception( "Unsupported Format" );
118                }
119
120                this.rms = Math.sqrt( (double) accum / (double) size );
121
122                return sample;
123        }
124
125        /**
126         * @return The effective sound pressure
127         */
128        public double getEffectiveSoundPressure()
129        {
130                return this.rms;
131        }
132}