2015-03-31 53 views
0

我正在使用musicg库来比较两个音频声音。其中一个被保存,另一个被记录检测到。 musicg库有whistle演示;我编辑它是工作,因为我想要的。问题是当我想要检测到的声音[这是一个缓冲]转换为wav,并得到它的fingurprint,它返回nullpointerexption。比较Android中的两个声音

这是MainAcivity.java

公共静态波W1;

InputStream sw1 = getBaseContext()。getResources()。openRawResource(R.raw.top_of_the_world_rec);

w1 = new Wave(sw1);

并且这在DetectorThread.java

   InputStream obj = new ByteArrayInputStream(buffer); 
       Wave woo = new Wave(obj); 
       if (woo.toString() == null) 
        Log.i("", "recoded sound is null"); 
       if (MainActivity.w1.toString() == null) 
        Log.i("", "the detected sound is null"); 
       try { 
        similarity = woo.getFingerprintSimilarity(MainActivity.w1);/// this couse null pointer exption 
        Log.i("", "1-" + similarity); // not appear 
        if (similarity == null) { // not enter this if at all ~! 
         Log.i("", "lolol" + similarity); 
         TheSemilarityISnull = true; 
        } 
       } 
       catch (NullPointerException n) { 
        Log.i("", "first error"); 
        TheSemilarityISnull = true; 
       } 
       // EnD of addition 

       Log.i("", "2-" + similarity); //appear 2-null 
       // byte[] io = MainActivity.w1.getFingerprint(); // good no problem 
       // Log.i("", "00-"+io); // apear bk09823 like this 
       try { 

        Log.i("", "0-"); 
        byte[] po = woo.getFingerprint(); // ~> this couse null pointer exption 
        Log.i("", "0-" + po); 
        if (po == null) 
         Log.i("", "fingurprint for detected sound is null ");//not appear 
        byte[] io = MainActivity.w1.getFingerprint(); 
        Log.i("", "00-" + io); 
        if (io == null) 
         Log.i("", "fingurprint for recorded sound is null");//not appear 
        double scoure = new FingerprintSimilarityComputer(po, io).getFingerprintsSimilarity().getSimilarity(); 

       } catch (Exception e) { 
        Log.i("", "second error "); 
       } 

左右;

1-how to byte [] to wav?

2,我可以调用class或Mothed内部线程吗? Becouse我不知道,每次我打电话给他们其中一个都没有发现任何东西!

3-我根本不编辑记录线程>是否会引起问题? 请帮助我。

+0

对于你的第一个问题。 [见这里](http://stackoverflow.com/questions/731120/convert-byte-array-to-wav-java)。 – yiabiten 2015-03-31 08:46:05

+0

我没有得到我的答案...问题是当我想要转换缓冲区 - 类中的变量检测到线程 - 浪潮它couse nullpointerexption! – user3495695 2015-03-31 17:14:48

回答

0

读缓冲区不包含任何波头。这些只是音频字节。 我已经开始实施可能有效的课程。但它还没有最终确定

public class WavApi extends DetectionApi { 
    Wave waveToMatch; 

    public WavApi(Wave wave) { 
    super(wave.getWaveHeader()); 
    this.waveToMatch = wave; 
    } 

    public boolean matches(byte[] audioBytes) { 
    Wave wave = new Wave(waveToMatch.getWaveHeader(), audioBytes); 

    FingerprintSimilarityComputer fpc = new FingerprintSimilarityComputer(waveToMatch.getFingerprint(), wave.getFingerprint()); 
    FingerprintSimilarity similarity = fpc.getFingerprintsSimilarity(); 

    //--- TODO: find optimal condition to decide whether {audioBytes} matches {waveToMatch} or not 
    float sim = similarity.getScore() * 100f; 
    int pos = similarity.getMostSimilarFramePosition(); 

    if (pos >= 0 && sim > 0) 
     return true; 
    //---- 

    return false; 
    } 
}