Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.47 KB | None | 0 0
  1. package xiphias3.util;
  2.  
  3. import javax.sound.sampled.AudioFormat;
  4. import java.util.ArrayList;
  5.  
  6. /*
  7.  * Author: Xiphias3
  8.  * Date:   Oct 19, 2010
  9.  * Time:   9:26:05 AM
  10.  */
  11.  
  12. public class AudioUtil
  13. {
  14.     public static long millis2Bytes(long lMillis, AudioFormat format) {
  15.         long lBytes = (long) (lMillis * format.getFrameRate()) / (1000 * format.getFrameSize());
  16.         if (format.getFrameSize() > 1)
  17.             lBytes -= (lBytes % format.getFrameSize());
  18.         return lBytes;
  19.     }
  20.  
  21.     public static long bytesToNanos(long lBytes, javax.media.format.AudioFormat format) {
  22.         long lFrames = lBytes / (format.getSampleSizeInBits() / 8);
  23.         double dSeconds = ((double) lFrames / format.getSampleRate());
  24.         return (long) (dSeconds * 1000000000.0d);
  25.     }
  26.  
  27.     public static byte[] mixPCM16(byte[] abOutput, int[] anCalcBuffer, ArrayList<byte[]> pcmStreams) {
  28.         if (pcmStreams.size() == 1)
  29.             return pcmStreams.get(0);
  30.  
  31.         int nMaxLen = -1;
  32.         for (int i = 0; i < pcmStreams.size(); i++)
  33.             if (pcmStreams.get(i).length > nMaxLen)
  34.                 nMaxLen = pcmStreams.get(i).length;
  35.  
  36.         if ((abOutput == null) || (abOutput.length < nMaxLen))
  37.             abOutput = new byte[nMaxLen];
  38.  
  39.         if ((anCalcBuffer == null) || (anCalcBuffer.length != (nMaxLen / 2)))
  40.             anCalcBuffer = new int[nMaxLen / 2];
  41.  
  42.         // Add a little amplification if to the mixed data
  43.         float fAmp = 1.0f + (pcmStreams.size() / 10.0f);
  44.         int nFactor = pcmStreams.size();
  45.         for (int i = 0; i < pcmStreams.size(); i++) {
  46.             byte[] abData = pcmStreams.get(i);
  47.             for (int j = 0, k = 0; j < abData.length; j += 2, k += 1) {
  48.                 int nSample = (abData[j] & 0xFF) | (abData[j + 1] << 8);
  49.                 anCalcBuffer[k] += nSample;
  50.             }
  51.         }
  52.  
  53.         for (int i = 0; i < anCalcBuffer.length; i++) {
  54.             anCalcBuffer[i] /= nFactor;
  55.             abOutput[i * 2 + 0] = (byte) (anCalcBuffer[i] & 0xFF);
  56.             abOutput[i * 2 + 1] = (byte) (anCalcBuffer[i] >>> 8);
  57.  
  58.             for (int j = 0; j < 2; j++) {
  59.                 int nVal = (int) (fAmp * abOutput[i * 2 + j]);
  60.                 if (nVal < Byte.MIN_VALUE)
  61.                     nVal = Byte.MIN_VALUE;
  62.                 else if (nVal > Byte.MAX_VALUE)
  63.                     nVal = Byte.MAX_VALUE;
  64.                 abOutput[i * 2 + j] = (byte) nVal;
  65.             }
  66.         }
  67.  
  68.         return abOutput;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement