All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bloodshura.x.assets.device.Microphone Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/*
 * Copyright (c) 2013-2018, João Vitor Verona Biazibetti - All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 *
 * https://www.github.com/BloodShura
 */

package com.github.bloodshura.x.assets.device;

import com.github.bloodshura.x.assets.XAssets;
import com.github.bloodshura.x.assets.exception.CodecNotFoundException;
import com.github.bloodshura.x.assets.sound.Sound;
import com.github.bloodshura.x.assets.sound.SoundFormat;
import com.github.bloodshura.x.runnable.XThread;

import javax.annotation.Nonnull;
import javax.sound.sampled.AudioFormat;

public class Microphone {
	public static final AudioFormat DEFAULT_AUDIO_FORMAT = new AudioFormat(8000.0F, 16, 1, true, false);
	private CaptureThread captureThread;

	public void capture() throws CaptureException {
		capture(SoundFormat.WAV);
	}

	public void capture(@Nonnull AudioFormat audioFormat) throws CaptureException {
		capture(audioFormat, SoundFormat.WAV);
	}

	public void capture(@Nonnull AudioFormat audioFormat, @Nonnull SoundFormat soundFormat) throws CaptureException {
		if (isCapturing()) {
			throw new CaptureException("Already capturing");
		}

		this.captureThread = new CaptureThread(audioFormat, soundFormat);

		captureThread.start();
	}

	public void capture(@Nonnull SoundFormat soundFormat) throws CaptureException {
		capture(DEFAULT_AUDIO_FORMAT, soundFormat);
	}

	@Nonnull
	public CaptureResult finish() throws CaptureException {
		if (!isCapturing()) {
			throw new CaptureException("Not capturing");
		}

		captureThread.close();
		captureThread.join();

		Object result = captureThread.result;

		try {
			if (result != null) {
				if (result instanceof byte[]) {
					Sound sound = null;

					if (XAssets.getManager().hasCodec(captureThread.soundFormat)) {
						try {
							sound = new Sound(captureThread.soundFormat, (byte[]) result);
						}
						catch (CodecNotFoundException exception) {
							throw new CaptureException("No codec available for " + captureThread.soundFormat);
						}
					}

					return new CaptureResult(sound, captureThread.audioFormat, captureThread.soundFormat);
				}

				if (result instanceof Exception) {
					throw new CaptureException((Exception) result);
				}
			}

			throw new CaptureException("Capture thread returned invalid result: " + result);
		}
		finally {
			this.captureThread = null;
		}
	}

	public boolean isCapturing() {
		return captureThread != null;
	}

	@Nonnull
	public CaptureResult waitFinish(long duration) throws CaptureException {
		XThread.stay(duration);

		return finish();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy