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.demos.sandbox.audio;
034
035import java.io.File;
036
037import org.openimaj.audio.AudioEventAdapter;
038import org.openimaj.audio.AudioFormat;
039import org.openimaj.audio.AudioPlayer;
040import org.openimaj.audio.SampleChunk;
041import org.openimaj.audio.conversion.BitDepthConverter;
042import org.openimaj.audio.conversion.MultichannelToMonoProcessor;
043import org.openimaj.audio.conversion.BitDepthConverter.BitDepthConversionAlgorithm;
044import org.openimaj.audio.conversion.SampleRateConverter;
045import org.openimaj.audio.conversion.SampleRateConverter.SampleRateConversionAlgorithm;
046import org.openimaj.video.xuggle.XuggleAudio;
047
048/**
049 *      
050 *
051 *      @author David Dupplaw <dpd@ecs.soton.ac.uk>
052 *  @created 20 Jun 2012
053 *      @version $Author$, $Revision$, $Date$
054 */
055public class AudioConversionTest
056{
057        /**
058         *      @param args
059         */
060        public static void main( String[] args )
061        {
062                final File f = new File( "videoplayback.3gp" );
063                
064                // ================================================================= //
065                // Original Sample
066                // ================================================================= //
067                XuggleAudio x1 = new XuggleAudio( f );          
068                System.out.println( "Input Audio Format: "+x1.getFormat() );
069                
070                System.out.println( "Playing original audio... "+x1.getFormat() );
071                AudioPlayer ap = new AudioPlayer( x1 );
072                ap.addAudioEventListener( new AudioEventAdapter()
073                {
074                        @Override
075                        public void audioEnded()
076                        {
077                                
078                                // ================================================================= //
079                                // Sample rate conversion to 16KHz
080                                // ================================================================= //
081                                XuggleAudio x2 = new XuggleAudio( f );
082                                SampleRateConverter src = new SampleRateConverter( x2, 
083                                        SampleRateConversionAlgorithm.LINEAR_INTERPOLATION,
084                                        new AudioFormat( x2.getFormat().getNBits(), 11.025, 
085                                                        x2.getFormat().getNumChannels() ) );
086                                System.out.println( "Playing audio "+src.getFormat() );
087                                AudioPlayer ap2 = new AudioPlayer( src );                               
088                                ap2.addAudioEventListener( new AudioEventAdapter()
089                                {
090                                        @Override
091                                        public void audioEnded() 
092                                        {
093                                                // ================================================================= //
094                                                // Sample rate conversion to 16KHz
095                                                // Bit depth conversion to 8bit
096                                                // ================================================================= //
097                                                XuggleAudio x3 = new XuggleAudio( f );
098                                                SampleRateConverter src2 = new SampleRateConverter( x3, 
099                                                        SampleRateConversionAlgorithm.LINEAR_INTERPOLATION,
100                                                        new AudioFormat( x3.getFormat().getNBits(), 11.025, 
101                                                                        x3.getFormat().getNumChannels() ) );
102                                                BitDepthConverter bd1 = new BitDepthConverter( src2, 
103                                                        BitDepthConversionAlgorithm.NEAREST, 
104                                                        new AudioFormat( 8, src2.getFormat().getSampleRateKHz(),
105                                                                src2.getFormat().getNumChannels() ) );
106                                                System.out.println( "Playing audio "+src2.getFormat() );
107                                                AudioPlayer ap3 = new AudioPlayer( bd1 );
108                                                ap3.addAudioEventListener( new AudioEventAdapter()
109                                                {
110                                                        @Override
111                                                        public void audioEnded() 
112                                                        {
113                                                                // ================================================================= //
114                                                                // Sample rate conversion to 16KHz
115                                                                // Bit depth conversion to 8bit
116                                                                // Channel reduced to mono
117                                                                // ================================================================= //
118                                                                XuggleAudio x4 = new XuggleAudio( f );
119                                                                SampleRateConverter src3 = new SampleRateConverter( x4, 
120                                                                        SampleRateConversionAlgorithm.LINEAR_INTERPOLATION,
121                                                                        new AudioFormat( x4.getFormat().getNBits(), 16, 
122                                                                                        x4.getFormat().getNumChannels() ) );
123                                                                BitDepthConverter bd2 = new BitDepthConverter( src3, 
124                                                                        BitDepthConversionAlgorithm.NEAREST, 
125                                                                        new AudioFormat( 8, src3.getFormat().getSampleRateKHz(),
126                                                                                src3.getFormat().getNumChannels() ) );
127                                                                MultichannelToMonoProcessor mc = new MultichannelToMonoProcessor( bd2 );
128                                                                System.out.println( "Playing audio "+src3.getFormat() );
129                                                                AudioPlayer ap4 = new AudioPlayer( mc );
130                                                                ap4.run();
131                                                                System.out.println( "4----------------------------------------- ");                                                     
132                                                        }
133
134                                                        @Override
135                                                        public void beforePlay( SampleChunk sc )
136                                                        {
137                                                        }
138
139                                                        @Override
140                                                        public void afterPlay( AudioPlayer ap,
141                                                                        SampleChunk sc )
142                                                        {
143                                                        }
144                                                } );
145                                                ap3.run();
146                                                System.out.println( "3----------------------------------------- ");
147                                        }
148
149                                        @Override
150                                        public void beforePlay( SampleChunk sc )
151                                        {
152                                        }
153
154                                        @Override
155                                        public void afterPlay( AudioPlayer ap, SampleChunk sc )
156                                        {
157                                        }
158                                } );
159                                ap2.run();
160                                System.out.println( "2----------------------------------------- ");
161                        }
162
163                        @Override
164                        public void beforePlay( SampleChunk sc )
165                        {
166                        }
167
168                        @Override
169                        public void afterPlay( AudioPlayer ap, SampleChunk sc )
170                        {
171                        }
172                } );
173                ap.run();
174                System.out.println( "1----------------------------------------- ");
175        }
176}