Conversion from PCM to WAV (Java)

Source: Internet
Author: User

Another product of the company, when monitoring CTI, the recording format is PCM.
To play in IE, You need to convert it into a standard WAV. previously, I used a com library for conversion. Now I can't use that library for various reasons (64-bit support is not good), So I implemented it in Java. The basic code is as follows.
In fact, WAV is only a PCM file with the relevant file header and description information, not complicated. This program references some implementations on the Internet (C/C ++) and modifies some of the errors. For example, the converted file can be played but the length of time is incorrect; or you cannot drag it.

For the code, see.

Private void convertaudiofiles (string SRC, string target) throws exception {
Fileinputstream FCM = new fileinputstream (SRC );
Fileoutputstream Fos = new fileoutputstream (target );

// Calculate the length
Byte [] Buf = new byte [1024*4];
Int size = FCM. Read (BUF );
Int pcmsize = 0;
While (size! =-1 ){
Pcmsize + = size;
Size = FCM. Read (BUF );
}
FCM. Close ();


// Enter parameters, bit rate, and so on. Here we use 16-bit single-channel 8000Hz
Waveheader header = new waveheader ();
// Length field = content size (pcmsize) + header field size (excluding the first 4-byte identifier riff and the 4-byte filelength)
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 header should be 44 bytes
// Write Header
FOS. Write (H, 0, H. Length );
// Write data stream
FS = new fileinputstream (SRC );
Size = FCM. Read (BUF );
While (size! =-1 ){
FOS. Write (BUF, 0, size );
Size = FCM. Read (BUF );
}
FCM. Close ();
FOS. Close ();
System. Out. println ("Convert OK! ");
}

Wavheader helper class. 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 <16)> 24 );
Mybyte [0] = (byte) (S <24)> 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 <16)> 24 );
Buf [0] = (byte) (n <24)> 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.