在 OBOE 中打开音频流时字段被覆盖

Field gets overwritten when opening audiostream in OBOE

我正在使用 OBOE 构建声音应用程序。我在 AudioEngine.cpp 中定义了几个字段(audiosample - float,起始位置字段 - int 字段,样本长度字段 - int 字段等)但是其中一个字段(样本长度 - int 字段)在我时被覆盖使用 playbackBuilder 打开音频流。 见代码

 AudioStreamBuilder playbackBuilder;

playbackBuilder.setPerformanceMode(PerformanceMode::LowLatency);
playbackBuilder.setSharingMode(SharingMode::Exclusive);
playbackBuilder.setChannelCount(ChannelCount::Mono);
playbackBuilder.setFormat(AudioFormat::Float);
playbackBuilder.setSampleRate(44100);
playbackBuilder.setCallback(this);
LOGD("samplelength[46]=%d",sampleLengths_[46]);
Result result = playbackBuilder.openStream(&outputStream_);
if (result != Result::OK) {
    LOGE("Error opening output stream_ %s",convertToText(result));
    return false;
}
LOGD("samplelength[46]=%d",sampleLengths_[46]);
auto setBufferSizeResult = outputStream_->setBufferSizeInFrames(outputStream_->getFramesPerBurst() * 2);
if (setBufferSizeResult) {
    LOGD("New buffer size is %d in frames",setBufferSizeResult.value());
}

logcat 的输出是:

D/OboeAudio: samplelength[46]=2393
D/OboeAudio: openStream() OUTPUT -------- OboeVersion1.4.0 --------
D/OboeAudio: AAudioLoader():  dlopen(libaaudio.so) returned 0x73861583b1301b8f
I/cesthingsplaye: Waiting for a blocking GC ProfileSaver
D/OboeAudio: AudioStreamAAudio() call isSupported()
I/AAudio: AAudioStreamBuilder_openStream() called ----------------------------------------
I/AudioStreamBuilder: rate   =  44100, channels  = 1, format   = 5, sharing = EX, dir = OUTPUT
I/AudioStreamBuilder: device =      0, sessionId = -1, perfMode = 12, callback: ON with frames = 0
I/AudioStreamBuilder: usage  =      1, contentType = 2, inputPreset = 6, allowedCapturePolicy = 0
D/AudioStreamBuilder: build() EXCLUSIVE sharing mode not supported. Use SHARED.
D/AudioStreamTrack: open(), request notificationFrames = -8, frameCount = 0
I/cesthingsplaye: WaitForGcToComplete blocked ProfileSaver on ClassLinker for 26.177ms
I/cesthingsplaye: WaitForGcToComplete blocked HeapTrim on ProfileSaver for 5.568ms
W/AudioTrack: createTrack_l(0): AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount 0 -> 1772
W/AudioStreamTrack: open() flags changed from 0x00000104 to 0x00000000
W/AudioStreamTrack: open() perfMode changed from 12 to 10
I/AAudio: AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#1 ----------------
D/OboeAudio: AudioStreamAAudio.open() app    format = 2
D/OboeAudio: AudioStreamAAudio.open() sample rate   = 44100
D/OboeAudio: AudioStreamAAudio.open() capacity      = 1772
D/OboeAudio: AudioStreamAAudio.open: AAudioStream_Open() returned AAUDIO_OK, mAAudioStream = 0x731d2dd580
D/OboeAudio: samplelength[46]=0

注意:第 46 个元素的值 2393 正是我所期望的。它不是随机数。它用下面的行初始化。然而,它在打开流时被覆盖(实际上是整个字段)。

这是这个特定字段的初始化代码 - 被覆盖的 sampleLengths_:

sampleLengthsArrayLen_ = sampleLengthArrayLen;
sampleLengths_ = new int[sampleLengthArrayLen];
sampleLengths_ = sampleLengths;

任何提示为什么会发生这种情况?内存管理不好? 非常感谢。 j

我强烈怀疑这是因为你没有初始化 sampleLengths 所以你正在读取的值实际上只是随机的内存片段,在程序执行期间会发生变化。

C++ 通过执行以下操作支持将 int 数组初始化为全零:

int sampleLengths[10] = { 0 };

解决方案是使用标准库容器代替 C 样式数组。在我的例子中,它 std::vector 因为我不知道编译时的确切大小。所以我从 JNI 获得了一个 C 风格的数组并创建了一个向量,这样值就不会被覆盖。在 OBOE 中打开流时,C 样式数组不会以某种方式为数据提供保护。

vector<int> sampleLengths_;
sampleLengthsArrayLen_ = sampleLengthArrayLen;
sampleLengths_.assign(sampleLengths,sampleLengths+sampleLengthArrayLen);