package com.ym.mec.biz.dal.config;

import be.tarsos.dsp.io.TarsosDSPAudioFormat;
import be.tarsos.dsp.io.TarsosDSPAudioInputStream;
import org.springframework.stereotype.Component;

import javax.sound.sampled.AudioFormat;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @Author Joburgess
 * @Date 2021/8/4 0004
 */
public class NioAudioInputStream implements TarsosDSPAudioInputStream {

    private RandomAccessFile randomAccessFile;
    private AudioFormat format;

    public NioAudioInputStream() {
    }

    public NioAudioInputStream(RandomAccessFile randomAccessFile, AudioFormat audioFormat) {
        this.randomAccessFile = randomAccessFile;
        this.format = audioFormat;
    }

    @Override
    public long skip(long bytesToSkip) throws IOException {
        return randomAccessFile.skipBytes((int) bytesToSkip);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return randomAccessFile.read(b, off, len);
    }

    @Override
    public void close() throws IOException {
        randomAccessFile.close();
    }

    @Override
    public TarsosDSPAudioFormat getFormat() {
        boolean isSigned = format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED;
        TarsosDSPAudioFormat tarsosDSPFormat = new TarsosDSPAudioFormat(format.getSampleRate(), format.getSampleSizeInBits(), format.getChannels(), isSigned, format.isBigEndian());
        return tarsosDSPFormat;
    }

    @Override
    public long getFrameLength() {
        return 0;
    }
}
