回答
查看Beeper
是一個獨立的例子。
也許更簡單的東西?
這51行代碼段(下面重複 - 單行&在行註釋隔開)作爲鏈接的答案頂部顯示,大約是生成音調一樣簡單得(OK,你可以爲諧波取出5行以上)。
人們似乎認爲它應該是內置於工具包中以產生純音的方法。它不是,並且需要一點點計算來製作一個。
/** Generates a tone, and assigns it to the Clip. */
public void generateTone()
throws LineUnavailableException {
if (clip!=null) {
clip.stop();
clip.close();
} else {
clip = AudioSystem.getClip();
}
boolean addHarmonic = harmonic.isSelected();
int intSR = ((Integer)sampleRate.getSelectedItem()).intValue();
int intFPW = framesPerWavelength.getValue();
float sampleRate = (float)intSR;
// oddly, the sound does not loop well for less than
// around 5 or so, wavelengths
int wavelengths = 20;
byte[] buf = new byte[2*intFPW*wavelengths];
AudioFormat af = new AudioFormat(
sampleRate,
8, // sample size in bits
2, // channels
true, // signed
false // bigendian
);
int maxVol = 127;
for(int i=0; i<intFPW*wavelengths; i++){
double angle = ((float)(i*2)/((float)intFPW))*(Math.PI);
buf[i*2]=getByteValue(angle);
if(addHarmonic) {
buf[(i*2)+1]=getByteValue(2*angle);
} else {
buf[(i*2)+1] = buf[i*2];
}
}
try {
byte[] b = buf;
AudioInputStream ais = new AudioInputStream(
new ByteArrayInputStream(b),
af,
buf.length/2);
clip.open(ais);
} catch(Exception e) {
e.printStackTrace();
}
}
使用Java Sound API和Math.sin
創建實際的波動水平。
http://www.developer.com/java/other/article.php/2226701有一個很好的教程,圍繞這個,我前面引用過。 http://jsresources.org/examples/是另一個有用的參考。
如果你想一些簡單的代碼,讓你開始,這應有助於
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class SinSynth {
//
protected static final int SAMPLE_RATE = 16 * 1024;
public static byte[] createSinWaveBuffer(double freq, int ms) {
int samples = (int)((ms * SAMPLE_RATE)/1000);
byte[] output = new byte[samples];
//
double period = (double)SAMPLE_RATE/freq;
for (int i = 0; i < output.length; i++) {
double angle = 2.0 * Math.PI * i/period;
output[i] = (byte)(Math.sin(angle) * 127f); }
return output;
}
public static void main(String[] args) throws LineUnavailableException {
final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
SourceDataLine line = AudioSystem.getSourceDataLine(af);
line.open(af, SAMPLE_RATE);
line.start();
boolean forwardNotBack = true;
for(double freq = 400; freq <= 800;) {
byte [] toneBuffer = createSinWaveBuffer(freq, 50);
int count = line.write(toneBuffer, 0, toneBuffer.length);
if(forwardNotBack) {
freq += 20;
forwardNotBack = false; }
else {
freq -= 10;
forwardNotBack = true;
} }
line.drain();
line.close();
}
}
這很容易閱讀,但魔術數字127f從哪裏來? – nont 2017-08-29 19:22:38
@nont sin返回-1.0到1.0的值,乘以127會得到一個正弦波,滿量程爲一個字節-127到+127,(它被轉換爲一個字節) – 2017-11-02 19:40:40
我只是想指出的是,有產生正弦波一個非常高效的算法。
DSP絕招:正弦音頻發生器 http://www.dspguru.com/dsp/tricks/sine_tone_generator
在第一個我建議創建類注意,這回注的頻率的頻道,並將其轉換爲字節數組。
然後流很伊斯利
protected static final int SAMPLE_RATE = 8 * 1024;
public static void main(String[] args) throws LineUnavailableException {
final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
SourceDataLine line = AudioSystem.getSourceDataLine(af);
line.open(af, SAMPLE_RATE);
line.start();
// fist argument is duration of playing note
byte[] noteDo = Note.DO.getTone(1, SAMPLE_RATE);
byte[] noteRe = Note.RE.getTone(0.5, SAMPLE_RATE);
byte[] noteMi = Note.MI.getTone(1.5, SAMPLE_RATE);
line.write(noteDo, 0, noteDo.length);
line.write(noteRe, 0, noteRe.length);
line.write(noteMi, 0, noteMi.length);
line.drain();
line.close();
}
public enum Note {
DO(0.0f), DO_DIEZ(1.0f),
RE(2.0f), RE_DIEZ(3.0f),
MI(4.0f),
FA(5.0f), FA_DIEZ(6.0f),
SOL(7.0f),SOL_DIEZ(8.0f),
LYA(9.0f),LYA_DIEZ(10.0f),
SI(11.0f);
private final double mPhase;
Note(double phase) {
mPhase = phase;
}
public double getNoteFrequencies() {
double index = getmPhase()/ 12.0d;
return 440 * Math.pow(2, index);
}
public static Note getNote(double phase) throws Exception {
Note findNote = null;
for (Note note : Note.values()){
if (note.getmPhase() == phase){
findNote = note;
}
}
if (findNote == null)
throw new Exception("Note not found: Ilegal phase " + phase);
else
return findNote;
}
public byte[] getTone(double duration, int rate){
double frequencies = getNoteFrequencies();
int maxLength = (int)(duration * rate);
byte generatedTone[] = new byte[2 * maxLength];
double[] sample = new double[maxLength];
int idx = 0;
for (int x = 0; x < maxLength; x++){
sample[x] = sine(x, frequencies/rate);
}
for (final double dVal : sample) {
final short val = (short) ((dVal * 100f));
// in 16 bit wav PCM, first byte is the low order byte
generatedTone[idx++] = (byte) (val & 0x00ff);
generatedTone[idx++] = (byte) ((val & 0xff00) >>> 8);
}
return generatedTone;
}
private double sine(int x, double frequencies){
return Math.sin( 2*Math.PI * x * frequencies);
}
public double getmPhase() {
return mPhase;
}
}
- 1. Java錯誤生成聲音正弦波
- 2. 在Python中生成正弦波聲音
- 3. Java快速正弦波音
- 4. 在Java中生成正弦波時的背景噪音
- 5. 從波形文件中刪除正弦波聲音
- 6. n音訊正弦波
- 7. 採樣率改變正弦波的音調,聲音()函數,MATLAB
- 8. 我正在爲actionscript3尋找一個正弦波音頻發生器
- 9. python中正弦波發生器的不同頻率
- 10. java SourceDataLine正弦波點擊
- 11. Android音頻 - 串流正弦音發生器的奇怪行爲
- 12. Linux正弦發生器
- 13. 持續時間的生成的音頻正弦波的
- 14. iOS音頻單元 - 創建立體聲正弦波
- 15. 使用SDL2播放正弦聲波 - 噪音/劃痕問題
- 16. 錯誤與重複正弦波音
- 17. javascript生成方波聲音
- 18. 生成平滑的正弦波
- 19. OpenGL - jogl中的正弦和餘弦波(JAVA)
- 20. 當我用Java在Android上產生正弦波時噪音很大
- 21. 從陣列發送正弦波值到音頻輸出
- 22. 正弦波繪圖
- 23. 動畫正弦波
- 24. 使用ADC生成正弦波
- 25. 生成正弦波,方波,三角波,鋸齒波的音頻使用Android的AudioTrack類
- 26. 正弦波交替扭曲在Java
- 27. Android java:繪製正弦波形圖
- 28. 使用計時器在CodeVisionAVR中生成正弦波形
- 29. 卸下與巴特沃斯正弦噪聲濾波器
- 30. Android中的FSK調製和播放正弦波音
另請參閱['Note'](http://stackoverflow.com/a/2065693/230513)。 – trashgod 2011-12-26 01:50:51