0
私は音データを含むバッファを生成し、次のクラスがあります。プログラムで生成された楽音コードが正しく発音されないのはなぜですか?
package musicbox.example;
import javax.sound.sampled.LineUnavailableException;
import musicbox.engine.SoundPlayer;
public class CChordTest {
private static final int SAMPLE_RATE = 1024 * 64;
private static final double PI2 = 2 * Math.PI;
/*
* Note frequencies in Hz.
*/
private static final double C4 = 261.626;
private static final double E4 = 329.628;
private static final double G4 = 391.995;
/**
* Returns buffer containing audio information representing the C chord
* played for the specified duration.
*
* @param duration The duration in milliseconds.
* @return Array of bytes representing the audio information.
*/
private static byte[] generateSoundBuffer(int duration) {
double durationInSeconds = duration/1000.0;
int samples = (int) durationInSeconds * SAMPLE_RATE;
byte[] out = new byte[samples];
for (int i = 0; i < samples; i++) {
double value = 0.0;
double t = (i * durationInSeconds)/samples;
value += Math.sin(t * C4 * PI2); // C note
value += Math.sin(t * E4 * PI2); // E note
value += Math.sin(t * G4 * PI2); // G note
out[i] = (byte) (value * Byte.MAX_VALUE);
}
return out;
}
public static void main(String... args) throws LineUnavailableException {
SoundPlayer player = new SoundPlayer(SAMPLE_RATE);
player.play(generateSoundBuffer(1000));
}
}
はおそらく、私はここにいくつかの物理学や数学を誤解していますが、それは、各正弦波のように思えるが、各ノート(C、の音を表現するべきであるがE、G)、そして3つの正弦波を合計すると、キーボードで3つの音符を同時に鳴らすときと同じような音が聞こえるはずです。しかし、私が聞いていることはそれに近いものではありません。
私が正弦波のうちの2つをコメントして3番目を残すと、その正弦波に対応する(正しい)音が聞こえます。
誰かが私が間違っていることを見つけることができますか?
私は信号を平均化する必要があると確信しています。 3で割ってみてください。 – Amit
ビンゴ!信号を平均化することはやりました。あなたが答えとしてそれを書いたら、私は正しい印を付けるでしょう。 – Deomachus