pcm如何转码成opus?
已完成private fun sendData(){
opusEncoder?.putData(File(mSDCardPath,"tts_output.pcm").readBytes())
}
private fun enCoderOpus() {
//初始化opus文件存储位置
val file = File(mSDCardPath, "output.opus")
mAudioBos = BufferedOutputStream(FileOutputStream(file))
//初始化opus编码器
opusEncoder = OpusEncoder()
val audioConfig = AudioConfig()
audioConfig.audioChannelCount = 1 //通道数
audioConfig.audioSampleRate = 16000 //采样率
audioConfig.audioFormat = AudioFormat.ENCODING_PCM_16BIT
audioConfig.bufferSize = 2560
opusEncoder?.config(audioConfig)
opusEncoder?.setEncodedDataCallback(object : EncodedDataCallback {
@RequiresApi(Build.VERSION_CODES.O)
override fun onAudioEncodedCallback(
data: ByteArray?,size: Int) {
//opus编码后的数据大小为160
mAudioBos!!.write(data)
}
})
opusEncoder?.start()
}
启动后只有这种结果。
另外请问如果使用startPushingFileToMegaphone的FileInfo的Data[ ]是不是就不用经过opus的转换?data[ ]又有什么要求?
-
建议您参考这篇文章:如何将mp3文件转化为opus文件?(https://sdk-forum.dji.net/hc/zh-cn/articles/16710729420697-%E5%A6%82%E4%BD%95%E5%B0%86mp3%E6%96%87%E4%BB%B6%E8%BD%AC%E5%8C%96%E4%B8%BAopus%E6%96%87%E4%BB%B6) putData接收到数组长度需要是1280. startPushingFileToMegaphone接收到音频数据也需要是opus格式. -
通过concentus编码pcm终于实现我要的效果。效果不是很好,参数需要调整。
FileInputStream fileIn = new FileInputStream(
"C:\\Users\\Downloads\\opus\\audio_file.pcm");
OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_AUDIO);
encoder.setBitrate(16000);
encoder.setSignalType(OpusSignal.OPUS_SIGNAL_AUTO);
encoder.setComplexity(0);
encoder.setUseVBR(false);
int frameSizeMilliseconds = 40;
int encoderSampleRate = 16000;
FileOutputStream fileOut = new FileOutputStream(
"C:\\Users\\Downloads\\opus\\audio_file.opus");
// OpusInfo info = new OpusInfo();
// info.setNumChannels(1);
// info.setSampleRate(encoderSampleRate);
// OpusTags tags = new OpusTags();
// tags.setVendor("Concentus");
// tags.addComment("title", "A test!");
// OpusFile file = new OpusFile(fileOut);
int packetSamples = encoderSampleRate * frameSizeMilliseconds / 1000;
byte[] inBuf = new byte[packetSamples * 2 * 2];
byte[] data_packet = new byte[2560];
long start = System.currentTimeMillis();
// long granulePos = 0;
while (fileIn.available() >= inBuf.length) {
int bytesRead = fileIn.read(inBuf, 0, inBuf.length);
short[] pcm = BytesToShorts(inBuf, 0, inBuf.length);
int bytesEncoded = encoder.encode(pcm, 0, packetSamples, data_packet, 0, 2560);
byte[] packet = new byte[bytesEncoded];
System.arraycopy(data_packet, 0, packet, 0, bytesEncoded);
fileOut.write(packet, 0, packet.length);
请先登录再写评论。
评论
3 条评论