import java.io.*; import javax.sound.sampled.*; public class Sound { // 8 kHz, 8 bit, mono // private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F, 8, 1, 1, 8000.0F, true); // 44.1 kHz, 16 bit, stereo // private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false); // 8 kHz, 16 bit, stereo // encoding, sampleRate, sampleSizeInBits, channels, frameSize, frameRate, bigEndian private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000.0F, 16, 1, 2, 8000.0F, false); // Funkar inte, man måste konvertera, ljudkortet stöder inte direkt verkar det som (???) // private AudioFormat format = new AudioFormat(AudioFormat.Encoding.ULAW, 8000.0F, 8, 1, 4, 8000.0F, true); private DataLine.Info info; private TargetDataLine lineIn; private SourceDataLine lineOut; private ByteArrayOutputStream out; private boolean running; public boolean recorderRunning; public synchronized void record() { try { if(info == null) { 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() { return new Storage(out); } public synchronized void load(Storage storage) { try { out = new ByteArrayOutputStream(); out.write(storage.getData(), 0, storage.getData().length); } catch (Exception e) { System.err.println("I/O problems: " + e); } } }