Record sound: Difference between revisions

(Add Nimrod)
Line 280:
 
=={{header|Scala}}==
[[Category:Scala Implementations]]{{libheader|Scala}}<lang Scala>import java.io.{File, IOException}
import javax.sound.sampled.{ AudioFileFormat, AudioFormat, AudioInputStream, AudioSystem }
{{libheader|Scala}}
import javax.sound.sampled.{AudioSystem, DataLine, LineUnavailableException, TargetDataLine }
<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 = 3000060000 // 1 minute
 
// path of the wav file
val wavFile = new File("RecordAudio.wav")
 
// format of audio file
val fileType = AudioFileFormat.Type.WAVE
 
// path and format of the wav file
val format = new AudioFormat( /*sampleRate =*/ 16000f,
val (wavFile, fileType) = (new File("RecordAudio.wav"), AudioFileFormat.Type.WAVE)
val format = new AudioFormat( /*sampleRate =*/ 16000f,
/*sampleSizeInBits =*/ 16,
/*channels =*/ 2,
Line 304 ⟶ 297:
 
val info = new DataLine.Info(classOf[TargetDataLine], format)
val line: TargetDataLine = AudioSystem.getLine(info).asInstanceOf[TargetDataLine]
 
/**/ Entry to run the program
*/
 
// createsCreates a new thread that waits for a specified of time before stopping
// of time before stopping
new Thread(new Runnable() {
def run() {
Line 318 ⟶ 309:
case ex: InterruptedException => ex.printStackTrace()
}
line.stop()finally {
line.closestop()
line.close()
*/ }
println("Finished")
}
}).start()
 
//Captures the sound and record into a WAV file
Line 328 ⟶ 321:
// 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")
val ais = AudioSystem.write(new AudioInputStream(line), fileType, wavFile)
 
AudioSystem.write(ais, fileType, wavFile)
} else println("Line not supported")
} catch {
case ex: LineUnavailableException => ex.printStackTrace()
case ioe: IOException => ioe.printStackTrace()
}
}</lang>
Anonymous user