ProcessAudio_replace_normal_mono.java revision 829:b06c29386f63
195677Sdougb/*
2174422Sdougb * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
32490Sjkh * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42490Sjkh *
52490Sjkh * This code is free software; you can redistribute it and/or modify it
62490Sjkh * under the terms of the GNU General Public License version 2 only, as
72490Sjkh * published by the Free Software Foundation.  Sun designates this
82490Sjkh * particular file as subject to the "Classpath" exception as provided
92490Sjkh * by Sun in the LICENSE file that accompanied this code.
102490Sjkh *
112490Sjkh * This code is distributed in the hope that it will be useful, but WITHOUT
122490Sjkh * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
132490Sjkh * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
142490Sjkh * version 2 for more details (a copy is included in the LICENSE file that
152490Sjkh * accompanied this code).
162490Sjkh *
172490Sjkh * You should have received a copy of the GNU General Public License version
182490Sjkh * 2 along with this work; if not, write to the Free Software Foundation,
192490Sjkh * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
202490Sjkh *
212490Sjkh * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2249464Smpp * CA 95054 USA or visit www.sun.com if you need additional information or
232490Sjkh * have any questions.
242490Sjkh */
252490Sjkh
262490Sjkh/* @test
272490Sjkh   @summary Test SoftLimiter processAudio method */
282490Sjkh
292490Sjkhimport javax.sound.midi.MidiUnavailableException;
302490Sjkhimport javax.sound.midi.Patch;
312490Sjkhimport javax.sound.sampled.*;
322490Sjkh
332490Sjkhimport com.sun.media.sound.*;
342490Sjkh
352490Sjkhpublic class ProcessAudio_replace_normal_mono {
362490Sjkh
372490Sjkh    private static void assertEquals(Object a, Object b) throws Exception
382490Sjkh    {
392490Sjkh        if(!a.equals(b))
402490Sjkh            throw new RuntimeException("assertEquals fails!");
412490Sjkh    }
422490Sjkh
432490Sjkh    private static void assertTrue(boolean value) throws Exception
442490Sjkh    {
452490Sjkh        if(!value)
462490Sjkh            throw new RuntimeException("assertTrue fails!");
472490Sjkh    }
482490Sjkh
492490Sjkh    public static void main(String[] args) throws Exception {
502490Sjkh        SoftSynthesizer synth = new SoftSynthesizer();
512490Sjkh        synth.openStream(new AudioFormat(44100, 16, 1, true, false), null);
522490Sjkh
532490Sjkh        SoftAudioBuffer in1 = new SoftAudioBuffer(250, synth.getFormat());
542490Sjkh        SoftAudioBuffer out1 = new SoftAudioBuffer(250, synth.getFormat());
552490Sjkh
562490Sjkh        float[] testdata1 = new float[in1.getSize()];
572490Sjkh        float[] n1a = in1.array();
5849464Smpp        float[] out1a = out1.array();
592490Sjkh        for (int i = 0; i < n1a.length; i++) {
602490Sjkh            testdata1[i] = (float)Math.sin(i*0.3)*0.9f;
612490Sjkh            n1a[i] = testdata1[i];
622490Sjkh            out1a[i] = 1;
632490Sjkh        }
642490Sjkh
652490Sjkh        SoftLimiter limiter = new SoftLimiter();
662490Sjkh        limiter.init(44100, 147);
672490Sjkh        limiter.setMixMode(false);
682490Sjkh        limiter.setInput(0, in1);
692490Sjkh        limiter.setOutput(0, out1);
702490Sjkh        limiter.processControlLogic();
712490Sjkh        limiter.processAudio();
722490Sjkh        limiter.processControlLogic();
732490Sjkh        limiter.processAudio();
742490Sjkh        // Limiter should delay audio by one buffer,
752490Sjkh        // and there should almost no different in output v.s. input
762490Sjkh        for (int i = 0; i < n1a.length; i++) {
772490Sjkh            if(Math.abs(out1a[i] - testdata1[i]) > 0.00001)
782490Sjkh                throw new Exception("input != output");
792490Sjkh        }
802490Sjkh
812490Sjkh        synth.close();
822490Sjkh    }
832490Sjkh}
842490Sjkh