001 /* 002 * Copyright (C) 2006-2010 Enrique Lara (k957@68k.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package net.sf.jmorse; 018 019 import net.sf.jmorse.util.logging.Logger; 020 021 import java.io.BufferedReader; 022 import java.io.File; 023 import java.io.FileReader; 024 import java.io.IOException; 025 import java.io.InputStreamReader; 026 import java.io.Reader; 027 import java.io.StringReader; 028 029 /** 030 * @author <a href="mailto:k957@68k.org">Enrique Lara</a> 031 */ 032 public class JMorse extends Thread { 033 private static final Logger s_log = Logger.getLogger(JMorse.class); 034 private static final boolean DEBUG_ENABLED = s_log.isDebugEnabled(); 035 protected AudioMorseWriter morse = new AudioMorseWriter(); 036 private boolean doRun = true; 037 038 public JMorse() { 039 init(new String[] { }); 040 } 041 042 public JMorse(String[] args) { 043 init(args); 044 } 045 046 protected void init(String[] args) { 047 morse.setWpm(15); 048 morse.setTone(450); 049 morse.setVolume(5); 050 051 String outputFileName = null; 052 String inputFileName = null; 053 054 StringBuffer msg = new StringBuffer(); 055 boolean done = false; 056 057 for (int i = 0; (i < args.length) && !done; i++) { 058 String arg = args[i]; 059 060 if (DEBUG_ENABLED) { 061 s_log.debug("arg=" + arg); 062 } 063 064 if ("--help".equals(arg)) { 065 usage(); 066 doRun = false; 067 done = true; 068 } else if ("--version".equals(arg)) { 069 version(); 070 doRun = false; 071 done = true; 072 } else if (arg.startsWith("-") && (arg.indexOf("=") != -1)) { 073 String[] tmp = arg.substring(1).split("[=]"); 074 String key = tmp[0]; 075 String value = tmp[1]; 076 077 if ("w".equals(key)) { 078 morse.setWpm(Integer.parseInt(value)); 079 } else if ("t".equals(key)) { 080 morse.setTone(Integer.parseInt(value)); 081 } else if ("v".equals(key)) { 082 morse.setVolume(Integer.parseInt(value)); 083 } else if ("o".equals(key)) { 084 outputFileName = value; 085 } else if ("i".equals(key)) { 086 inputFileName = value; 087 } else { 088 s_log.warn("Unrecognize argument:" + arg); 089 done = true; 090 } 091 } else { 092 if (msg.length() > 0) { 093 msg.append(" "); 094 } 095 096 msg.append(arg); 097 } 098 } 099 100 if (outputFileName != null) { 101 try { 102 MorseAudioFileWriter tmpMorse = new MorseAudioFileWriter(outputFileName); 103 tmpMorse.setWpm(morse.getWpm()); 104 tmpMorse.setTone(morse.getTone()); 105 tmpMorse.setVolume(morse.getVolume()); 106 107 morse = tmpMorse; 108 } catch (Exception e) { 109 s_log.error("Problems Creating output File", e); 110 } 111 } 112 113 if (inputFileName != null) { 114 msg = new StringBuffer(); 115 116 File inputFile = new File(inputFileName); 117 118 try { 119 reader = new BufferedReader(new FileReader(inputFile)); 120 } catch (Exception e) { 121 s_log.error("Problems reading input file:" + inputFile, e); 122 } 123 } else if (msg.length() > 0) { 124 reader = new StringReader(msg.toString()); 125 } 126 } 127 128 String singlePlayMsg = null; 129 File outputFile = null; 130 Reader reader = new BufferedReader(new InputStreamReader(System.in)); 131 132 public void run() { 133 if (doRun) { 134 try { 135 char[] cbuf = new char[1024]; 136 int len; 137 138 while ((len = reader.read(cbuf)) != -1) { 139 morse.write(cbuf, len); 140 } 141 142 if (DEBUG_ENABLED) { 143 s_log.debug("bye from JMorse"); 144 } 145 } catch (IOException e) { 146 s_log.warn("IOException", e); 147 } 148 } 149 150 if (DEBUG_ENABLED) { 151 s_log.debug("DONE."); 152 } 153 } 154 155 private void version() { 156 System.out.println("jmorse 1.2"); 157 System.out.println("Copyright (C) 2006-2010 Enrique Lara (k957@68k.org)"); 158 System.out.println("Licensed under the Apache License, Version 2.0"); 159 } 160 161 private void usage() { 162 System.out.println("Usage: java -jar jmorse.jar [OPTION] {string} [FILE]"); 163 System.out.println("Use string, file, or standard input, to generate morse code on the default audio output device."); 164 System.out.println(""); 165 System.out.println(" -t tone"); 166 System.out.println(" -w word speed"); 167 System.out.println(" -v volume [0-10]"); 168 System.out.println(" -i text file to use as message text"); 169 System.out.println(" -o output file to store morse code as wave"); 170 System.out.println(" --help display this help and exit"); 171 System.out.println(" --version output version information and exit"); 172 System.out.println(" --version output version information and exit"); 173 System.out.println(""); 174 System.out.println("If no input file or string is specified, then the standard input is read."); 175 } 176 177 public static void main(String[] args) { 178 JMorse app = new JMorse(args); 179 app.start(); 180 } 181 }