gpt4 book ai didi

java - jAudio 特征提取器 : Null Exception

转载 作者:行者123 更新时间:2023-11-29 08:54:27 24 4
gpt4 key购买 nike

我的项目是创建一个可以对音频文件执行特征提取和分类的 Android 应用程序。因此,首先,我将创建一个 Java 应用程序作为测试运行。

我正在尝试使用 jAudio 的特征提取器包从音频文件中提取音频特征。

首先,我想输入一个 .wav 文件并对该文件运行特征提取操作,然后将结果存储为一个 .ARFF 文件。

但是,我从项目中的包中收到以下 NullPointer 异常错误:

Exception in thread "main" java.lang.NullPointerException
at java.io.DataOutputStream.writeBytes(Unknown Source)
at jAudioFeatureExtractor.jAudioTools.FeatureProcessor.writeValuesARFFHeader(FeatureProcessor.java:853)
at jAudioFeatureExtractor.jAudioTools.FeatureProcessor.<init>(FeatureProcessor.java:258)
at jAudioFeatureExtractor.DataModel.extract(DataModel.java:308)
at Mfccarffwriter.main(Mfccarffwriter.java:70)

最初我认为这是一个文件权限问题(即由于缺少权限而不允许程序写入文件),但即使在向 Eclipse 4.2.2 授予各种权限之后(我正在运行Windows 7,64 位版本),我仍然遇到 NullException 错误。

下面给出了引发异常的包代码:

/**
* Write headers for an ARFF file. If saving for overall features, this must
* be postponed until the overall features have been calculated. If this a
* perWindow arff file, then all the feature headers can be extracted now
* and no hacks are needed.
* <p>
* <b>NOTE</b>: This procedure breaks if a feature to be saved has a
* variable number of dimensions
*
* @throws Exception
*/
private void writeValuesARFFHeader() throws Exception {
String sep = System.getProperty("line.separator");
String feature_value_header = "@relation jAudio" + sep;
values_writer.writeBytes(feature_value_header); // exception here
if (save_features_for_each_window && !save_overall_recording_features) {
for (int i = 0; i < feature_extractors.length; ++i) {
if (features_to_save[i]) {
String name = feature_extractors[i].getFeatureDefinition().name;
int dimension = feature_extractors[i]
.getFeatureDefinition().dimensions;
for (int j = 0; j < dimension; ++j) {
values_writer.writeBytes("@ATTRIBUTE \"" + name + j
+ "\" NUMERIC" + sep);
}
}
}
values_writer.writeBytes(sep);
values_writer.writeBytes("@DATA" + sep);
}
}

这是主要的应用程序代码:

 import java.io.*;
import java.util.Arrays;

import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil.ToStringAdapter;

import jAudioFeatureExtractor.Cancel;
import jAudioFeatureExtractor.DataModel;
import jAudioFeatureExtractor.Updater;
import jAudioFeatureExtractor.Aggregators.AggregatorContainer;
import jAudioFeatureExtractor.AudioFeatures.FeatureExtractor;
import jAudioFeatureExtractor.AudioFeatures.MFCC;
import jAudioFeatureExtractor.DataTypes.RecordingInfo;
import jAudioFeatureExtractor.jAudioTools.*;


public static void main(String[] args) throws Exception {

// Display information about the wav file
File extractedFiletoTest = new File("./microwave1.wav");

String randomID = Integer.toString((int) Math.random());

String file_path = "E:/Weka-3-6/tmp/microwave1.wav";
AudioSamples sampledExampleFile = new AudioSamples(extractedFiletoTest,randomID,false);

RecordingInfo[] samplefileInfo = new RecordingInfo[5];
samplefileInfo[1] = new RecordingInfo(randomID, file_path, sampledExampleFile, true);

double samplingrate= sampledExampleFile.getSamplingRateAsDouble();
int windowsize= 4096;
boolean normalize = false;

OutputStream valsavepath = new FileOutputStream(".\\values");
OutputStream defsavepath = new FileOutputStream(".\\definitions");

boolean[] featurestosaveamongall = new boolean[10];
Arrays.fill(featurestosaveamongall, Boolean.TRUE);

double windowoverlap = 0.0;

DataModel mfccDM = new DataModel("features.xml",null);

mfccDM.extract(windowsize, 0.5, samplingrate, true, true, false, samplefileInfo, 1); /// invokes the writeValuesARFFHeader function.

}
}

您可以下载整个项目(到目前为止完成)here .

最佳答案

这可能有点晚了,但我遇到了同样的问题,我追踪到 featureKey 和 featureValue 从未在 DataModel 中设置。这些没有固定方法,但它们是公共(public)领域。这是我的代码:

    package Sound;

import jAudioFeatureExtractor.ACE.DataTypes.Batch;
import jAudioFeatureExtractor.DataModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class Analysis {

private static String musicFile = "/home/chris/IdeaProjects/AnotherProj/res/SheMovesInHerOwnWay15s.wav";
private static String featureFile = "/home/chris/IdeaProjects/AnotherProj/res/features.xml";
private static String settingsFile = "/home/chris/IdeaProjects/AnotherProj/res/settings.xml";
private static String FKOuputFile = "/home/chris/IdeaProjects/AnotherProj/res/fk.xml";
private static String FVOuputFile = "/home/chris/IdeaProjects/AnotherProj/res/fv.xml";

public static void main(String[] args){
Batch batch = new Batch(featureFile, null);
try{
batch.setRecordings(new File[]{new File(musicFile)});
batch.getAggregator();
batch.setSettings(settingsFile);

DataModel dm = batch.getDataModel();
OutputStream valsavepath = new FileOutputStream(FVOuputFile);
OutputStream defsavepath = new FileOutputStream(FKOuputFile);
dm.featureKey = defsavepath;
dm.featureValue = valsavepath;
batch.setDataModel(dm);

batch.execute();
}
catch (Exception e){
e.printStackTrace();
}
}

}

我使用 GUI 创建了 settings.xml 文件,并从您保存 jar 的目录中复制了 features.xml 文件。

希望对你有帮助

关于java - jAudio 特征提取器 : Null Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com