讯飞文字转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=???");
SpeechSynthesizer mTts= SpeechSynthesizer.createSynthesizer( );
mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
mTts.setParameter(SpeechConstant.SPEED, "50");
mTts.setParameter(SpeechConstant.PITCH, "50");
mTts.setParameter(SpeechConstant.VOLUME, "50");
String pcm = "D:/upload/test/" + System.currentTimeMillis() + ".pcm";
SynthesizeToUriListener synthesizeToUriListener = new SynthesizeToUriListener() {
public void onBufferProgress(int progress) {}
public void onSynthesizeCompleted(String uri, SpeechError error) {}
@Override
public void onEvent(int i, int j, int k, int l, Object obj, Object obj1) {
}
};
mTts.synthesizeToUri("测试一下,我要上天", pcm,synthesizeToUriListener);
PCM转WAV
public static void main(String[] args) {
try {
pcmToWav("D:/upload/test/1499913535283.pcm","D:/upload/test/b.wav");
} catch (Exception e) {
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();
WaveHeader header = new WaveHeader();
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;
fos.write(h, 0, h.length);
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();
}
}