1 /*
   2  * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @summary Test SoftFilter processAudio method
  26    @modules java.desktop/com.sun.media.sound
  27    @key randomness
  28 */
  29 
  30 import java.io.File;
  31 import java.io.FileOutputStream;
  32 import java.io.IOException;
  33 import java.util.Random;
  34 
  35 import javax.sound.sampled.*;
  36 
  37 import com.sun.media.sound.*;
  38 
  39 public class TestProcessAudio {
  40 
  41     public static void main(String[] args) throws Exception {
  42         AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
  43         SoftAudioBuffer sbuffer = new SoftAudioBuffer(3600, format);
  44         SoftFilter filter = new SoftFilter(format.getSampleRate());
  45         Random random = new Random(42);
  46 
  47 
  48         for (int t = 0; t <= 6; t++)
  49         {
  50             if(t == 0) filter.setFilterType(SoftFilter.FILTERTYPE_BP12);
  51             if(t == 1) filter.setFilterType(SoftFilter.FILTERTYPE_HP12);
  52             if(t == 2) filter.setFilterType(SoftFilter.FILTERTYPE_HP24);
  53             if(t == 3) filter.setFilterType(SoftFilter.FILTERTYPE_LP12);
  54             if(t == 4) filter.setFilterType(SoftFilter.FILTERTYPE_LP24);
  55             if(t == 5) filter.setFilterType(SoftFilter.FILTERTYPE_LP6);
  56             if(t == 6) filter.setFilterType(SoftFilter.FILTERTYPE_NP12);
  57 
  58 
  59             // Try first by reseting always
  60             for (int f = 1200; f < 3600; f+=100)
  61                 for (int r = 0; r <= 30; r+=5) {
  62                     filter.reset();
  63                     filter.setResonance(r);
  64                     filter.setFrequency(f);
  65                     float[] data = sbuffer.array();
  66                     int len = sbuffer.getSize();
  67                     for (int i = 0; i < len; i++)
  68                         data[i] = random.nextFloat() - 0.5f;
  69                     filter.processAudio(sbuffer);
  70                 }
  71 
  72             // Now we skip reseting
  73             // to test how changing frequency and resonance
  74             // affect active filter
  75             for (int f = 100; f < 12800; f+=1200)
  76             for (int r = 0; r <= 30; r+=5) {
  77                 filter.setResonance(r);
  78                 filter.setFrequency(f);
  79                 float[] data = sbuffer.array();
  80                 int len = sbuffer.getSize();
  81                 for (int i = 0; i < len; i++)
  82                     data[i] = random.nextFloat() - 0.5f;
  83                 filter.processAudio(sbuffer);
  84             }
  85             for (int f = 12800; f >= 100; f-=1200)
  86                 for (int r = 30; r >= 0; r-=5) {
  87                     filter.setResonance(r);
  88                     filter.setFrequency(f);
  89                     float[] data = sbuffer.array();
  90                     int len = sbuffer.getSize();
  91                     for (int i = 0; i < len; i++)
  92                         data[i] = random.nextFloat() - 0.5f;
  93                     filter.processAudio(sbuffer);
  94                 }
  95             filter.reset();
  96         }
  97 
  98     }
  99 
 100 }