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; 036import java.util.Arrays; 037 038import org.openimaj.audio.AudioStream; 039import org.openimaj.audio.SampleChunk; 040import org.openimaj.audio.analysis.FourierTransform; 041import org.openimaj.audio.conversion.MultichannelToMonoProcessor; 042import org.openimaj.audio.filters.HanningAudioProcessor; 043import org.openimaj.video.xuggle.XuggleAudio; 044import org.openimaj.vis.audio.AudioWaveform; 045import org.openimaj.vis.general.BarVisualisation; 046 047/** 048 * Simple class that shows a sample, the Hanning function and the FFT 049 * of the hanning-windowed sample using the OpenIMAJ visualisations. 050 * 051 * @author David Dupplaw (dpd@ecs.soton.ac.uk) 052 * @created 1 Aug 2012 053 * @version $Author$, $Revision$, $Date$ 054 */ 055public class AudioFFT 056{ 057 /** 058 * @param args 059 */ 060 public static void main( final String[] args ) 061 { 062 String name = "heads1.mpeg"; 063 if( args.length > 0 ) 064 name = args[0]; 065 066 final XuggleAudio xa = new XuggleAudio( new File( name ) ); 067 final MultichannelToMonoProcessor mm = new MultichannelToMonoProcessor( xa ); 068 final HanningAudioProcessor hanning = new HanningAudioProcessor( mm, 128 ); 069 final FourierTransform fft = new FourierTransform(); 070 071 final AudioStream toUse = hanning; 072 073 // Jump past the quiet start of the audio 074 toUse.nextSampleChunk(); 075 toUse.nextSampleChunk(); 076 toUse.nextSampleChunk(); 077 078 final SampleChunk sc = toUse.nextSampleChunk(); 079 final double[] d = sc.getSampleBuffer().asDoubleArray(); 080 081 System.out.println( Arrays.toString(d) ); 082 083 final AudioWaveform hanningw = new AudioWaveform( 1000, 500 ); 084 hanningw.setData( hanning.getWeights() ); 085 086 // Show the audio waveform 087 final AudioWaveform aw = new AudioWaveform( hanningw ); 088 aw.setData( d, sc.getFormat() ); 089 aw.showWindow( "Audio" ); 090 091// ArrayUtils.normaliseMax( d, 256d ); 092// SampleBuffer sb = new FloatSampleBuffer( d, sc.getFormat() ); 093 094 fft.process( sc ); 095 final float[][] lastFFT = fft.getLastFFT(); 096 097 System.out.println( Arrays.deepToString( lastFFT ) ); 098 099// float[] f = new float[d.length*2]; 100// for( int i = 0; i < d.length; i++ ) 101// f[i] = (float)d[i]; 102// FloatFFT_1D offt = new FloatFFT_1D( d.length ); 103// offt.complexForward( f ); 104// 105// System.out.println( Arrays.toString( f ) ); 106// 107 final BarVisualisation bv = new BarVisualisation( 1500, 400 ); 108 bv.setData( lastFFT[0] ); 109 bv.showWindow( "FFT" ); 110 } 111}