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

com.github.bloodshura.x.assets.sound.SoundSource Maven / Gradle / Ivy

/*
 * 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.AssetSource;
import com.github.bloodshura.x.assets.exception.AssetException;
import com.github.bloodshura.x.assets.sound.source.DataSoundSource;
import com.github.bloodshura.x.assets.sound.source.InputSoundSource;
import com.github.bloodshura.x.enumeration.Enumerations;
import com.github.bloodshura.x.io.Url;
import com.github.bloodshura.x.io.file.File;
import com.github.bloodshura.x.loader.ResourceLoader;
import com.github.bloodshura.x.resource.PathResource;
import com.github.bloodshura.x.resource.Resource;
import com.github.bloodshura.x.worker.StringWorker;

import javax.annotation.Nonnull;
import java.io.InputStream;

public abstract class SoundSource extends AssetSource {
	private static int UNNAMED_COUNT = 0;

	public SoundSource(@Nonnull String name, @Nonnull SoundFormat format) {
		super(name, format);
	}

	@Nonnull
	public static SoundSource of(@Nonnull File file) throws AssetException {
		SoundFormat format = Enumerations.search(SoundFormat.class, file.getFormat());

		if (format != null) {
			return of(file, format);
		}

		throw new AssetException("Could not define sound format for \"" + file.getPath() + "\"");
	}

	@Nonnull
	public static SoundSource of(@Nonnull File file, @Nonnull SoundFormat format) {
		return of(file.getPath(), format, file);
	}

	@Nonnull
	public static SoundSource of(@Nonnull ResourceLoader loader, @Nonnull String path) throws AssetException {
		String s = StringWorker.removeTrailingFileSeparator(path);
		int dot = s.lastIndexOf('.');

		if (dot == -1 || dot >= s.length() - 1) {
			throw new AssetException("Could not find format for \"" + s + "\"");
		}

		String formatStr = s.substring(dot + 1);
		int sep = formatStr.indexOf('?');

		if (sep != -1) {
			formatStr = formatStr.substring(0, sep);
		}

		SoundFormat format = Enumerations.search(SoundFormat.class, formatStr);

		if (format != null) {
			return of(loader, path, format);
		}

		throw new AssetException("Invalid sound format \"" + formatStr + "\"");
	}

	@Nonnull
	public static SoundSource of(@Nonnull ResourceLoader loader, @Nonnull String path, @Nonnull SoundFormat format) throws AssetException {
		return of(path, format, new PathResource(loader, path));
	}

	@Nonnull
	public static SoundSource of(@Nonnull SoundFormat format, @Nonnull byte[] input) {
		return of(unnamed(), format, input);
	}

	@Nonnull
	public static SoundSource of(@Nonnull SoundFormat format, @Nonnull InputStream input) {
		return of(unnamed(), format, input);
	}

	@Nonnull
	public static SoundSource of(@Nonnull SoundFormat format, @Nonnull Resource input) {
		return of(unnamed(), format, input);
	}

	@Nonnull
	public static SoundSource of(@Nonnull String name, @Nonnull SoundFormat format, @Nonnull byte[] input) {
		return new DataSoundSource(name, format, input);
	}

	@Nonnull
	public static SoundSource of(@Nonnull String name, @Nonnull SoundFormat format, @Nonnull InputStream input) {
		return of(name, format, () -> input);
	}

	@Nonnull
	public static SoundSource of(@Nonnull String name, @Nonnull SoundFormat format, @Nonnull Resource input) {
		return new InputSoundSource(name, format, input);
	}

	@Nonnull
	public static SoundSource of(@Nonnull Url url) throws AssetException {
		if (url.hasFormat()) {
			SoundFormat format = Enumerations.search(SoundFormat.class, url.getFormat());

			if (format != null) {
				return of(url, format);
			}
		}

		throw new AssetException("Could not define sound format for \"" + url.getPath() + "\"");
	}

	@Nonnull
	public static SoundSource of(@Nonnull Url url, @Nonnull SoundFormat format) {
		return of(url.getPath(), format, url);
	}

	@Nonnull
	protected static String unnamed() {
		return "XImage-" + UNNAMED_COUNT++;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy