package com.sun.media.sound;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.util.Objects;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
public final class AiffFileWriter extends SunFileWriter {
public AiffFileWriter() {
super(new AudioFileFormat.Type[]{AudioFileFormat.Type.AIFF});
}
@Override
public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
System.arraycopy(types, 0, filetypes, 0, types.length);
AudioFormat format = stream.getFormat();
AudioFormat.Encoding encoding = format.getEncoding();
if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
(AudioFormat.Encoding.ULAW.equals(encoding)) ||
(AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
(AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
return filetypes;
}
return new AudioFileFormat.Type[0];
}
@Override
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
Objects.requireNonNull(stream);
Objects.requireNonNull(fileType);
Objects.requireNonNull(out);
AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
throw new IOException("stream length not specified");
}
return writeAiffFile(stream, aiffFileFormat, out);
}
@Override
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
Objects.requireNonNull(stream);
Objects.requireNonNull(fileType);
Objects.requireNonNull(out);
AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
final int bytesWritten;
try (final FileOutputStream fos = new FileOutputStream(out);
final BufferedOutputStream bos = new BufferedOutputStream(fos)) {
bytesWritten = writeAiffFile(stream, aiffFileFormat, bos);
}
if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
int channels = aiffFileFormat.getFormat().getChannels();
int sampleSize = aiffFileFormat.getFormat().getSampleSizeInBits();
int ssndBlockSize = channels * ((sampleSize + 7) / 8);
int aiffLength=bytesWritten;
int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
long dataSize=ssndChunkSize-16;
int numFrames = (int) (dataSize / ssndBlockSize);
try (final RandomAccessFile raf = new RandomAccessFile(out, "rw")) {
raf.skipBytes(4);
raf.writeInt(aiffLength - 8);
raf.skipBytes(4 + aiffFileFormat.getFverChunkSize() + 4 + 4 + 2);
raf.writeInt(numFrames);
raf.skipBytes(2 + 10 + 4);
raf.writeInt(ssndChunkSize - 8);
}
}
return bytesWritten;
}
private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
if (!isFileTypeSupported(type, stream)) {
throw new IllegalArgumentException("File type " + type + " not supported.");
}
AudioFormat format = null;
AiffFileFormat fileFormat = null;
AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
AudioFormat streamFormat = stream.getFormat();
AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
int sampleSizeInBits;
int fileSize;
boolean convert8to16 = false;
if( (AudioFormat.Encoding.ALAW.equals(streamEncoding)) ||
(AudioFormat.Encoding.ULAW.equals(streamEncoding)) ) {
if( streamFormat.getSampleSizeInBits()==8 ) {
encoding = AudioFormat.Encoding.PCM_SIGNED;
sampleSizeInBits=16;
convert8to16 = true;
} else {
throw new IllegalArgumentException("Encoding " + streamEncoding + " supported only for 8-bit data.");
}
} else if ( streamFormat.getSampleSizeInBits()==8 ) {
encoding = AudioFormat.Encoding.PCM_UNSIGNED;
sampleSizeInBits=8;
} else {
encoding = AudioFormat.Encoding.PCM_SIGNED;
sampleSizeInBits=streamFormat.getSampleSizeInBits();
}
format = new AudioFormat( encoding,
streamFormat.getSampleRate(),
sampleSizeInBits,
streamFormat.getChannels(),
streamFormat.getFrameSize(),
streamFormat.getFrameRate(),
true);
if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
if( convert8to16 ) {
fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize()*2 + AiffFileFormat.AIFF_HEADERSIZE;
} else {
fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AiffFileFormat.AIFF_HEADERSIZE;
}
} else {
fileSize = AudioSystem.NOT_SPECIFIED;
}
fileFormat = new AiffFileFormat( AudioFileFormat.Type.AIFF,
fileSize,
format,
(int)stream.getFrameLength() );
return fileFormat;
}
private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
int bytesRead = 0;
int bytesWritten = 0;
InputStream fileStream = getFileStream(aiffFileFormat, in);
byte[] buffer = new byte[bisBufferSize];
int maxLength = aiffFileFormat.getByteLength();
while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
if (maxLength>0) {
if( bytesRead < maxLength ) {
out.write( buffer, 0, bytesRead );
bytesWritten += bytesRead;
maxLength -= bytesRead;
} else {
out.write( buffer, 0, maxLength );
bytesWritten += maxLength;
maxLength = 0;
break;
}
} else {
out.write( buffer, 0, bytesRead );
bytesWritten += bytesRead;
}
}
return bytesWritten;
}
private InputStream getFileStream(AiffFileFormat aiffFileFormat, InputStream audioStream) throws IOException {
AudioFormat format = aiffFileFormat.getFormat();
AudioFormat streamFormat = null;
AudioFormat.Encoding encoding = null;
int headerSize = aiffFileFormat.getHeaderSize();
int fverChunkSize = aiffFileFormat.getFverChunkSize();
int commChunkSize = aiffFileFormat.getCommChunkSize();
int aiffLength = -1;
int ssndChunkSize = -1;
int ssndOffset = aiffFileFormat.getSsndChunkOffset();
short channels = (short) format.getChannels();
short sampleSize = (short) format.getSampleSizeInBits();
int ssndBlockSize = channels * ((sampleSize + 7) / 8);
int numFrames = aiffFileFormat.getFrameLength();
long dataSize = -1;
if( numFrames != AudioSystem.NOT_SPECIFIED) {
dataSize = (long) numFrames * ssndBlockSize;
ssndChunkSize = (int)dataSize + 16;
aiffLength = (int)dataSize+headerSize;
}
float sampleFramesPerSecond = format.getSampleRate();
int compCode = AiffFileFormat.AIFC_PCM;
byte[] header = null;
InputStream codedAudioStream = audioStream;
if( audioStream instanceof AudioInputStream ) {
streamFormat = ((AudioInputStream)audioStream).getFormat();
encoding = streamFormat.getEncoding();
if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) && !streamFormat.isBigEndian() ) ) {
codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
AudioFormat.Encoding.PCM_SIGNED,
streamFormat.getSampleRate(),
streamFormat.getSampleSizeInBits(),
streamFormat.getChannels(),
streamFormat.getFrameSize(),
streamFormat.getFrameRate(),
true ),
(AudioInputStream)audioStream );
} else if( (AudioFormat.Encoding.ULAW.equals(encoding)) ||
(AudioFormat.Encoding.ALAW.equals(encoding)) ) {
if( streamFormat.getSampleSizeInBits() != 8 ) {
throw new IllegalArgumentException("unsupported encoding");
}
codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
AudioFormat.Encoding.PCM_SIGNED,
streamFormat.getSampleRate(),
streamFormat.getSampleSizeInBits() * 2,
streamFormat.getChannels(),
streamFormat.getFrameSize() * 2,
streamFormat.getFrameRate(),
true ),
(AudioInputStream)audioStream );
}
}
try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(baos)) {
dos.writeInt(AiffFileFormat.AIFF_MAGIC);
dos.writeInt((aiffLength - 8));
dos.writeInt(AiffFileFormat.AIFF_MAGIC2);
dos.writeInt(AiffFileFormat.COMM_MAGIC);
dos.writeInt((commChunkSize - 8));
dos.writeShort(channels);
dos.writeInt(numFrames);
dos.writeShort(sampleSize);
write_ieee_extended(dos, sampleFramesPerSecond);
dos.writeInt(AiffFileFormat.SSND_MAGIC);
dos.writeInt((ssndChunkSize - 8));
dos.writeInt(0);
dos.writeInt(0);
header = baos.toByteArray();
}
return new SequenceInputStream(new ByteArrayInputStream(header),
new NoCloseInputStream(codedAudioStream));
}
private static final int DOUBLE_MANTISSA_LENGTH = 52;
private static final int DOUBLE_EXPONENT_LENGTH = 11;
private static final long DOUBLE_SIGN_MASK = 0x8000000000000000L;
private static final long DOUBLE_EXPONENT_MASK = 0x7FF0000000000000L;
private static final long DOUBLE_MANTISSA_MASK = 0x000FFFFFFFFFFFFFL;
private static final int DOUBLE_EXPONENT_OFFSET = 1023;
private static final int EXTENDED_EXPONENT_OFFSET = 16383;
private static final int EXTENDED_MANTISSA_LENGTH = 63;
private static final int EXTENDED_EXPONENT_LENGTH = 15;
private static final long EXTENDED_INTEGER_MASK = 0x8000000000000000L;
private void write_ieee_extended(DataOutputStream dos, float f) throws IOException {
long doubleBits = Double.doubleToLongBits((double) f);
long sign = (doubleBits & DOUBLE_SIGN_MASK)
>> (DOUBLE_EXPONENT_LENGTH + DOUBLE_MANTISSA_LENGTH);
long doubleExponent = (doubleBits & DOUBLE_EXPONENT_MASK)
>> DOUBLE_MANTISSA_LENGTH;
long doubleMantissa = doubleBits & DOUBLE_MANTISSA_MASK;
long extendedExponent = doubleExponent - DOUBLE_EXPONENT_OFFSET
+ EXTENDED_EXPONENT_OFFSET;
long extendedMantissa = doubleMantissa
<< (EXTENDED_MANTISSA_LENGTH - DOUBLE_MANTISSA_LENGTH);
long extendedSign = sign << EXTENDED_EXPONENT_LENGTH;
short extendedBits79To64 = (short) (extendedSign | extendedExponent);
long extendedBits63To0 = EXTENDED_INTEGER_MASK | extendedMantissa;
dos.writeShort(extendedBits79To64);
dos.writeLong(extendedBits63To0);
}
}