Last active
June 10, 2016 11:13
-
-
Save joshuajnoble/4e93ae0f53425c0803e8b7a6af4fee72 to your computer and use it in GitHub Desktop.
This file contains 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
public class JavaSoundRecorder { | |
long length; | |
// format of audio file | |
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE; | |
// the line from which audio data is captured | |
TargetDataLine line; | |
/** | |
* Defines an audio format | |
*/ | |
AudioFormat getAudioFormat() { | |
float sampleRate = 8000; | |
int sampleSizeInBits = 8; | |
int channels = 2; | |
boolean signed = true; | |
boolean bigEndian = true; | |
AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); | |
return format; | |
} | |
/** | |
* Captures the sound and record into a WAV file | |
*/ | |
void start( String path) { | |
String finalPath = sketchPath() + "/" + path; | |
File wavFile = new File(finalPath); | |
System.out.println(finalPath); | |
try { | |
AudioFormat format = getAudioFormat(); | |
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); | |
// checks if system supports the data line | |
if (!AudioSystem.isLineSupported(info)) { | |
System.out.println("Line not supported"); | |
System.exit(0); | |
} | |
line = (TargetDataLine) AudioSystem.getLine(info); | |
line.open(format); | |
line.start(); // start capturing | |
println("Start capturing..."); | |
AudioInputStream ais = new AudioInputStream(line); | |
length = ais.getFrameLength() * ais.getFormat().getFrameSize(); | |
println("Start recording..."); | |
// start recording | |
AudioSystem.write(ais, fileType, wavFile); | |
} catch (LineUnavailableException ex) { | |
ex.printStackTrace(); | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
} | |
/** | |
* Closes the target data line to finish capturing and recording | |
*/ | |
void finish() { | |
println("Finished"); | |
line.stop(); | |
line.close(); | |
} | |
} |
This file contains 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.net.URLDecoder; | |
import java.net.URLEncoder; | |
import http.requests.*; | |
import java.util.concurrent.atomic.AtomicInteger; | |
int maxRecord = 0; | |
final JavaSoundRecorder recorder = new JavaSoundRecorder(); | |
Thread thread; | |
boolean recording = false; | |
void setup() | |
{ | |
} | |
void draw() | |
{ | |
} | |
void mousePressed() | |
{ | |
println(" started "); | |
recording = true; | |
thread = new Thread(new Runnable() { | |
public void run() { | |
try { | |
Thread.sleep(5000); // this is how long you'll record for | |
recorder.finish(); | |
thread.join(); | |
} | |
catch (InterruptedException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
); | |
thread.start(); | |
// start recording | |
recorder.start("test.wav"); | |
println(" OK " ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment