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}
import javax.sound.sampled.{AudioFileFormat, AudioFormat, AudioInputStream}
{{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 {
object SoundRecorder extends App {
// record duration, in milliseconds
// record duration, in milliseconds
final val RECORD_TIME = 30000 // 1 minute
final val RECORD_TIME = 60000 // 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,
/*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
// Entry to run the program
*/


// creates a new thread that waits for a specified
// 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()
}
}
line.stop()
finally {
line.close()
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

val ais = new AudioInputStream(line)

println("Recording started")
println("Recording started")
AudioSystem.write(new AudioInputStream(line), fileType, wavFile)

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 => ioe.printStackTrace()
case ioe: IOException => ioe.printStackTrace()
}
}
}</lang>
}</lang>