Java Code Examples for javax.sound.sampled.AudioSystem#getAudioInputStream()

The following examples show how to use javax.sound.sampled.AudioSystem#getAudioInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AudioSignalSource.java    From jipes with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Convert signal to signed PCM for further processing.
 *
 * @param file audio file
 * @return PCM audio inputstream
 * @throws UnsupportedAudioFileException if the file is not supported by {@link AudioSystem}.
 * @throws IOException if something goes wrong while opening the audio file
 */
private static AudioInputStream openStream(final File file) throws UnsupportedAudioFileException, IOException {
    final AudioInputStream origAudioInputStream = AudioSystem.getAudioInputStream(file);
    final AudioFormat origFormat = origAudioInputStream.getFormat();
    return AudioSystem.getAudioInputStream(
            new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    origFormat.getSampleRate(),
                    origFormat.getSampleSizeInBits() <= 0 ? 16 : origFormat.getSampleSizeInBits(),
                    origFormat.getChannels(),
                    origFormat.getFrameSize() <= 0 ? origFormat.getChannels() * 2 : origFormat.getFrameSize(),
                    origFormat.getSampleRate(),
                    false),
            origAudioInputStream
    );
}
 
Example 2
Source File: AlawEncoderSync.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void run() {
    log("ConversionThread[" + num + "] started.");
    try {
        InputStream inStream = new ByteArrayInputStream(pcmBuffer);

        AudioInputStream pcmStream = new AudioInputStream(
                inStream, pcmFormat, AudioSystem.NOT_SPECIFIED);
        AudioInputStream alawStream = AudioSystem.getAudioInputStream(alawFormat, pcmStream);

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int read = 0;
        byte[] data = new byte[4096];
        while((read = alawStream.read(data)) != -1) {
            outStream.write(data, 0, read);
       }
       alawStream.close();
       resultArray = outStream.toByteArray();
    } catch (Exception ex) {
        log("ConversionThread[" + num + "] exception:");
        log(ex);
    }
    log("ConversionThread[" + num + "] completed.");
}
 
Example 3
Source File: UI.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
private void playSirenSound() {
	try {
		File soundFile = new File(sirenFile);
		AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
		AudioFormat format = soundIn.getFormat();
		DataLine.Info info = new DataLine.Info(Clip.class, format);
		clip = (Clip) AudioSystem.getLine(info);
		clip.addLineListener(new LineListener() {
			@Override
			public void update(LineEvent event) {
				if (event.getType() == LineEvent.Type.STOP) {
					soundOn = false;
				}
			}
		});
		clip.open(soundIn);
		clip.start();
		soundOn = true;
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: SoundController.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Loads and returns a Clip from a resource.
 * @param ref the resource name
 * @param isMP3 true if MP3, false if WAV
 * @return the loaded and opened clip
 */
private static MultiClip loadClip(String ref, boolean isMP3) {
	try {
		URL url = ResourceLoader.getResource(ref);

		// check for 0 length files
		InputStream in = url.openStream();
		if (in.available() == 0) {
			in.close();
			return new MultiClip(ref, null);
		}
		in.close();

		AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
		return loadClip(ref, audioIn, isMP3);
	} catch (Exception e) {
		softErr(e, "Failed to load clip %s", ref);
		return null;
	}
}
 
Example 5
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
/**
 * WAVE�t�@�C�������[�h
 * @param url WAVE�t�@�C����URL
 */
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    // �I�[�f�B�I�X�g���[�����J��
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    // WAVE�t�@�C���̃t�H�[�}�b�g���擾
    AudioFormat format = ais.getFormat();
    // ���C�����擾
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);

    // WAVE�f�[�^���擾
    DataClip clip = new DataClip(ais);
    
    // WAVE�f�[�^��o�^
    clips[counter] = clip;
    lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
    
    // ���C�����J��
    lines[counter].open(format);

    counter++;
}
 
Example 6
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
/**
 * WAVE�t�@�C�������[�h
 * @param url WAVE�t�@�C����URL
 */
