View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  /**
31   *
32   */
33  package org.openimaj.audio.analysis;
34  
35  import org.openimaj.audio.SampleChunk;
36  import org.openimaj.audio.filters.VolumeAdjustProcessor;
37  import org.openimaj.audio.processor.AudioProcessor;
38  import org.openimaj.audio.samples.SampleBuffer;
39  
40  /**
41   * Calculates the scalar necessary to achieve peak value of the loudest part of
42   * the given input signal. This scalar can be retrieved using the
43   * {@link #getVolumeScalar()} method which will return the scalar as it has
44   * currently been calculated for the stream which has passed. This will only
45   * return the correct value for the whole audio stream once the whole audio
46   * stream has passed through the processor. This processor will not perform
47   * attenuation - that is, the volume scalar will always be greater than 1 (and
48   * positive).
49   * <p>
50   * The value of the peak volume scalar is compatible with the
51   * {@link VolumeAdjustProcessor} which can adjust the volume to the maximum peak
52   * value.
53   *
54   * @author David Dupplaw (dpd@ecs.soton.ac.uk)
55   * @created 10 Dec 2012
56   * @version $Author$, $Revision$, $Date$
57   */
58  public class PeakNormalisationCalculator extends AudioProcessor
59  {
60  	/** The peak scalar as it's been calcultated so far in the stream */
61  	private float scalar = Float.MAX_VALUE;
62  
63  	/**
64  	 * {@inheritDoc}
65  	 * 
66  	 * @see org.openimaj.audio.processor.AudioProcessor#process(org.openimaj.audio.SampleChunk)
67  	 */
68  	@Override
69  	public SampleChunk process(final SampleChunk sample) throws Exception
70  	{
71  		// This allows us to retrieve samples that are scaled to 0..1 float
72  		// values
73  		final SampleBuffer sb = sample.getSampleBuffer();
74  
75  		for (final float s : sb)
76  			if (Math.abs(s) * this.scalar > 1)
77  				this.scalar = 1 / Math.abs(s);
78  
79  		return sample;
80  	}
81  
82  	/**
83  	 * Returns the calculated peak scalar as it currently stands in the stream.
84  	 * If no audio has passed through the processor, this will return
85  	 * {@link Float#MAX_VALUE} otherwise the value will always be positive and
86  	 * greater than 1.
87  	 *
88  	 * @return The calculated peak scalar.
89  	 */
90  	public float getVolumeScalar()
91  	{
92  		return this.scalar;
93  	}
94  }