Voice files pcm silent judgment

Reprinted: http://www.voidcn.com/relative/p-fwdkigvh-bro.html

pcm file is stored in the original sound wave binary stream, there is no head.

(1) First, to confirm the number of bits per sample pcm data sample file, typically 8bit or 16bit.

(2) then determines whether or two-channel mono, two-channel data are alternately arranged two channels, it is necessary to extract data for each channel individually.

(3) There is no sign bit is then determined, such as point sampling 16bit signed bit range of -32768 to 32767

(4) determining the current operating system's memory is big-endian mode, or small side storage. Specifically to see http://blog.csdn.net/u013378306/article/details/78904238

(5) According to the above four pairs of analyzing pcm file, the file is converted to decimal

NOTE: For 1-3 cooledit tool may be used to set parameters in the playback file pcm windows to determine the specific parameters, the following java code may be used for testing:

The present example is a voice: silence one second, then say "hello" and silence two seconds. pcm files download path: http: //download.csdn.net/download/u013378306/10175068

package test;
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
  
import javax.sound.sampled.AudioFormat;  
import javax.sound.sampled.AudioSystem;  
import javax.sound.sampled.DataLine;  
import javax.sound.sampled.LineUnavailableException;  
import javax.sound.sampled.SourceDataLine;  
  
public class test {  
  
    /** 
     * @param args 
     * @throws Exception 
     */  
    public static void main(String[] args) throws Exception {  
        // TODO Auto-generated method stub  

            File file = new File("3.pcm");  
            System.out.println(file.length());  
            int offset = 0;  
            int bufferSize = Integer.valueOf(String.valueOf(file.length())) ;  
            byte[] audioData = new byte[bufferSize];  
            InputStream in = new FileInputStream(file);  
            in.read (audiodata);  
  
              
              
            float= 20000 the sampleRate ;  
             int sampleSizeInBits = 16 ;  
             int channels =. 1 ;  
             Boolean Signed = to true ;  
             Boolean the bigEndian = to false ;  
             // the sampleRate - the number of samples per second  
             // sampleSizeInBits - bits per sample  
             // channels - channel number (1 for mono, 2 for stereo)  
             // signed - indicating that the data is signed or unsigned  
             // the bigEndian - whether big-endian is stored, indicating whether big-endian byte order in a single sample data (false means  
             // Little-endian).  
            AudioFormat af = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);  
            SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, af, bufferSize);  
            SourceDataLine sdl = (SourceDataLine) AudioSystem.getLine(info);  
            sdl.open(af);  
            sdl.start();  
                        for(int i=0;i<audioData.length;i++)
                            audioData[i]*=1;
            while (offset < audioData.length) {  
                offset += sdl.write(audioData, offset, bufferSize);
            }  
    }
  

} 

If the test parameters can be determined by parsing the pcm file, the following java code for each sampling data of 16bits, mono pcm, 10 parses the binary files at the operating system stored in memory is little-endian.

Package Test; 

Import java.io.File;
 Import a java.io.FileInputStream;
 Import java.io.FileWriter;
 Import a java.io.InputStream;
 Import java.math.BigInteger; 

public  class FFFF { 

    / ** 
     * 16bits sample bits is , store small end, mono resolved to decimal file 
     * @param args
      * / 
    public  static  void main (String [] args) {
         the try { 
            file file = new new ( "3.pcm" file ); 
            the System.out. println (file.length ()); 
            the System.out. the println (file.length ()); 
            int bufferSize = Integer.valueOf(String.valueOf(file.length()));
            byte[] buffers = new byte[bufferSize];
            InputStream in = new FileInputStream(file);
            in.read(buffers);
            String rs = "";
            for (int i = 0; i < buffers.length; i++) {
                byte[] bs = new byte[2];
                bs[0]=buffers[i+1];//小端存储,
                bs[1]=buffers[i];
                int s = Integer.valueOf(binary(bs, 10));
                i = i + 1;
                rs += " " + s;

            }
            writeFile(rs);
            in.close();

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void writeFile(String s) {
        try {

            FileWriter fw = new FileWriter("hello3.txt");

            fw.write(s, 0, s.length());
            fw.flush ();
            fw.close();

        } The catch (Exception E) { 
            e.printStackTrace (); 
        } 

    } 

    public  static String binary ( byte [] bytes, int the radix) {
         return  new new a BigInteger (bytes) .toString (the radix); // where a represents a positive number 
    } 
}

After execution can view hello.txt, a start amplitude is small can be seen, as follows, substantially less than 100:

-15 -12 -18 -24 -17 -8 -8 -17 -22 -14 -5 -18 -47 -67 -60 -41 -28 -28 -23 -12 -6 -9 -13 -8 0 6 21 49 68 48 -2 -43 -47 -32 -22 -10 22 56 

But to say hello when the amplitude becomes very large:

 -2507 -2585 -2600 -2596 -2620 -2670 -2703 -2674 -2581 -2468 -2378 -2305 -2200 -2018 -1774 -1523 -1307 -1127 -962 -806 -652 -505 -384 -313 -281 -241 -163 

Then silence two seconds, and the amplitude change is very small:

5 3 0 -4 -5 -6 -6 -7 -7 -8 -9 -8 -10 -10 -11 -10 -11 -11 -11 -11 -11 -11 -10 -9 -7 -6 -3 -2 -2 -3 -3 -3 -1 2 4 4

Specific waveform diagram display python code may be used:

Import numpy AS NP
 Import pylab AS PL
 Import Math
 Import the codecs 
File = codecs.open ( " hello3.txt " , " R & lt " ) // Code description file = codecs.open ( "hello3.txt", "rb"), b It is binary, read in binary mode, is wrong. 
Lines = "  " 
for Line in file.readlines (): 
    Lines = + Lines Line 
YS = lines.split ( "  "0
max1=pow(2,16)-1
for y in ys:
    if y.strip()=="":
        continue
    yss.append(y)

for index in range(len(yss)):

    y1=yss[index]

    i+=1;
    y=int(y1)

    ays.append(y)
    axs.append(i)
#print  i
file.close()
pl.plot(axs, ays,"ro")# use pylab to plot x and y
pl.show()# show the plot on the screen

A waveform of FIG.

 Here audacity audio amplitude results presented anastomosis, but here the amplitude amplifying order to observe with the naked eye.

 

Guess you like

Origin www.cnblogs.com/passedbylove/p/11429213.html