I am trying to output some sound using Scala. My problem is that I get a short “noise” / “click” every second. I did not have this problem with a similar Java program. Does anyone know what is wrong?
Scala 2.9.2 java 1.6.0_31 OS X 10.7.3
import javax.sound.sampled._
object SinSoundMain extends App {
val SAMPLE_RATE = 44100
val SAMPLE_SIZE = 16
val CHANNELS = 1
val SIGNED = true
val BIG_ENDIAN = true
var format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, CHANNELS, SIGNED, BIG_ENDIAN)
var info = new DataLine.Info(classOf[SourceDataLine], format);
val auline = (AudioSystem.getLine(info)).asInstanceOf[SourceDataLine]
auline.open(format)
auline.start
val start = System.currentTimeMillis()
while(System.currentTimeMillis() < (start + 10000)) {
var index = 0
var samples = 0.until(10000).map {x => math.sin((x+index) * 800.0 / 44100 * math.Pi)}
var byteSamples:Array[Byte] = samples.flatMap{ s =>
val ss = (s * Short.MaxValue).toShort
List((ss >> 8).toByte, (ss & 0xFF).toByte)
}.toArray
auline.write(byteSamples, 0, byteSamples.length)
}
auline.drain
auline.close
}
source
share