Conversion of PCM to WAV (Java) __java

Source: Internet
Author: User
Tags int size

Another product of the company, when monitoring CTI, the format of recording is PCM.
In order to be able to play in IE, you need to convert to a standard WAV. Used to be a COM library to do the conversion, now for a variety of reasons can not use the library (64-bit support is not good, etc.), the implementation of a java. The basic code is as follows.
In fact, WAV is only PCM file plus related file headers and descriptive information, not complicated. This program refers to some of the online implementations (c + +), modified some of these errors, such as the converted file can play but the length of time is wrong, or can not drag and so on.

code see below.

private void Convertaudiofiles (string src, string target) throws Exception {
FileInputStream fis = new FileInputStream (SRC);
FileOutputStream fos = new FileOutputStream (target);

Calculate length
byte[] buf = new byte[1024 * 4];
int size = Fis.read (BUF);
int pcmsize = 0;
while (size!=-1) {
Pcmsize + = size;
Size = Fis.read (BUF);
}
Fis.close ();


Fill in the parameters, bit rate, and so on. This is a 16-bit mono 8000 Hz.
Waveheader Header = new Waveheader ();
Length field = content Size (pcmsize) + header field size (excluding previous 4-byte identifiers riff and Filelength itself 4 bytes)
Header.filelength = Pcmsize + (44-8);
Header. Fmthdrleth = 16;
Header. BitsPerSample = 16;
Header. channels = 1;
Header. Formattag = 0x0001;
Header. SamplesPerSec = 8000;
Header. Blockalign = (short) header. Channels * header. BITSPERSAMPLE/8);
Header. Avgbytespersec = header. Blockalign * header. SamplesPerSec;
Header. Datahdrleth = pcmsize;

byte[] H = header.getheader ();

Assert h.length = = 44; WAV Standard, the head should be 44 bytes
Write header
Fos.write (h, 0, h.length);
Write data stream
FIS = new FileInputStream (SRC);
Size = Fis.read (BUF);
while (size!=-1) {
Fos.write (buf, 0, size);
Size = Fis.read (BUF);
}
Fis.close ();
Fos.close ();
System.out.println ("Convert ok!");
}

Wavheader helper classes. Used to generate header information.

public class Waveheader {
Public final char fileid[] = {' R ', ' I ', ' f ', ' F '};
public int filelength;
Public char wavtag[] = {' W ', ' A ', ' V ', ' E '};;
Public char fmthdrid[] = {' F ', ' m ', ' t ', '};
public int fmthdrleth;
public short Formattag;
public short channels;
public int samplespersec;
public int avgbytespersec;
public short blockalign;
public short bitspersample;
Public char datahdrid[] = {' d ', ' a ', ' t ', ' a '};
public int datahdrleth;

Public byte[] GetHeader () throws IOException {
Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
Writechar (Bos, Fileid);
Writeint (Bos, filelength);
Writechar (Bos, Wavtag);
Writechar (Bos, Fmthdrid);
Writeint (Bos,fmthdrleth);
Writeshort (Bos,formattag);
Writeshort (Bos,channels);
Writeint (BOS,SAMPLESPERSEC);
Writeint (BOS,AVGBYTESPERSEC);
Writeshort (bos,blockalign);
Writeshort (bos,bitspersample);
Writechar (Bos,datahdrid);
Writeint (Bos,datahdrleth);
Bos.flush ();
Byte[] r = Bos.tobytearray ();
Bos.close ();
return R;
}

private void Writeshort (Bytearrayoutputstream bos, int s) throws IOException {
byte[] MyByte = new byte[2];
MYBYTE[1] = (byte) ((S <<) >> 24);
Mybyte[0] = (byte) ((S <<) >> 24);
Bos.write (MyByte);
}


private void Writeint (bytearrayoutputstream bos, int n) throws IOException {
byte[] buf = new Byte[4];
BUF[3] = (byte) (n >> 24);
BUF[2] = (byte) ((n << 8) >> 24);
BUF[1] = (byte) ((n <<) >> 24);
Buf[0] = (byte) ((n <<) >> 24);
Bos.write (BUF);
}

private void Writechar (Bytearrayoutputstream bos, char[] ID) {
for (int i=0; i<id.length; i++) {
char C = id[i];
Bos.write (c);
}
}
}


Source: http://hi.baidu.com/lff0305/blog/item/7d8ba938246f22cfd5622596.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.