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.filters;
034
035import java.nio.ByteBuffer;
036import java.nio.ShortBuffer;
037
038import org.openimaj.audio.AudioStream;
039import org.openimaj.audio.SampleChunk;
040import org.openimaj.audio.processor.AudioProcessor;
041
042/**
043 *      A processor that processes the audio file by adjusting the volume
044 *      by a given factor.
045 *
046 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
047 *  @created 8 Jun 2011
048 *      
049 */
050public class VolumeAdjustProcessor extends AudioProcessor
051{
052        /** The factor to adjust the volume by */
053        private double factor = 1;
054        
055        /**
056         *      Default constructor that takes the volume adjustment
057         *      factor as a double.
058         *  @param factor 
059         */
060        public VolumeAdjustProcessor( final double factor )
061        {
062                this( factor, null );
063        }
064        
065        /**
066         *      Constructor that takes the volume adjustment factor to apply
067         *      to the given stream. This allows this processor to be chainable.
068         * 
069         *      @param factor the factor to apply
070         *      @param a The audio stream to apply the factor to
071         */
072        public VolumeAdjustProcessor( final double factor, final AudioStream a )
073        {
074                super( a );
075                this.factor = factor;
076        }
077        
078        /**
079         *      @throws Exception 
080         * {@inheritDoc}
081         *      @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk)
082         */
083        @Override
084        public SampleChunk process( final SampleChunk sample ) throws Exception
085        {               
086                switch( sample.getFormat().getNBits() )
087                {
088                        case 16:
089                        {
090                                final ShortBuffer b = sample.getSamplesAsByteBuffer().asShortBuffer();
091                                for( int x = 0; x < b.limit(); x++ )
092                                        b.put( x, (short)(b.get( x )*this.factor) );
093                                break;
094                        }
095                        case 8:
096                        {
097                                final ByteBuffer b = sample.getSamplesAsByteBuffer();
098                                for( int x = 0; x < b.limit(); x++ )
099                                        b.put( x, (byte)(b.get( x )*this.factor) );
100                                break;
101                        }
102                        default:
103                                throw new Exception( "Unsupported Format" );
104                }
105                
106                return sample;
107        }
108}