private synchronized void startSampled(AudioInputStream as, InputStream in) throws UnsupportedAudioFileException, LineUnavailableException { Info info = null; DataPusher datapusher = null; DataLine.Info lineinfo = null; SourceDataLine sourcedataline = null; // if ALAW or ULAW, we must convert.... as = Toolkit.getPCMConvertedAudioInputStream(as); if (as == null) { // could not convert return; } lineinfo = new DataLine.Info(SourceDataLine.class, as.getFormat()); if (!(AudioSystem.isLineSupported(lineinfo))) { return; } sourcedataline = (SourceDataLine) AudioSystem.getLine(lineinfo); datapusher = new DataPusher(sourcedataline, as); info = new Info(null, in, datapusher); infos.addElement(info); datapusher.start(); }
public static void render(OutputStream os, AudioFormat format, Map<String, Object> info) throws Exception { AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer(); AudioInputStream stream = synth.openStream(format, info); Receiver recv = synth.getReceiver(); Soundbank defsbk = synth.getDefaultSoundbank(); if (defsbk != null) synth.unloadAllInstruments(defsbk); synth.loadAllInstruments(soundbank); double totalTime = 5; send(sequence, recv); long len = (long) (stream.getFormat().getFrameRate() * (totalTime + 4)); stream = new AudioInputStream(stream, stream.getFormat(), len); long t = System.currentTimeMillis(); AudioSystem.write(stream, AudioFileFormat.Type.WAVE, os); t = System.currentTimeMillis() - t; stream.close(); }
/** Loads a Sound from an AudioInputStream. */ public Sound getSound(AudioInputStream audioStream) { if (audioStream == null) { return null; } // get the number of bytes to read int length = (int) (audioStream.getFrameLength() * audioStream.getFormat().getFrameSize()); // read the entire stream byte[] samples = new byte[length]; DataInputStream is = new DataInputStream(audioStream); try { is.readFully(samples); is.close(); } catch (IOException ex) { ex.printStackTrace(); } // return the samples return new Sound(samples); }