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