qcode
  • 创作工场专业版,一问多答!更智能、更完整、更原创
  • 这是一个超强内容创作平台,它会写文章、做设计、绘图、敲代码,有问必答!
🔥点击使用-高级专业版🔥

用java写一个音频动态炫酷的代码

时间2023-03-29 10:08:45 提问来自 江苏省淮安市 的网友 分类创作 浏览530
import java.io.*; import javax.sound.sampled.*; public class AudioDynamicCool { // the line from which audio data is captured private TargetDataLine line; /** Constructor to set up a microphone for capture of audio */ public AudioDynamicCool(){ try { AudioFormat format = getAudioFormat(); // get the audio format DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // get and open the target data line for capture if (!AudioSystem.isLineSupported(info)) { // check if the system supports the data line System.out.println("The system does not support the specified format."); System.exit(0); } line = (TargetDataLine) AudioSystem.getLine(info); line.open(format); } catch (Exception ex) { // print an error message with details of what went wrong System.out.println("Error: " + ex); ex .printStackTrace(); System .exit(0); } } /** Captures the sound and record into a WAV file */ public void start(){ try{ AudioInputStream ais = new AudioInputStream (line ); File wavFile = new File ("RecordAudio" + ".wav"); AudioSystem .write (ais ,AudioFileFormat .Type .WAVE , wavFile ); while (true ){ int cnt = line .read (tempBuffer , 0 , tempBuffer .length ); if (cnt > 0){ DynamicProcessing (); } }}catch (Exception e ){ e .printStackTrace(); }} private void DynamicProcessing (){ float[] samples=new float[tempBuffer]; for(int i=0;i<samples ;i++){ samples[i]=(float)tempBuffer[i]/32768f; float rms=0f; for(int j=0;j<samples ;j++){ rms+=samples[j]*samples[j]; } rms=rms/samples ;//Calculate RMS value rms=(float)Math .sqrt ((double )rms );//Get Root Mean Square value if(rms>threshold){ do something cool here } else{ do something else here }} }}

点击这里,进行内容创作

创作工场 免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!

上一篇 深度剖析自我 下一篇 对于班会主题“感恩于心,诚信于行”生成一份600字的感恩诚信教育的活动总结