在不降低音频质量的情况下将短数组从音频记录转换为字节数组? [英] Converting Short array from Audio Record to Byte array without degrading audio quality?

查看:77
本文介绍了在不降低音频质量的情况下将短数组从音频记录转换为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Audio Record类录制音频.录音是PCM 16位的,所以(我听说)最好让Audio Record将数据写入Short数组.但是,对于我要执行的操作,我需要将Short数组转换为字节数组.我尝试了几种方法,但是所有方法都会以不同的方式降低音频质量.

I'm recording audio using the Android Audio Record class. The recording is PCM 16bit so (I heard) it is best to let Audio Record write the data into a Short array. However for what I'm trying to do I need to convert the Short array into a byte array. I have tried a few methods but all of them degrades the quality of the audio in different ways.

我第一次尝试:

byte[] bytes2 = new byte[shortsA.length * 2];
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortsA);

但是结果是非常安静和断断续续的音频.

but the result is very quiet and choppy audio.

我尝试的第二种方法是:

The second method I tried is:

byte[] MyShortToByte(short[] buffer) {
int N = buffer.length;
float f[] = new float[N];
float min = 0.0f;
float max = 0.0f;
for (int i=0; i<N; i++) {
     f[i] = (float)(buffer[i]);
     if (f[i] > max) max = f[i];
     if (f[i] < min) min = f[i];
     }
float scaling = 1.0f+(max-min)/256.0f; // +1 ensures we stay within range and guarantee no divide by zero if sequence is pure silence ...

ByteBuffer byteBuf = ByteBuffer.allocate(N);
for (int i=0; i<N; i++) {
    byte b = (byte)(f[i]/scaling);  /*convert to byte. */
    byteBuf.put(b);
}
return byteBuf.array();
}

来自 https://stackoverflow.com/a/15188181/902631 ,但结果加快了速度,音调音频,就像原始音频以2倍的速度快进一样.但是音量比第一个高,并且不断断续续.

from https://stackoverflow.com/a/15188181/902631 but the result is sped up and high-pitched audio, like the original audio was fast-forwarded at 2x speed. However the volume is higher than the first one and it's not choppy.

是否可以使用任何外部库或不降低音频质量的特定于音频的转换方法?

Are there any external libraries that I can use or any audio-specific conversion methods that don't degrade the quality of the audio?

任何帮助将不胜感激!

推荐答案

为什么不尝试此操作,当从AudioRecord对象读取数据时,将其读入byte []数组而不是短数组

Why don't you try this, While read data from AudioRecord object, read it into byte[] array instead of short array

audioRecord.startRecording();
byte[] buffer = new byte[_bufferSize]; //recorded data is stored in this byte array
audioRecord.read(buffer, 0, _bufferSize);

这篇关于在不降低音频质量的情况下将短数组从音频记录转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