1 /* 2 * Copyright (c) 2002-2021, City of Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.captcha.modules.jcaptcha.service.sound.filter; 35 36 import javax.sound.sampled.AudioFormat; 37 import javax.sound.sampled.AudioFormat.Encoding; 38 import javax.sound.sampled.AudioInputStream; 39 40 /** 41 * 42 */ 43 public class PitchFilter extends SoundFilter 44 { 45 private float _pitchMinValue; 46 private float _pitchMaxValue; 47 48 /** 49 * Creates an PitchFilter with the specified minimum and maximum relative original sound pitch (in percent) 50 * <p> 51 * 52 * @param pitchMinValue 53 * the pitch Min Value 54 * @param pitchMaxValue 55 * the pitch Max Value 56 */ 57 public PitchFilter( float pitchMinValue, float pitchMaxValue ) 58 { 59 _pitchMinValue = pitchMinValue / 100f; 60 this._pitchMaxValue = pitchMaxValue / 100f; 61 } 62 63 /** 64 * Filter do nothing, only AudioFormat was modified 65 * 66 * @param samples 67 * the samples 68 * @param offset 69 * the offset 70 * @param length 71 * the length 72 * @param sampleSizeInBits 73 * the sample size in bits 74 */ 75 public void filter( byte [ ] samples, int offset, int length, int sampleSizeInBits ) 76 { 77 // Do nothing 78 } 79 80 /** 81 * Apply coefficient to the original audio input format pitch 82 * 83 * @param audioInputStream 84 * the audio input stream 85 * @return the audio format 86 */ 87 public AudioFormat getAudioFormat( AudioInputStream audioInputStream ) 88 { 89 AudioFormat originalFormat = audioInputStream.getFormat( ); 90 float coef = (float) ( Math.random( ) * ( _pitchMaxValue - _pitchMinValue ) ) + _pitchMinValue; 91 float messagePitchRate = originalFormat.getFrameRate( ) * coef; 92 93 return new AudioFormat( messagePitchRate, originalFormat.getSampleSizeInBits( ), originalFormat.getChannels( ), 94 originalFormat.getEncoding( ).equals( Encoding.PCM_SIGNED ), originalFormat.isBigEndian( ) ); 95 } 96 97 /** 98 * 99 * @return the pitch min value 100 */ 101 public float getPitchMinValue( ) 102 { 103 return _pitchMinValue; 104 } 105 106 /** 107 * ste the pitch min value 108 * 109 * @param pitchMinValue 110 * the pitch min value 111 */ 112 public void setPitchMinValue( float pitchMinValue ) 113 { 114 _pitchMinValue = pitchMinValue; 115 } 116 117 /** 118 * 119 * @return the pitch max value 120 */ 121 public float getPitchMaxValue( ) 122 { 123 return _pitchMaxValue; 124 } 125 126 /** 127 * set the pitch max value 128 * 129 * @param pitchMaxValue 130 * the pitch max value 131 */ 132 public void setPitchMaxValue( float pitchMaxValue ) 133 { 134 this._pitchMaxValue = pitchMaxValue; 135 } 136 }