Path: blob/master/test/jdk/javax/sound/sampled/Lines/ChangingBuffer.java
41153 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.sound.sampled.AudioFormat;24import javax.sound.sampled.AudioSystem;25import javax.sound.sampled.Clip;26import javax.sound.sampled.DataLine;27import javax.sound.sampled.Mixer;28import javax.sound.sampled.SourceDataLine;2930/**31* @test32* @bug 451512633* @summary Verify that the buffer passed to SourceDataLine.write() and34* Clip.open() will not be changed35*/36public class ChangingBuffer {3738final static int samplerate = 44100;39final static byte[] buffer = new byte[16384];40static int successfulTests = 0;4142private static void makeBuffer() {43for (int i=0; i<buffer.length; i++) {44buffer[i] = (byte) (i % 128);45}46}4748private static void checkBufferSDL() throws Exception {49successfulTests++;50for (int i=0; i<buffer.length; i++) {51if (buffer[i] != ((byte) (i % 128))) {52throw new Exception("Buffer was changed by SourceDataLine.write()!. Test FAILED");53}54}55System.out.println(" -> passed for this line");56System.out.println("");57}5859private static void checkBufferClip() throws Exception {60for (int i=0; i<buffer.length; i++) {61if (buffer[i] != (i % 128)) {62throw new Exception("Buffer was changed by Clip.open()!. Test FAILED");63}64}65System.out.println(" -> passed for this clip");66System.out.println("");67}6869private static boolean doMixerClip(Mixer mixer, AudioFormat format) {70if (mixer==null) return false;71try {72System.out.println("Trying mixer "+mixer+":");73DataLine.Info info = new DataLine.Info(74Clip.class,75format,76(int) samplerate);7778Clip clip = (Clip) mixer.getLine(info);79System.out.println(" - got clip: "+clip);80System.out.println(" - open with format "+format);81clip.open(format, buffer, 0, buffer.length);82System.out.println(" - playing...");83clip.start();84System.out.println(" - waiting while it's active...");85while (clip.isActive())86Thread.sleep(100);87System.out.println(" - waiting 100millis");88Thread.sleep(100);89System.out.println(" - drain1");90clip.drain();91System.out.println(" - drain2");92clip.drain();93System.out.println(" - stop");94clip.stop();95System.out.println(" - close");96clip.close();97System.out.println(" - closed");98} catch (Throwable t) {99System.out.println(" - Caught exception. Not failed.");100System.out.println(" - "+t.toString());101return false;102}103return true;104}105106private static boolean doMixerSDL(Mixer mixer, AudioFormat format) {107if (mixer==null) return false;108try {109System.out.println("Trying mixer "+mixer+":");110DataLine.Info info = new DataLine.Info(111SourceDataLine.class,112format,113(int) samplerate);114115SourceDataLine sdl = (SourceDataLine) mixer.getLine(info);116System.out.println(" - got sdl: "+sdl);117System.out.println(" - open with format "+format);118sdl.open(format);119System.out.println(" - start...");120sdl.start();121System.out.println(" - write...");122sdl.write(buffer, 0, buffer.length);123Thread.sleep(200);124System.out.println(" - drain...");125sdl.drain();126System.out.println(" - stop...");127sdl.stop();128System.out.println(" - close...");129sdl.close();130System.out.println(" - closed");131} catch (Throwable t) {132System.out.println(" - Caught exception. Not failed.");133System.out.println(" - "+t.toString());134return false;135}136return true;137}138139private static void doAll(boolean bigEndian) throws Exception {140AudioFormat pcm = new AudioFormat(141AudioFormat.Encoding.PCM_SIGNED,142samplerate, 16, 1, 2, samplerate, bigEndian);143Mixer.Info[] mixers = AudioSystem.getMixerInfo();144for (int i=0; i<mixers.length; i++) {145Mixer mixer = AudioSystem.getMixer(mixers[i]);146makeBuffer(); if (doMixerClip(mixer, pcm)) checkBufferClip();147makeBuffer(); if (doMixerSDL(mixer, pcm)) checkBufferSDL();148}149if (mixers.length==0) {150System.out.println("No mixers available!");151}152153}154155public static void main(String args[]) throws Exception{156doAll(true);157doAll(false);158if (successfulTests==0) {159System.out.println("Could not execute any of the tests. Test NOT failed.");160} else {161System.out.println("Test PASSED.");162}163}164}165166167