3 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
此Sun論壇帖子中包含一些有趣的代碼,用于生成單音。同樣,鑒于WAV文件格式不是太復(fù)雜,您可以創(chuàng)建一個(gè)代表所需波形的表,然后將其寫入文件。周圍有一些示例,例如原始音頻轉(zhuǎn)換器以及如何編寫wav文件。

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊
最簡(jiǎn)單的方法是使用Java的內(nèi)置MIDI庫:
int channel = 0; // 0 is a piano, 9 is percussion, other channels are for other instruments
int volume = 80; // between 0 et 127
int duration = 200; // in milliseconds
try {
Synthesizer synth = MidiSystem.getSynthesizer();
synth.open();
MidiChannel[] channels = synth.getChannels();
// --------------------------------------
// Play a few notes.
// The two arguments to the noteOn() method are:
// "MIDI note number" (pitch of the note),
// and "velocity" (i.e., volume, or intensity).
// Each of these arguments is between 0 and 127.
channels[channel].noteOn( 60, volume ); // C note
Thread.sleep( duration );
channels[channel].noteOff( 60 );
channels[channel].noteOn( 62, volume ); // D note
Thread.sleep( duration );
channels[channel].noteOff( 62 );
channels[channel].noteOn( 64, volume ); // E note
Thread.sleep( duration );
channels[channel].noteOff( 64 );
Thread.sleep( 500 );
// --------------------------------------
// Play a C major chord.
channels[channel].noteOn( 60, volume ); // C
channels[channel].noteOn( 64, volume ); // E
channels[channel].noteOn( 67, volume ); // G
Thread.sleep( 3000 );
channels[channel].allNotesOff();
Thread.sleep( 500 );
synth.close();
}
catch (Exception e) {
e.printStackTrace();
}
添加回答
舉報(bào)