<commit_before>// reference: http://hackvalue.de/hv_atmel_stackmat

package com.puzzletimer.timer;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.TimerTask;
import java.util.Map.Entry;

import javax.sound.sampled.TargetDataLine;

import com.puzzletimer.models.Timing;
import com.puzzletimer.state.TimerManager;

interface StackmatTimerReaderListener {
    void dataReceived(byte[] data);
}

class StackmatTimerReader implements Runnable {
    private double sampleRate;
    private double period;
    private TargetDataLine targetDataLine;
    private ArrayList<StackmatTimerReaderListener> listeners;
    private boolean running;

    StackmatTimerReader(TargetDataLine targetDataLine) {
        this.sampleRate = targetDataLine.getFormat().getFrameRate();
        this.period = this.sampleRate / 1200d;
        this.targetDataLine = targetDataLine;
        this.listeners = new ArrayList<StackmatTimerReaderListener>();
        this.running = false;
    }

    private byte[] readPacket(byte[] samples, int offset, byte bitThreshold, boolean isInverted) {
        byte[] data = new byte[9];
        for (int i = 0; i < 9; i++) {
            // start bit
            boolean startBit = samples[offset + (int) (10 * i * this.period)] <= bitThreshold;
            if ((isInverted && startBit) || (!isInverted && !startBit)) {
                return new byte[9]; // invalid data
            }

            // data bits
            data[i] = 0x00;
            for (int j = 0; j < 8; j++) {
                if (samples[offset + (int) ((10 * i + j + 1) * this.period)] > bitThreshold) {
                    data[i] |= 0x01 << j;
                }
            }

            if (isInverted) {
                data[i] = (byte) ~data[i];
            }

            // stop bit
            boolean stopBit = samples[offset + (int) ((10 * i + 9) * this.period)] <= bitThreshold;
            if ((isInverted && !stopBit) || (!isInverted && stopBit)) {
                return new byte[9]; // invalid data
            }
        }

        return data;
    }

    private boolean isValidPacket(byte[] data) {
        int sum = 0;
        for (int i = 1; i < 6; i++) {
            sum += data[i] - '0';
        }

        return " ACILRS".contains(String.valueOf((char) data[0])) &&
               Character.isDigit(data[1]) &&
               Character.isDigit(data[2]) &&
               Character.isDigit(data[3]) &&
               Character.isDigit(data[4]) &&
               Character.isDigit(data[5]) &&
               data[6] == sum + 64 &&
               data[7] == '\n' &&
               data[8] == '\r';
    }

    @Override
    public void run() {
        this.running = true;

        this.targetDataLine.start();

        byte[] buffer = new byte[(int) (this.sampleRate / 4)];
        int offset = buffer.length;

        while (this.running) {
            // update buffer in a queue fashion
            for (int i = offset; i < buffer.length; i++) {
                buffer[i - offset] = buffer[i];
            }<commit_msg>Shortened audio buffer in stackmat interpreter<commit_after>// reference: http://hackvalue.de/hv_atmel_stackmat

package com.puzzletimer.timer;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.TimerTask;
import java.util.Map.Entry;

import javax.sound.sampled.TargetDataLine;

import com.puzzletimer.models.Timing;
import com.puzzletimer.state.TimerManager;

interface StackmatTimerReaderListener {
    void dataReceived(byte[] data);
}

class StackmatTimerReader implements Runnable {
    private double sampleRate;
    private double period;
    private TargetDataLine targetDataLine;
    private ArrayList<StackmatTimerReaderListener> listeners;
    private boolean running;

    StackmatTimerReader(TargetDataLine targetDataLine) {
        this.sampleRate = targetDataLine.getFormat().getFrameRate();
        this.period = this.sampleRate / 1200d;
        this.targetDataLine = targetDataLine;
        this.listeners = new ArrayList<StackmatTimerReaderListener>();
        this.running = false;
    }

    private byte[] readPacket(byte[] samples, int offset, byte bitThreshold, boolean isInverted) {
        byte[] data = new byte[9];
        for (int i = 0; i < 9; i++) {
            // start bit
            boolean startBit = samples[offset + (int) (10 * i * this.period)] <= bitThreshold;
            if ((isInverted && startBit) || (!isInverted && !startBit)) {
                return new byte[9]; // invalid data
            }

            // data bits
            data[i] = 0x00;
            for (int j = 0; j < 8; j++) {
                if (samples[offset + (int) ((10 * i + j + 1) * this.period)] > bitThreshold) {
                    data[i] |= 0x01 << j;
                }
            }

            if (isInverted) {
                data[i] = (byte) ~data[i];
            }

            // stop bit
            boolean stopBit = samples[offset + (int) ((10 * i + 9) * this.period)] <= bitThreshold;
            if ((isInverted && !stopBit) || (!isInverted && stopBit)) {
                return new byte[9]; // invalid data
            }
        }

        return data;
    }

    private boolean isValidPacket(byte[] data) {
        int sum = 0;
        for (int i = 1; i < 6; i++) {
            sum += data[i] - '0';
        }

        return " ACILRS".contains(String.valueOf((char) data[0])) &&
               Character.isDigit(data[1]) &&
               Character.isDigit(data[2]) &&
               Character.isDigit(data[3]) &&
               Character.isDigit(data[4]) &&
               Character.isDigit(data[5]) &&
               data[6] == sum + 64 &&
               data[7] == '\n' &&
               data[8] == '\r';
    }

    @Override
    public void run() {
        this.running = true;

        this.targetDataLine.start();

        byte[] buffer = new byte[(int) (this.sampleRate / 8)];
        int offset = buffer.length;

        while (this.running) {
            // update buffer in a queue fashion
            for (int i = offset; i < buffer.length; i++) {
                buffer[i - offset] = buffer[i];
            }
{'fraction_non_alphanumeric': 0.12899043915086697, 'fraction_numerical': 0.013287959812023983, 'mean_word_length': 1.9195837275307475, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2263990', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1758, 'n_words': 439}