/[svn]/jlscp/trunk/src/org/linuxsampler/lscp/Client.java
ViewVC logotype

Diff of /jlscp/trunk/src/org/linuxsampler/lscp/Client.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1346 by iliev, Thu Sep 13 22:02:03 2007 UTC revision 1542 by iliev, Tue Dec 4 18:14:31 2007 UTC
# Line 34  import java.util.Vector; Line 34  import java.util.Vector;
34  import java.util.logging.Level;  import java.util.logging.Level;
35  import java.util.logging.Logger;  import java.util.logging.Logger;
36    
 import static org.linuxsampler.lscp.Parser.*;  
37  import org.linuxsampler.lscp.event.*;  import org.linuxsampler.lscp.event.*;
38    
39    import static org.linuxsampler.lscp.Parser.*;
40    
41    
42  /**  /**
43   * This class is the abstraction representing a client endpoint for communication with LinuxSampler   * This class is the abstraction representing a client endpoint for communication with LinuxSampler
# Line 343  public class Client { Line 344  public class Client {
344                  if(!llFSI.isEmpty()) subscribe("FX_SEND_INFO");                  if(!llFSI.isEmpty()) subscribe("FX_SEND_INFO");
345                  if(!llSC.isEmpty()) subscribe("STREAM_COUNT");                  if(!llSC.isEmpty()) subscribe("STREAM_COUNT");
346                  if(!llVC.isEmpty()) subscribe("VOICE_COUNT");                  if(!llVC.isEmpty()) subscribe("VOICE_COUNT");
347                    if(!llTSC.isEmpty()) subscribe("TOTAL_STREAM_COUNT");
348                  if(!llTVC.isEmpty()) subscribe("TOTAL_VOICE_COUNT");                  if(!llTVC.isEmpty()) subscribe("TOTAL_VOICE_COUNT");
349                  if(!llMIMC.isEmpty()) subscribe("MIDI_INSTRUMENT_MAP_COUNT");                  if(!llMIMC.isEmpty()) subscribe("MIDI_INSTRUMENT_MAP_COUNT");
350                  if(!llMIMI.isEmpty()) subscribe("MIDI_INSTRUMENT_MAP_INFO");                  if(!llMIMI.isEmpty()) subscribe("MIDI_INSTRUMENT_MAP_INFO");
# Line 484  public class Client { Line 486  public class Client {
486          private final Vector<ItemInfoListener> llMIDI = new Vector<ItemInfoListener>();          private final Vector<ItemInfoListener> llMIDI = new Vector<ItemInfoListener>();
487          private final Vector<StreamCountListener> llSC = new Vector<StreamCountListener>();          private final Vector<StreamCountListener> llSC = new Vector<StreamCountListener>();
488          private final Vector<VoiceCountListener> llVC = new Vector<VoiceCountListener>();          private final Vector<VoiceCountListener> llVC = new Vector<VoiceCountListener>();
489            private final Vector<TotalStreamCountListener> llTSC = new Vector<TotalStreamCountListener>();
490          private final Vector<TotalVoiceCountListener> llTVC = new Vector<TotalVoiceCountListener>();          private final Vector<TotalVoiceCountListener> llTVC = new Vector<TotalVoiceCountListener>();
491                    
492          /** MIDI instrument map count listeners */          /** MIDI instrument map count listeners */
# Line 521  public class Client { Line 524  public class Client {
524                          !llMIDI.isEmpty() ||                          !llMIDI.isEmpty() ||
525                          !llSC.isEmpty()   ||                          !llSC.isEmpty()   ||
526                          !llVC.isEmpty()   ||                          !llVC.isEmpty()   ||
527                            !llTSC.isEmpty()  ||
528                          !llTVC.isEmpty()  ||                          !llTVC.isEmpty()  ||
529                          !llMIMC.isEmpty() ||                          !llMIMC.isEmpty() ||
530                          !llMIMI.isEmpty() ||                          !llMIMI.isEmpty() ||
# Line 545  public class Client { Line 549  public class Client {
549                                          s = s.substring("NAME ".length());                                          s = s.substring("NAME ".length());
550                                          list = parseEscapedStringList(s, ' ');                                          list = parseEscapedStringList(s, ' ');
551                                          if(list.length != 2) throw new LscpException();                                          if(list.length != 2) throw new LscpException();
552                                          list[1] = toNonEscapedText(list[1]);                                          list[1] = toNonEscapedString(list[1]);
553                                          e = new InstrumentsDbEvent(this, list[0], list[1]);                                          e = new InstrumentsDbEvent(this, list[0], list[1]);
554                                          for(InstrumentsDbListener l : llID) {                                          for(InstrumentsDbListener l : llID) {
555                                                  l.directoryNameChanged(e);                                                  l.directoryNameChanged(e);
# Line 574  public class Client { Line 578  public class Client {
578                                          s = s.substring("NAME ".length());                                          s = s.substring("NAME ".length());
579                                          list = parseEscapedStringList(s, ' ');                                          list = parseEscapedStringList(s, ' ');
580                                          if(list.length != 2) throw new LscpException();                                          if(list.length != 2) throw new LscpException();
581                                          list[1] = toNonEscapedText(list[1]);                                          list[1] = toNonEscapedString(list[1]);
582                                          e = new InstrumentsDbEvent(this, list[0], list[1]);                                          e = new InstrumentsDbEvent(this, list[0], list[1]);
583                                          for(InstrumentsDbListener l : llID) {                                          for(InstrumentsDbListener l : llID) {
584                                                  l.instrumentNameChanged(e);                                                  l.instrumentNameChanged(e);
# Line 661  public class Client { Line 665  public class Client {
665                          } catch(NumberFormatException x) {                          } catch(NumberFormatException x) {
666                                  getLogger().log(Level.WARNING, "Unknown CHANNEL_INFO format", x);                                  getLogger().log(Level.WARNING, "Unknown CHANNEL_INFO format", x);
667                          }                          }
668                    } else if(s.startsWith("TOTAL_STREAM_COUNT:")) {
669                            try {
670                                    s = s.substring("TOTAL_STREAM_COUNT:".length());
671                                    int i = Integer.parseInt(s);
672                                    TotalStreamCountEvent e = new TotalStreamCountEvent(this, i);
673                                    for(TotalStreamCountListener l : llTSC) l.totalStreamCountChanged(e);
674                            } catch(NumberFormatException x) {
675                                    getLogger().log (
676                                            Level.WARNING, "Unknown TOTAL_STREAM_COUNT format", x
677                                    );
678                            }
679                  } else if(s.startsWith("TOTAL_VOICE_COUNT:")) {                  } else if(s.startsWith("TOTAL_VOICE_COUNT:")) {
680                          try {                          try {
681                                  s = s.substring("TOTAL_VOICE_COUNT:".length());                                  s = s.substring("TOTAL_VOICE_COUNT:".length());
# Line 1133  public class Client { Line 1148  public class Client {
1148          /**          /**
1149           * Registers the specified listener for receiving event messages.           * Registers the specified listener for receiving event messages.
1150           * Listeners can be registered regardless of the connection state.           * Listeners can be registered regardless of the connection state.
1151             * @param l The <code>TotalStreamCountListener</code> to register.
1152             */
1153            public synchronized void
1154            addTotalStreamCountListener(TotalStreamCountListener l) {
1155                    if(llTSC.isEmpty()) subscribe("TOTAL_STREAM_COUNT");
1156                    llTSC.add(l);
1157            }
1158            
1159            /**
1160             * Removes the specified listener.
1161             * Listeners can be removed regardless of the connection state.
1162             * @param l The <code>TotalStreamCountListener</code> to remove.
1163             */
1164            public synchronized void
1165            removeTotalStreamCountListener(TotalStreamCountListener l) {
1166                    boolean b = llTSC.remove(l);
1167                    if(b && llTSC.isEmpty()) unsubscribe("TOTAL_STREAM_COUNT");
1168            }
1169            
1170            /**
1171             * Registers the specified listener for receiving event messages.
1172             * Listeners can be registered regardless of the connection state.
1173           * @param l The <code>TotalVoiceCountListener</code> to register.           * @param l The <code>TotalVoiceCountListener</code> to register.
1174           */           */
1175          public synchronized void          public synchronized void
# Line 2215  public class Client { Line 2252  public class Client {
2252                                  mid.setActive(Boolean.parseBoolean(s));                                  mid.setActive(Boolean.parseBoolean(s));
2253                          } else if(s.startsWith("PORTS: ")) {                          } else if(s.startsWith("PORTS: ")) {
2254                                  s = s.substring("PORTS: ".length());                                  s = s.substring("PORTS: ".length());
2255                                  int ports = Parser.parseInt(s);                                  
2256                                  MidiPort[] midiPorts = new MidiPort[ports > 0 ? ports : 0];                                  Parameter<Integer> ports = (Parameter<Integer>)
2257                                            getMidiInputDriverParameterInfo(drv, "PORTS");
2258                                    
2259                                    ports.parseValue(s);
2260                                    mid.setPortsParameter(ports);
2261                                    
2262                                    int j = ports.getValue();
2263                                    MidiPort[] midiPorts = new MidiPort[j > 0 ? j : 0];
2264                                                                    
2265                                  for(int i = 0; i < midiPorts.length; i++)                                  for(int i = 0; i < midiPorts.length; i++)
2266                                          midiPorts[i] = getMidiInputPortInfo(deviceId, i);                                          midiPorts[i] = getMidiInputPortInfo(deviceId, i);
# Line 2459  public class Client { Line 2503  public class Client {
2503          public synchronized int          public synchronized int
2504          addMidiInstrumentMap(String name) throws IOException, LSException, LscpException {          addMidiInstrumentMap(String name) throws IOException, LSException, LscpException {
2505                  verifyConnection();                  verifyConnection();
2506                  out.writeLine("ADD MIDI_INSTRUMENT_MAP '" + name + "'");                  out.writeLine("ADD MIDI_INSTRUMENT_MAP '" + toEscapedString(name) + "'");
2507                  if(getPrintOnlyMode()) return -1;                  if(getPrintOnlyMode()) return -1;
2508                                    
2509                  ResultSet rs = getEmptyResultSet();                  ResultSet rs = getEmptyResultSet();
# Line 2562  public class Client { Line 2606  public class Client {
2606                                    
2607                  for(String s : lnS) {                  for(String s : lnS) {
2608                          if(s.startsWith("NAME: ")) {                          if(s.startsWith("NAME: ")) {
2609                                  name = s.substring("NAME: ".length());                                  name = toNonEscapedString(s.substring("NAME: ".length()));
2610                          } else if(s.startsWith("DEFAULT: ")) {                          } else if(s.startsWith("DEFAULT: ")) {
2611                                  b = Boolean.parseBoolean(s.substring("DEFAULT: ".length()));                                  b = Boolean.parseBoolean(s.substring("DEFAULT: ".length()));
2612                          } else {                          } else {
# Line 2610  public class Client { Line 2654  public class Client {
2654                                  throws IOException, LscpException, LSException {                                  throws IOException, LscpException, LSException {
2655                                    
2656                  verifyConnection();                  verifyConnection();
2657                    name = toEscapedString(name);
2658                  out.writeLine("SET MIDI_INSTRUMENT_MAP NAME " +  + mapId + " '" + name + "'");                  out.writeLine("SET MIDI_INSTRUMENT_MAP NAME " +  + mapId + " '" + name + "'");
2659                  if(getPrintOnlyMode()) return;                  if(getPrintOnlyMode()) return;
2660                                    
# Line 2665  public class Client { Line 2710  public class Client {
2710                  if(!info.getLoadMode().name().equals("DEFAULT")) {                  if(!info.getLoadMode().name().equals("DEFAULT")) {
2711                          cmd.append(' ').append(info.getLoadMode().name());                          cmd.append(' ').append(info.getLoadMode().name());
2712                  }                  }
2713                  if(info.getName() != null) cmd.append(" '").append(info.getName()).append("'");                  
2714                    if(info.getName() != null) {
2715                            String s = toEscapedString(info.getName());
2716                            cmd.append(" '").append(s).append("'");
2717                    }
2718                                    
2719                  out.writeLine(cmd.toString());                  out.writeLine(cmd.toString());
2720                  if(getPrintOnlyMode()) return;                  if(getPrintOnlyMode()) return;
# Line 2861  public class Client { Line 2910  public class Client {
2910          loadInstrument(String filename, int instrIdx, int samplerChn, boolean nonModal)          loadInstrument(String filename, int instrIdx, int samplerChn, boolean nonModal)
2911                                                  throws IOException, LscpException, LSException {                                                  throws IOException, LscpException, LSException {
2912                                    
                 filename = toEscapedString(filename);  
2913                  String cmd = nonModal ? "LOAD INSTRUMENT NON_MODAL " : "LOAD INSTRUMENT ";                  String cmd = nonModal ? "LOAD INSTRUMENT NON_MODAL " : "LOAD INSTRUMENT ";
2914                  String args = '\'' + filename + "' " + instrIdx + ' ' + samplerChn;                  String args = '\'' + filename + "' " + instrIdx + ' ' + samplerChn;
2915                                    
# Line 3044  public class Client { Line 3092  public class Client {
3092                  out.writeLine("LIST AVAILABLE_ENGINES");                  out.writeLine("LIST AVAILABLE_ENGINES");
3093                  if(getPrintOnlyMode()) return null;                  if(getPrintOnlyMode()) return null;
3094                                    
3095                  return parseQuotedStringList(getSingleLineResultSet().getResult());                  return parseStringList(getSingleLineResultSet().getResult());
3096          }          }
3097                    
3098          /**          /**
# Line 3515  public class Client { Line 3563  public class Client {
3563                                    
3564                  verifyConnection();                  verifyConnection();
3565                  String s = String.valueOf(channel) + " " + String.valueOf(midiCtrl);                  String s = String.valueOf(channel) + " " + String.valueOf(midiCtrl);
3566                  if(name != null) s += " '" + name + "'";                  if(name != null) s += " '" + toEscapedString(name) + "'";
3567                  out.writeLine("CREATE FX_SEND " + s);                  out.writeLine("CREATE FX_SEND " + s);
3568                  if(getPrintOnlyMode()) return -1;                  if(getPrintOnlyMode()) return -1;
3569                                    
# Line 3646  public class Client { Line 3694  public class Client {
3694                                  throws IOException, LscpException, LSException {                                  throws IOException, LscpException, LSException {
3695                                    
3696                  verifyConnection();                  verifyConnection();
3697                  String args = " " + channel + " " + fxSend + " '" + name + "'";                  String args = " " + channel + " " + fxSend + " '" + toEscapedString(name) + "'";
3698                  out.writeLine("SET FX_SEND NAME" + args);                  out.writeLine("SET FX_SEND NAME" + args);
3699                  if(getPrintOnlyMode()) return;                  if(getPrintOnlyMode()) return;
3700                                    
# Line 3745  public class Client { Line 3793  public class Client {
3793           * @see #getSamplerChannels           * @see #getSamplerChannels
3794           */           */
3795          public synchronized void          public synchronized void
3796          editInstrument(int samplerChn) throws IOException, LscpException, LSException {          editChannelInstrument(int samplerChn) throws IOException, LscpException, LSException {
3797                  verifyConnection();                  verifyConnection();
3798                  out.writeLine("EDIT INSTRUMENT " + samplerChn);                  out.writeLine("EDIT CHANNEL INSTRUMENT " + samplerChn);
3799                  if(getPrintOnlyMode()) return;                  if(getPrintOnlyMode()) return;
3800                                    
3801                  ResultSet rs = getEmptyResultSet();                  ResultSet rs = getEmptyResultSet();
# Line 3885  public class Client { Line 3933  public class Client {
3933                                    
3934                  String[] names = parseEscapedStringList(getSingleLineResultSet().getResult());                  String[] names = parseEscapedStringList(getSingleLineResultSet().getResult());
3935                  for(int i = 0; i < names.length; i++) {                  for(int i = 0; i < names.length; i++) {
3936                          names[i] = toNonEscapedText(names[i]);                          names[i] = toNonEscapedString(names[i]);
3937                  }                  }
3938                  return names;                  return names;
3939          }          }
# Line 4143  public class Client { Line 4191  public class Client {
4191                  verifyConnection();                  verifyConnection();
4192                  String s = "ADD DB_INSTRUMENTS";                  String s = "ADD DB_INSTRUMENTS";
4193                  if(background) s += " NON_MODAL";                  if(background) s += " NON_MODAL";
4194                  s += " '" + dbDir + "' '" + toEscapedString(filePath) + "' ";                  s += " '" + dbDir + "' '" + filePath + "' ";
4195                  out.writeLine(s + String.valueOf(instrIndex));                  out.writeLine(s + String.valueOf(instrIndex));
4196                  if(getPrintOnlyMode()) return -1;                  if(getPrintOnlyMode()) return -1;
4197                                    
# Line 4190  public class Client { Line 4238  public class Client {
4238                  verifyConnection();                  verifyConnection();
4239                  String s = "ADD DB_INSTRUMENTS";                  String s = "ADD DB_INSTRUMENTS";
4240                  if(background) s += " NON_MODAL";                  if(background) s += " NON_MODAL";
4241                  out.writeLine(s + " '" + dbDir + "' '" + toEscapedString(filePath) + "'");                  out.writeLine(s + " '" + dbDir + "' '" + filePath + "'");
4242                  if(getPrintOnlyMode()) return -1;                  if(getPrintOnlyMode()) return -1;
4243                                    
4244                  ResultSet rs = getEmptyResultSet();                  ResultSet rs = getEmptyResultSet();
# Line 4274  public class Client { Line 4322  public class Client {
4322                  }                  }
4323                                    
4324                  sb.append(" '").append(dbDir).append("' '");                  sb.append(" '").append(dbDir).append("' '");
4325                  sb.append(toEscapedString(fsDir)).append("'");                  sb.append(fsDir).append("'");
4326                  out.writeLine(sb.toString());                  out.writeLine(sb.toString());
4327                  if(getPrintOnlyMode()) return -1;                  if(getPrintOnlyMode()) return -1;
4328                                    
# Line 4372  public class Client { Line 4420  public class Client {
4420                                    
4421                  String[] names = parseEscapedStringList(getSingleLineResultSet().getResult());                  String[] names = parseEscapedStringList(getSingleLineResultSet().getResult());
4422                  for(int i = 0; i < names.length; i++) {                  for(int i = 0; i < names.length; i++) {
4423                          names[i] = toNonEscapedText(names[i]);                          names[i] = toNonEscapedString(names[i]);
4424                  }                  }
4425                  return names;                  return names;
4426          }          }
# Line 4807  public class Client { Line 4855  public class Client {
4855          }          }
4856                    
4857          /**          /**
4858             * Removes all instruments and directories and re-creates
4859             * the instruments database structure.
4860             * @throws IOException If some I/O error occurs.
4861             * @throws LscpException If LSCP protocol corruption occurs.
4862             * @throws LSException If the formatting of the instruments database failed.
4863             */
4864            public synchronized void
4865            formatInstrumentsDb() throws IOException, LscpException, LSException {
4866                    verifyConnection();
4867                    out.writeLine("FORMAT INSTRUMENTS_DB");
4868                    if(getPrintOnlyMode()) return;
4869                    
4870                    ResultSet rs = getEmptyResultSet();
4871            }
4872            
4873            /**
4874           * Resets the specified sampler channel.           * Resets the specified sampler channel.
4875           *           *
4876           * @param samplerChn The sampler channel number.           * @param samplerChn The sampler channel number.
# Line 4843  public class Client { Line 4907  public class Client {
4907          }          }
4908                    
4909          /**          /**
4910             * Gets the current number of all active streams.
4911             * @return The current number of all active streams.
4912             * @throws IOException If some I/O error occurs.
4913             * @throws LscpException If LSCP protocol corruption occurs.
4914             * @throws LSException If some other error occurs.
4915             */
4916            public synchronized int
4917            getTotalStreamCount() throws IOException, LscpException, LSException {
4918                    verifyConnection();
4919                    out.writeLine("GET TOTAL_STREAM_COUNT");
4920                    if(getPrintOnlyMode()) return -1;
4921                    
4922                    String s = getSingleLineResultSet().getResult();
4923                    return parseInt(s);
4924            }
4925            
4926            /**
4927           * Gets the current number of all active voices.           * Gets the current number of all active voices.
4928           * @return The current number of all active voices.           * @return The current number of all active voices.
4929           * @throws IOException If some I/O error occurs.           * @throws IOException If some I/O error occurs.
# Line 4931  public class Client { Line 5012  public class Client {
5012                  ResultSet rs = getEmptyResultSet();                  ResultSet rs = getEmptyResultSet();
5013          }          }
5014                    
5015            /**
5016             * Gets the number of instruments in the specified instrument file.
5017             * @param filename The absolute path name of the instrument file.
5018             * @return The number of instruments in the specified instrument file.
5019             * @throws IOException If some I/O error occurs.
5020             * @throws LscpException If LSCP protocol corruption occurs.
5021             * @throws LSException If the file is not found, or other error occur.
5022             */
5023            public synchronized int
5024            getFileInstrumentCount(String filename) throws IOException, LscpException, LSException {
5025                    verifyConnection();
5026                    out.writeLine("GET FILE INSTRUMENTS '" + filename +"'");
5027                    if(getPrintOnlyMode()) return -1;
5028                    
5029                    String s = getSingleLineResultSet().getResult();
5030                    return parseInt(s);
5031            }
5032            
5033            /**
5034             * Gets information about the instrument with index
5035             * <code>instrIdx</code> in the specified instrument file.
5036             * @param filename The absolute path name of the instrument file.
5037             * @param instrIdx The index of the instrument in the specified instrument file.
5038             * @throws IOException If some I/O error occurs.
5039             * @throws LscpException If LSCP protocol corruption occurs.
5040             * @throws LSException If failed to retrieve information.
5041             */
5042            public synchronized Instrument
5043            getFileInstrumentInfo(String filename, int instrIdx)
5044                                    throws IOException, LscpException, LSException {
5045                    
5046                    verifyConnection();
5047                    out.writeLine("GET FILE INSTRUMENT INFO '" + filename + "' " + String.valueOf(instrIdx));
5048                    if(getPrintOnlyMode()) return null;
5049                    
5050                    ResultSet rs = getMultiLineResultSet();
5051                    Instrument instr = new FileInstrument(rs.getMultiLineResult()) { };
5052                    
5053                    return instr;
5054            }
5055            
5056            /**
5057             * Gets the list of instruments in the specified instrument file.
5058             * @param filename The absolute path name of the instrument file.
5059             * @return An <code>Instrument</code> array providing
5060             * information about all instruments in the specified instrument file.
5061             * @throws IOException If some I/O error occurs.
5062             * @throws LscpException If LSCP protocol corruption occurs.
5063             * @throws LSException If the specified file name is invalid.
5064             */
5065            public synchronized Instrument[]
5066            getFileInstruments(String filename) throws IOException, LscpException, LSException {
5067                    int l = getFileInstrumentCount(filename);
5068                    if(l < 0) return null;
5069                    Instrument[] instrS = new FileInstrument[l];
5070                    
5071                    for(int i = 0; i < instrS.length; i++) {
5072                            instrS[i] = getFileInstrumentInfo(filename, i);
5073                    }
5074                    return instrS;
5075            }
5076            
5077            private static class FileInstrument extends AbstractInstrument {
5078                    FileInstrument(String[] resultSet) throws LscpException {
5079                            super(resultSet);
5080                    }
5081                    
5082                    public String
5083                    getEngine() {
5084                            // TODO: engine lookup?
5085                            return getFormatFamily();
5086                    }
5087                    
5088                    public boolean
5089                    parse(String s) throws LscpException {
5090                            if(s.startsWith("PRODUCT: ") || s.startsWith("ARTISTS: ")) return true;
5091                            return super.parse(s);
5092                    }
5093            }
5094            
5095          private void          private void
5096          getEmptyResultSets(int count, String err) throws LSException {          getEmptyResultSets(int count, String err) throws LSException {
5097                  StringBuffer sb = new StringBuffer();                  StringBuffer sb = new StringBuffer();

Legend:
Removed from v.1346  
changed lines
  Added in v.1542

  ViewVC Help
Powered by ViewVC