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.File; 022 import java.io.FileOutputStream; 023 import java.io.OutputStream; 024 025 import javax.sound.sampled.AudioFileFormat; 026 import javax.sound.sampled.AudioFormat; 027 import javax.sound.sampled.AudioInputStream; 028 import javax.sound.sampled.AudioSystem; 029 030 /** 031 * @author <a href="mailto:k957@68k.org">Enrique Lara</a> 032 */ 033 public class MorseAudioFileWriter extends AudioMorseWriter { 034 private static final Logger s_log = Logger.getLogger(MorseAudioFileWriter.class); 035 private static final boolean DEBUG_ENABLED = s_log.isDebugEnabled(); 036 037 public MorseAudioFileWriter(String fileBaseName) { 038 super(); 039 040 setFileName(fileBaseName); 041 } 042 043 // --------------------------------------------------- Instance Variables 044 private AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE; 045 int writeCount = 0; 046 protected String fileBaseName; 047 048 // --------------------------------------------------- Properties 049 public AudioFileFormat.Type getFileType() { 050 return this.fileType; 051 } 052 053 private String getFileName() { 054 String rval = this.fileBaseName; 055 056 if (writeCount > 0) { 057 rval = rval + "." + Integer.toString(writeCount); 058 } 059 060 return rval; 061 } 062 063 public void setFileType(AudioFileFormat.Type fileType) { 064 this.fileType = fileType; 065 } 066 067 public void setFileName(String fileBaseName) { 068 writeCount = 0; 069 this.fileBaseName = fileBaseName; 070 } 071 072 protected void writeMorse(char[] msg, int len) { 073 short v = (short) (12.7 * volume); 074 075 if (DEBUG_ENABLED) { 076 s_log.debug("tone=" + tone); 077 s_log.debug("volume=" + volume); 078 } 079 080 MorseBeep mb = new MorseBeep(); 081 mb.setVolume(v); 082 mb.setTone((double) tone); 083 mb.setWpm((short) wpm); 084 085 if (saveFile(mb, msg, len)) { 086 if (DEBUG_ENABLED) { 087 s_log.debug("OK"); 088 } 089 } else { 090 s_log.error("Problems writing morse to file."); 091 } 092 } 093 094 public boolean saveFile(MorseBeep mb, char[] msg, int len) { 095 boolean result = true; 096 097 String fileName = getFileName(); 098 099 if (DEBUG_ENABLED) { 100 s_log.debug("save msg:" + new String(msg, 0, len) + ", to file:" + fileName); 101 } 102 103 byte[] audioData = mb.generateAudioData(msg, len); 104 result = (audioData != null); 105 106 if (result) { 107 try { 108 File file = new File(fileName); 109 OutputStream os = new FileOutputStream(file); 110 111 result = saveAudioData(audioData, os, fileType); 112 113 os.close(); 114 115 writeCount++; 116 } catch (Exception e) { 117 s_log.error("Problems writing file:" + fileName, e); 118 } 119 } 120 121 return result; 122 } 123 124 private static boolean saveAudioData(byte[] audioData, OutputStream os, AudioFileFormat.Type fileType) { 125 boolean result = true; 126 127 try { 128 AudioFormat af = MorseUtil.getAudioFormat(); 129 AudioInputStream is = MorseUtil.getAudioInputStream(audioData, af); 130 131 int nbytesWritten = AudioSystem.write(is, fileType, os); 132 133 if (DEBUG_ENABLED) { 134 s_log.debug("audioData nbytes=" + audioData.length); 135 s_log.debug("nbytes written=" + nbytesWritten); 136 } 137 } catch (Exception e) { 138 result = false; 139 s_log.error("Exception", e); 140 } 141 142 return result; 143 } 144 }