目前我正在做一个关于"延迟听觉反馈"(DAF(的项目。基本上,我想用麦克风录制声音,延迟一段特定的时间,然后播放。使用大约200毫秒的延迟和一个戴耳机的人,这种反馈会关闭人们流利说话的能力。(相当有趣:DAF在youtube上(
现在,我正试图使用256字节的byte[]缓冲区,使用SourceDataLine和TargetDataLine进行循环。如果缓冲区变大,延迟也会变大。我现在的问题是:我不知道以毫秒为单位的延迟是多少
有没有办法根据缓冲区的大小来计算以毫秒为单位的实际延迟?或者有没有其他方法可以得到这个结果?
这就是我现在的循环:
private int mBufferSize; // 256
private TargetDataLine mLineOutput;
private SourceDataLine mLineInput;
public void run() {
... creating the DataLines and getting the lines from AudioSystem ...
// byte buffer for audio
byte[] data = new byte[mBufferSize];
// start the data lines
mLineOutput.start();
mLineInput.start();
// start recording and playing back
while (running) {
mLineOutput.read(data, 0, mBufferSize);
mLineInput.write(data, 0, mBufferSize);
}
... closing the lines and exiting ...
}
您可以很容易地计算延迟,因为它取决于音频的采样率。假设这是CD质量(单声道(音频,则采样率为每秒44100个采样。200毫秒是0.2秒,因此44100 X 0.2=8820。
因此,您的音频播放需要延迟8820个样本(或17640个字节(。如果您将录制和播放缓冲区设置为这个大小(17640字节(,那么代码就会变得非常简单。当每个录制缓冲区被填满时,您将其传递给播放;这将实现正好一个缓冲器的持续时间的回放滞后。
Android中存在一些固有的延迟,您应该考虑到这一点,但除此之外。。。
创建一个循环缓冲区。不管多大,只要它足够大,可以容纳N 0个样本。现在用N个"0"个样本来编写它。
在这种情况下,N是(以秒为单位的延迟(*(以赫兹为单位的采样率(。
示例:200ms,16kHz立体声:
0.2s*1600Hz*(2通道(=3200*2个样本=6400个样本
您可能也将使用pcm数据,它是16位的,所以使用short而不是byte。
在用适量的零填充缓冲区后,开始读取扬声器的数据,同时填充麦克风的数据。
PCM Fifo:
public class PcmQueue
{
private short mBuf[] = null;
private int mWrIdx = 0;
private int mRdIdx = 0;
private int mCount = 0;
private int mBufSz = 0;
private Object mSync = new Object();
private PcmQueue(){}
public PcmQueue( int nBufSz )
{
try {
mBuf = new short[nBufSz];
} catch (Exception e) {
Log.e(this.getClass().getName(), "AudioQueue allocation failed.", e);
mBuf = null;
mBufSz = 0;
}
}
public int doWrite( final short pWrBuf[], final int nWrBufIdx, final int nLen )
{
int sampsWritten = 0;
if ( nLen > 0 ) {
int toWrite;
synchronized(mSync) {
// Write nothing if there isn't room in the buffer.
toWrite = (nLen <= (mBufSz - mCount)) ? nLen : 0;
}
// We can definitely read toWrite shorts.
while (toWrite > 0)
{
// Calculate how many contiguous shorts to the end of the buffer
final int sampsToCopy = Math.min( toWrite, (mBufSz - mWrIdx) );
// Copy that many shorts.
System.arraycopy(pWrBuf, sampsWritten + nWrBufIdx, mBuf, mWrIdx, sampsToCopy);
// Circular buffering.
mWrIdx += sampsToCopy;
if (mWrIdx >= mBufSz) {
mWrIdx -= mBufSz;
}
// Increment the number of shorts sampsWritten.
sampsWritten += sampsToCopy;
toWrite -= sampsToCopy;
}
synchronized(mSync) {
// Increment the count.
mCount = mCount + sampsWritten;
}
}
return sampsWritten;
}
public int doRead( short pcmBuffer[], final int nRdBufIdx, final int nRdBufLen )
{
int sampsRead = 0;
final int nSampsToRead = Math.min( nRdBufLen, pcmBuffer.length - nRdBufIdx );
if ( nSampsToRead > 0 ) {
int sampsToRead;
synchronized(mSync) {
// Calculate how many shorts can be read from the RdBuffer.
sampsToRead = Math.min(mCount, nSampsToRead);
}
// We can definitely read sampsToRead shorts.
while (sampsToRead > 0)
{
// Calculate how many contiguous shorts to the end of the buffer
final int sampsToCopy = Math.min( sampsToRead, (mBufSz - mRdIdx) );
// Copy that many shorts.
System.arraycopy( mBuf, mRdIdx, pcmBuffer, sampsRead + nRdBufIdx, sampsToCopy);
// Circular buffering.
mRdIdx += sampsToCopy;
if (mRdIdx >= mBufSz) {
mRdIdx -= mBufSz;
}
// Increment the number of shorts read.
sampsRead += sampsToCopy;
sampsToRead -= sampsToCopy;
}
// Decrement the count.
synchronized(mSync) {
mCount = mCount - sampsRead;
}
}
return sampsRead;
}
}
和你的代码,修改为先进先出。。。我没有使用TargetDataLine/SourceDataLine的经验,所以如果它们只处理字节数组,请将FIFO修改为字节而不是短字节。
private int mBufferSize; // 256
private TargetDataLine mLineOutput;
private SourceDataLine mLineInput;
public void run() {
... creating the DataLines and getting the lines from AudioSystem ...
// short buffer for audio
short[] data = new short[256];
final int emptySamples = (int)(44100.0 * 0.2);
final int bufferSize = emptySamples*2;
PcmQueue pcmQueue = new PcmQueue( bufferSize );
// Create a temporary empty buffer to write to the PCM queue
{
short[] emptyBuf = new short[emptySamples];
Arrays.fill(emptyBuf, (short)emptySamples );
pcmQueue.doWrite(emptyBuf, 0, emptySamples);
}
// start recording and playing back
while (running) {
mLineOutput.read(data, 0, mBufferSize);
pcmQueue.doWrite(data, 0, mBufferSize);
pcmQueue.doRead(data, 0, mBufferSize);
mLineInput.write(data, 0, mBufferSize);
}
... closing the lines and exiting ...
}