使用JAVA从wav文件中提取振幅数组

时间:2016-09-04 04:45:54

标签: java wav

我正在尝试从音频文件(WAV文件)中提取振幅数组。我将使用此振幅数组来绘制给定wav文件的幅度与时间关系图。我能够自己绘制图形,但不知道如何从java中的给定音频(wav)文件中提取幅度?

2 个答案:

答案 0 :(得分:5)

这是一个可以使用的辅助类。 <?php ?> <!doctype html> <html> <head> <title>Basic Contact Form - investigating email send failure.</title> <script type='text/javascript' charset='utf-8'></script> <style type='text/css' charset='utf-8'> form{ width:50%; } form,output,input[type='text'],textarea,input[type='button'],input[type='submit']{ display:block; box-sizing:border-box; padding:0.5rem; margin:0.5rem; } output{ color:red; } input[type='text'],textarea{ width:60%; } </style> </head> <body> <form action="" method="post"> <div class="col-md-6"> <input name="name" type="text" class="form-control" placeholder="Name"> </div> <div class="col-md-6"><!-- // incorrect quotation used on `text` //--> <input name="email" type='text' class="form-control" placeholder="Email"> </div> <div class="col-md-12"> <input name="subject" type="text" class="form-control" placeholder="Subject"> </div> <div class="col-md-12"> <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea> </div> <div class="col-md-8"> <input type="submit" class="form-control text-uppercase" name="send" value="submit" /> </div> <output id='msgs'></output> </form> <?php if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['subject'],$_POST['email'],$_POST['name'],$_POST['message'] ) ){ $to = 'info@autonomousdata.com'; $name = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING ); $subject = filter_input( INPUT_POST, 'subject', FILTER_SANITIZE_STRING ); $message = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING ); $email = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_STRING ), FILTER_VALIDATE_EMAIL ); if( !$to or !$name or !$subject or !$message or !$email ){ echo "Error: one or more required variables are not available."; } else { $headers=array(); $headers[]="From: {$email}"; $headers[]="Reply-To: {$email}"; $headers[]="Content-type: text/html; charset=iso-8859-1"; $headers[]="X-Mailer: PHP/" . phpversion(); /* generate final headers string */ $headers=implode( "\r\n", $headers ); $message=array(); $message[]="Name: {$name}"; $message[]="Email: {$email}"; $message[]="Comment: {$message}"; /* generate final message string */ $message=implode( "\n", $message ); $result=@mail( $to, $subject, $message, $headers ); switch( $result ){ case true: $msg='Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.'; break; case false: $msg='Failed to send the mail'; break; } /* Let the user know the status of the form submission somehow */ echo " <script type='text/javascript'> document.getElementById('msgs').innerHTML='{$msg}'; /* alert('{$msg}'); */ </script>"; } } ?> </body> </html> 方法是获得幅度所需的方法:

getSampleInt()

它还可以播放文件,以便您可以测试它,但只能测试8位或16位文件。对于其他情况,您只能阅读它们。

另外,请查看these diagrams以查看WAV文件的组成,并更好地了解此课程的用途。

File file = ...;
WavFile wav = new WavFile(file);

int amplitudeExample = wav.getSampleInt(140); // 140th amplitude value.

for (int i = 0; i < wav.getFramesCount(); i++) {
    int amplitude = wav.getSampleInt(i);
    // Plot.
}

答案 1 :(得分:0)

我尝试了您的代码,并通过一些小的更改创建了一个结果。代码输出的数据有什么问题?

我改变了以下几行:

// create file input stream
      DataInputStream fis = new DataInputStream(new FileInputStream(wavFile));
      // create byte array from file
      arrFile = new byte[(int) wavFile.length()];
      fis.readFully(arrFile); // make sure you always read the full file, you did not check its return value, so you might be missing some data

我改变的第二件事是:

System.out.println(Arrays.toString(s.extractAmplitudeFromFile(f)));

在您的Main方法中,因为您只打印出了arary的地址。在这些更改之后,代码放出了一个具有值的数组,它似乎与所需的数据相关。

你错过了什么,或者你对数据的期望是什么?你能再详细澄清一下这个问题吗?