用java写一个音频动态炫酷的代码
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 }} }}
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!