Record sound: Difference between revisions
Content deleted Content added
Add Nimrod |
|||
Line 280: | Line 280: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
[[Category:Scala Implementations]] |
[[Category:Scala Implementations]]{{libheader|Scala}}<lang Scala>import java.io.{File, IOException} |
||
⚫ | |||
{{libheader|Scala}} |
|||
⚫ | |||
<lang Scala>import java.io.{ File, IOException } |
|||
⚫ | |||
⚫ | |||
object SoundRecorder extends App { |
object SoundRecorder extends App { |
||
// record duration, in milliseconds |
// record duration, in milliseconds |
||
final val RECORD_TIME = |
final val RECORD_TIME = 60000 // 1 minute |
||
⚫ | |||
⚫ | |||
// format of audio file |
|||
val fileType = AudioFileFormat.Type.WAVE |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
/*sampleSizeInBits =*/ 16, |
/*sampleSizeInBits =*/ 16, |
||
/*channels =*/ 2, |
/*channels =*/ 2, |
||
Line 304: | Line 297: | ||
val info = new DataLine.Info(classOf[TargetDataLine], format) |
val info = new DataLine.Info(classOf[TargetDataLine], format) |
||
val line = AudioSystem.getLine(info).asInstanceOf[TargetDataLine] |
val line: TargetDataLine = AudioSystem.getLine(info).asInstanceOf[TargetDataLine] |
||
/ |
// Entry to run the program |
||
⚫ | |||
// |
// Creates a new thread that waits for a specified of time before stopping |
||
// of time before stopping |
|||
new Thread(new Runnable() { |
new Thread(new Runnable() { |
||
def run() { |
def run() { |
||
Line 318: | Line 309: | ||
case ex: InterruptedException => ex.printStackTrace() |
case ex: InterruptedException => ex.printStackTrace() |
||
} |
} |
||
finally { |
|||
line. |
line.stop() |
||
line.close() |
|||
⚫ | |||
println("Finished") |
println("Finished") |
||
} |
} |
||
}).start |
}).start() |
||
//Captures the sound and record into a WAV file |
//Captures the sound and record into a WAV file |
||
Line 328: | Line 321: | ||
// checks if system supports the data line |
// checks if system supports the data line |
||
if (AudioSystem.isLineSupported(info)) { |
if (AudioSystem.isLineSupported(info)) { |
||
line.open(format) |
line.open(format) |
||
line.start() // start capturing |
line.start() // start capturing |
||
⚫ | |||
println("Recording started") |
println("Recording started") |
||
⚫ | |||
AudioSystem.write(ais, fileType, wavFile) |
|||
} else println("Line not supported") |
} else println("Line not supported") |
||
} catch { |
} catch { |
||
case ex: LineUnavailableException => ex.printStackTrace() |
case ex: LineUnavailableException => ex.printStackTrace() |
||
case ioe: IOException |
case ioe: IOException => ioe.printStackTrace() |
||
} |
} |
||
}</lang> |
}</lang> |