View Javadoc
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.AudioInputStream;
38  
39  /**
40   * A abstract class designed to filter sound samples. Since SoundFilters may use internal buffering of samples, a new SoundFilter object should be created for
41   * every sound played. However, SoundFilters can be reused after they are finished by called the reset() method.
42   * <p>
43   *
44   * @see FilteredSoundStream
45   */
46  public abstract class SoundFilter
47  {
48      protected static final int SAMPLE_SIZE_16_BIT = 16;
49      protected static final int SAMPLE_SIZE_8_BIT = 8;
50  
51      /**
52       * Resets this SoundFilter. Does nothing by default.
53       */
54      public void reset( )
55      {
56          // do nothing
57      }
58  
59      /**
60       * Gets the remaining size, in bytes, that this filter plays after the sound is finished. An example would be an echo that plays longer than it's original
61       * sound. This method returns 0 by default.
62       *
63       * @return remaining size
64       */
65      public int getRemainingSize( )
66      {
67          return 0;
68      }
69  
70      /**
71       * Filters an array of samples. Samples should be in 16-bit, signed, little-endian format. This method should be implemented by subclasses.
72       *
73       * @param samples
74       *            the samples
75       * @param offset
76       *            the offset
77       * @param length
78       *            the length
79       * @param sampleSizeInBits
80       *            the sample size in bits
81       */
82      public abstract void filter( byte [ ] samples, int offset, int length, int sampleSizeInBits );
83  
84      /**
85       * Convenience method for getting a 8-bit sample from a byte array. Samples should be in 8-bit, unsigned, little-endian format.
86       *
87       * @param buffer
88       *            the buffer
89       * @param position
90       *            the position
91       * @return 8 bit sample
92       */
93      public static short get8bitSample( byte [ ] buffer, int position )
94      {
95          return (short) ( buffer [position] & 0xff );
96      }
97  
98      /**
99       * Convenience method for setting a 8-bit sample in a byte array. Samples should be in 8-bit, unsigned, little-endian format.
100      *
101      * @param buffer
102      *            the buffer
103      * @param position
104      *            the position
105      * @param sample
106      *            the sample
107      *
108      */
109     public static void set8bitSample( byte [ ] buffer, int position, short sample )
110     {
111         buffer [position] = (byte) ( sample & 0xff );
112     }
113 
114     /**
115      * Convenience method for getting a 16-bit sample from a byte array. Samples should be in 16-bit, signed, little-endian format.
116      *
117      * @param buffer
118      *            the buffer
119      * @param position
120      *            the position
121      *
122      * @return 16 bit sample
123      *
124      */
125     public static short get16bitSample( byte [ ] buffer, int position )
126     {
127         return (short) ( ( ( buffer [position + 1] & 0xff ) << 8 ) | ( buffer [position] & 0xff ) );
128     }
129 
130     /**
131      * Convenience method for setting a 16-bit sample in a byte array. Samples should be in 16-bit, signed, little-endian format.
132      *
133      * @param buffer
134      *            the buffer
135      * @param position
136      *            the position
137      * @param sample
138      *            the sample
139      *
140      */
141     public static void set16bitSample( byte [ ] buffer, int position, short sample )
142     {
143         buffer [position] = (byte) ( sample & 0xff );
144         buffer [position + 1] = (byte) ( ( sample >> 8 ) & 0xff );
145     }
146 
147     /**
148      * Return the audio format corresponding to the input stream Overrided by the filters that mofify AudioFormat
149      *
150      * @param audioInputStream
151      *            the audio input stream
152      * @return the audio format corresponding to the input stream
153      */
154     public AudioFormat getAudioFormat( AudioInputStream audioInputStream )
155     {
156         return audioInputStream.getFormat( );
157     }
158 }