com.github.bloodshura.x.assets.sound.Sound Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shurax-assets Show documentation
Show all versions of shurax-assets Show documentation
An API for reading, writing and manipulating fonts, images and sounds.
/*
* 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.sound;
import com.github.bloodshura.x.assets.Asset;
import com.github.bloodshura.x.assets.XAssets;
import com.github.bloodshura.x.assets.exception.CodecNotFoundException;
import com.github.bloodshura.x.assets.sound.codec.SoundCodec;
import com.github.bloodshura.x.assets.sound.exception.SoundException;
import com.github.bloodshura.x.memory.Bytes;
import com.github.bloodshura.x.object.storable.Storable;
import com.github.bloodshura.x.resource.Resource;
import com.github.bloodshura.x.runnable.XThread;
import com.github.bloodshura.x.worker.StreamWorker;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
public class Sound extends Storable implements Asset, Resource {
private final SoundCodec codec;
private final byte[] data;
private final SoundFormat format;
public Sound(@Nonnull SoundFormat format, @Nonnull byte[] data) throws CodecNotFoundException {
this.data = data;
this.format = format;
this.codec = XAssets.getManager().createCodec(this);
}
@Nonnull
@Override
public Sound clone() throws CodecNotFoundException {
return new Sound(getFormat(), getData().clone());
}
@Nonnull
public SoundCodec getCodec() {
return codec;
}
@Nonnull
public byte[] getData() {
return data;
}
@Nonnull
public Duration getDuration() {
return getCodec().getDuration();
}
@Nonnull
public SoundFormat getFormat() {
return format;
}
public double getGain() {
return getCodec().getGain();
}
public double getPitch() {
return getCodec().getPitch();
}
@Nonnull
public Duration getPosition() {
return getCodec().getPosition();
}
@Nonnull
public Bytes getSize() {
return new Bytes(getData().length);
}
public boolean isPlaying() {
return getCodec().isPlaying();
}
@Nonnull
@Override
public InputStream newInputStream() {
return StreamWorker.asInput(getData());
}
public void play() throws SoundException {
play(PlayMode.NORMAL);
}
public void play(@Nonnull PlayMode playMode) throws SoundException {
presetup();
getCodec().play(playMode);
}
public void presetup() throws SoundException {
if (!getCodec().isSetUp()) {
try {
getCodec().setup();
}
catch (IOException exception) {
throw new SoundException("Could not setup sound codec", exception);
}
}
}
public void restart() throws SoundException {
stop();
play();
}
public void setGain(double gain) {
getCodec().setGain(gain);
}
public void setPitch(double pitch) {
getCodec().setPitch(pitch);
}
public void setPosition(@Nonnull Duration duration) {
getCodec().setPosition(duration);
}
public void stop() throws SoundException {
getCodec().stop();
}
public synchronized void waitUntilPlayed() {
while (isPlaying()) {
XThread.stay(5L);
}
}
public static enum PlayMode {
LOOP,
NORMAL
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy