Record sound: Difference between revisions
Content deleted Content added
Added zkl |
Scala added |
||
Line 264: | Line 264: | ||
</lang> |
</lang> |
||
=={{header| |
=={{header|Scala}}== |
||
[[Category:Scala Implementations]] |
|||
{{libheader| |
{{libheader|Scala}} |
||
<lang Scala>import java.io.{ File, IOException } |
|||
import javax.sound.sampled.{ AudioFileFormat, AudioFormat, AudioInputStream, AudioSystem } |
|||
⚫ | |||
import javax.sound.sampled.{ DataLine, LineUnavailableException, TargetDataLine } |
|||
object SoundRecorder extends App { |
|||
// record duration, in milliseconds |
|||
final val RECORD_TIME = 30000 // 1 minute |
|||
// path of the wav file |
|||
val wavFile = new File("RecordAudio.wav") |
|||
// format of audio file |
|||
val fileType = AudioFileFormat.Type.WAVE |
|||
val format = new AudioFormat( /*sampleRate =*/ 16000f, |
|||
/*sampleSizeInBits =*/ 16, |
|||
/*channels =*/ 2, |
|||
/*signed =*/ true, |
|||
/*bigEndian =*/ true) |
|||
val info = new DataLine.Info(classOf[TargetDataLine], format) |
|||
val line = AudioSystem.getLine(info).asInstanceOf[TargetDataLine] |
|||
/** Entry to run the program |
|||
*/ |
|||
// creates a new thread that waits for a specified |
|||
// of time before stopping |
|||
new Thread(new Runnable() { |
|||
def run() { |
|||
try { |
|||
Thread.sleep(RECORD_TIME) |
|||
} catch { |
|||
case ex: InterruptedException => ex.printStackTrace() |
|||
} |
|||
line.stop() |
|||
line.close() |
|||
println("Finished") |
|||
} |
|||
}).start |
|||
//Captures the sound and record into a WAV file |
|||
try { |
|||
// checks if system supports the data line |
|||
if (AudioSystem.isLineSupported(info)) { |
|||
line.open(format) |
|||
line.start() // start capturing |
|||
val ais = new AudioInputStream(line) |
|||
println("Recording started") |
|||
AudioSystem.write(ais, fileType, wavFile) |
|||
} else println("Line not supported") |
|||
} catch { |
|||
case ex: LineUnavailableException => ex.printStackTrace() |
|||
case ioe: IOException => ioe.printStackTrace() |
|||
} |
|||
}</lang> |
|||
=={{header|Tcl}}== |
|||
⚫ | |||
# Helper to do a responsive wait |
# Helper to do a responsive wait |