当前位置:   article > 正文

科大讯飞语音开发包上手体验(2)_讯飞离线生成的pcm播放不了

讯飞离线生成的pcm播放不了

第一篇文章已经可以让电脑发出声音.
http://blog.csdn.net/a897180673/article/details/78758055

但是有的时候我们希望将声音保存下来,这样用播放软件打开就可以直接的播放,
但是用sdk 合成的声音是pcm格式的 用播放器直接打开是 无法播放的.在网上搜了下 解决方法

找到了一种解决办法

1.首先使用科大讯飞的无声合成,也就是合成声音到文件

上代码


import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SpeechUtility;
import com.iflytek.cloud.speech.SynthesizeToUriListener;

public class SaveTextToFile {



    public static void main(String[] args) {
        SpeechUtility.createUtility( SpeechConstant.APPID +"=你的APPID");

        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 //3.开始合成 //设置合成音频保存位置(可自定义保存位置),默认保存在“./tts_test.pcm” 
        mTts.synthesizeToUri("语音合成测试程序", "./666.pcm",synthesizeToUriListener); //合成监听器 
    }

    static 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 arg0, int arg1, int arg2, int arg3, Object arg4, Object arg5) {
            // TODO Auto-generated method stub
        } }; 
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

记得把App ID换成 自己注册的ID

运行后 就会在java 工程的目录下面生成一个 666.pcm 文件

这里写图片描述

2.用软件根本就打开不这个文件 ,都会提示无法解析

3.下面我们新建一个工具类 把这个文件 转成 可以让 播放器识别的声音文件 并播放

上 工具类的代码


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class Util {

    public static void main(String[] args) {
        try {
            //填写两个参数 输入文件 和输出文件
            convertAudioFiles("h:\\666.pcm", "H:\\888.wav");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void convertAudioFiles(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();


           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!");
        }


        static 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);
           }
        }
        }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129

运行代码后 到H盘 去查找文件 就可以用 软件 打开 并 播放啦

注意上面 有比特率 声道 等参数 注意 填写正确 ,要不然 可以播放声音,但是声音会变

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/830485
推荐阅读
相关标签
  

闽ICP备14008679号