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

📄 audiosystem.java

📁 java处理声音文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *	AudioSystem.java *//* *  Copyright (c) 1999, 2000 by Matthias Pfisterer <Matthias.Pfisterer@gmx.de> * * *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU Library General Public License as published *   by the Free Software Foundation; either version 2 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 Library General Public License for more details. * *   You should have received a copy of the GNU Library General Public *   License along with this program; if not, write to the Free Software *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */package	javax.sound.sampled;import	java.io.BufferedInputStream;import	java.io.File;import	java.io.InputStream;import	java.io.IOException;import	java.io.OutputStream;import	java.net.URL;import	java.util.Arrays;import	java.util.Collection;import	java.util.ArrayList;import	java.util.HashSet;import	java.util.Set;import	java.util.Iterator;import	javax.sound.sampled.spi.AudioFileReader;import	javax.sound.sampled.spi.AudioFileWriter;import	javax.sound.sampled.spi.FormatConversionProvider;import	javax.sound.sampled.spi.MixerProvider;import	org.tritonus.TDebug;import	org.tritonus.sampled.TAudioConfig;import	org.tritonus.util.ArraySet;public class AudioSystem{	public static final int		NOT_SPECIFIED = -1;	private static final AudioFormat[]	EMPTY_FORMAT_ARRAY = new AudioFormat[0];	private static final AudioFormat.Encoding[]	EMPTY_ENCODING_ARRAY = new AudioFormat.Encoding[0];	private static final AudioFileFormat.Type[]	EMPTY_TYPE_ARRAY = new AudioFileFormat.Type[0];	private static final Mixer.Info[]	EMPTY_MIXER_INFO_ARRAY = new Mixer.Info[0];	private static final Line.Info[]	EMPTY_LINE_INFO_ARRAY = new Line.Info[0];	/**	MixerProviderAction for getMixerInfo().	 */	private static class GetMixerInfoMixerProviderAction		implements	MixerProviderAction	{		private Collection	m_mixerInfos = new ArrayList();		public GetMixerInfoMixerProviderAction()		{		}		public boolean handleMixerProvider(			MixerProvider mixerProvider)		{			Mixer.Info[]	aMixerInfos = mixerProvider.getMixerInfo();			if (aMixerInfos != null)			{				// TODO: is this if needed?				if (aMixerInfos.length > 0)				{					if (TDebug.TraceAudioSystem)					{						TDebug.out("AudioSystem.getMixerInfo(): MixerProvider returns array:");						for (int i = 0; i < aMixerInfos.length; i++)						{							TDebug.out("" + aMixerInfos[i]);						}					}				}				else if (TDebug.TraceAudioSystem)				{					TDebug.out("AudioSystem.getMixerInfo(): MixerProvider returns empty array.");				}				m_mixerInfos.addAll(Arrays.asList(aMixerInfos));			}			else if (TDebug.TraceAudioSystem)			{				TDebug.out("AudioSystem.getMixerInfo(): MixerProvider returns null.");			}			// always continue			return false;		}		public Mixer.Info[] getMixerInfos()		{			return (Mixer.Info[]) m_mixerInfos.toArray(EMPTY_MIXER_INFO_ARRAY);		}	}	public static Mixer.Info[] getMixerInfo()	{		GetMixerInfoMixerProviderAction	action = new GetMixerInfoMixerProviderAction();		doMixerProviderIteration(action);		Mixer.Info[] infos = action.getMixerInfos();		// TDebug.out("MI length: " + infos.length);		return action.getMixerInfos();	}	/**	MixerProviderAction for getMixer().	 */	private static class GetMixerMixerProviderAction		implements	MixerProviderAction	{		private Mixer.Info	m_info = null;		private Mixer		m_mixer = null;		public GetMixerMixerProviderAction(Mixer.Info info)		{			m_info = info;		}		public boolean handleMixerProvider(			MixerProvider mixerProvider)		{			try			{				Mixer	mixer = mixerProvider.getMixer(m_info);				if (m_mixer == null)				{					m_mixer = mixer;					// now interrupt the iteration					return true;				}			}			catch (IllegalArgumentException e)			{				if (TDebug.TraceAudioSystem)				{					TDebug.out(e);				}			}			// continue the iteration			return false;		}		public Mixer getMixer()		{			return m_mixer;		}	}	public static Mixer getMixer(Mixer.Info info)	{		if (info == null)		{			// TODO: this currently always yields null!!!			info = TAudioConfig.getDefaultMixerInfo();		}		GetMixerMixerProviderAction	action = new GetMixerMixerProviderAction(info);		doMixerProviderIteration(action);		Mixer	mixer = action.getMixer();		if (mixer != null)		{			return mixer;		}		else		{			throw new IllegalArgumentException("no mixer found for " + info);		}	}	/**	MixerAction for getSourceLineInfo().	 */	private static class GetSourceLineInfoMixerAction		implements	MixerAction	{		private Line.Info	m_info = null;		private Set		m_infos;		public GetSourceLineInfoMixerAction(Line.Info info)		{			m_info = info;			m_infos = new ArraySet();		}		public boolean handleMixer(			Mixer mixer)		{			Line.Info[]	infos = mixer.getSourceLineInfo(m_info);			m_infos.addAll(Arrays.asList(infos));			// always continue the iteration			return false;		}		public Line.Info[] getInfos()		{			return (Line.Info[]) m_infos.toArray(EMPTY_LINE_INFO_ARRAY);		}	}	public static Line.Info[] getSourceLineInfo(Line.Info info)	{		GetSourceLineInfoMixerAction	action = new GetSourceLineInfoMixerAction(info);		doMixerIteration(action);		return action.getInfos();	}	/**	MixerAction for getTargetLineInfo().	 */	private static class GetTargetLineInfoMixerAction		implements	MixerAction	{		private Line.Info	m_info = null;		private Set		m_infos;		public GetTargetLineInfoMixerAction(Line.Info info)		{			m_info = info;			m_infos = new ArraySet();		}		public boolean handleMixer(			Mixer mixer)		{			Line.Info[]	infos = mixer.getTargetLineInfo(m_info);			m_infos.addAll(Arrays.asList(infos));			// always continue the iteration			return false;		}		public Line.Info[] getInfos()		{			return (Line.Info[]) m_infos.toArray(EMPTY_LINE_INFO_ARRAY);		}	}	public static Line.Info[] getTargetLineInfo(Line.Info info)	{		GetTargetLineInfoMixerAction	action = new GetTargetLineInfoMixerAction(info);		doMixerIteration(action);		return action.getInfos();	}	/**	MixerAction for isLineSupported().	 */	private static class IsLineSupportedMixerAction		implements	MixerAction	{		private Line.Info	m_info = null;		private boolean		m_bSupported = false;		public IsLineSupportedMixerAction(Line.Info info)		{			m_info = info;		}		public boolean handleMixer(			Mixer mixer)		{			boolean bSupported = mixer.isLineSupported(m_info);			m_bSupported |= bSupported;			// interrupt the iteration depending on the result			return bSupported;		}		public boolean isSupported()		{			return m_bSupported;		}	}	public static boolean isLineSupported(Line.Info info)	{		IsLineSupportedMixerAction	action = new IsLineSupportedMixerAction(info);		doMixerIteration(action);		return action.isSupported();	}	/**	MixerAction for getLine().	 */	private static class GetLineMixerAction		implements	MixerAction	{		private Line.Info	m_info = null;		private Line		m_line = null;		private boolean		m_bLineTypeSupported = false;		public GetLineMixerAction(Line.Info info)		{			m_info = info;		}		public boolean handleMixer(			Mixer mixer)		{			try			{				Line	line = mixer.getLine(m_info);				if (m_line == null)				{					m_line = line;					// interrupt the iteration					return true;				}			}			catch (LineUnavailableException e)			{				m_bLineTypeSupported = true;				if (TDebug.TraceAudioSystem)				{					TDebug.out(e);				}			}			catch (IllegalArgumentException e)			{				if (TDebug.TraceAudioSystem)				{					TDebug.out(e);				}			}			// continue the iteration			return false;		}		public Line getLine()		{			return m_line;		}		public boolean isLineTypeSupported()		{			return m_bLineTypeSupported;		}	}	public static Line getLine(Line.Info info)		throws	LineUnavailableException	{		GetLineMixerAction	action = new GetLineMixerAction(info);		doMixerIteration(action);		Line	line = action.getLine();		if (line != null)		{			return line;		}		else if (action.isLineTypeSupported())		{			throw new LineUnavailableException("currently no line available due to resource restrictions");		}		else		{			throw new IllegalArgumentException("no mixer supporting this type of line: " + info);		}	}	/**	FormatConversionProviderAction for getTargetEncodings().	 */	private static class GetTargetEncodingsFormatConversionProviderAction		implements	FormatConversionProviderAction	{		private Object		m_sourceDescription;		private Collection	m_targetEncodings;		//$$fb 2000-08-15: added for workaround below		public GetTargetEncodingsFormatConversionProviderAction() {			this(null);		}		public GetTargetEncodingsFormatConversionProviderAction(			Object sourceDescription)		{			m_sourceDescription = sourceDescription;			m_targetEncodings = new ArraySet();		}		public boolean handleFormatConversionProvider(			FormatConversionProvider formatConversionProvider)		{			// $$fb 2000-03-30 default to empty array			AudioFormat.Encoding[]	encodings = EMPTY_ENCODING_ARRAY;			if (m_sourceDescription instanceof AudioFormat.Encoding)			{				// TODO: not directely implementable. Contact Sun.				//$$fb 2000-08-15: see workaround below				/*				encodings = formatConversionProvider.getTargetEncodings(					(AudioFormat.Encoding) m_sourceDescription);				*/			}			else if (m_sourceDescription instanceof AudioFormat)			{				encodings = formatConversionProvider.getTargetEncodings(					(AudioFormat) m_sourceDescription);			}			else			{				// TODO: debug message			}			m_targetEncodings.addAll(Arrays.asList(encodings));				// continue the iteration			return false;		}		public AudioFormat.Encoding[] getEncodings()		{			return (AudioFormat.Encoding[]) m_targetEncodings.toArray(EMPTY_ENCODING_ARRAY);		}		//$$fb 2000-08-15: added for workaround below		public void setSourceDescription(Object sourceDescription) {			m_sourceDescription = sourceDescription;		}				}	//$$fb 2000-08-15: added for workaround below	private static void doEncodingActionWorkaround(boolean bigEndian, AudioFormat.Encoding encoding, 					  GetTargetEncodingsFormatConversionProviderAction action) {		AudioFormat format=new AudioFormat(

⌨️ 快捷键说明

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