Android 的 TARSOS DSP:检索 SpectralPeaks

TARSOS DSP for Android: retrieving SpectralPeaks

我正在使用 Tarsos Dsp for android 从音频文件中检索频谱峰值。从版本 2 开始,TarsosDSP 不再使用 javax,这应该会使一切变得容易得多。

困扰我的是我的代码工作正常,但 android 正在努力处理音频文件:这似乎是用 avconv 制作的。

    PipedAudioStream localPipedAudioStream = new PipedAudioStream(fileName);
    TarsosDSPAudioInputStream localTarsosDSPAudioInputStream1 = localPipedAudioStream.getMonoStream(this.sampleRate);

如何创建解码器:

public PipeDecoder(){
        //Use sensible defaults depending on the platform
        if(System.getProperty("os.name").indexOf("indows") > 0 ){
            pipeEnvironment = "cmd.exe";
            pipeArgument = "/C";
        }else{
            pipeEnvironment = "/bin/bash";
            pipeArgument = "-c";
        }
        pipeCommand = "avconv -i \"%resource%\" -vn -ar %sample_rate% -ac %channels% -sample_fmt s16 -f s16le pipe:1";
        //pipeLogFile = new File("decoder_log.txt");
        pipeBuffer = 10000;
    }

是缺少 avconv 库,还是android谁不能处理这些命令?

这是错误输出:

4651/com.example.giuseppe.tsptest I/PipeDecoder﹕ Starting piped decoding process for /sdcard/sample.wav
4651/com.example.giuseppe.tsptest W/PipeDecoder﹕ IO exception while decoding audio via sub process.Error running exec(). Command: [/bin/bash, -c, avconv -i "/sdcard/sample.wav" -vn -ar 44100 -ac 1 -sample_fmt s16 -f s16le pipe:1] Working Directory: null Environment: [ANDROID_ROOT=/system, EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, LOOP_MOUNTPOINT=/mnt/obb, LD_PRELOAD=libsigchain.so, ANDROID_BOOTLOGO=1, EMULATED_STORAGE_TARGET=/storage/emulated, EXTERNAL_STORAGE=/storage/emulated/legacy, SYSTEMSERVERCLASSPATH=/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar, ANDROID_SOCKET_zygote=10, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ANDROID_DATA=/data, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, BOOTCLASSPATH=/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/apache-xml.jar, ANDROID_PROPERTY_WORKSPACE=9,0, ANDROID_STORAGE=/storage]
    02-20 17:53:54.020    4651-4651/com.example.giuseppe.tsptest W/System.err﹕ java.io.IOException: Error running exec(). Command: [/bin/bash, -c, avconv -i "/sdcard/sample.wav" -vn -ar 44100 -ac 1 -sample_fmt s16 -f s16le pipe:1] Working Directory: null Environment: [ANDROID_ROOT=/system, EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, LOOP_MOUNTPOINT=/mnt/obb, LD_PRELOAD=libsigchain.so, ANDROID_BOOTLOGO=1, EMULATED_STORAGE_TARGET=/storage/emulated, EXTERNAL_STORAGE=/storage/emulated/legacy, SYSTEMSERVERCLASSPATH=/system/framework/services.jar:/system/framework/ethernet-service.jar:/system/framework/wifi-service.jar, ANDROID_SOCKET_zygote=10, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ANDROID_DATA=/data, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, BOOTCLASSPATH=/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/apache-xml.jar, ANDROID_PROPERTY_WORKSPACE=9,0, ANDROID_STORAGE=/storage]
    [...]
        --------- beginning of crash
    02-20 17:53:54.033    4651-4651/com.example.giuseppe.tsptest E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.giuseppe.tsptest, PID: 4651
        java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference
                at be.tarsos.dsp.io.UniversalAudioInputStream.read(Unknown Source)
                at be.tarsos.dsp.AudioDispatcher.processFirstBuffer(Unknown Source)
                at be.tarsos.dsp.AudioDispatcher.runSourcedDispatcher(Unknown Source)
                at be.tarsos.dsp.AudioDispatcher.run(Unknown Source)   [...]

PipeDecoder 确实是特定于平台的。它仅适用于存在 avconv 可执行文件的机器(UNIX,Windows)。它通过调用这个可执行文件来工作,例如一个以 44.1kHz 采样的 MP3,它 returns 通过管道以请求的采样率解码 PCM 样本。 “avconv”可以替换为任何可以通过管道解码、重新采样和流式传输音频的可执行文件(例如 ffmpeg 和 SoX)。

由于 Android 本质上是 Unix,这原则上可以在 Android 上运行,但实际上(据我所知)很难在 Android 上安装 avconv。解决方案是寻找平台特定的音频解码器或提供此功能的 Java 库(例如 jlayer)。

一旦你有了 pcm 样本,这些样本就可以很容易地进入 TarsosDSP 音频管道。