View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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   */
44  public class PitchFilter extends SoundFilter
45  {
46      private float _pitchMinValue;
47      private float _pitchMaxValue;
48  
49      /**
50       * Creates an PitchFilter with the specified minimum and maximum relative original sound pitch (in percent)
51       * <p>
52       * @param pitchMinValue the pitch Min Value
53       * @param pitchMaxValue the pitch Max Value
54       */
55      public PitchFilter( float pitchMinValue, float pitchMaxValue )
56      {
57          _pitchMinValue = pitchMinValue / 100f;
58          this._pitchMaxValue = pitchMaxValue / 100f;
59      }
60  
61      /**
62       * Filter do nothing, only AudioFormat was modified
63       *
64       * @param samples the samples
65       * @param offset the offset
66       * @param length the length
67       * @param sampleSizeInBits the sample size in bits
68       */
69      public void filter( byte[] samples, int offset, int length, int sampleSizeInBits )
70      {
71          // Do nothing
72      }
73  
74      /**
75       * Apply coefficient to the original audio input format pitch
76       *
77       * @param audioInputStream the audio input stream
78       * @return the audio format
79       */
80      public AudioFormat getAudioFormat( AudioInputStream audioInputStream )
81      {
82          AudioFormat originalFormat = audioInputStream.getFormat(  );
83          float coef = (float) ( Math.random(  ) * ( _pitchMaxValue - _pitchMinValue ) ) + _pitchMinValue;
84          float messagePitchRate = originalFormat.getFrameRate(  ) * coef;
85  
86          return new AudioFormat( messagePitchRate, originalFormat.getSampleSizeInBits(  ),
87              originalFormat.getChannels(  ), originalFormat.getEncoding(  ).equals( Encoding.PCM_SIGNED ),
88              originalFormat.isBigEndian(  ) );
89      }
90  
91      /**
92       *
93       * @return the pitch min value
94       */
95      public float getPitchMinValue(  )
96      {
97          return _pitchMinValue;
98      }
99  
100     /**
101      * ste the pitch min value
102      *
103      * @param pitchMinValue the pitch min value
104      */
105     public void setPitchMinValue( float pitchMinValue )
106     {
107         _pitchMinValue = pitchMinValue;
108     }
109 
110     /**
111      *
112      * @return the pitch max value
113      */
114     public float getPitchMaxValue(  )
115     {
116         return _pitchMaxValue;
117     }
118 
119     /**
120      * set the pitch max value
121      *
122      * @param pitchMaxValue the pitch max value
123      */
124     public void setPitchMaxValue( float pitchMaxValue )
125     {
126         this._pitchMaxValue = pitchMaxValue;
127     }
128 }