public static void load(URL url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    // �I�[�f�B�I�X�g���[�����J��
    AudioInputStream ais = AudioSystem.getAudioInputStream(url);
    // WAVE�t�@�C���̃t�H�[�}�b�g���擾
    AudioFormat format = ais.getFormat();
    // ���C�����擾
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format, AudioSystem.NOT_SPECIFIED);

    // WAVE�f�[�^���擾
    DataClip clip = new DataClip(ais);
    // WAVE�f�[�^��o�^
    clips[counter] = clip;
    lines[counter] = (SourceDataLine)AudioSystem.getLine(info);
    
    // ���C�����J��
    lines[counter].open(format);

    counter++;
}
 
Example 7
Source File: SoundTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static synchronized Clip playback(URL url, float addVolume) {
    try {
        try ( AudioInputStream in = AudioSystem.getAudioInputStream(url)) {
            return playback(in, addVolume);
        }
    } catch (Exception e) {
        logger.debug(e.toString());
        return null;
    }
}
 
Example 8
Source File: JavaSoundAudioClip.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public JavaSoundAudioClip(InputStream in) throws IOException {
    if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");

    BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
    bis.mark(STREAM_BUFFER_SIZE);
    boolean success = false;
    try {
        AudioInputStream as = AudioSystem.getAudioInputStream(bis);
        // load the stream data into memory
        success = loadAudioData(as);

        if (success) {
            success = false;
            if (loadedAudioByteLength < CLIP_THRESHOLD) {
                success = createClip();
            }
            if (!success) {
                success = createSourceDataLine();
            }
        }
    } catch (UnsupportedAudioFileException e) {
        // not an audio file
        try {
            MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
            success = createSequencer(bis);
        } catch (InvalidMidiDataException e1) {
            success = false;
        }
    }
    if (!success) {
        throw new IOException("Unable to create AudioClip from input stream");
    }
}
 
Example 9
Source File: BackgroundMusicUtils.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public static Clip readMusicFile(InputStream audioFilePath, SoundOutput soundOutput) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
	AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFilePath);
	AudioFormat format = audioStream.getFormat();
	DataLine.Info info = new DataLine.Info(Clip.class, format);
	
	Mixer mixer = soundOutput.getMixer();
	Clip audioClip = (Clip) mixer.getLine(info);
	audioClip.open(audioStream);
	return audioClip;
}
 
Example 10
Source File: RecognizeHugeAuFiles.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests the {@code AudioInputStream} fetched from the fake header.
 * <p>
 * Note that the frameLength is stored as long which means
 * that {@code AudioInputStream} must store all possible data from au file.
 */
private static void testAIS(final byte[] type, final int rate,
                            final int channel, final long size)
        throws Exception {
    final byte[] header = createHeader(type, rate, channel, size);
    final ByteArrayInputStream fake = new ByteArrayInputStream(header);
    final AudioInputStream ais = AudioSystem.getAudioInputStream(fake);
    final AudioFormat format = ais.getFormat();
    final long frameLength = size / format.getFrameSize();
    if (size != MAX_UNSIGNED_INT) {
        if (frameLength != ais.getFrameLength()) {
            System.err.println("Expected: " + frameLength);
            System.err.println("Actual: " + ais.getFrameLength());
            throw new RuntimeException();
        }
    } else {
        if (ais.getFrameLength() != AudioSystem.NOT_SPECIFIED) {
            System.err.println("Expected: " + AudioSystem.NOT_SPECIFIED);
            System.err.println("Actual: " + ais.getFrameLength());
            throw new RuntimeException();
        }
    }
    if (ais.available() < 0) {
        System.err.println("available should be >=0: " + ais.available());
        throw new RuntimeException();
    }
    validateFormat(type[1], rate, channel, format);
}
 
Example 11
Source File: AIFFLargeHeader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    System.out.println();
    System.out.println();
    System.out.println("4399551: Repost of bug candidate: cannot replay aif file (Review ID: 108108)");
    // try to read this file
    AudioSystem.getAudioInputStream(new ByteArrayInputStream(SHORT_AIFC_ULAW));
    System.out.println("  test passed.");
}
 
