829N/A/*
3261N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/A
829N/Aimport java.util.ArrayList;
829N/A
829N/Aimport javax.sound.sampled.AudioFormat;
829N/Aimport javax.sound.sampled.AudioSystem;
829N/Aimport javax.sound.sampled.Control;
829N/Aimport javax.sound.sampled.DataLine;
829N/Aimport javax.sound.sampled.LineListener;
829N/Aimport javax.sound.sampled.LineUnavailableException;
829N/Aimport javax.sound.sampled.SourceDataLine;
829N/Aimport javax.sound.sampled.AudioFormat.Encoding;
829N/Aimport javax.sound.sampled.Control.Type;
829N/A
829N/Aimport com.sun.media.sound.AudioFloatConverter;
829N/A
829N/A/**
829N/A * This is a SourceDataLine simulator used for testing SoftSynthesizer
829N/A * without using real SourceDataLine / Audio Device.
829N/A *
829N/A * @author Karl Helgason
829N/A */
829N/A
829N/Apublic class DummySourceDataLine implements SourceDataLine {
829N/A
829N/A private int bufferSize = -1;
829N/A
829N/A private AudioFormat format = new AudioFormat(44100.0f, 16, 2, true, false);
829N/A
829N/A private DataLine.Info sourceLineInfo;
829N/A
829N/A private boolean active = false;
829N/A
829N/A private long framepos = 0;
829N/A
829N/A private boolean opened = false;
829N/A
829N/A private int framesize = 0;
829N/A
829N/A public DummySourceDataLine()
829N/A {
829N/A ArrayList<AudioFormat> formats = new ArrayList<AudioFormat>();
829N/A for (int channels = 1; channels <= 2; channels++) {
829N/A formats.add(new AudioFormat(Encoding.PCM_SIGNED,
829N/A AudioSystem.NOT_SPECIFIED, 8, channels, channels,
829N/A AudioSystem.NOT_SPECIFIED, false));
829N/A formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
829N/A AudioSystem.NOT_SPECIFIED, 8, channels, channels,
829N/A AudioSystem.NOT_SPECIFIED, false));
829N/A for (int bits = 16; bits < 32; bits += 8) {
829N/A formats.add(new AudioFormat(Encoding.PCM_SIGNED,
829N/A AudioSystem.NOT_SPECIFIED, bits, channels, channels
829N/A * bits / 8, AudioSystem.NOT_SPECIFIED, false));
829N/A formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
829N/A AudioSystem.NOT_SPECIFIED, bits, channels, channels
829N/A * bits / 8, AudioSystem.NOT_SPECIFIED, false));
829N/A formats.add(new AudioFormat(Encoding.PCM_SIGNED,
829N/A AudioSystem.NOT_SPECIFIED, bits, channels, channels
829N/A * bits / 8, AudioSystem.NOT_SPECIFIED, true));
829N/A formats.add(new AudioFormat(Encoding.PCM_UNSIGNED,
829N/A AudioSystem.NOT_SPECIFIED, bits, channels, channels
829N/A * bits / 8, AudioSystem.NOT_SPECIFIED, true));
829N/A }
2867N/A formats.add(new AudioFormat(Encoding.PCM_FLOAT,
829N/A AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
829N/A AudioSystem.NOT_SPECIFIED, false));
2867N/A formats.add(new AudioFormat(Encoding.PCM_FLOAT,
829N/A AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4,
829N/A AudioSystem.NOT_SPECIFIED, true));
2867N/A formats.add(new AudioFormat(Encoding.PCM_FLOAT,
829N/A AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
829N/A AudioSystem.NOT_SPECIFIED, false));
2867N/A formats.add(new AudioFormat(Encoding.PCM_FLOAT,
829N/A AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8,
829N/A AudioSystem.NOT_SPECIFIED, true));
829N/A }
829N/A AudioFormat[] formats_array = formats.toArray(new AudioFormat[formats
829N/A .size()]);
829N/A sourceLineInfo = new DataLine.Info(SourceDataLine.class,
829N/A formats_array, AudioSystem.NOT_SPECIFIED,
829N/A AudioSystem.NOT_SPECIFIED);
829N/A
829N/A }
829N/A
829N/A public void open() throws LineUnavailableException {
829N/A open(format);
829N/A }
829N/A
829N/A public void open(AudioFormat format) throws LineUnavailableException {
829N/A if (bufferSize == -1)
829N/A bufferSize = ((int) (format.getFrameRate() / 2))
829N/A * format.getFrameSize();
829N/A open(format, bufferSize);
829N/A }
829N/A
829N/A public void open(AudioFormat format, int bufferSize)
829N/A throws LineUnavailableException {
829N/A this.format = format;
829N/A this.bufferSize = bufferSize;
829N/A this.framesize = format.getFrameSize();
829N/A opened = true;
829N/A }
829N/A
829N/A public boolean isOpen() {
829N/A return opened;
829N/A }
829N/A
829N/A public int write(byte[] b, int off, int len) {
829N/A if (!isOpen())
829N/A return 0;
829N/A if (len % framesize != 0)
829N/A throw new IllegalArgumentException(
829N/A "Number of bytes does not represent an integral number of sample frames.");
829N/A
829N/A
829N/A int flen = len / framesize;
829N/A framepos += flen;
829N/A
829N/A long time = (long) (flen * (1000.0 / (double) getFormat()
829N/A .getSampleRate()));
829N/A try {
829N/A Thread.sleep(time);
829N/A } catch (InterruptedException e) {
829N/A e.printStackTrace();
829N/A return 0;
829N/A }
829N/A
829N/A return len;
829N/A }
829N/A
829N/A public int available() {
829N/A return 0;
829N/A }
829N/A
829N/A public void drain() {
829N/A }
829N/A
829N/A public void flush() {
829N/A }
829N/A
829N/A public int getBufferSize() {
829N/A return bufferSize;
829N/A }
829N/A
829N/A public AudioFormat getFormat() {
829N/A return format;
829N/A }
829N/A
829N/A public int getFramePosition() {
829N/A return (int) getLongFramePosition();
829N/A }
829N/A
829N/A public float getLevel() {
829N/A return AudioSystem.NOT_SPECIFIED;
829N/A }
829N/A
829N/A public long getLongFramePosition() {
829N/A return framepos;
829N/A }
829N/A
829N/A public long getMicrosecondPosition() {
829N/A return (long) (getLongFramePosition() * (1000000.0 / (double) getFormat()
829N/A .getSampleRate()));
829N/A }
829N/A
829N/A public boolean isActive() {
829N/A return active;
829N/A }
829N/A
829N/A public boolean isRunning() {
829N/A return active;
829N/A }
829N/A
829N/A public void start() {
829N/A active = true;
829N/A }
829N/A
829N/A public void stop() {
829N/A active = false;
829N/A }
829N/A
829N/A public void close() {
829N/A stop();
829N/A }
829N/A
829N/A public Control getControl(Type control) {
829N/A throw new IllegalArgumentException("Unsupported control type : "
829N/A + control);
829N/A }
829N/A
829N/A public Control[] getControls() {
829N/A return new Control[0];
829N/A }
829N/A
829N/A public javax.sound.sampled.Line.Info getLineInfo() {
829N/A return sourceLineInfo;
829N/A }
829N/A
829N/A public boolean isControlSupported(Type control) {
829N/A return false;
829N/A }
829N/A
829N/A public void addLineListener(LineListener listener) {
829N/A }
829N/A
829N/A public void removeLineListener(LineListener listener) {
829N/A }
829N/A
829N/A}