java - 在 Android 中将循环音频与动画精确同步

标签 java android audio audiotrack opensl

问题: 有没有什么方法可以使用 AudioTrack 和 setLoopPoints() 来配置一个基于每毫秒样本/帧的精度的循环?

编辑:我明白,从大多数 Android 设备所拥有的处理能力来看,不能指望完美的准确性。但是,我希望平均循环时间接近节奏的“真实”间隔(以毫秒为单位),因为这就是我基于“动画”的方式,它也应该与节奏同步(动画是重绘的 SurfaceView节奏间隔期间的一条线的坐标)。

详细信息: 我正在尝试将 AudioTrack 与 setLoopPoints 结合使用来创建准确的节拍器。为此,我使用两个 wav 文件(Tick 和 Tock)来填充 byte[] 数组以馈送到 AudioTrack。考虑一个 4/4 时间的例子,我将填充一个 byte[],一次是从 [0] 开始的 Tick,三次是 Tock(使用 arrayCopy())到 [length/4],[length/2],和[3*length/4],并假设wav数据不会相互重叠。

我的代码的粗略示例:

// read each wav file's header into its own ByteBuffer (with LITTLE_ENDIAN order)
// ... then get the size of the audio data
tickDataSize = tickHeaderBuffer.getInt(40); 
tockDataSize = tockHeaderBuffer.getInt(40); 

// allocate space for one loop at the current tempo into a byte[] array (to be given to    AudioTrack) 
// e.g. 22050hz * 2 Bytes (1 per channel) * 2 Bytes (1 per 8 bit PCM sample) = 22050*4 Bytes/second
//      If my tempo were 60 BPM I'd have 88200 Bytes/second (where 1 second is the whole loop interval);
// 110 BPM = 48109.0909091 Bytes per loop interval (where 1 loop interval is 0.54545 seconds)

int tempo = 110;
int bytesPerSecond  = sampleRate * 2 * 2;
int bytesPerInterval = (int)((((float)bytesPerSecond * 60.0F)/(float)tempo) * 4); 

byte[] wavData = new byte[bytesPerInterval];

// ... then fill wavData[] as mentioned above with 1*wavOne and 3*wavTwo

// Then feed to an instance of AudioTrack and set loop points
audioTrack.write(wavData, 0, bytesPerInterval);
int numSamples = bytesPerInterval/4;
audioTrack.setLoopPoints(0, numSamples, -1);
audioTrack.play();

希望您已经开始看到问题所在。 在某些节奏下,我只能在循环中播放静态音乐(但仅限于第 1 和第 3 个 Tock [循环中的第 2 和第 4 个样本])。

静电停止,如果我:

  • 不要用任何 wav 数据填充 byte[] 但保留 bytesPerInterval 和 numSamples 相同(正确持续时间的无声循环)。
  • 设置 bytesPerInterval = bytesPerInterval % 4(因此失去速度准确性)

工作(非静态)和非工作(静态)节奏的示例及其所需的帧数(考虑一秒 = 88200 帧):

tempo: 110 (static)
wavData.length: 192436 
numFrames: 48109

tempo: 120 (no static)
wavData.length: 176400 
numFrames: 44100

tempo: 130 (static)
wavData.length: 162828 
numFrames: 40707

tempo: 140 (no static)
wavData.length: 151200 
numFrames: 37800

tempo: 150 (no static)
wavData.length: 141120 
numFrames: 35280

tempo: 160 (static)
wavData.length: 132300 
numFrames: 33075

tempo: 170 (static)
wavData.length: 124516 
numFrames: 31129

tempo: 180 (no static)
wavData.length: 117600 
numFrames: 29400

如果问题的答案是“不,您不能使用 setLoopPoints() 来配置精确到任何毫秒的循环”,那么我想知道任何其他选项。 NDK、SoundPool 或 MediaPlayer 中的 OpenSL ES 是否更适合生成精确循环?

编辑 2:我缩小了导致静态问题的位置:

// Assume a tempo of 160 BPM which requires byte[132300]
wavStream1 = this.context.getResources().openRawResource(R.raw.tick);
wavStream2 = this.context.getResources().openRawResource(R.raw.tock);

ByteBuffer headerBuffer1 = ByteBuffer.allocate(44);
ByteBuffer headerBuffer2 = ByteBuffer.allocate(44);
headerBuffer1.order(ByteOrder.LITTLE_ENDIAN);
headerBuffer2.order(ByteOrder.LITTLE_ENDIAN);
wavStream1.read(headerBuffer1.array(), 0, 44);   
wavStream2.read(headerBuffer2.array(), 0, 44);
int tickDataSize = headerBuffer1.getInt(40); 
int tockDataSize = headerBuffer2.getInt(40);    

byte[] wavData = new byte[bytesPerInterval * 4];

byte[] tickWavData = new byte[bytesPerInterval];
byte[] tockWavData = new byte[bytesPerInterval];

wavStream1.read(accentWavData, 0, tickDataSize);
wavStream2.read(normalWavData, 0, tockDataSize);

System.arraycopy(tickWavData, 0, wavData, 0, bytesPerInterval);
System.arraycopy(tockWavData, 0, wavData, 33075, bytesPerInterval);
System.arraycopy(tockWavData, 0, wavData, 66150, bytesPerInterval);
System.arraycopy(tockWavData, 0, wavData, 99225, bytesPerInterval);
// bytesPerInterval of 33075 and 99225 (or any odd number) will be 
// static when wavData is played 

AudioTrack audioTrack = new AudioTrack(3, 22050, 12, 2, wavData.length, 0);
audioTrack.write(wavData, 0, wavData.length);
audioTrack.setLoopPoints(0, bytesPerInterval, -1);
audioTrack.play();  

最重要的是,我想了解为什么从 wavData 的奇数索引开始的音频数据会生成静态而不是预期的声音,以及是否有任何补救措施。

最佳答案

阅读您的编辑后,我认为奇数索引导致问题的原因是因为您正在使用 ENCODING_PCM_16BIT(您在构造函数中传递的“2”)创建 AudioTrack ).这意味着每个样本都应该是 16 位。如果样本是 8 位,请尝试使用“3”或 ENCODING_PCM_8BIT

关于java - 在 Android 中将循环音频与动画精确同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671478/

相关文章:

audio - JavaME:如何检测耳机插件和插入?

android - 录制时如何动态绘制图形?

java - 如何将从数据库收集的数据存储在对象数组中?

java - Collections.synchronizedList 传递到构造函数中

android - Android Studio 中的 "There is no default constructor available in android.database.sqlite.SQLitepenhelper"

java - 如何将 .so 文件打包到 jar 中?

python - audioop的 "sound fragment"参数是什么类型的文件?

java - 动态更改 JSESSIONID cookie 的域?

java - 如果在 Hibernate 中不存在,则创建一个表

android - 如何均衡按钮之间的空间?