Example usage for javax.sound.sampled AudioSystem getMixer

List of usage examples for javax.sound.sampled AudioSystem getMixer

Introduction

In this page you can find the example usage for javax.sound.sampled AudioSystem getMixer.

Prototype

public static Mixer getMixer(final Mixer.Info info) 

Source Link

Document

Obtains the requested audio mixer.

Usage

From source file:com.gameminers.mav.audio.AudioManager.java

public void init() {
    Mixer.Info info = AudioSystem.getMixerInfo()[0];
    mixer = AudioSystem.getMixer(info); // TODO
    loadClip("notif1");
    loadClip("notif2");
    loadClip("notif3");
    loadClip("recog1");
    loadClip("fail1");
    loadClip("fail2");
    loadClip("listen1");
    sink = new AudioPlayback(Constants.FORMAT_CODE_CD, mixer,
            Constants.BUFFER_SIZE_MILLIS[Constants.BUFFER_SIZE_INDEX_DEFAULT]);
    source = new AudioCapture(Constants.FORMAT_CODE_SPHINX, mixer,
            Constants.BUFFER_SIZE_MILLIS[Constants.BUFFER_SIZE_INDEX_DEFAULT]);
    try {/*from   w  w w.j  ava 2  s .c o m*/
        sink.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        source.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        sink.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        source.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.offbynull.voip.audio.gateways.io.AudioRunnable.java

private Object loadDevices() {
    Map<Integer, LineEntry> newOutputDevices = new HashMap<>();
    Map<Integer, LineEntry> newInputDevices = new HashMap<>();
    List<OutputDevice> respOutputDevices = new LinkedList<>();
    List<InputDevice> respInputDevices = new LinkedList<>();

    Mixer.Info[] mixerInfos;/*from w w  w. ja  v a2 s  .  com*/
    try {
        mixerInfos = AudioSystem.getMixerInfo();
    } catch (Exception e) {
        LOG.error("Unable to get mixers", e);
        return new ErrorResponse("Unable to get mixers: " + e);
    }

    for (Mixer.Info info : mixerInfos) {
        Mixer mixer;
        try {
            mixer = AudioSystem.getMixer(info);
        } catch (Exception e) {
            LOG.error("Unable to get mixer", e);
            continue;
        }

        Line.Info[] lineInfos;
        Map<Integer, LineEntry> newEntries;

        // out devices
        try {
            lineInfos = mixer.getSourceLineInfo(new Line.Info(SourceDataLine.class));
            newEntries = generateLineEntriesFromJavaSound(mixer, lineInfos);
            newOutputDevices.putAll(newEntries);
            newEntries.entrySet().forEach(x -> {
                Mixer.Info mi = x.getValue().getMixer().getMixerInfo();
                String name = "OUT:" + mi.getName() + "/" + mi.getVersion() + "/" + mi.getVendor() + "/"
                        + mi.getDescription();
                OutputDevice outputDevice = new OutputDevice(x.getKey(), name);
                respOutputDevices.add(outputDevice);
            });
        } catch (LineUnavailableException lue) {
            LOG.error("Unable to get line from mixer", lue);
        }

        // in devices
        try {
            lineInfos = mixer.getTargetLineInfo(new Line.Info(TargetDataLine.class));
            newEntries = generateLineEntriesFromJavaSound(mixer, lineInfos);
            newInputDevices.putAll(newEntries);
            newEntries.entrySet().forEach(x -> {
                Mixer.Info mi = x.getValue().getMixer().getMixerInfo();
                String name = "IN:" + mi.getName() + "/" + mi.getVersion() + "/" + mi.getVendor() + "/"
                        + mi.getDescription();
                InputDevice inputDevice = new InputDevice(x.getKey(), name);
                respInputDevices.add(inputDevice);
            });
        } catch (LineUnavailableException lue) {
            LOG.error("Unable to get line from mixer", lue);
        }
    }

    inputDevices = newInputDevices;
    outputDevices = newOutputDevices;
    return new LoadDevicesResponse(respOutputDevices, respInputDevices);
}

From source file:SoundManagerTest.java

/**
 * Gets the maximum number of simultaneous sounds with the specified
 * AudioFormat that the default mixer can play.
 *///from  w w w  .  j  a  v  a 2 s. c om
public static int getMaxSimultaneousSounds(AudioFormat playbackFormat) {
    DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat);
    Mixer mixer = AudioSystem.getMixer(null);
    return mixer.getMaxLines(lineInfo);
}

From source file:SoundManagerTest.java

/**
 * Does any clean up before closing.//from w w w  .  jav  a2 s .c o  m
 */
protected void cleanUp() {
    // signal to unpause
    setPaused(false);

    // close the mixer (stops any running sounds)
    Mixer mixer = AudioSystem.getMixer(null);
    if (mixer.isOpen()) {
        mixer.close();
    }
}

From source file:com.lfv.lanzius.application.Controller.java

public synchronized void init(int id) {

    clear();/*from   w  w w .  j a  va 2 s  .  com*/
    state = CLIENT_STATE_DISCONNECTED;
    reconnectPrintCount = 5;

    // Load terminal properties
    try {
        properties = new TerminalProperties(id);
    } catch (Exception ex) {
        log.error("Invalid or no terminal properties file (data/properties/terminalproperties.xml). Exiting!",
                ex);
        System.exit(0);
    }

    // Overwrite if specified externally on the command line
    if (id > 0)
        properties.setTerminalId(id);

    log.info("Starting terminal " + properties.getTerminalId());

    // Setup codec values
    AudioDecoder.setJitterBufferSize(properties.getJitterBufferSize());
    AudioDecoder.setOutputBufferSize(properties.getOutputBufferSize());
    AudioEncoder.setInputBufferSize(properties.getInputBufferSize());

    // Initialize the timing system
    Time.init();

    // Setup a socket address to the server
    try {
        serverSocketAddr = new SocketAddress(properties.getServerAddress(), properties.getServerUdpPort());
    } catch (UnknownHostException ex) {
        log.error("Invalid server address. Exiting!", ex);
        System.exit(0);
    }

    // Create a bundle
    bundle = new ClientBundle();

    // Create the network manager
    try {
        networkManager = new ClientNetworkManager(properties.getTerminalId(), bundle, serverSocketAddr,
                new SocketAddress(properties.getLocalBindAddress(), properties.getLocalBindPort()),
                new SocketAddress(properties.getMulticastAddress(), properties.getMulticastPort()),
                properties.getMulticastTTL(), properties);
        packetReceiver = networkManager.getReceiver();
    } catch (IOException ex) {
        log.error(
                "The network manager could not be created, possibly due to an invalid multicast socket setting. Exiting!",
                ex);
        System.exit(0);
    }

    // Create push to talk interfaces
    if (properties.getEventDeviceName().length() > 0) {
        eventSwitch = new EventSwitchController(this, properties.getEventDeviceName());
    }
    if (properties.getFootSwitchInterface().length() > 0) {
        try {
            footSwitch = new FootSwitchController(this, properties.getFootSwitchInterface(),
                    properties.getFootSwitchPollTimeMillis(), properties.isFootSwitchInverted());
        } catch (UnsatisfiedLinkError err) {
            log.error("UnsatisfiedLinkError: " + err.getMessage());
            log.warn("Missing ftsw library (ftsw.dll or libftsw.so)");
            if (!properties.getFootSwitchInterface().equalsIgnoreCase("ftdi"))
                log.warn("Missing rxtxSerial library (rxtxSerial.dll or librxtxSerial.so)");
            log.warn("The footswitch has been disabled!");
            footSwitch = null;
        } catch (NoClassDefFoundError err) {
            log.warn("Missing ftsw library (ftsw.dll or libftsw.so)");
            if (!properties.getFootSwitchInterface().equalsIgnoreCase("ftdi"))
                log.warn("Missing rxtxSerial library (rxtxSerial.dll or librxtxSerial.so)");
            log.warn("The footswitch has been disabled!");
            footSwitch = null;
        }
    }

    // Get the selected audio devices
    Mixer.Info[] mixerinfo = AudioSystem.getMixerInfo();

    if ((properties.getSoundOutputDevice() >= mixerinfo.length)
            || (properties.getSoundInputDevice() >= mixerinfo.length)) {
        log.error("Invalid sound device. Exiting!");
        //System.exit(0);
    }

    outputMixer = AudioSystem.getMixer(mixerinfo[properties.getSoundOutputDevice()]);
    inputMixer = AudioSystem.getMixer(mixerinfo[properties.getSoundInputDevice()]);

    if (log.isDebugEnabled()) {
        log.debug("Using output device " + properties.getSoundOutputDevice() + " ("
                + mixerinfo[properties.getSoundOutputDevice()] + ")");
        log.debug("Using input device " + properties.getSoundInputDevice() + " ("
                + mixerinfo[properties.getSoundInputDevice()] + ")");
    }

    // Creating sound clips
    clipRingTone = new SoundClip(outputMixer, "data/resources/sounds/phone_ringtone_mono.wav",
            "data/resources/sounds/phone_ringtone_stereo.wav", 1100, properties.getSignalVolumeAdjustment());
    volumeClipList.add(clipRingTone);
    clipRingBack = new SoundClip(outputMixer, "data/resources/sounds/phone_ringback_mono.wav",
            "data/resources/sounds/phone_ringback_stereo.wav", 2100, properties.getSignalVolumeAdjustment());
    volumeClipList.add(clipRingBack);
    clipRingBusy = new SoundClip(outputMixer, "data/resources/sounds/phone_busy_mono.wav",
            "data/resources/sounds/phone_busy_stereo.wav", 400, properties.getSignalVolumeAdjustment());
    volumeClipList.add(clipRingBusy);
    clipWarning = new SoundClip(outputMixer, "data/resources/sounds/radio_error_mono.wav",
            "data/resources/sounds/radio_error_stereo.wav", 1000, properties.getSignalVolumeAdjustment());
    volumeClipList.add(clipWarning);
    clipNotify = new SoundClip(outputMixer, "data/resources/sounds/ding_mono.wav",
            "data/resources/sounds/ding_stereo.wav", 1000, properties.getSignalVolumeAdjustment());
    volumeClipList.add(clipNotify);

    // Create peripheral link
    peripheralLink = null;
    int peripheralLinkPort = properties.getPeripheralLinkPort();
    if (peripheralLinkPort > 0) {
        try {
            peripheralLink = new PeripheralLink(properties.getTerminalId(), peripheralLinkPort);
            peripheralLink.startModule();
        } catch (IOException ex) {
            log.warn("Unable to create peripheral link, disabling!");
            peripheralLink = null;
        }
    }

    // Add shutdown hook
    if (hookThread != null)
        Runtime.getRuntime().removeShutdownHook(hookThread);
    hookThread = new Thread(new Runnable() {
        public void run() {
            stop(true, true);
            shutdown(true);
            sleep(25);
            dbgListThreads();
            log.info("Bye!");
        }
    }, "Thook");
    Runtime.getRuntime().addShutdownHook(hookThread);

    if (Config.CLIENT_SERVERLESS) {
        state = CLIENT_STATE_CONNECTED;
        sessionStart();
    } else {
        // Setup handler for incoming requests and timeouts
        networkManager.setNetworkHandler(this);

        // Try to connect to server
        log.info("Connecting to server on " + properties.getServerAddress() + "...");
        networkManager.serverConnect();
    }

    if (autoTesterEnabled) {
        autoTester = new AutoTester(this);
    }
}

From source file:BasicPlayer.java

public List getMixers() {
    ArrayList mixers = new ArrayList();
    Mixer.Info[] mInfos = AudioSystem.getMixerInfo();
    if (mInfos != null) {
        for (int i = 0; i < mInfos.length; i++) {
            Line.Info lineInfo = new Line.Info(SourceDataLine.class);
            Mixer mixer = AudioSystem.getMixer(mInfos[i]);
            if (mixer.isLineSupported(lineInfo)) {
                mixers.add(mInfos[i].getName());
            }// w  w w . j av  a  2 s. co  m
        }
    }
    return mixers;
}

From source file:BasicPlayer.java

public Mixer getMixer(String name) {
    Mixer mixer = null;//from ww w. ja va2  s  .  c om
    if (name != null) {
        Mixer.Info[] mInfos = AudioSystem.getMixerInfo();
        if (mInfos != null) {
            for (int i = 0; i < mInfos.length; i++) {
                if (mInfos[i].getName().equals(name)) {
                    mixer = AudioSystem.getMixer(mInfos[i]);
                    break;
                }
            }
        }
    }
    return mixer;
}

From source file:GUI.AllForDealFrame.java

private void btnSonOnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSonOnActionPerformed
    // TODO add your handling code here:
    Mixer.Info[] mixInfo = AudioSystem.getMixerInfo();
    mixer = AudioSystem.getMixer(mixInfo[0]);
    DataLine.Info dataLine = new DataLine.Info(Clip.class, null);
    try {/*from   w w  w . j  ava 2s.  co  m*/
        clip = (Clip) mixer.getLine(dataLine);
    } catch (LineUnavailableException ex) {
        ex.printStackTrace();
    }
    try {
        URL soundURL = this.getClass().getResource("/images/The_Eagles-Hotel_California_acoustic_live_www.wav");
        AudioInputStream audioImput = AudioSystem.getAudioInputStream(soundURL);
        clip.open(audioImput);

    } catch (LineUnavailableException ex) {
        ex.printStackTrace();
    } catch (UnsupportedAudioFileException exp) {
        exp.printStackTrace();
    } catch (IOException io) {
        io.printStackTrace();
    }

    clip.start();
}

From source file:org.eclipse.smarthome.io.javasound.internal.JavaSoundAudioSink.java

private void runVolumeCommand(Closure closure) {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    for (Mixer.Info info : infos) {
        Mixer mixer = AudioSystem.getMixer(info);
        if (mixer.isLineSupported(Port.Info.SPEAKER)) {
            Port port;//  ww  w. jav  a  2s.  co m
            try {
                port = (Port) mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                    FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                    closure.execute(volume);
                }
                port.close();
            } catch (LineUnavailableException e) {
                logger.error("Cannot access master volume control", e);
            }
        }
    }
}

From source file:org.openhab.io.javasound.internal.JavaSoundAudioSink.java

private void runVolumeCommand(Closure closure) {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    for (Mixer.Info info : infos) {
        Mixer mixer = AudioSystem.getMixer(info);
        if (mixer.isLineSupported(Port.Info.SPEAKER)) {
            Port port;//from www.j  a  v a2s.co m
            try {
                port = (Port) mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                    FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                    closure.execute(volume);
                }
                port.close();
            } catch (LineUnavailableException e) {
                LOGGER.error("Cannot access master volume control", e);
            }
        }
    }
}