<commit_before>package go.graphics.swing.sound;

import go.graphics.sound.ForgettingQueue;
import go.graphics.sound.ForgettingQueue.Sound;
import go.graphics.sound.ISoundDataRetriever;
import go.graphics.sound.SoundPlayer;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class SwingSoundPlayer implements SoundPlayer {
	private static final int BUFFER_SIZE = 4048 * 4;
	private static final int SOUND_THREADS = 3;

	ForgettingQueue<Integer> queue = new ForgettingQueue<Integer>();
	private ISoundDataRetriever soundDataRetriever;

	public SwingSoundPlayer() {
		ThreadGroup soundgroup = new ThreadGroup("soundplayer");
		for (int i = 0; i < SOUND_THREADS; i++) {
			new Thread(soundgroup, new SoundPlayerTask(), "soundplayer" + i).start();
		}
	}

	@Override
	public void playSound(int soundStart, float lvolume, float rvolume) {
		queue.offer(soundStart, lvolume, rvolume);
	}

	public byte[] transformData(short[] data) {
		try {
			byte[] buffer = new byte[data.length * 4];
			for (int i = 0; i < data.length; i++) {
				buffer[4 * i] = buffer[4 * i + 2] = (byte) data[i];
				buffer[4 * i + 1] = buffer[4 * i + 3] = (byte) (data[i] >> 8);
			}

			return buffer;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private class SoundPlayerTask implements Runnable {

		@Override
		public void run() {
			AudioFormat format = new AudioFormat(22050, 16, 2, true, false);

			Line.Info info = new Line.Info(SourceDataLine.class);

			try {
				SourceDataLine dataLine = (SourceDataLine) AudioSystem.getMixer(null).getLine(info);
				dataLine.open(format, BUFFER_SIZE);

				while (true) {
					try {
						// start sound playing
						dataLine.start();

						Sound<Integer> sound = queue.take();
						long start = System.currentTimeMillis();
						byte[] buffer = transformData(soundDataRetriever.getSoundData(sound.getStart()));
						System.out.println("loading sound took: " + (System.currentTimeMillis() - start) + " ms");

						dataLine.write(buffer, 0, buffer.length);

						// stop playing
						dataLine.drain();
						dataLine.stop();<commit_msg>Removed sound clicks on the end and increased number of parallel sounds on Android.<commit_after>package go.graphics.swing.sound;

import go.graphics.sound.ForgettingQueue;
import go.graphics.sound.ForgettingQueue.Sound;
import go.graphics.sound.ISoundDataRetriever;
import go.graphics.sound.SoundPlayer;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class SwingSoundPlayer implements SoundPlayer {
	private static final int BUFFER_SIZE = 4048 * 4;
	private static final int SOUND_THREADS = 3;

	ForgettingQueue<Integer> queue = new ForgettingQueue<Integer>();
	private ISoundDataRetriever soundDataRetriever;

	public SwingSoundPlayer() {
		ThreadGroup soundgroup = new ThreadGroup("soundplayer");
		for (int i = 0; i < SOUND_THREADS; i++) {
			new Thread(soundgroup, new SoundPlayerTask(), "soundplayer" + i).start();
		}
	}

	@Override
	public void playSound(int soundStart, float lvolume, float rvolume) {
		queue.offer(soundStart, lvolume, rvolume);
	}

	public byte[] transformData(short[] data) {
		try {
			byte[] buffer = new byte[data.length * 4];
			for (int i = 0; i < data.length; i++) {
				buffer[4 * i] = buffer[4 * i + 2] = (byte) data[i];
				buffer[4 * i + 1] = buffer[4 * i + 3] = (byte) (data[i] >> 8);
			}

			return buffer;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private class SoundPlayerTask implements Runnable {

		@Override
		public void run() {
			AudioFormat format = new AudioFormat(22050, 16, 2, true, false);

			Line.Info info = new Line.Info(SourceDataLine.class);

			try {
				SourceDataLine dataLine = (SourceDataLine) AudioSystem.getMixer(null).getLine(info);
				dataLine.open(format, BUFFER_SIZE);

				while (true) {
					try {
						// start sound playing
						dataLine.start();

						Sound<Integer> sound = queue.take();
						long start = System.currentTimeMillis();
						byte[] buffer = transformData(soundDataRetriever.getSoundData(sound.getData()));
						System.out.println("loading sound took: " + (System.currentTimeMillis() - start) + " ms");

						dataLine.write(buffer, 0, buffer.length);

						// stop playing
						dataLine.drain();
						dataLine.stop();
{'fraction_non_alphanumeric': 0.12313996118179858, 'fraction_numerical': 0.011214147077852059, 'mean_word_length': 4.014054054054054, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '1454610', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1442, 'n_words': 364}