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.conversion;
034
035import org.openimaj.audio.AudioStream;
036import org.openimaj.audio.SampleChunk;
037import org.openimaj.audio.processor.AudioProcessor;
038import org.openimaj.audio.samples.SampleBuffer;
039import org.openimaj.audio.samples.SampleBufferFactory;
040
041/**
042 *      Converts a stereo audio stream into a mono one by averaging the
043 *      channels' samples and creating a mono sample set. The audio
044 *      format of the stream is changed. The process() method creates new
045 *      SampleChunks with a new format.
046 *
047 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
048 *  @created 10 Jun 2011
049 *      
050 */
051public class MultichannelToMonoProcessor extends AudioProcessor
052{
053        /**
054         *      Create a processor to process chunks.
055         */
056        public MultichannelToMonoProcessor()
057        {
058        }
059        
060        /**
061         *      Create a processor for the given audio stream. The output
062         *      of this audio stream will be a mono stream.
063         * 
064         *      @param a The audio stream to process.
065         */
066        public MultichannelToMonoProcessor( final AudioStream a )
067        {
068                super( a );
069                this.setFormat( this.getFormat().clone().setNumChannels( 1 ) );
070        }
071        
072        /** 
073         *      {@inheritDoc}
074         *      @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk)
075         */
076        @Override
077        public SampleChunk process( final SampleChunk sample )
078        {
079                if( sample.getFormat().getNumChannels() == 1 )
080                        return sample;
081                
082                // Get the samples.
083                final SampleBuffer sb = sample.getSampleBuffer();
084                final int nChannels = sample.getFormat().getNumChannels();
085                
086                // Create a new buffer for the mono samples.
087                final SampleBuffer sb2 = SampleBufferFactory.createSampleBuffer(
088                        sb.getFormat().clone().setNumChannels( 1 ),
089                        sb.size()/nChannels );
090                
091                // For all the mono samples...
092                for( int i = 0; i < sb2.size(); i++ )
093                {
094                        // Accumulate the sample value 
095                        double acc = 0;
096                        for( int c = 0; c < nChannels; c++ )
097                                acc += sb.get(i*nChannels+c);
098                        
099                        // Store the average to the mono channel
100                        sb2.set( i, (int)(acc / (double)nChannels) );
101                }
102                        
103                // Update the samples in the sample chunk 
104                return sb2.getSampleChunk();
105        }
106}