Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java
38924 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 {4647public Soundbank getSoundbank(URL url)48throws InvalidMidiDataException, IOException {49try {50AudioInputStream ais = AudioSystem.getAudioInputStream(url);51Soundbank sbk = getSoundbank(ais);52ais.close();53return sbk;54} catch (UnsupportedAudioFileException e) {55return null;56} catch (IOException e) {57return null;58}59}6061public Soundbank getSoundbank(InputStream stream)62throws InvalidMidiDataException, IOException {63stream.mark(512);64try {65AudioInputStream ais = AudioSystem.getAudioInputStream(stream);66Soundbank sbk = getSoundbank(ais);67if (sbk != null)68return sbk;69} catch (UnsupportedAudioFileException e) {70} catch (IOException e) {71}72stream.reset();73return null;74}7576public Soundbank getSoundbank(AudioInputStream ais)77throws InvalidMidiDataException, IOException {78try {79byte[] buffer;80if (ais.getFrameLength() == -1) {81ByteArrayOutputStream baos = new ByteArrayOutputStream();82byte[] buff = new byte[102483- (1024 % ais.getFormat().getFrameSize())];84int ret;85while ((ret = ais.read(buff)) != -1) {86baos.write(buff, 0, ret);87}88ais.close();89buffer = baos.toByteArray();90} else {91buffer = new byte[(int) (ais.getFrameLength()92* ais.getFormat().getFrameSize())];93new DataInputStream(ais).readFully(buffer);94}95ModelByteBufferWavetable osc = new ModelByteBufferWavetable(96new ModelByteBuffer(buffer), ais.getFormat(), -4800);97ModelPerformer performer = new ModelPerformer();98performer.getOscillators().add(osc);99100SimpleSoundbank sbk = new SimpleSoundbank();101SimpleInstrument ins = new SimpleInstrument();102ins.add(performer);103sbk.addInstrument(ins);104return sbk;105} catch (Exception e) {106return null;107}108}109110public Soundbank getSoundbank(File file)111throws InvalidMidiDataException, IOException {112try {113AudioInputStream ais = AudioSystem.getAudioInputStream(file);114ais.close();115ModelByteBufferWavetable osc = new ModelByteBufferWavetable(116new ModelByteBuffer(file, 0, file.length()), -4800);117ModelPerformer performer = new ModelPerformer();118performer.getOscillators().add(osc);119SimpleSoundbank sbk = new SimpleSoundbank();120SimpleInstrument ins = new SimpleInstrument();121ins.add(performer);122sbk.addInstrument(ins);123return sbk;124} catch (UnsupportedAudioFileException e1) {125return null;126} catch (IOException e) {127return null;128}129}130}131132133