Last active
July 24, 2020 00:56
-
-
Save joshpetit/e673b274d5a3d94da0b3696786c864c1 to your computer and use it in GitHub Desktop.
Simple microphone that replays what is recorded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.sound.sampled.*; | |
import java.io.*; | |
import java.util.Scanner; | |
public class MicStuff { | |
public static void main(String[] args) throws LineUnavailableException, IOException, UnsupportedAudioFileException { | |
AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, | |
44100, | |
16, | |
2, | |
4, | |
44100, | |
false); | |
TargetDataLine tdl = AudioSystem.getTargetDataLine(format); | |
tdl.open(); | |
tdl.start(); | |
File out = File.createTempFile("prefix", "suffix"); | |
out.deleteOnExit(); | |
Thread thread = new Thread(new Thread(() -> { | |
try { | |
AudioInputStream ais = new AudioInputStream(tdl); | |
AudioSystem.write(ais, AudioFileFormat.Type.WAVE, out); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
})); | |
thread.start(); | |
try { | |
Thread.sleep(2500); | |
tdl.stop(); | |
tdl.close(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
Clip clip = AudioSystem.getClip(); | |
AudioInputStream ais = AudioSystem.getAudioInputStream(out); | |
clip.open(ais); | |
clip.start(); | |
Scanner scan = new Scanner(System.in); | |
while(true) { | |
int num = scan.nextInt(); | |
if (num == 0) { | |
clip.setFramePosition(0); | |
if (!clip.isActive()) { | |
clip.start(); | |
} | |
} else { | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment