见
Beeper一个自包含的例子。
也许更简单?
如链接答案顶部所示,这51行代码段(在下面重复-分隔为单行和行内注释)大约与
生成音调一样简单(好的,您可以取出5行以上用于谐波)。
人们似乎认为这应该是工具包中内置的一种产生
纯净音调的方法。并非如此,要花一点时间就可以做到。
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(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)