Source for gnu.javax.sound.midi.dssi.DSSIMidiDeviceProvider

   1: /* DSSIMidiDeviceProvider.java -- DSSI Device Provider
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.javax.sound.midi.dssi;
  40: 
  41: import java.util.ArrayList;
  42: import java.util.List;
  43: import java.io.File;
  44: import java.io.FilenameFilter;
  45: 
  46: import gnu.classpath.Configuration;
  47: import javax.sound.midi.MidiDevice;
  48: import javax.sound.midi.MidiDevice.Info;
  49: import javax.sound.midi.spi.MidiDeviceProvider;
  50: 
  51: /**
  52:  * A DSSI MIDI device provider.
  53:  *
  54:  * DSSI (pronounced "dizzy") is an API for audio plugins, with particular
  55:  * application for software synthesis plugins with native user interfaces.
  56:  *
  57:  * Read about DSSI at http://dssi.sourceforge.net
  58:  *
  59:  * @author Anthony Green (green@redhat.com)
  60:  *
  61:  */
  62: public class DSSIMidiDeviceProvider extends MidiDeviceProvider
  63: {
  64:   /**
  65:    * The MidiDevice.Info specialized for DSSI synthesizers.
  66:    *
  67:    * @author Anthony Green (green@redhat.com)
  68:    *
  69:    */
  70:   private static class DSSIInfo extends Info
  71:   {
  72:     String soname;
  73:     long index;
  74: 
  75:     public DSSIInfo(String name, String vendor, String description,
  76:                     String version, String soname, long index)
  77:     {
  78:       super(name, vendor, description, version);
  79:       this.soname = soname;
  80:       this.index = index;
  81:     }
  82:   }
  83: 
  84:   static native long dlopen_(String soname);
  85:   static native void dlclose_(long sohandle);
  86:   static native long getDSSIHandle_(long sohandle, long index);
  87:   static native String getDSSIName_(long handle);
  88:   static native String getDSSICopyright_(long handle);
  89:   static native String getDSSIVendor_(long handle);
  90:   static native String getDSSILabel_(long handle);
  91: 
  92:   private static List examineLibrary(String soname)
  93:   {
  94:      List list = new ArrayList();
  95:      long index = 0;
  96:      long handle;
  97: 
  98:      long sohandle = dlopen_(soname);
  99:      if (sohandle == 0)
 100:        return list;
 101:      do
 102:      {
 103:        handle = getDSSIHandle_(sohandle, index);
 104:        if (handle == 0)
 105:          break;
 106:        String name = getDSSILabel_(handle);
 107:        String copyright = getDSSICopyright_(handle);
 108:        String label = getDSSIName_(handle);
 109:        String vendor = getDSSIVendor_(handle);
 110:        list.add(new DSSIInfo(name, vendor, label,
 111:                              "DSSI-1", soname, index));
 112:        index++;
 113:      } while (true);
 114: 
 115:      // Close the library and free memory
 116:      dlclose_(sohandle);
 117: 
 118:      return list;
 119:   }
 120: 
 121:   private static DSSIInfo[] infos;
 122: 
 123:   static
 124:   {
 125:     if (Configuration.INIT_LOAD_LIBRARY)
 126:       System.loadLibrary("gjsmdssi");
 127: 
 128:     File dssidir = new File("/usr/lib/dssi/");
 129:     String sofiles[] = dssidir.list(new FilenameFilter()
 130:                                     {
 131:                                       public boolean accept(File dir, String n)
 132:                                       {
 133:                                         return n.endsWith(".so");
 134:                                       }
 135:                                     });
 136:     List ilist = new ArrayList();
 137:     for (int i = 0; i < sofiles.length; i++)
 138:       ilist.addAll(examineLibrary(new File(dssidir, sofiles[i]).getAbsolutePath()));
 139:     infos = (DSSIInfo[]) ilist.toArray(new DSSIInfo[ilist.size()]);
 140:   }
 141: 
 142:   public DSSIMidiDeviceProvider()
 143:   {
 144:     // Empty.
 145:   }
 146: 
 147:   /* Return the Info array.
 148:    * @see javax.sound.midi.spi.MidiDeviceProvider#getDeviceInfo()
 149:    */
 150:   public Info[] getDeviceInfo()
 151:   {
 152:     return infos;
 153:   }
 154: 
 155:   /* Get a MIDI Device for info.
 156:    * @see javax.sound.midi.spi.MidiDeviceProvider#getDevice(javax.sound.midi.MidiDevice.Info)
 157:    */
 158:   public MidiDevice getDevice(Info info)
 159:   {
 160:     for (int i = 0; i < infos.length; i++)
 161:     {
 162:       if (info.equals(infos[i]))
 163:       {
 164:           return new DSSISynthesizer(infos[i],
 165:                                      infos[i].soname,
 166:                                      infos[i].index);
 167:       }
 168:     }
 169:     throw new IllegalArgumentException("Don't recognize MIDI device " + info);
 170:   }
 171: }