Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xiphias3.util;
- import javax.sound.sampled.AudioFormat;
- import java.util.ArrayList;
- /*
- * Author: Xiphias3
- * Date: Oct 19, 2010
- * Time: 9:26:05 AM
- */
- public class AudioUtil
- {
- public static long millis2Bytes(long lMillis, AudioFormat format) {
- long lBytes = (long) (lMillis * format.getFrameRate()) / (1000 * format.getFrameSize());
- if (format.getFrameSize() > 1)
- lBytes -= (lBytes % format.getFrameSize());
- return lBytes;
- }
- public static long bytesToNanos(long lBytes, javax.media.format.AudioFormat format) {
- long lFrames = lBytes / (format.getSampleSizeInBits() / 8);
- double dSeconds = ((double) lFrames / format.getSampleRate());
- return (long) (dSeconds * 1000000000.0d);
- }
- public static byte[] mixPCM16(byte[] abOutput, int[] anCalcBuffer, ArrayList<byte[]> pcmStreams) {
- if (pcmStreams.size() == 1)
- return pcmStreams.get(0);
- int nMaxLen = -1;
- for (int i = 0; i < pcmStreams.size(); i++)
- if (pcmStreams.get(i).length > nMaxLen)
- nMaxLen = pcmStreams.get(i).length;
- if ((abOutput == null) || (abOutput.length < nMaxLen))
- abOutput = new byte[nMaxLen];
- if ((anCalcBuffer == null) || (anCalcBuffer.length != (nMaxLen / 2)))
- anCalcBuffer = new int[nMaxLen / 2];
- // Add a little amplification if to the mixed data
- float fAmp = 1.0f + (pcmStreams.size() / 10.0f);
- int nFactor = pcmStreams.size();
- for (int i = 0; i < pcmStreams.size(); i++) {
- byte[] abData = pcmStreams.get(i);
- for (int j = 0, k = 0; j < abData.length; j += 2, k += 1) {
- int nSample = (abData[j] & 0xFF) | (abData[j + 1] << 8);
- anCalcBuffer[k] += nSample;
- }
- }
- for (int i = 0; i < anCalcBuffer.length; i++) {
- anCalcBuffer[i] /= nFactor;
- abOutput[i * 2 + 0] = (byte) (anCalcBuffer[i] & 0xFF);
- abOutput[i * 2 + 1] = (byte) (anCalcBuffer[i] >>> 8);
- for (int j = 0; j < 2; j++) {
- int nVal = (int) (fAmp * abOutput[i * 2 + j]);
- if (nVal < Byte.MIN_VALUE)
- nVal = Byte.MIN_VALUE;
- else if (nVal > Byte.MAX_VALUE)
- nVal = Byte.MAX_VALUE;
- abOutput[i * 2 + j] = (byte) nVal;
- }
- }
- return abOutput;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement