2012-05-16 6 views
3

나는 스칼라로 소리를 출력하려고한다. 내 문제는 내가 짧은 "잡음"/ "클릭"매초 얻을. 비슷한 자바 프로그램에이 문제가 없었습니다. 누군가 잘못된 생각이 있습니까?정기적 인 "클릭"with javax.sound.sampled

스칼라 2.9.2 자바 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() 

    // play 10s 
    while(System.currentTimeMillis() < (start + 10000)) { 
    var index = 0 

    // output blocks of 10000 samples 
    var samples = 0.until(10000).map {x => math.sin((x+index) * 800.0/44100 * math.Pi)} 

    // convert samples to Byte Array 
    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) 
    } 

    // cleanup  
    auline.drain 
    auline.close 
} 

답변

2

코드에서 오류가 var index = 0는 동안 루프가 시작되기 전에해야하고, 당신이 index += 10000을해야한다는 것입니다 루프의 끝 (내부). 이것을하면 괜찮아 보입니다.

+0

감사합니다. 아마 저녁에 코드를 작성해서는 안됩니다 ... – Fabian

관련 문제