@Override
 protected void terminating() {
   super.terminating();
   AudioServer s = server;
   server = null;
   if (s != null) {
     s.shutdown();
   }
   BusClient b = bus;
   bus = null;
   if (b != null) {
     b.disconnectAll();
   }
 }
 @Override
 protected void starting() {
   if (outputClient == null) {
     try {
       setIdle();
     } catch (IllegalRootStateException ex) {
     }
     return;
   }
   bus =
       new BusClient(
           blockSize.value,
           inputClient == null ? 0 : inputClient.getInputCount(),
           outputClient.getOutputCount());
   busListener = new BusListener();
   bus.addBufferRateListener(busListener);
   bus.addConfigurationListener(busListener);
   if (inputClient != null) {
     makeInputConnections();
   }
   makeOutputConnections();
   try {
     server = createServer(bus);
   } catch (Exception ex) {
     Logger.getLogger(DefaultAudioRoot.class.getName()).log(Level.SEVERE, null, ex);
     try {
       setIdle();
     } catch (IllegalRootStateException ex1) {
     }
     return;
   }
   setDelegate(
       new Runnable() {
         public void run() {
           try {
             server.run();
           } catch (Exception ex) {
             Logger.getLogger(DefaultAudioRoot.class.getName()).log(Level.SEVERE, null, ex);
           }
           try {
             setIdle();
           } catch (IllegalRootStateException ex) {
             // ignore - state already changed
           }
         }
       });
   interrupt();
 }
  private AudioServer createServer(BusClient bus) throws Exception {
    float srate = sampleRate.value;
    int buffersize = getBuffersize();

    boolean usingDefault = false;
    LibraryInfo info = libraries.get(audioLib.value);
    if (info == null) {
      info = libraries.get(AudioSettings.getLibrary());
      if (info == null) {
        throw new IllegalStateException("Audio library not found");
      }
      usingDefault = true;
    }
    LOG.log(
        Level.FINE,
        "Found audio library {0}\n{1}",
        new Object[] {info.provider.getLibraryName(), info.provider.getLibraryDescription()});

    Device device = findDevice(info, usingDefault, false);
    if (device != null) {
      LOG.log(Level.FINE, "Found device : {0}", device.getName());
    }
    Device inputDevice = null;
    if (device != null && device.getMaxInputChannels() == 0 && bus.getSourceCount() > 0) {
      inputDevice = findDevice(info, usingDefault, true);
      if (inputDevice != null) {
        LOG.log(Level.FINE, "Found input device : {0}", inputDevice.getName());
      }
    }

    ClientID clientID = new ClientID("PraxisLIVE-" + getAddress().getRootID());

    AudioConfiguration ctxt =
        new AudioConfiguration(
            srate,
            bus.getSourceCount(),
            bus.getSinkCount(),
            buffersize,
            createCheckedExts(device, inputDevice, clientID));
    return info.provider.createServer(ctxt, bus);
  }
 @Override
 protected void stopping() {
   if (bus == null) {
     return;
   }
   //        setInterrupt(new Runnable() {
   //            public void run() {
   server.shutdown();
   bus.disconnectAll();
   server = null;
   bus = null;
   busListener = null;
   //            }
   //        });
   interrupt();
 }
 private void makeOutputConnections() {
   int count = Math.min(outputClient.getOutputCount(), bus.getSinkCount());
   for (int i = 0; i < count; i++) {
     bus.getSink(i).addSource(outputClient.getOutputSource(i));
   }
 }