Here you can find the source of toFile(String line)
public static String toFile(String line) throws LineUnavailableException, InterruptedException, IOException, UnsupportedAudioFileException
//package com.java2s; //License from project: Apache License import javax.sound.sampled.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Main { public static final int DOT = 200, DASH = DOT * 3, FREQ = 500; static Map<String, String> morseAlphabet = new HashMap<String, String>(); public static String toFile(String line) throws LineUnavailableException, InterruptedException, IOException, UnsupportedAudioFileException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); StringBuilder sb = new StringBuilder(); for (char c : line.toLowerCase().toCharArray()) { String s = String.valueOf(c); if (" ".equals(s)) { sb.append("/"); sleep(bos, 7 * 8);//from w w w . ja v a 2s . c om } else if (morseAlphabet.containsKey(s)) { for (char note : morseAlphabet.get(s).toCharArray()) { sb.append(note); for (int i = 0; i < (note == '.' ? DOT : DASH) * 8; i++) { bos.write( new byte[] { (byte) (Math.sin(i / (16000F / FREQ) * 2.0 * Math.PI) * 127.0) }, 0, 1); } sleep(bos, 1 * 8); } } sleep(bos, 3 * 8); sb.append(" "); } byte[] b = bos.toByteArray(); bos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(b); AudioInputStream ais2 = new AudioInputStream(bis, getAudioFormat(), b.length); File fileOut = new File(DOT + "/" + line + "-" + DOT + ".wav"); AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE; AudioSystem.write(ais2, fileType, fileOut); System.out.println(fileOut.getAbsolutePath()); ffmpeg(line); bis.close(); ais2.close(); return sb.toString(); } private static void sleep(SourceDataLine sdl, Integer factor) { for (int i = 0; i < (DOT * factor); i++) { sdl.write(new byte[] { (byte) (0) }, 0, 1); } } private static void sleep(ByteArrayOutputStream bos, Integer factor) { for (int i = 0; i < (DOT * factor); i++) { bos.write(new byte[] { (byte) (0) }, 0, 1); } } /** * Defines an audio format */ static AudioFormat getAudioFormat() { return new AudioFormat(16000F, 8, 1, true, false); } private static void ffmpeg(String filename) { String cmd = "ffmpeg -y -i morse/" + DOT + "/" + filename + "-" + DOT + ".wav -ar 16000 -ab 48k -codec:a libmp3lame -ac 1 morse/" + DOT + "/mp3/" + filename + ".mp3"; try { Runtime.getRuntime().exec(cmd); } catch (Exception e) { e.printStackTrace(System.err); } } }