Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/AudioFileSoundbankReader.java
66646 views
/*1* Copyright (c) 2007, 2021, 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 {80int MEGABYTE = 1048576;81int DEFAULT_BUFFER_SIZE = 65536;82int MAX_FRAME_SIZE = 1024;83try {84byte[] buffer;85int frameSize = ais.getFormat().getFrameSize();86if (frameSize <= 0 || frameSize > MAX_FRAME_SIZE) {87throw new InvalidMidiDataException("Formats with frame size "88+ frameSize + " are not supported");89}9091long totalSize = ais.getFrameLength() * frameSize;92if (totalSize >= Integer.MAX_VALUE - 2) {93throw new InvalidMidiDataException(94"Can not allocate enough memory to read audio data.");95}9697if (ais.getFrameLength() == -1 || totalSize > MEGABYTE) {98ByteArrayOutputStream baos = new ByteArrayOutputStream();99byte[] buff = new byte[DEFAULT_BUFFER_SIZE - (DEFAULT_BUFFER_SIZE % frameSize)];100int ret;101while ((ret = ais.read(buff)) != -1) {102baos.write(buff, 0, ret);103}104ais.close();105buffer = baos.toByteArray();106} else {107buffer = new byte[(int) totalSize];108new DataInputStream(ais).readFully(buffer);109}110ModelByteBufferWavetable osc = new ModelByteBufferWavetable(111new ModelByteBuffer(buffer), ais.getFormat(), -4800);112ModelPerformer performer = new ModelPerformer();113performer.getOscillators().add(osc);114115SimpleSoundbank sbk = new SimpleSoundbank();116SimpleInstrument ins = new SimpleInstrument();117ins.add(performer);118sbk.addInstrument(ins);119return sbk;120} catch (Exception e) {121return null;122}123}124125@Override126public Soundbank getSoundbank(File file)127throws InvalidMidiDataException, IOException {128try {129AudioInputStream ais = AudioSystem.getAudioInputStream(file);130ais.close();131ModelByteBufferWavetable osc = new ModelByteBufferWavetable(132new ModelByteBuffer(file, 0, file.length()), -4800);133ModelPerformer performer = new ModelPerformer();134performer.getOscillators().add(osc);135SimpleSoundbank sbk = new SimpleSoundbank();136SimpleInstrument ins = new SimpleInstrument();137ins.add(performer);138sbk.addInstrument(ins);139return sbk;140} catch (UnsupportedAudioFileException e1) {141return null;142} catch (IOException e) {143return null;144}145}146}147148149