|
| 1 | +/* |
| 2 | + * Copyright (C) 2010 Preston Lacey http://javaflacencoder.sourceforge.net/ |
| 3 | + * All Rights Reserved. |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package javaFlacEncoder; |
| 21 | +import javax.sound.sampled.AudioInputStream; |
| 22 | +import javax.sound.sampled.AudioFormat; |
| 23 | +import javax.sound.sampled.AudioSystem; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +/** |
| 27 | + * AudioStreamEncoder provides commonly needed methods for encoding with a |
| 28 | + * FLACEncoder from an AudioInputStream. |
| 29 | + * |
| 30 | + * @author preston |
| 31 | + */ |
| 32 | +public class AudioStreamEncoder { |
| 33 | + |
| 34 | + public static final int SUPPORTED = 0; |
| 35 | + public static final int UNSUPPORTED_CHANNELCOUNT = 1; |
| 36 | + public static final int UNSUPPORTED_SAMPLESIZE = 2; |
| 37 | + public static final int UNSUPPORTED_SAMPLERATE = 4; |
| 38 | + public static final int UNSUPPORTED_ENCODINGTYPE = 8; |
| 39 | + |
| 40 | + /** |
| 41 | + * Encodes the given AudioInputStream, using the given FLACEncoder. |
| 42 | + * FLACEncoder must be in a state to accept samples and encode(FLACOutputStream, |
| 43 | + * EncodingConfiguration, and StreamConfiguration have been set, and FLAC stream |
| 44 | + * has been opened). |
| 45 | + * @param sin |
| 46 | + * @return |
| 47 | + * @throws IOException |
| 48 | + * @throws IllegalArgumentException thrown if input sample size is not supported |
| 49 | + */ |
| 50 | + public static int encodeAudioInputStream(AudioInputStream sin, int maxRead, |
| 51 | + FLACEncoder flac, boolean useThreads) throws IOException, IllegalArgumentException { |
| 52 | + AudioFormat format = sin.getFormat(); |
| 53 | + int frameSize = format.getFrameSize(); |
| 54 | + int sampleSize = format.getSampleSizeInBits(); |
| 55 | + int bytesPerSample = sampleSize/8; |
| 56 | + if(sampleSize %8 != 0) { |
| 57 | + //end processing now |
| 58 | + throw new IllegalArgumentException("Unsupported Sample Size: size = "+sampleSize); |
| 59 | + } |
| 60 | + int channels = format.getChannels(); |
| 61 | + boolean bigEndian = format.isBigEndian(); |
| 62 | + boolean isSigned = format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED; |
| 63 | + byte[] samplesIn = new byte[(int)maxRead]; |
| 64 | + int samplesRead; |
| 65 | + int framesRead; |
| 66 | + int[] sampleData = new int[maxRead*channels/frameSize]; |
| 67 | + int unencodedSamples = 0; |
| 68 | + int totalSamples = 0; |
| 69 | + while((samplesRead = sin.read(samplesIn, 0, maxRead)) > 0 ) { |
| 70 | + framesRead = samplesRead/(frameSize); |
| 71 | + if(bigEndian) { |
| 72 | + for(int i = 0; i < framesRead*channels; i++) { |
| 73 | + int lower8Mask = 255; |
| 74 | + int temp = 0; |
| 75 | + int totalTemp = 0; |
| 76 | + for(int x = bytesPerSample-1; x >= 0; x++) { |
| 77 | + int upShift = 8*x; |
| 78 | + if(x == 0)//don't mask...we want sign |
| 79 | + temp = ((samplesIn[bytesPerSample*i+x]) << upShift); |
| 80 | + else |
| 81 | + temp = ((samplesIn[bytesPerSample*i+x] & lower8Mask) << upShift); |
| 82 | + totalTemp = totalTemp | temp; |
| 83 | + } |
| 84 | + if(!isSigned) { |
| 85 | + int reducer = 1 << (bytesPerSample*8-1); |
| 86 | + totalTemp -= reducer; |
| 87 | + } |
| 88 | + sampleData[i] = totalTemp; |
| 89 | + } |
| 90 | + } |
| 91 | + else { |
| 92 | + for(int i = 0; i < framesRead*channels; i++) { |
| 93 | + int lower8Mask = 255; |
| 94 | + int temp = 0; |
| 95 | + int totalTemp = 0; |
| 96 | + for(int x = 0; x < bytesPerSample; x++) { |
| 97 | + int upShift = 8*x; |
| 98 | + if(x == bytesPerSample-1 && isSigned)//don't mask...we want sign |
| 99 | + temp = ((samplesIn[bytesPerSample*i+x]) << upShift); |
| 100 | + else |
| 101 | + temp = ((samplesIn[bytesPerSample*i+x] & lower8Mask) << upShift); |
| 102 | + totalTemp = totalTemp | temp; |
| 103 | + } |
| 104 | + if(!isSigned) { |
| 105 | + int reducer = 1 << (bytesPerSample*8-1); |
| 106 | + totalTemp -= reducer; |
| 107 | + } |
| 108 | + sampleData[i] = totalTemp; |
| 109 | + } |
| 110 | + } |
| 111 | + if(framesRead > 0) { |
| 112 | + flac.addSamples(sampleData, framesRead); |
| 113 | + unencodedSamples += framesRead; |
| 114 | + } |
| 115 | + |
| 116 | + if(useThreads) |
| 117 | + unencodedSamples -= flac.t_encodeSamples(unencodedSamples, false, 5); |
| 118 | + else |
| 119 | + unencodedSamples -= flac.encodeSamples(unencodedSamples, false); |
| 120 | + totalSamples += unencodedSamples; |
| 121 | + } |
| 122 | + totalSamples += unencodedSamples; |
| 123 | + if(useThreads) |
| 124 | + unencodedSamples -= flac.t_encodeSamples(unencodedSamples, true, 5); |
| 125 | + else |
| 126 | + unencodedSamples -= flac.encodeSamples(unencodedSamples, true); |
| 127 | + return totalSamples; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Checks whether the given AudioFormat can be properly encoded by this |
| 132 | + * FLAC library. |
| 133 | + * @param format AudioFormat to test for support. |
| 134 | + * @return Bit positions set according to issue: |
| 135 | + * Bit Position : Problem-area |
| 136 | + * 0 : Channel count unsupported |
| 137 | + * 1 : Sample size unsupported |
| 138 | + * 2 : Sample Rate unsupported |
| 139 | + * 3 : Encoding Type Unsupported |
| 140 | + * 4-7: unused, always zero. |
| 141 | + * return value of 0 means supported |
| 142 | + */ |
| 143 | + public static int getDataFormatSupport(AudioFormat format) { |
| 144 | + int result = SUPPORTED; |
| 145 | + float sampleRate = format.getSampleRate(); |
| 146 | + AudioFormat.Encoding encoding = format.getEncoding(); |
| 147 | + if(format.getChannels() > 8 || format.getChannels() < 1) |
| 148 | + result |= UNSUPPORTED_CHANNELCOUNT; |
| 149 | + if(format.getSampleSizeInBits() > 24 || format.getSampleSizeInBits()%8 != 0) |
| 150 | + result |= UNSUPPORTED_SAMPLESIZE; |
| 151 | + if(sampleRate <= 0 || sampleRate > 655350 || sampleRate == AudioSystem.NOT_SPECIFIED) |
| 152 | + result |= UNSUPPORTED_SAMPLERATE; |
| 153 | + if ( !(AudioFormat.Encoding.ALAW.equals(encoding) || |
| 154 | + AudioFormat.Encoding.ULAW.equals(encoding) || |
| 155 | + AudioFormat.Encoding.PCM_SIGNED.equals(encoding) || |
| 156 | + AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding) ) ) |
| 157 | + result |= UNSUPPORTED_ENCODINGTYPE; |
| 158 | + return result; |
| 159 | + } |
| 160 | +} |
0 commit comments