[Java] Java把V3音频文件转化为wav文件的算法 →→→→→进入此内容的聊天室

来自 , 2020-11-01, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/74306eef
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7.  
  8. import org.apache.commons.net.ftp.FTPClient;
  9. import org.apache.commons.net.ftp.FTPClientConfig;
  10. import org.apache.commons.net.ftp.FTPFile;
  11. import com.eshore.ccf.CCFLogger;
  12.  
  13. public class V3FileUtil {
  14.         private String voxfreq = "6000";
  15.  
  16.         public V3FileUtil() {
  17.         }
  18.        
  19.         public void long2Byte(byte[] output, long[] input, int len) {
  20.                 int i, j;
  21.                 for (i = 0, j = 0; j < len; i++, j += 4) {
  22.                         output[j] = (byte) (input[i] & 0xffL);
  23.                         output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
  24.                         output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
  25.                         output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
  26.                 }
  27.         }
  28.  
  29.         public void short2Byte(byte[] output, short sh) {
  30.                 output[0] = (byte) (sh & 0xffL);
  31.                 output[1] = (byte) ((sh >>> 8) & 0xffL);
  32.         }
  33.  
  34.         public short byte2Short(byte b) {
  35.                 return b < 0 ? (short) (b & 0x7F + 128) : b;
  36.         }
  37.  
  38.         public void convert(String v3File , String wavFile) throws Exception{
  39.                 BufferedInputStream fileInput = null;
  40.                 BufferedOutputStream fileOut = null;
  41.                 int[] indsft = { -1, -1, -1,-1, 2, 4, 6, 8 };
  42.  
  43.                 int[] stpsz = { 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55,
  44.                                 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209,
  45.                                 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
  46.                                 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878,
  47.                                 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871,
  48.                                 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635,
  49.                                 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794 };
  50.  
  51.                 /* nibble to bit map */
  52.                 int nbl2bit[][] = { { 0, 0, 0, 0 }, { 0, 0, 0, 1 }, { 0, 0, 1, 0 },
  53.                                 { 0, 0, 1, 1 }, { 0, 1, 0, 0 }, { 0, 1, 0, 1 }, { 0, 1, 1, 0 },
  54.                                 { 0, 1, 1, 1 }, { 1, 0, 0, 0 }, { 1, 0, 0, 1 }, { 1, 0, 1, 0 },
  55.                                 { 1, 0, 1, 1 }, { 1, 1, 0, 0 }, { 1, 1, 0, 1 }, { 1, 1, 1, 0 },
  56.                                 { 1, 1, 1, 1 } };
  57.  
  58.                 /* sign table */
  59.                 int sgns[] = { 1, -1 };
  60.                 File file = new File(v3File);
  61.                 /* step size index */
  62.                 int ssindex = 0;
  63.                         if(file == null) {
  64.                                 CCFLogger.logger.info("没有找到文件:" + v3File);
  65.                                 return ;
  66.                         }
  67.                        
  68.                         fileInput = new BufferedInputStream(new FileInputStream(file));
  69.                         fileOut = new BufferedOutputStream (new FileOutputStream(wavFile));
  70.  
  71.                         long lPCMHDR[] = { 0x46464952, 0, 0x45564157, 0x20746d66, 16,
  72.                                         0x10001, 0, 0, 0x100002, 0x61746164, 0 };
  73.                         lPCMHDR[1] = 4 * file.length() + 0x30; // 文件长度
  74.                         lPCMHDR[6] = Integer.parseInt(voxfreq);
  75.                         lPCMHDR[7] = 2 * Integer.parseInt(voxfreq);
  76.                         lPCMHDR[10] = 4 * file.length();
  77.  
  78.                         // 先写一个头
  79.                         byte[] b = new byte[44];
  80.                         long2Byte(b, lPCMHDR, 44);
  81.                         fileOut.write(b, (int) 0, b.length);
  82.  
  83.                         short iVal = 0;
  84.                         int pos = 0;
  85.                         byte[] szBuf = new byte[128 * 1024];
  86.                         int diff;
  87.                         short incoded;
  88.                         byte[] szDesBuf = new byte[4 * 128 * 1024];
  89.                         int dwRead = 0;
  90.                        
  91.                         while ((dwRead = fileInput.read(szBuf, 0, 128 * 1024))!= -1) {
  92.                                 for (int idx = 0; idx < dwRead - 1; idx++) {
  93.                                         incoded = (short) (byte2Short(szBuf[idx]) / 16);
  94.                                         diff = sgns[nbl2bit[incoded][0]]
  95.                                                         * (stpsz[ssindex] * nbl2bit[incoded][1]
  96.                                                                         + (stpsz[ssindex] / 2)
  97.                                                                         * nbl2bit[incoded][2]
  98.                                                                         + (stpsz[ssindex] / 4)
  99.                                                                         * nbl2bit[incoded][3] + (stpsz[ssindex] / 8));
  100.  
  101.                                         ssindex = ssindex + indsft[(incoded % 8)];
  102.  
  103.                                         if (ssindex < 0)
  104.                                                 ssindex = 0;
  105.                                         if (ssindex > 48)
  106.                                                 ssindex = 48;
  107.                                         iVal += diff;
  108.  
  109.                                         if (iVal > 2047)
  110.                                                 iVal = 2047;
  111.                                         else if (iVal < -2047)
  112.                                                 iVal = -2047;
  113.                                        
  114.                                         byte[] b2 = new byte[2];
  115.                                         short2Byte(b2, (short) (iVal * 16));
  116.  
  117.                                         szDesBuf[pos] = b2[0];
  118.                                         pos++;
  119.                                         szDesBuf[pos] = b2[1];
  120.                                         pos++;
  121.                                        
  122.                                         // /////////////////////////////////////////////////
  123.                                         incoded = (short) (byte2Short(szBuf[idx]) % 16);
  124.  
  125.                                         diff = sgns[nbl2bit[incoded][0]]
  126.                                                         * (stpsz[ssindex] * nbl2bit[incoded][1]
  127.                                                                         + (stpsz[ssindex] / 2)
  128.                                                                         * nbl2bit[incoded][2]
  129.                                                                         + (stpsz[ssindex] / 4)
  130.                                                                         * nbl2bit[incoded][3] + (stpsz[ssindex] / 8));
  131.  
  132.                                         ssindex = ssindex + indsft[(incoded % 8)];
  133.  
  134.                                         if (ssindex < 0)
  135.                                                 ssindex = 0;
  136.                                         if (ssindex > 48)
  137.                                                 ssindex = 48;
  138.                                         iVal += diff;
  139.                                         //波长
  140.                                         if (iVal > 2047)
  141.                                                 iVal = 2047;
  142.                                         else if (iVal < -2047)
  143.                                                 iVal = -2047;
  144.  
  145.                                         byte[] b3 = new byte[2];
  146.                                         short2Byte(b3, (short) (iVal * 16));
  147.                                         szDesBuf[pos] = b3[0];
  148.                                         pos++;
  149.                                         szDesBuf[pos] = b3[1];
  150.                                         pos++;
  151.                                 }
  152.                                 try {
  153.                                         fileOut.write(szDesBuf, 0,  dwRead * 4 );
  154.                                         fileOut.flush();
  155.                                 } catch (Exception e) {
  156.                                         CCFLogger.logger.error("转换文件失败!", e);
  157.                                 }
  158.                                 pos = 0;
  159.                         }
  160.                         CCFLogger.logger.error("转换文件到wav成功.......................");
  161.                         try {
  162.                                 if (fileInput != null) fileInput.close();
  163.                                 if (fileOut != null) fileOut.close();
  164.                         } catch (IOException e) {}
  165.                         return ;
  166.         }
  167.  
  168. }
  169. //java/5530

回复 "Java把V3音频文件转化为wav文件的算法"

这儿你可以回复上面这条便签

captcha