2

I want to compare two audio file, so for this I'm using library musicg. but i'm getting an error "WaveHeader: only supports bitsPerSample 8 or 16"

Code :

String root = getExternalFilesDir(null).toString();
Log.i(TAG, root);

String bell1 = "bell1.wav";
String bell2 = "DOORBELL-3_SPLITTED.wav";
String fullPath1 = root + File.separator + bell1;               
String fullPath2 = root + File.separator + bell2;               

Log.i(TAG, "Full Path 1: " + fullPath1);
Log.i(TAG, "Full Path 2: " + fullPath2);

File f = new File(fullPath2);
Log.i(TAG, "" + f.canRead());

Wave b1 = new Wave(fullPath1);
Wave b2 = new Wave(fullPath2);

and i'm getting following error.

StackTrace :

11-25 00:17:08.597: I/MainActivity(26472): /storage/sdcard0/Android/data/com.musicg.demo.android/files
11-25 00:17:08.597: I/MainActivity(26472): Full Path 1: /storage/sdcard0/Android/data/com.musicg.demo.android/files/bell1.wav
11-25 00:17:08.597: I/MainActivity(26472): Full Path 2: /storage/sdcard0/Android/data/com.musicg.demo.android/files/DOORBELL-3_SPLITTED.wav
11-25 00:17:08.597: I/MainActivity(26472): true
11-25 00:17:08.602: W/System.err(26472): WaveHeader: only supports bitsPerSample 8 or 16
11-25 00:17:08.602: W/System.err(26472): Invalid Wave Header
11-25 00:17:08.602: I/MainActivity(26472): Length : 0.25
11-25 00:17:08.602: I/MainActivity(26472): Size : 48001
11-25 00:17:08.602: I/MainActivity(26472): Length : NaN
11-25 00:17:08.602: D/AndroidRuntime(26472): Shutting down VM
11-25 00:17:08.602: W/dalvikvm(26472): threadid=1: thread exiting with uncaught exception (group=0x41e542a0)
11-25 00:17:08.612: E/AndroidRuntime(26472): FATAL EXCEPTION: main
11-25 00:17:08.612: E/AndroidRuntime(26472): java.lang.NullPointerException
11-25 00:17:08.612: E/AndroidRuntime(26472):    at com.musicg.wave.Wave.size(Wave.java:255)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at com.musicg.demo.android.MainActivity$ClickEvent.onClick(MainActivity.java:139)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at android.view.View.performClick(View.java:4261)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at android.view.View$PerformClick.run(View.java:17356)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at android.os.Handler.handleCallback(Handler.java:615)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at android.os.Looper.loop(Looper.java:137)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at android.app.ActivityThread.main(ActivityThread.java:4921)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-25 00:17:08.612: E/AndroidRuntime(26472):    at dalvik.system.NativeStart.main(Native Method)
Arsalan
  • 513
  • 1
  • 7
  • 22
  • What is there on line Wave.java:255 and MainActivity.java:139? – Joel Fernandes Nov 24 '13 at 19:46
  • I haven't looked at musicg source so I cannot be sure. But what I think is happening, is that musicg only supports bits per Sample size of 8 or 16. So when it reads the wave file, it parses the header and throws an error if the bitsPerSample is not 8 or 16. Might be worth trying another audio file which has one of these sample sizes. There's other libraries out there that may support different bitsPerSample or atleast parse the file and allow you to read this info to make sure this speculation is true (like for example libsndfile). – Sun Nov 24 '13 at 19:55
  • @Sun can you suggest a good library for offline audio sound detection in java. – Arsalan Nov 25 '13 at 15:15
  • @Arsalan might be worth reading this post http://stackoverflow.com/questions/3297749/java-reading-manipulating-and-writing-wav-files . One of the answers linked to this library: http://www.labbookpages.co.uk/audio/javaWavFiles.html#reading . I havn't really used any java libraries for this sort of thing so I cannot comment on how good they are, but the WavFile file class seems to support 24,26,8 bit resolutions. – Sun Nov 26 '13 at 12:07
  • @Arsalan have you fixed your problem? I'm having the same as yours. – fsi Mar 20 '14 at 16:33
  • @fsi Nope, I failed to fix the problem...! – Arsalan Mar 21 '14 at 09:14
  • From @Sun that give the link `javaWavFiles`, I tested and the converted file isn't a wav file! even though it says 16bits per sample or 2 bytes. – fsi Mar 21 '14 at 12:20
  • I'm here with the same issue in 2019 :( – Asif Sb Mar 21 '19 at 10:57

1 Answers1

0

The code created by the authors of the musicg library requires from you what the message in the thrown exception informs you about:
"WaveHeader: only supports bitsPerSample 8 or 16"

If you do not want to change the implementation of the functionality in the ready library, it is enough to convert the WAV files you are comparing to the WAV version, which will have a sampling of 8 or 16 bits.
You can use it this online converter: https://www.3cx.com/docs/converting-wav-file/

The second approach relies on modifying library code - you should add additional function which will be checking current sampling of wav file which you are adding. If the value is 8 or 16 bits, you start the process, if not, you call a new method that replaces the value. You can use the following package for this:

https://github.com/JorenSix/TarsosDSP/blob/master/src/core/be/tarsos/dsp/writer/WaveHeader.java

Specially look at WaveHeader class.

This class represents the header of a WAVE format audio file, which usually have a .wav suffix. The following integer valued fields are contained:
format - usually PCM, ALAW or ULAW.
numChannels - 1 for mono, 2 for stereo.
sampleRate - usually 8000, 11025, 16000, 22050, or 44100 hz.
bitsPerSample - usually 16 for PCM, 8 for ALAW, or 8 for ULAW.
numBytes - size of audio data after this header, in bytes.

Oskarro
  • 323
  • 2
  • 9