Record sound: Difference between revisions
Content deleted Content added
Line 391: | Line 391: | ||
<syntaxhighlight> |
<syntaxhighlight> |
||
import java.io.IOException; |
|||
</syntaxhighlight> |
|||
import java.util.concurrent.Executors; |
|||
{{ out }} |
|||
import java.util.concurrent.ScheduledExecutorService; |
|||
<pre> |
|||
import java.util.concurrent.TimeUnit; |
|||
import java.io.File; |
|||
import javax.sound.sampled.AudioFileFormat.*; |
|||
</pre> |
|||
public final class SoundRecorder { |
|||
public static void main(String[] args) { |
|||
SoundRecorder recorder = new SoundRecorder(); |
|||
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
|||
scheduler.schedule( () -> recorder.finish(), 10, TimeUnit.SECONDS); |
|||
scheduler.shutdown(); |
|||
recorder.start(); |
|||
} |
|||
private void start() { |
|||
try { |
|||
AudioFormat format = createAudioFormat(); |
|||
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); |
|||
if ( ! AudioSystem.isLineSupported(info) ) { |
|||
System.out.println("Data line format is not supported"); |
|||
Runtime.getRuntime().exit(0); |
|||
} |
|||
line = (TargetDataLine) AudioSystem.getLine(info); |
|||
line.open(format); |
|||
line.start(); |
|||
System.out.println("Start capturing audio"); |
|||
AudioInputStream audioInputStream = new AudioInputStream(line); |
|||
AudioSystem.write(audioInputStream, audioFileType, wavFile); |
|||
} catch (LineUnavailableException | IOException exception) { |
|||
exception.printStackTrace(System.err); |
|||
} |
|||
} |
|||
private AudioFormat createAudioFormat() { |
|||
final float sampleRate = 16_000.0F; |
|||
final int sampleSizeInBits = 16; |
|||
final int channels = 1; |
|||
final boolean signed = true; |
|||
final boolean bigEndian = true; |
|||
// Monophonic 16-bit PCM audio format |
|||
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); |
|||
} |
|||
private void finish() { |
|||
line.stop(); |
|||
line.close(); |
|||
System.out.println("Finished capturing and recording audio"); |
|||
} |
|||
private TargetDataLine line; |
|||
private final File wavFile = new File("C:/Users/Public/SoundRecorder.wav"); |
|||
private final AudioFileFormat.Type audioFileType = AudioFileFormat.Type.WAVE; |
|||
} |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |