2009-12-19 3 views
7

나는 퐁 복제본을 만들었고 충돌이 발생할 때 몇 가지 사운드 효과를 추가하고 싶습니다. 내 문제는 내 전체 응용 프로그램이 단지 90 줄의 코드를 가지고 있으므로 사운드를 합성하는 모든 예제에서 약 30 줄의 코드가 필요하다는 것입니다. 나는 더 간단한 접근법을 찾고있다. 서로 다른 음색의 삐 소리를 만드는 간단한 방법이 있습니까? 기간은 중요하지 않습니다. 나는 단지 다른 음색으로 일련의 삐 소리를 내고 싶다.자바 생성 사운드

+2

코드 30 줄은 그리 많지 않습니다. 발견 한 예제를 사용하는 것이 잘못된 이유는 무엇입니까? – Joe

+1

네,하지만 전 복제본은 90 줄입니다. 코드의 1/3은 단순한 경고음을 만드는 데 사용됩니다. 나에게 다소 무의미한. 그러나 만일 내가 다른 어떤 길을 발견 할 수 없으면 나는 그것과 함께 갈 것이다. –

+4

코드의 1/4, 사실 이후. 그게 기분이 나아진다면 ... – Sev

답변

17

다음은 JSyn을 사용할 수 있습니다 Java Sound - Example: Code to generate audio tone

byte[] buf = new byte[ 1 ];; 
    AudioFormat af = new AudioFormat((float)44100, 8, 1, true, false); 
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af); 
    sdl.open(); 
    sdl.start(); 
    for(int i = 0; i < 1000 * (float)44100/1000; i++) { 
     double angle = i/((float)44100/440) * 2.0 * Math.PI; 
     buf[ 0 ] = (byte)(Math.sin(angle) * 100); 
     sdl.write(buf, 0, 1); 
    } 
    sdl.drain(); 
    sdl.stop(); 
+1

이 줄에서 128 대신 100을 곱하는 이유를 설명 할 수 있습니까? buf [0] = (byte) (Math.sin (angle) * 100); 나는 신호가 -127에서 127 사이에있을 것이라고 의심하기 때문에 매우 혼란 스럽습니다. 또한 링크가 작동하지 않습니다. 가능한 경우 업데이트하십시오. – Felix

+0

이것은 사운드의 진폭 (즉 볼륨)에 영향을줍니다. – while

+1

'i <1000 * (float) 44100/1000'의 요점은'i <(float) 44100'과 같지 않습니까? – dk14

1

java.awt.Toolkit.getDefaultToolkit(). 경고음 경고음()

시리즈?

int numbeeps = 10; 

for(int x=0;x<numbeeps;x++) 
{ 
    java.awt.Toolkit.getDefaultToolkit().beep(); 
} 
+1

"다른 색조"라고 그는 말했습니다. –

+1

플러스 .beep()이 모든 플랫폼에서 작동하지 않습니다. –

1

에서 작은 예를 들어 촬영 (짧아)입니다. 이 라이브러리는 설치해야하는 라이브러리입니다 (.DLL.JAR). 그러나 다른 톤을 만드는 것은 매우 간단합니다.

Link

이것은 일례이다 (또한 가능한 자습서)

public static void main(String[] args) throws Exception { 
    SawtoothOscillatorBL osc; 
    LineOut lineOut; 
    // Start JSyn synthesizer. 
    Synth.startEngine(0); 

    // Create some unit generators. 
    osc = new SawtoothOscillatorBL(); 
    lineOut = new LineOut(); 

    // Connect oscillator to both left and right channels of output. 
    osc.output.connect(0, lineOut.input, 0); 
    osc.output.connect(0, lineOut.input, 1); 

    // Start the unit generators so they make sound. 
    osc.start(); 
    lineOut.start(); 

    // Set the frequency of the oscillator to 200 Hz. 
    osc.frequency.set(200.0); 
    osc.amplitude.set(0.8); 

    // Sleep for awhile so we can hear the sound. 
    Synth.sleepForTicks(400); 

    // Change the frequency of the oscillator. 
    osc.frequency.set(300.0); 
    Synth.sleepForTicks(400); 

    // Stop units and delete them to reclaim their resources. 
    osc.stop(); 
    lineOut.stop(); 
    osc.delete(); 
    lineOut.delete(); 

    // Stop JSyn synthesizer. 
    Synth.stopEngine(); 
} 

마티

여기
+0

JSyn은 이제 순수 Java이며 더 이상 기본 DLL이 필요하지 않습니다. – philburk

0

16 비트

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 

public class MakeSound { 
    public static void main(String[] args) throws LineUnavailableException { 
    System.out.println("Make sound"); 
    byte[] buf = new byte[2]; 
    int frequency = 44100; //44100 sample points per 1 second 
    AudioFormat af = new AudioFormat((float) frequency, 16, 1, true, false); 
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af); 
    sdl.open(); 
    sdl.start(); 
    int durationMs = 5000; 
    int numberOfTimesFullSinFuncPerSec = 441; //number of times in 1sec sin function repeats 
    for (int i = 0; i < durationMs * (float) 44100/1000; i++) { //1000 ms in 1 second 
     float numberOfSamplesToRepresentFullSin= (float) frequency/numberOfTimesFullSinFuncPerSec; 
     double angle = i/(numberOfSamplesToRepresentFullSin/ 2.0) * Math.PI; // /divide with 2 since sin goes 0PI to 2PI 
     short a = (short) (Math.sin(angle) * 32767); //32767 - max value for sample to take (-32767 to 32767) 
     buf[0] = (byte) (a & 0xFF); //write 8bits ________WWWWWWWW out of 16 
     buf[1] = (byte) (a >> 8); //write 8bits WWWWWWWW________ out of 16 
     sdl.write(buf, 0, 2); 
    } 
    sdl.drain(); 
    sdl.stop(); 
    } 
} 
묘사의 비트와 상기와 동일한 코드이며
관련 문제