import java.io.*; import javax.sound.sampled.*; public class Sound { private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F, 16, 2, 4, 8000.0F, false); private DataLine.Info info; private TargetDataLine lineIn; private SourceDataLine lineOut; private ByteArrayOutputStream out; private boolean running; public boolean recorderRunning; public synchronized void record() { try { info = new DataLine.Info(TargetDataLine.class, format); lineIn = (TargetDataLine)AudioSystem.getLine(info); lineIn.open(format); lineIn.start(); new Recorder(); } catch (LineUnavailableException e) { System.err.println("Line unavailable: " + e); System.exit(-2); } } public synchronized void play() { try { byte[] audio = out.toByteArray(); InputStream input = new ByteArrayInputStream(audio); AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize()); info = new DataLine.Info(SourceDataLine.class, format); lineOut = (SourceDataLine)AudioSystem.getLine(info); lineOut.open(format); lineOut.start(); new Player(ais); } catch (LineUnavailableException e) { System.err.println("Line unavailable: " + e); System.exit(-4); } } public synchronized void stop() { running = false; } class Recorder extends Thread { int bufferSize = (int)format.getSampleRate() * format.getFrameSize(); byte buffer[] = new byte[bufferSize]; public Recorder() { start(); } public synchronized void run() { recorderRunning = true; out = new ByteArrayOutputStream(); running = true; try { while (running) { int count = lineIn.read(buffer, 0, buffer.length); if (count > 0) { out.write(buffer, 0, count); } } out.close(); } catch (IOException e) { System.err.println("I/O problems: " + e); System.exit(-1); } recorderRunning = false; System.out.println("Sound recorded!"); } } class Player extends Thread { AudioInputStream ais = null; int bufferSize = (int)format.getSampleRate() * format.getFrameSize(); byte buffer[] = new byte[bufferSize]; public Player(AudioInputStream ais) { this.ais = ais; start(); } public synchronized void run() { try { int count = 0; while ((count = ais.read(buffer, 0, buffer.length)) != -1) { if (count > 0) { lineOut.write(buffer, 0, count); } } lineOut.drain(); //EX lineOut.close(); } catch (IOException e) { System.err.println("I/O problems: " + e); System.exit(-3); } System.out.println("Sound played!"); } } public synchronized Storage get() { // Konvertera från PCM till ULAW här --- alla data ligger i out System.out.println("Converting from PCM to ULAW, size before: " + out.toByteArray().length); byte[] xxx = out.toByteArray(); byte[] yyy = new byte[xxx.length / 2]; TConversionTool.pcm162ulaw(xxx, 0, yyy, 0, yyy.length, false); System.out.println("Converted from PCM to ULAW, size after: " + yyy.length); return new Storage(yyy); } public synchronized void load(Storage storage) { try { out=null; // Innan hade jag inte den raden och blev förvillad av "öändlig" komrimeringsgrad när jag experimenterade och det blev exception nedan :-) // Konvertera från ULAW till PCM här System.out.println("Converting from ULAW to PCM, size before: " + storage.getData().length); byte[] xxx = storage.getData(); byte[] yyy = new byte[xxx.length * 2]; TConversionTool.ulaw2pcm16(xxx, 0, yyy, 0, xxx.length, false); System.out.println("Converted from ULAW to PCM, size after: " + yyy.length); out = new ByteArrayOutputStream(); out.write(yyy, 0, yyy.length); } catch (Exception e) { System.err.println("I/O problems: " + e); } } }