WhatsSidSupport.java

package sidplay.fingerprinting;

import builder.resid.resample.Resampler;
import libsidplay.common.SamplingMethod;
import libsidplay.common.SamplingRate;
import libsidutils.fingerprinting.IFingerprintMatcher;
import libsidutils.fingerprinting.rest.beans.MusicInfoWithConfidenceBean;
import libsidutils.fingerprinting.rest.beans.WAVBean;
import sidplay.audio.wav.WAVHeader;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;

import static sidplay.audio.wav.WAVHeader.HEADER_LENGTH;

/**
 *
 * WhatsSID? client part of tune recognition.
 * <p>
 * Use a WhatsSID capture buffer to match a currently played tune. Capture buffer contains always the last N seconds of
 * sound samples in a ring buffer.
 *
 * @author ken
 *
 */
public final class WhatsSidSupport {

    /**
     * Number of channels.
     */
    private static final int CHANNELS = 1;

    /**
     * Resampler to 8Khz.
     */
    private final Resampler downSampler;

    /**
     * Random source for triangular dithering
     */
    private final Random RANDOM = new Random();
    /**
     * State of HP-TPDF.
     */
    private int oldRandomValue;

    /**
     * Capture buffer.
     */
    private final ByteBuffer whatsSidBuffer;
    private final ByteBuffer wavBuffer;

    /**
     * Capacity of the capture buffer.
     */
    private final int whatsSidBufferSize;

    /**
     * Last match
     */
    private volatile MusicInfoWithConfidenceBean lastWhatsSidMatch;

    /**
     * Minimum confidence to detect a match
     */
    private final double minimumRelativeConfidence;

    public WhatsSidSupport(double cpuFrequency, int captureTimeInS, double minimumRelativeConfidence) {
        this.downSampler = Resampler.createResampler(cpuFrequency, SamplingMethod.RESAMPLE,
                SamplingRate.VERY_LOW.getFrequency(), SamplingRate.VERY_LOW.getHighestAccurateFrequency());
        this.whatsSidBufferSize = Short.BYTES * CHANNELS * SamplingRate.VERY_LOW.getFrequency() * captureTimeInS;
        this.whatsSidBuffer = ByteBuffer.allocateDirect(whatsSidBufferSize).order(ByteOrder.LITTLE_ENDIAN);
        this.wavBuffer = ByteBuffer.allocate(HEADER_LENGTH + whatsSidBufferSize).order(ByteOrder.LITTLE_ENDIAN);
        this.minimumRelativeConfidence = minimumRelativeConfidence;
    }

    public int getWavBufferSize() {
        return HEADER_LENGTH + whatsSidBufferSize;
    }

    /**
     * Output sample data to fill WhatsSid capture buffer
     *
     * @param valL left channel sample data
     * @param valR right channel sample data
     * @return capture buffer full
     */
    public boolean output(int valL, int valR) {
        int dither = triangularDithering();
        if (downSampler.input(valL + valR >> 1)) {
            if (!whatsSidBuffer
                    .putShort(
                            (short) Math.max(Math.min(downSampler.output() + dither, Short.MAX_VALUE), Short.MIN_VALUE))
                    .hasRemaining()) {
                whatsSidBuffer.clear();
                return true;
            }
        }
        return false;
    }

    /**
     * Match WhatsSid capture buffer using the given matcher
     *
     * @param matcher matcher used to match the capture buffer contents
     * @return matched music info or null (no match)
     * @throws IOException I/O error
     */
    public MusicInfoWithConfidenceBean match(IFingerprintMatcher matcher) throws IOException {
        byte[] wav = createWAV();
        if (wav.length > 0) {
            MusicInfoWithConfidenceBean result = matcher.match(new WAVBean(wav));
            if (result != null && !result.equals(lastWhatsSidMatch)
                    && result.getRelativeConfidence() > minimumRelativeConfidence) {
                lastWhatsSidMatch = result;
                return result;
            }
        }
        return null;
    }

    /**
     * Reset the capture buffer (call for a new tune to play)
     */
    public void reset() {
        whatsSidBuffer.clear();
        whatsSidBuffer.put(new byte[whatsSidBufferSize]).clear();
        lastWhatsSidMatch = null;
    }

    /**
     * Triangularly shaped noise source for audio applications. Output of this PRNG is between ]-1, 1[.
     *
     * @return triangular noise sample
     */
    private int triangularDithering() {
        int prevValue = oldRandomValue;
        oldRandomValue = RANDOM.nextInt(2);
        return oldRandomValue - prevValue;
    }

    /**
     * Create WAV sample data form ring-buffer.
     *
     * @return WAV bytes
     */
    public byte[] createWAV() {
        wavBuffer.clear();
        WAVHeader wavHeader = new WAVHeader(CHANNELS, SamplingRate.VERY_LOW.getFrequency());
        wavHeader.advance(whatsSidBufferSize);
        wavBuffer.put(wavHeader.getBytes());
        ByteBuffer copy = whatsSidBuffer.asReadOnlyBuffer();
        copy.mark();
        wavBuffer.put(copy);
        copy.reset();
        copy.flip();
        wavBuffer.put(copy);
        return wavBuffer.array();
    }

}