Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java
41161 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.media.sound;2526import java.io.ByteArrayOutputStream;27import java.io.DataInputStream;28import java.io.File;29import java.io.IOException;30import java.io.InputStream;31import java.net.URL;3233import javax.sound.midi.InvalidMidiDataException;34import javax.sound.midi.Soundbank;35import javax.sound.midi.spi.SoundbankReader;36import javax.sound.sampled.AudioInputStream;37import javax.sound.sampled.AudioSystem;38import javax.sound.sampled.UnsupportedAudioFileException;3940/**41* Soundbank reader that uses audio files as soundbanks.42*43* @author Karl Helgason44*/45public final class AudioFileSoundbankReader extends SoundbankReader {4647@Override48public Soundbank getSoundbank(URL url)49throws InvalidMidiDataException, IOException {50try {51AudioInputStream ais = AudioSystem.getAudioInputStream(url);52Soundbank sbk = getSoundbank(ais);53ais.close();54return sbk;55} catch (UnsupportedAudioFileException e) {56return null;57} catch (IOException e) {58return null;59}60}6162@Override63public Soundbank getSoundbank(InputStream stream)64throws InvalidMidiDataException, IOException {65stream.mark(512);66try {67AudioInputStream ais = AudioSystem.getAudioInputStream(stream);68Soundbank sbk = getSoundbank(ais);69if (sbk != null)70return sbk;71} catch (UnsupportedAudioFileException e) {72} catch (IOException e) {73}74stream.reset();75return null;76}7778public Soundbank getSoundbank(AudioInputStream ais)79throws InvalidMidiDataException, IOException {80try {81byte[] buffer;82if (ais.getFrameLength() == -1) {83ByteArrayOutputStream baos = new ByteArrayOutputStream();84byte[] buff = new byte[102485- (1024 % ais.getFormat().getFrameSize())];86int ret;87while ((ret = ais.read(buff)) != -1) {88baos.write(buff, 0, ret);89}90ais.close();91buffer = baos.toByteArray();92} else {93buffer = new byte[(int) (ais.getFrameLength()94* ais.getFormat().getFrameSize())];95new DataInputStream(ais).readFully(buffer);96}97ModelByteBufferWavetable osc = new ModelByteBufferWavetable(98new ModelByteBuffer(buffer), ais.getFormat(), -4800);99ModelPerformer performer = new ModelPerformer();100performer.getOscillators().add(osc);101102SimpleSoundbank sbk = new SimpleSoundbank();103SimpleInstrument ins = new SimpleInstrument();104ins.add(performer);105sbk.addInstrument(ins);106return sbk;107} catch (Exception e) {108return null;109}110}111112@Override113public Soundbank getSoundbank(File file)114throws InvalidMidiDataException, IOException {115try {116AudioInputStream ais = AudioSystem.getAudioInputStream(file);117ais.close();118ModelByteBufferWavetable osc = new ModelByteBufferWavetable(119new ModelByteBuffer(file, 0, file.length()), -4800);120ModelPerformer performer = new ModelPerformer();121performer.getOscillators().add(osc);122SimpleSoundbank sbk = new SimpleSoundbank();123SimpleInstrument ins = new SimpleInstrument();124ins.add(performer);125sbk.addInstrument(ins);126return sbk;127} catch (UnsupportedAudioFileException e1) {128return null;129} catch (IOException e) {130return null;131}132}133}134135136