📄 taudiofilewriter.java
字号:
/* * TAudioFileWriter.java *//* * Copyright (c) 1999, 2000 by Matthias Pfisterer <Matthias.Pfisterer@gmx.de> * Copyright (c) 1999, 2000 by Florian Bomers <florian@bome.com> * * 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 org.tritonus.share.sampled.file;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.util.Collection;import java.util.Iterator;import javax.sound.sampled.AudioFileFormat;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.spi.AudioFileWriter;import org.tritonus.share.ArraySet;import org.tritonus.share.TDebug;import org.tritonus.share.sampled.AudioFormats;import org.tritonus.share.sampled.AudioUtils;import org.tritonus.share.sampled.Encodings;import org.tritonus.share.sampled.TConversionTool;/** * Common base class for implementing classes of AudioFileWriter. * <p>It provides often-used functionality and the new architecture using * an AudioOutputStream. * <p>There should be only one set of audio formats supported by any given * class of TAudioFileWriter. This class assumes implicitely that all * supported file types have a common set of audio formats they can handle. * * @author Matthias Pfisterer * @author Florian Bomers */public abstract class TAudioFileWriter extends AudioFileWriter{ protected static final int ALL = AudioSystem.NOT_SPECIFIED; public static AudioFormat.Encoding PCM_SIGNED=Encodings.getEncoding("PCM_SIGNED"); public static AudioFormat.Encoding PCM_UNSIGNED=Encodings.getEncoding("PCM_UNSIGNED"); /** Buffer length for the loop in the write() method. * This is in bytes. Perhaps it should be in frames to give an * equal amount of latency. */ private static final int BUFFER_LENGTH = 16384; // only needed for Collection.toArray() protected static final AudioFileFormat.Type[] NULL_TYPE_ARRAY = new AudioFileFormat.Type[0]; /** The audio file types (AudioFileFormat.Type) that can be * handled by the AudioFileWriter. */ private Collection m_audioFileTypes; /** The AudioFormats that can be handled by the * AudioFileWriter. */ // IDEA: implement a special collection that uses matches() to test whether an element is already in private Collection m_audioFormats; /** * Inheriting classes should call this constructor * in order to make use of the functionality of TAudioFileWriter. */ protected TAudioFileWriter(Collection fileTypes, Collection audioFormats) { if (TDebug.TraceAudioFileWriter) { TDebug.out("TAudioFileWriter.<init>(): begin"); } m_audioFileTypes = fileTypes; m_audioFormats = audioFormats; if (TDebug.TraceAudioFileWriter) { TDebug.out("TAudioFileWriter.<init>(): end"); } } // implementing the interface public AudioFileFormat.Type[] getAudioFileTypes() { return (AudioFileFormat.Type[]) m_audioFileTypes.toArray(NULL_TYPE_ARRAY); } // implementing the interface public boolean isFileTypeSupported(AudioFileFormat.Type fileType) { return m_audioFileTypes.contains(fileType); } // implementing the interface public AudioFileFormat.Type[] getAudioFileTypes( AudioInputStream audioInputStream) { //$$fb 2000-08-16: rewrote this method. We need to check for *each* // file type, whether the format is supported ! AudioFormat format = audioInputStream.getFormat(); ArraySet res=new ArraySet(); Iterator it=m_audioFileTypes.iterator(); while (it.hasNext()) { AudioFileFormat.Type thisType=(AudioFileFormat.Type) it.next(); if (isAudioFormatSupportedImpl(format, thisType)) { res.add(thisType); } } return (AudioFileFormat.Type[]) res.toArray(NULL_TYPE_ARRAY); } // implementing the interface public boolean isFileTypeSupported(AudioFileFormat.Type fileType, AudioInputStream audioInputStream) { // $$fb 2000-08-16: finally this method works reliably ! return isFileTypeSupported(fileType) && (isAudioFormatSupportedImpl(audioInputStream.getFormat(), fileType) || findConvertableFormat(audioInputStream.getFormat(), fileType)!=null); // we may soft it up by including the possibility of endian/sign // changing for PCM formats. // I prefer to return false if the format is not exactly supported // but still exectute the write, if only sign/endian changing is necessary. } // implementing the interface public int write(AudioInputStream audioInputStream, AudioFileFormat.Type fileType, File file) throws IOException { if (TDebug.TraceAudioFileWriter) { TDebug.out(">TAudioFileWriter.write(.., File): called"); TDebug.out("class: "+getClass().getName()); } //$$fb added this check if (!isFileTypeSupported(fileType)) { if (TDebug.TraceAudioFileWriter) { TDebug.out("< file type is not supported"); } throw new IllegalArgumentException("file type is not supported."); } AudioFormat inputFormat = audioInputStream.getFormat(); if (TDebug.TraceAudioFileWriter) { TDebug.out("input format: " + inputFormat); } AudioFormat outputFormat = null; boolean bNeedsConversion = false; if (isAudioFormatSupportedImpl(inputFormat, fileType)) { if (TDebug.TraceAudioFileWriter) { TDebug.out("input format is supported directely"); } outputFormat = inputFormat; bNeedsConversion = false; } else { if (TDebug.TraceAudioFileWriter) { TDebug.out("input format is not supported directely; trying to find a convertable format"); } outputFormat = findConvertableFormat(inputFormat, fileType); if (outputFormat != null) { 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 { if (TDebug.TraceAudioFileWriter) { TDebug.out("< input format is not supported and not convertable."); } throw new IllegalArgumentException("format not supported and not convertable"); } } long lLengthInBytes = AudioUtils.getLengthInBytes(audioInputStream); TDataOutputStream dataOutputStream = new TSeekableDataOutputStream(file); AudioOutputStream audioOutputStream = getAudioOutputStream( outputFormat, lLengthInBytes, fileType, dataOutputStream); int written=writeImpl(audioInputStream, audioOutputStream, bNeedsConversion); if (TDebug.TraceAudioFileWriter) { TDebug.out("< wrote "+written+" bytes."); } return written; } // implementing the interface public int write(AudioInputStream audioInputStream, AudioFileFormat.Type fileType, OutputStream outputStream) throws IOException { //$$fb added this check if (!isFileTypeSupported(fileType)) { throw new IllegalArgumentException("file type is not supported."); } if (TDebug.TraceAudioFileWriter) { TDebug.out(">TAudioFileWriter.write(.., OutputStream): called"); TDebug.out("class: "+getClass().getName()); } AudioFormat inputFormat = audioInputStream.getFormat();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -