⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 samplefactory.java

📁 Java version of ABC/HR comparator v0.5. by schnofler. Runs on Sun JRE 1.5 or later
💻 JAVA
字号:
package abchr.audio;

import abchr.gui.SampleConfig;

import javax.swing.filechooser.FileFilter;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;

import guiutils.OrFileFilter;

public class SampleFactory {
	private static class WaveDecoder implements SampleProcessor {
		public boolean accept(File f){String n=f.getName();return n.length()>=4 && n.substring(n.length()-4).equalsIgnoreCase(".wav");}
		public File process(File f) throws DecodeFailedException {return f;}
	}

	private static class SampleWrapper extends SampleDelegator implements Serializable {
		private SampleConfig config;

		public SampleWrapper(Sample baseSample,SampleConfig config) {
			super(baseSample);
			this.config=config;
		}

		private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
			in.defaultReadObject();
			try {
				setBaseSample(createSample(config));
			} catch(UnsupportedAudioFileException e) {
				throw new IOException(e.getMessage());
			}
		}

		public String toString() {
			return config.getFilename();
		}
	}

	private static List<SampleProcessor> decoders=new ArrayList<SampleProcessor>();

	private static class Decoder implements SampleProcessor {
		public boolean accept(File f) {
			return true;
		}

		public File process(File file) throws DecodeFailedException {
			File wave=null;
			DecodeFailedException dfe=null;
			for(int i=0;i<decoders.size();i++) {
				if(((SampleProcessor)decoders.get(i)).accept(file)) {
					try {
						wave=((SampleProcessor)decoders.get(i)).process(file);
						break;
					} catch(DecodeFailedException e) {
						dfe=e;
					}
				}
			}
			if(wave==null){throw new DecodeFailedException("File "+file+" could not be decoded.\n"+(dfe!=null?dfe.getMessage():"No decoder available for this format."));}
			return wave;
		}

		public String toString() {
			return "Decode";
		}
	}

	private static final SampleProcessor decoder=new Decoder();

	public static Sample createSample(SampleConfig config) throws IOException,UnsupportedAudioFileException {
		File file=config.getAbsoluteFile();
		List<SampleProcessor> queue=SampleProcessing.getProcessingQueue(config);
		if(queue==null) {
			queue=new ArrayList<SampleProcessor>(4);
		} else {
			queue=new ArrayList<SampleProcessor>(queue);
		}
		/*for(ListIterator<SampleProcessor> it=queue.listIterator();it.hasNext();) {
			it.add(decoder);
			it.next();
		}
		queue.add(decoder);*/
		File wave=file;
		try {
			for(Iterator<SampleProcessor> it=queue.iterator();it.hasNext();) {
				SampleProcessor sp=it.next();
				if(!sp.accept(wave)) {
//					System.out.println("decoding");
					wave=decoder.process(wave);
				}
//				System.out.println("processing "+sp);
				wave=sp.process(wave);
			}
//			System.out.println("decoding");
			wave=decoder.process(wave);
		} catch(DecodeFailedException e) {
			e.printStackTrace();
			throw new UnsupportedAudioFileException(e.getMessage());
		}
		Sample sample=new SampleWrapper(new FileSample(wave),config);
		for(Map.Entry e : config) {
			sample.setAttribute(e.getKey(),e.getValue());
		}
		return sample;
	}

	public static void registerDecoder(SampleProcessor decoder) {
		decoders.add(decoder);
		fileFilter=null;
	}

	public static void unregisterDecoder(SampleProcessor decoder) {
		decoders.remove(decoder);
		fileFilter=null;
	}

	public static SampleProcessor[] getDecoders() {
		return (SampleProcessor[])decoders.toArray(new SampleProcessor[0]);
	}

	private static FileFilter fileFilter=null;

	public static FileFilter getFileFilter() {
		class FileFilterWrapper extends FileFilter {
			SampleProcessor decoder;
			public boolean accept(File pathname){return decoder.accept(pathname)||pathname.isDirectory();}
			public String getDescription(){return "";}
			public FileFilterWrapper(SampleProcessor decoder){this.decoder=decoder;}
		}
		if(fileFilter!=null){return fileFilter;}
		FileFilter[] filters=new FileFilter[decoders.size()];
		for(int i=0;i<decoders.size();i++) {
			filters[i]=new FileFilterWrapper((SampleProcessor)decoders.get(i));
		}
		return fileFilter=new OrFileFilter(filters,"Audio Files");
	}

	static {
		registerDecoder(new WaveDecoder());
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -