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

📄 taudiofilewriter.java

📁 java处理声音文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				bNeedsConversion = true;				// $$fb 2000-08-16 made consistent with new conversion trials				// if 8 bit and only endianness changed, don't convert !				if (outputFormat.getSampleSizeInBits()==8 				    && outputFormat.getEncoding().equals(inputFormat.getEncoding())) {					bNeedsConversion = false;				}			}			else			{				throw new IllegalArgumentException("format not supported and not convertable");			}		}		long	lLengthInBytes = AudioUtils.getLengthInBytes(audioInputStream);		AudioOutputStream	audioOutputStream =			getAudioOutputStream(				outputFormat,				lLengthInBytes,				fileType,				outputStream);		return writeImpl(audioInputStream,				 audioOutputStream,				 bNeedsConversion);	}	protected int writeImpl(		AudioInputStream audioInputStream,		AudioOutputStream audioOutputStream,		boolean bNeedsConversion)		throws	IOException	{		if (TDebug.TraceAudioFileWriter)		{			TDebug.out("TAudioFileWriter.writeImpl(): called");		}		int	nTotalWritten = 0;		AudioFormat	inputFormat = audioInputStream.getFormat();		AudioFormat	outputFormat = audioOutputStream.getFormat();		// TODO: handle case when frame size is unknown ?		int	nBytesPerSample = outputFormat.getFrameSize() / outputFormat.getChannels();				//$$fb 2000-07-18: BUFFER_LENGTH must be a multiple of frame size...		int nBufferSize=((int)BUFFER_LENGTH/outputFormat.getFrameSize())*outputFormat.getFrameSize();		byte[]	abBuffer = new byte[nBufferSize];		while (true)		{			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.writeImpl(): trying to read (bytes): " + abBuffer.length);			}			int	nBytesRead = audioInputStream.read(abBuffer);			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.writeImpl(): read (bytes): " + nBytesRead);			}			if (nBytesRead == -1)			{				break;			}			if (bNeedsConversion)			{				TConversionTool.changeOrderOrSign(abBuffer, 0,						  nBytesRead, nBytesPerSample);			}			int	nWritten = audioOutputStream.write(abBuffer, 0, nBytesRead);			nTotalWritten += nWritten;		}		if (TDebug.TraceAudioFileWriter)		{			TDebug.out("TAudioFileWriter.writeImpl(): after main loop");		}		audioOutputStream.close();		// TODO: get bytes written for header etc. from AudioOutputStrem and add to nTotalWrittenBytes		return nTotalWritten;	}	/**	Returns the AudioFormat that can be handled for the given file type.	 *	In this simple implementation, all handled AudioFormats are	 *	returned (i.e. the fileType argument is ignored). If the	 *	handled AudioFormats depend on the file type, this method	 *	has to be overwritten by subclasses.	 */	protected Iterator getSupportedAudioFormats(AudioFileFormat.Type fileType)	{		return m_audioFormats.iterator();	}	/**	Checks whether the passed <b>AudioFormat</b> can be handled.	 *	In this simple implementation, it is only checked if the	 *	passed AudioFormat matches one of the generally handled	 *	formats (i.e. the fileType argument is ignored). If the	 *	handled AudioFormats depend on the file type, this method	 *	or getSupportedAudioFormats() (on which this method relies)	 *	has to be  overwritten by subclasses.	 *      <p>	 *      This is the central method for checking if a FORMAT is supported.	 *      Inheriting classes can overwrite this for performance	 *      or to exclude/include special type/format combinations.	 *      <p>	 *      This method is only called when the <code>fileType</code>	 *      is in the list of supported file types ! Overriding	 *      classes <b>need not</b> check this.	 */	//$$fb 2000-08-16 changed name, changed documentation. Semantics !	protected boolean isAudioFormatSupportedImpl(		AudioFormat audioFormat,		AudioFileFormat.Type fileType)	{		if (TDebug.TraceAudioFileWriter)		{			TDebug.out("TAudioFileWriter.isAudioFormatSupportedImpl(): format to test: " + audioFormat);		}		Iterator	audioFormats = getSupportedAudioFormats(fileType);		while (audioFormats.hasNext())		{			AudioFormat	handledFormat = (AudioFormat) audioFormats.next();			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.isAudioFormatSupportedImpl(): matching against format : " + handledFormat);			}			if (AudioFormats.matches(handledFormat, audioFormat))			{				if (TDebug.TraceAudioFileWriter)				{					TDebug.out("...succeeded.");				}				return true;			}			else			{				if (TDebug.TraceAudioFileWriter)				{					TDebug.out("...failed.");				}			}		}		return false;	}	protected abstract AudioOutputStream getAudioOutputStream(		AudioFormat audioFormat,		long lLengthInBytes,		AudioFileFormat.Type fileType,		File file)		throws	IOException;	protected abstract AudioOutputStream getAudioOutputStream(		AudioFormat audioFormat,		long lLengthInBytes,		AudioFileFormat.Type fileType,		OutputStream outputStream)		throws	IOException;	private AudioFormat findConvertableFormat(		AudioFormat inputFormat,		AudioFileFormat.Type fileType)	{		if (!isFileTypeSupported(fileType)) {			return null;		}		if (TDebug.TraceAudioFileWriter)		{			TDebug.out("TAudioFileWriter.findConvertableFormat(): input format: " + inputFormat);		}		AudioFormat.Encoding	inputEncoding = inputFormat.getEncoding();		if ((inputEncoding.equals(PCM_SIGNED) || inputEncoding.equals(PCM_UNSIGNED)) 		    && inputFormat.getSampleSizeInBits() == 8)		{			AudioFormat outputFormat = convertFormat(inputFormat, true, false);			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.findConvertableFormat(): trying output format: " + outputFormat);			}			if (isAudioFormatSupportedImpl(outputFormat, fileType))			{				return outputFormat;			}			//$$fb 2000-08-16: added trial of other endianness for 8bit. We try harder !			outputFormat = convertFormat(inputFormat, false, true);			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.findConvertableFormat(): trying output format: " + outputFormat);			}			if (isAudioFormatSupportedImpl(outputFormat, fileType))			{				return outputFormat;			}			outputFormat = convertFormat(inputFormat, true, true);			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.findConvertableFormat(): trying output format: " + outputFormat);			}			if (isAudioFormatSupportedImpl(outputFormat, fileType))			{				return outputFormat;			}			return null;		}		else if (inputEncoding.equals(PCM_SIGNED) &&			 (inputFormat.getSampleSizeInBits() == 16 ||			  inputFormat.getSampleSizeInBits() == 24 ||			  inputFormat.getSampleSizeInBits() == 32) )		{			// TODO: possible to allow all sample sized > 8 bit?			// $$ fb: don't think that this is necessary. Well, let's talk about that in 5 years :)			AudioFormat	outputFormat = convertFormat(inputFormat, false, true);			if (TDebug.TraceAudioFileWriter)			{				TDebug.out("TAudioFileWriter.findConvertableFormat(): trying output format: " + outputFormat);			}			if (isAudioFormatSupportedImpl(outputFormat, fileType))			{				return outputFormat;			}			else			{				return null;			}		}		else		{			return null;		}	}	// $$fb 2000-08-16: added convenience method	private AudioFormat convertFormat(AudioFormat format, boolean changeSign, boolean changeEndian) {		AudioFormat.Encoding enc=PCM_SIGNED;		if (format.getEncoding().equals(PCM_UNSIGNED)!=changeSign) {			enc=PCM_UNSIGNED;		}		return new AudioFormat(				       enc,				       format.getSampleRate(),				       format.getSampleSizeInBits(),				       format.getChannels(),				       format.getFrameSize(),				       format.getFrameRate(),				       format.isBigEndian() ^ changeEndian);	}}/*** TAudioFileWriter.java ***/

⌨️ 快捷键说明

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