i need to generate pure tone开发者_如何学Gos with different levels of decibels (dB) in java. is there any example of how to do this?
Tones with midi Synth
You can generate tones with the synth :
import javax.sound.midi.*;
public class MidiSynthesizerSample {
public static void main(String[] args) {
int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60};
try {
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
MidiChannel channel = synthesizer.getChannels()[0];
for (int note : notes) {
channel.noteOn(note, 50);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
break;
} finally {
channel.noteOff(note);
}
}
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
}
Where you can set up the volume of the channels, with
getSetChannelVolumeMessage(int, int)
http://www.springworldgames.com/rmleditor/javadoc/com/springworldgames/music/MidiUtils.html#getSetChannelVolumeMessage(int, int)
I don't know how to convert it to decibels, maybe it depends on your sound-card's volume level.
This http://jsresources.org/examples/OscillatorPlayer.html shows how to create pure sinusoidal (or pretty much any other) waveforms and play them. As for decibels, their relation to amplitude is db ~ 20 * log10 (amplitude)
, i.e. amplitude ~ 10 ^ (dB/20)
, so just calculate from that.
精彩评论