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 java.io.FilterInputStream;
37  import java.io.IOException;
38  
39  import javax.sound.sampled.AudioInputStream;
40  
41  /**
42   * The FilteredSoundStream class is a FilterInputStream that applies a SoundFilter to the underlying input stream.
43   *
44   * @see SoundFilter
45   */
46  public class FilteredSoundStream extends FilterInputStream
47  {
48      private static final int REMAINING_SIZE_UNKNOWN = -1;
49      private SoundFilter _soundFilter;
50      private int _remainingSize;
51      private int _sampleSizeInBits;
52  
53      /**
54       * Creates a new FilteredSoundStream object with the specified InputStream and SoundFilter.
55       *
56       * @param in
57       *            the file sound stream
58       * @param soundFilter
59       *            the sound filter
60       */
61      public FilteredSoundStream( AudioInputStream in, SoundFilter soundFilter )
62      {
63          super( in );
64          _soundFilter = soundFilter;
65          _remainingSize = REMAINING_SIZE_UNKNOWN;
66          _sampleSizeInBits = in.getFormat( ).getSampleSizeInBits( );
67      }
68  
69      /**
70       * Overrides the FilterInputStream method to apply this filter whenever bytes are read
71       *
72       * @param samples
73       *            the samples
74       * @param offset
75       *            the offset
76       * @param length
77       *            the length of sample
78       * @return the byte read
79       * @throws IOException
80       *             the IOException
81       */
82      public int read( byte [ ] samples, int offset, int length ) throws IOException
83      {
84          int nLengthReturn = length;
85  
86          // read and filter the sound samples in the stream
87          int bytesRead = super.read( samples, offset, nLengthReturn );
88  
89          if ( bytesRead > 0 )
90          {
91              _soundFilter.filter( samples, offset, bytesRead, _sampleSizeInBits );
92  
93              return bytesRead;
94          }
95  
96          // if there are no remaining bytes in the sound stream,
97          // check if the filter has any remaining bytes ("echoes").
98          if ( _remainingSize == REMAINING_SIZE_UNKNOWN )
99          {
100             _remainingSize = _soundFilter.getRemainingSize( );
101             // round down to nearest multiple of 4
102             // (typical frame size)
103             _remainingSize = _remainingSize / 4 * 4;
104         }
105 
106         if ( _remainingSize > 0 )
107         {
108             nLengthReturn = Math.min( nLengthReturn, _remainingSize );
109 
110             // clear the buffer
111             for ( int i = offset; i < ( offset + nLengthReturn ); i++ )
112             {
113                 samples [i] = 0;
114             }
115 
116             // filter the remaining bytes
117             _soundFilter.filter( samples, offset, nLengthReturn, _sampleSizeInBits );
118             _remainingSize -= nLengthReturn;
119 
120             return nLengthReturn;
121         }
122         else
123         {
124             // end of stream
125             return -1;
126         }
127     }
128 }