aiaiask.com©

开花即结果 提问即答案

用java写一个音频动态炫酷的代码
原始问题:

用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                       }}           }}
用java写一个音频动态炫酷的代码
https://www.aiaiask.com/ask/4324.html
AI / 模型DeepSeek
时间 / DATEMarch 29, 2023, 10:08 AM
语言zh-CN
IP / 区域江苏 淮安