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