讯飞文字转PCM语音 PCM转WAV WAV转AMR AMR转MP3

来源:互联网 发布:热血传奇首饰钢纹数据 编辑:程序博客网 时间:2024/06/06 02:51

讯飞文字转PCM语音 PCM转WAV WAV转AMR AMR转MP3

需要讯飞在线语音的jar (Msc.jar json-jena-1.0.jar 平台上下载 注册账号 appid)

jave的jar (jave-1.0.2.jar)

讯飞 文字转PCM格式语音

SpeechUtility.createUtility("appid=???");        //1.创建SpeechSynthesizer对象        SpeechSynthesizer mTts= SpeechSynthesizer.createSynthesizer( );        //2.合成参数设置,详见《MSC Reference Manual》SpeechSynthesizer 类        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");//设置发音人        mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速,范围0~100        mTts.setParameter(SpeechConstant.PITCH, "50");//设置语调,范围0~100        mTts.setParameter(SpeechConstant.VOLUME, "50");//设置音量,范围0~100        String pcm =  "D:/upload/test/" + System.currentTimeMillis() + ".pcm";        //合成监听器        SynthesizeToUriListener synthesizeToUriListener = new SynthesizeToUriListener() {            //progress为合成进度0~100             public void onBufferProgress(int progress) {}            //会话合成完成回调接口            //uri为合成保存地址,error为错误信息,为null时表示合成会话成功            public void onSynthesizeCompleted(String uri, SpeechError error) {}            @Override            public void onEvent(int i, int j, int k, int l, Object obj, Object obj1) {                // TODO Auto-generated method stub            }        };        //3.开始合成        //设置合成音频保存位置(可自定义保存位置),默认保存在“./tts_test.pcm”        mTts.synthesizeToUri("测试一下,我要上天", pcm,synthesizeToUriListener);

PCM转WAV

public static void main(String[] args) {        try {            //pcmToWav("D:/upload/voice/20170711/1499755596428.pcm","D:/upload/voice/20170711/1499755596428.wav");            pcmToWav("D:/upload/test/1499913535283.pcm","D:/upload/test/b.wav");        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public static void pcmToWav(String src, String target) throws Exception {        FileInputStream fis = new FileInputStream(src);        FileOutputStream fos = new FileOutputStream(target);        // 计算长度        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();        // 填入参数,比特率等等。这里用的是16位单声道 8000 hz        WaveHeader header = new WaveHeader();        // 长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节)        header.fileLength = PCMSize + (44 - 8);        header.FmtHdrLeth = 16;        header.BitsPerSample = 16;        header.Channels = 1;        header.FormatTag = 0x0001;        header.SamplesPerSec = 16000;        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标准,头部应该是44字节        // 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!");    }
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);        }    }}

WAV转AMR

String sourcePath = "D:/upload/test/b.wav";        String targetPath = "D:/upload/test/b.amr";        File source = new File(sourcePath);          File target = new File(targetPath);          AudioAttributes audio = new AudioAttributes();        audio.setCodec("libamr_nb");  //编解码器        audio.setBitRate(new Integer(12200));  //比特率        audio.setChannels(new Integer(1));  //渠道        audio.setSamplingRate(new Integer(8000));   //采样率        EncodingAttributes attrs = new EncodingAttributes();        attrs.setFormat("amr");        attrs.setAudioAttributes(audio);        Encoder encoder = new Encoder();        try {              encoder.encode(source, target, attrs);          } catch (IllegalArgumentException e) {              e.printStackTrace();          } catch (InputFormatException e) {              e.printStackTrace();          } catch (EncoderException e) {              e.printStackTrace();          }  

AMR TO MP3

public static void changeToMp3(String sourcePath, String targetPath) {          File source = new File(sourcePath);          File target = new File(targetPath);          AudioAttributes audio = new AudioAttributes();          Encoder encoder = new Encoder();          audio.setCodec("libmp3lame");          EncodingAttributes attrs = new EncodingAttributes();          attrs.setFormat("mp3");          attrs.setAudioAttributes(audio);          try {              encoder.encode(source, target, attrs);          } catch (IllegalArgumentException e) {              e.printStackTrace();          } catch (InputFormatException e) {              e.printStackTrace();          } catch (EncoderException e) {              e.printStackTrace();          }      }  
原创粉丝点击