public void activate() { active = true; microphone.flush(); speaker.flush(); speaker.start(); blocker.release(); microphone.start(); microphone.flush(); }
@Ignore @Test public void testSilenceWriter() throws UnsupportedAudioFileException, InterruptedException, LineUnavailableException, FileNotFoundException { float sampleRate = 44100; int bufferSize = 1024; int overlap = 0; // available mixers int index = 0; int selectedMixerIndex = 4; for (Mixer.Info mixer : AudioSystem.getMixerInfo()) { System.out.println(index + ": " + Shared.toLocalString(mixer)); index++; } Mixer.Info selectedMixer = AudioSystem.getMixerInfo()[selectedMixerIndex]; System.out.println("Selected mixer: " + Shared.toLocalString(selectedMixer)); // open a line final Mixer mixer = AudioSystem.getMixer(selectedMixer); final AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, true); final DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format); TargetDataLine line; line = (TargetDataLine) mixer.getLine(dataLineInfo); final int numberOfSamples = bufferSize; line.open(format, numberOfSamples); line.start(); final AudioInputStream stream = new AudioInputStream(line); // create a new dispatcher AudioDispatcher dispatcher = new AudioDispatcher(stream, bufferSize, overlap); WaveformWriter writer = new WaveformWriter(format, "01.file.wav"); // add a processor, handle percussion event. dispatcher.addAudioProcessor(new SilenceDetector()); dispatcher.addAudioProcessor(writer); // run the dispatcher (on the same thread, use start() to run it on // another thread). new Thread(dispatcher).start(); Thread.sleep(3000); dispatcher.removeAudioProcessor(writer); writer = new WaveformWriter(format, "02.file.wav"); dispatcher.addAudioProcessor(writer); Thread.sleep(3000); dispatcher.stop(); }
/** * Starts the recording. To accomplish this, (i) the line is started and (ii) the thread is * started. */ public void start() { /* * Starting the TargetDataLine. It tells the line that we now want to read data from it. If this method isn't called, we * won't be able to read data from the line at all. */ m_line.start(); /* * Starting the thread. This call results in the method 'run()' (see below) being called. There, the data is actually read * from the line. */ super.start(); }
/** * Starts the recording. To accomplish this, (i) the line is started and (ii) the thread is * started. */ public void start() { /* Starting the TargetDataLine. It tells the line that we now want to read data from it. If this method isn't called, we won't be able to read data from the line at all. */ // Globals.recorderStatus = "STATUS: recording..."; m_line.start(); /* Starting the thread. This call results in the method 'run()' (see below) being called. There, the data is actually read from the line. */ super.start(); }
public void run() { try { b = new byte[6300]; line.open(new AudioFormat(44100, 16, 1, true, true), 6300); line.start(); while (true) { line.read(b, 0, b.length); server.writeByteBuffers(b, sourceIndex); if (output) serverOutput.write(b, 0, b.length); } } catch (LineUnavailableException e) { e.printStackTrace(); System.out.println(sourceIndex); } finally { line.stop(); line.close(); } }
public void run() { // --------------init SourceDataLine sdl = Central.getGoutputSelector().getSourceDataLine(); TargetDataLine tdl = Central.getGinputSelector().getTargetDataLine(); try { tdl.open(af, READ_BUFFER_SIZE); sdl.open(af, WRITE_BUFFER_SIZE); sdl.start(); tdl.start(); while (checkRunning()) { tdl.read(sbMove, 0, READSIZE); sdl.write(sbMove, 0, READSIZE); } tdl.close(); sdl.close(); } catch (LineUnavailableException e) { e.printStackTrace(); } error.log("Continuous Loop Stopped"); }
private void setNewMixer(Mixer mixer) throws LineUnavailableException, UnsupportedAudioFileException { if (dispatcher != null) { dispatcher.stop(); } if (fileName == null) { final AudioFormat format = new AudioFormat(sampleRate, 16, 1, true, false); final DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format); TargetDataLine line; line = (TargetDataLine) mixer.getLine(dataLineInfo); final int numberOfSamples = bufferSize; line.open(format, numberOfSamples); line.start(); final AudioInputStream stream = new AudioInputStream(line); // create a new dispatcher dispatcher = new AudioDispatcher(stream, bufferSize, overlap); } else { try { File audioFile = new File(fileName); dispatcher = AudioDispatcher.fromFile(audioFile, bufferSize, overlap); AudioFormat format = AudioSystem.getAudioFileFormat(audioFile).getFormat(); dispatcher.addAudioProcessor(new AudioPlayer(format)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } currentMixer = mixer; // add a processor, handle pitch event. dispatcher.addAudioProcessor(new PitchProcessor(algo, sampleRate, bufferSize, this)); dispatcher.addAudioProcessor(fftProcessor); // run the dispatcher (on a new thread). new Thread(dispatcher, "Audio dispatching").start(); }
/** * Apply the given effect to this WaveTab´s audio data * * @param effect The effect to apply */ public void applyEffect(Effect effect) { Selection sel = waveDisplay.getSelection(); if (sel.getLeft() == sel.getRight()) waveDisplay.setSelection(new Selection(0, getTotalLength())); Thread thread = null; try { AudioInputStream stream = getAudioInputStream(); int sourceChannels = stream.getFormat().getChannels(); stream = AudioManager.getStereoInputStream(stream); final FXUnit unit = new FXUnit(effect); if (effect.needsAnalysis()) { Analyzer a = new Analyzer(unit, stream); ProgressMonitor monitor = new ProgressMonitor(getShell(), a, "Analyzing...", "Analyzing audio data"); monitor.start(); stream = AudioManager.getStereoInputStream(getAudioInputStream()); } final SourceDataLine sourceLine = unit.getEffectSourceLine(); sourceLine.open(); sourceLine.start(); final TargetDataLine targetLine = unit.getEffectTargetLine(); targetLine.open(); targetLine.start(); if (!stream.getFormat().equals(sourceLine.getFormat())) { if (AudioSystem.isConversionSupported(sourceLine.getFormat(), stream.getFormat())) stream = AudioSystem.getAudioInputStream(sourceLine.getFormat(), stream); else { editor.errorMessage( "Unable to apply effect:\nFormat conversion from " + stream.getFormat() + " to " + sourceLine.getFormat() + " not supported."); return; } } final AudioInputStream inStream = stream; thread = new Thread() { public void run() { int numBytesRead = 0; byte[] buffer = new byte[sourceLine.getBufferSize()]; while (numBytesRead != -1 && !getItem().isDisposed()) { try { numBytesRead = inStream.read(buffer, 0, buffer.length); } catch (IOException e1) { e1.printStackTrace(); numBytesRead = -1; } if (numBytesRead > 0) { sourceLine.write(buffer, 0, numBytesRead); } try { Thread.sleep(0, 1); } catch (InterruptedException e) { } } } }; thread.start(); AudioInputStream in = new AudioInputStream(targetLine); if (sourceChannels == 1) in = AudioManager.getMonoInputStream(in); File tempFile = File.createTempFile("gmtmp_", ".wav"); AudioFormat tempFormat = new AudioFormat( fileFormat.getFormat().getSampleRate(), 16, fileFormat.getFormat().getChannels(), true, false); AudioFileOutputStream out = AudioManager.getDefault() .getAudioFileOutputStream( tempFile, tempFormat, AudioFileFormat.Type.WAVE, null, null, null); if (!in.getFormat().equals(out.getFormat())) in = AudioSystem.getAudioInputStream(out.getFormat(), in); SaveFileThread saver = new SaveFileThread( in, out, (int) inStream.getFrameLength(), in.getFormat().getFrameSize(), true); ProgressMonitor monitor = new ProgressMonitor( getShell(), saver, "Apply Effect", "Applying " + effect.getName() + " to Selection"); monitor.start(); File tempPeak = File.createTempFile("gmtmp_", ".gmpk"); CreatePeakFileThread peak = new CreatePeakFileThread(AudioSystem.getAudioInputStream(tempFile), tempPeak); monitor = new ProgressMonitor( getShell(), peak, "Creating peak file", "Creating peak file for applied effect."); monitor.start(); PeakWaveForm pwf = new PeakWaveForm(tempPeak); AudioFileWaveForm awf = new AudioFileWaveForm(tempFile, pwf, 32 * 1024, 25); CutListSource newSource = new AudioFileSource(tempFile, awf); sel = waveDisplay.getSelection(); int left = sel.getLeft(); int right = sel.getRight(); ReplaceElement el = new ReplaceElement( effect.getName(), newSource, left, right - left, fileFormat.getFormat()); cutList.addElement(el); undoOperations.add(el); redoOperations.clear(); thread.stop(); } catch (NotReadyException e) { e.printStackTrace(); editor.errorMessage(e.getMessage()); if (thread != null) thread.stop(); } catch (NotFinishedException e) { e.printStackTrace(); editor.errorMessage(e.getMessage()); if (thread != null) thread.stop(); } catch (LineUnavailableException e) { e.printStackTrace(); editor.errorMessage(e.getMessage()); if (thread != null) thread.stop(); } catch (IOException e) { e.printStackTrace(); editor.errorMessage(e.getMessage()); if (thread != null) thread.stop(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); editor.errorMessage(e.getMessage()); if (thread != null) thread.stop(); } }
// are there any checks we need to do before starting or stopping? public void startMic() { microphone.start(); inputThread.start(); }
private void startFile(File inputFile, Mixer mixer) { if (dispatcher != null) { dispatcher.stop(); } AudioFormat format; int bufferSize = 1024; int overlap = 0; double sampleRate = 44100; try { if (inputFile != null) { format = AudioSystem.getAudioFileFormat(inputFile).getFormat(); sampleRate = format.getSampleRate(); } else { format = new AudioFormat((float) sampleRate, 16, 1, true, true); } inputGain = new GainProcessor(defaultInputGain / 100.0); AudioPlayer audioPlayer = new AudioPlayer(format); if (inputFile == null) { DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, format); TargetDataLine line; line = (TargetDataLine) mixer.getLine(dataLineInfo); line.open(format, bufferSize); line.start(); final AudioInputStream stream = new AudioInputStream(line); final TarsosDSPAudioInputStream audioStream = new JVMAudioInputStream(stream); dispatcher = new AudioDispatcher(audioStream, bufferSize, overlap); } else { if (format.getChannels() != 1) { dispatcher = AudioDispatcherFactory.fromFile( inputFile, bufferSize * format.getChannels(), overlap * format.getChannels()); dispatcher.addAudioProcessor(new MultichannelToMono(format.getChannels(), true)); } else { dispatcher = AudioDispatcherFactory.fromFile(inputFile, bufferSize, overlap); } } flangerEffect = new FlangerEffect( defaultLength / 1000.0, defaultImpact / 100.0, sampleRate, defaultFrequency / 10.0); dispatcher.addAudioProcessor(flangerEffect); dispatcher.addAudioProcessor(inputGain); dispatcher.addAudioProcessor(new WaveformWriter(format, "flanger.wav")); dispatcher.addAudioProcessor(audioPlayer); Thread t = new Thread(dispatcher); t.start(); } catch (UnsupportedAudioFileException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (LineUnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public SoundServer(Mixer mixer, DataLine.Info dataLineInfo, AudioFormat audioFormat) { try { final TargetDataLine targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo); targetDataLine.open(audioFormat); targetDataLine.start(); try { serverSocket = new ServerSocket(20000); } catch (IOException e1) { e1.printStackTrace(); return; } clients = new ArrayList<>(); streams = new HashMap<>(); runServer = true; acceptRunnable = new Runnable() { public void run() { while (runServer) { try { Socket socket = serverSocket.accept(); synchronized (clients) { clients.add(socket); synchronized (streams) { streams.put(socket, socket.getOutputStream()); } System.out.println("Client connected from " + socket.getInetAddress()); } } catch (IOException e) { e.printStackTrace(); } } } }; sendRunnable = new Runnable() { public void run() { System.out.println("Server is running..."); recordBuffer = new byte[BUFFER_SIZE]; while (runServer) { int count = targetDataLine.read(recordBuffer, 0, BUFFER_SIZE); if (count > 0) { synchronized (clients) { for (int i = 0; i < clients.size(); i++) { Socket client = clients.get(i); OutputStream os = streams.get(client); try { os.write(recordBuffer, 0, BUFFER_SIZE); } catch (SocketException e) { clients.set(i, null); System.out.println("Client connection dropped..."); } catch (IOException e) { e.printStackTrace(); } } for (int i = 0; i < clients.size(); i++) { if (clients.get(i) == null) { clients.remove(i); } } } } } } }; new Thread(acceptRunnable).start(); new Thread(sendRunnable).start(); } catch (LineUnavailableException lue) { lue.printStackTrace(); } }