網頁

2018年7月3日 星期二

tensorflow audio recognition 之 SpeechActivity.java

分為 record thread 和 recognize thread, 兩個 thread 依靠 recordingBuffer 交換資料
兩者速度不會一致,所以 recognize thread 可能重複 recognize, 也可能漏

short[] recordingBuffer = new short[RECORDING_LENGTH];
int recordingOffset = 0;

private void record() {
  int numberRead = record.read(audioBuffer, 0, audioBuffer.length);
  int maxLength = recordingBuffer.length;
  int newRecordingOffset = recordingOffset + numberRead;
  //int secondCopyLength = Math.max(0, newRecordingOffset - maxLength);
  if (newRecordingOffset > maxLength) {
    secondCopyLength = newRecordingOffset - maxLength;
  } else {
    secondCopyLength = 0;
  }
  int firstCopyLength = numberRead - secondCopyLength;
  System.arraycopy(audioBuffer, 0, recordingBuffer, recordingOffset, firstCopyLength);
  System.arraycopy(audioBuffer, firstCopyLength, recordingBuffer, 0, secondCopyLength);
  recordingOffset = newRecordingOffset % maxLength;
}

private void recognize() {
  int maxLength = recordingBuffer.length;
  int firstCopyLength = maxLength - recordingOffset;
  int secondCopyLength = recordingOffset;
  System.arraycopy(recordingBuffer, recordingOffset, inputBuffer, 0, firstCopyLength);
  System.arraycopy(recordingBuffer, 0, inputBuffer, firstCopyLength, secondCopyLength);
}

沒有留言:

張貼留言