Example 12
Source File: AudioFormat.java    From Azzet with Open Software License 3.0 5 votes vote down vote up
@Override
public Clip loadAsset( InputStream input, AssetInfo assetInfo ) throws Exception
{
	AudioInputStream ais = AudioSystem.getAudioInputStream( input );
	Clip clip = AudioSystem.getClip();
	clip.open( ais );
	return clip;
}
 
Example 13
Source File: AudioFloatInputStream.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public static AudioFloatInputStream getInputStream(File file)
        throws UnsupportedAudioFileException, IOException {
    return new DirectAudioFloatInputStream(AudioSystem
            .getAudioInputStream(file));
}
 
Example 14
Source File: AudioFloatInputStream.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static AudioFloatInputStream getInputStream(URL url)
        throws UnsupportedAudioFileException, IOException {
    return new DirectAudioFloatInputStream(AudioSystem
            .getAudioInputStream(url));
}
 
Example 15
Source File: BasicPlayer.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Inits Audio ressources from InputStream.
 */
protected void initAudioInputStream(final InputStream inputStream)
		throws UnsupportedAudioFileException, IOException {
	m_audioInputStream = AudioSystem.getAudioInputStream(inputStream);
	m_audioFileFormat = AudioSystem.getAudioFileFormat(inputStream);
}
 
Example 16
Source File: AudioFloatInputStream.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static AudioFloatInputStream getInputStream(InputStream stream)
        throws UnsupportedAudioFileException, IOException {
    return new DirectAudioFloatInputStream(AudioSystem
            .getAudioInputStream(stream));
}
 
Example 17
Source File: Sample.java    From mpcmaid with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Sample open(final File file) throws Exception {
	final AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
	return open(audioStream);
}
 
Example 18
Source File: AudioFloatInputStream.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static AudioFloatInputStream getInputStream(InputStream stream)
        throws UnsupportedAudioFileException, IOException {
    return new DirectAudioFloatInputStream(AudioSystem
            .getAudioInputStream(stream));
}
 
Example 19
Source File: AudioPlayer.java    From Java-Speech-Recognizer-Tutorial--Calculator with Apache License 2.0 3 votes vote down vote up
/**
 * 
 * @param audioFile
 *            audiofile
 * @param line
 *            line
 * @param lineListener
 *            lineListener
 * @param outputMode
 *            if MONO, force output to be mono; if STEREO, force output to
 *            be STEREO; if LEFT_ONLY, play a mono signal over the left
 *            channel of a stereo output, or mute the right channel of a
 *            stereo signal; if RIGHT_ONLY, do the same with the right
 *            output channel.
 * @throws IOException
 *             IOException
 * @throws UnsupportedAudioFileException
 *             UnsupportedAudioFileException
 */
public AudioPlayer(File audioFile, SourceDataLine line, LineListener lineListener, int outputMode)
		throws IOException, UnsupportedAudioFileException {
	this.ais = AudioSystem.getAudioInputStream(audioFile);
	this.line = line;
	this.lineListener = lineListener;
	this.outputMode = outputMode;
}
 
Example 20
Source File: AudioPlayer.java    From Java-Speech-Recognizer-Tutorial--Calculator with Apache License 2.0 3 votes vote down vote up
/**
 * 
 * @param audioFile
 *            audiofile
 * @param line
 *            line
 * @param lineListener
 *            lineListener
 * @param outputMode
 *            if MONO, force output to be mono; if STEREO, force output to
 *            be STEREO; if LEFT_ONLY, play a mono signal over the left
 *            channel of a stereo output, or mute the right channel of a
 *            stereo signal; if RIGHT_ONLY, do the same with the right
 *            output channel.
 * @throws IOException
 *             IOException
 * @throws UnsupportedAudioFileException
 *             UnsupportedAudioFileException
 */
public AudioPlayer(File audioFile, SourceDataLine line, LineListener lineListener, int outputMode)
		throws IOException, UnsupportedAudioFileException {
	this.ais = AudioSystem.getAudioInputStream(audioFile);
	this.line = line;
	this.lineListener = lineListener;
	this.outputMode = outputMode;
}