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

com.github.bloodshura.x.assets.ext.sound.OggCodec Maven / Gradle / Ivy

The 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.ext.sound;

import com.github.bloodshura.x.assets.ext.sound.internal.OggClip;
import com.github.bloodshura.x.assets.sound.Sound;
import com.github.bloodshura.x.assets.sound.Sound.PlayMode;
import com.github.bloodshura.x.assets.sound.codec.SoundCodec;
import com.github.bloodshura.x.assets.sound.exception.SoundException;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

// TODO Fix getDuration() and getPitch()
public class OggCodec implements SoundCodec {
	private OggClip clip;
	private final Sound sound;

	public OggCodec(@Nonnull Sound sound) {
		this.sound = sound;
	}

	@Deprecated
	@Nonnull
	@Override
	public Duration getDuration() throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	@Override
	public double getGain() {
		return clip.gain;
	}

	@Deprecated
	@Override
	public double getPitch() throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	@Nonnull
	@Override
	public Duration getPosition() {
		return Duration.ofNanos(TimeUnit.MICROSECONDS.toNanos(clip.outputLine.getMicrosecondPosition()));
	}

	@Override
	public boolean isPlaying() {
		return !clip.stopped();
	}

	@Override
	public boolean isSetUp() {
		return clip != null;
	}

	@Override
	public void play(@Nonnull PlayMode mode) {
		if (!isPlaying()) {
			if (mode == PlayMode.LOOP) {
				clip.loop();
			}
			else {
				clip.play();
			}
		}
	}

	@Override
	public void setGain(double gain) {
		clip.setGain((float) gain);
	}

	@Deprecated
	@Override
	public void setPitch(double pitch) throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	@Deprecated
	@Override
	public void setPosition(@Nonnull Duration duration) throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}

	@Override
	public void setup() throws SoundException {
		try {
			this.clip = new OggClip(sound.newInputStream());
		}
		catch (IOException exception) {
			throw new SoundException(exception.getMessage());
		}
	}

	@Override
	public void stop() {
		if (isPlaying()) {
			clip.stop();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy