Fix waveform array out of bounds.

This commit is contained in:
Alan Evans 2020-06-04 10:39:24 -03:00 committed by Greyson Parrelli
parent 26a9dd98c1
commit 91b142e0d9
2 changed files with 13 additions and 17 deletions

View File

@ -54,19 +54,19 @@ public final class AudioWaveForm {
private static final Executor AUDIO_DECODER_EXECUTOR = new SerialExecutor(SignalExecutors.BOUNDED); private static final Executor AUDIO_DECODER_EXECUTOR = new SerialExecutor(SignalExecutors.BOUNDED);
@AnyThread @AnyThread
public void getWaveForm(@NonNull Consumer<AudioFileInfo> onSuccess, @NonNull Consumer<IOException> onFailure) { public void getWaveForm(@NonNull Consumer<AudioFileInfo> onSuccess, @NonNull Runnable onFailure) {
Uri uri = slide.getUri(); Uri uri = slide.getUri();
Attachment attachment = slide.asAttachment(); Attachment attachment = slide.asAttachment();
if (uri == null) { if (uri == null) {
Log.w(TAG, "No uri"); Log.w(TAG, "No uri");
Util.runOnMain(() -> onFailure.accept(null)); Util.runOnMain(onFailure);
return; return;
} }
if (!(attachment instanceof DatabaseAttachment)) { if (!(attachment instanceof DatabaseAttachment)) {
Log.i(TAG, "Not yet in database"); Log.i(TAG, "Not yet in database");
Util.runOnMain(() -> onFailure.accept(null)); Util.runOnMain(onFailure);
return; return;
} }
@ -110,9 +110,9 @@ public final class AudioWaveForm {
WAVE_FORM_CACHE.put(cacheKey, fileInfo); WAVE_FORM_CACHE.put(cacheKey, fileInfo);
Util.runOnMain(() -> onSuccess.accept(fileInfo)); Util.runOnMain(() -> onSuccess.accept(fileInfo));
} catch (IOException e) { } catch (Throwable e) {
Log.w(TAG, "Failed to create audio wave form for " + cacheKey, e); Log.w(TAG, "Failed to create audio wave form for " + cacheKey, e);
Util.runOnMain(() -> onFailure.accept(e)); Util.runOnMain(onFailure);
} }
}); });
} }
@ -128,7 +128,6 @@ public final class AudioWaveForm {
try (MediaInput dataSource = DecryptableUriMediaInput.createForUri(context, uri)) { try (MediaInput dataSource = DecryptableUriMediaInput.createForUri(context, uri)) {
long[] wave = new long[BAR_COUNT]; long[] wave = new long[BAR_COUNT];
int[] waveSamples = new int[BAR_COUNT]; int[] waveSamples = new int[BAR_COUNT];
int[] inputSamples = new int[BAR_COUNT * SAMPLES_PER_BAR];
MediaExtractor extractor = dataSource.createExtractor(); MediaExtractor extractor = dataSource.createExtractor();
@ -194,9 +193,7 @@ public final class AudioWaveForm {
if (!sawInputEOS) { if (!sawInputEOS) {
int barSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs); int barSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs);
inputSamples[barSampleIndex]++;
sawInputEOS = !extractor.advance(); sawInputEOS = !extractor.advance();
if (inputSamples[barSampleIndex] > 0) {
int nextBarSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs); int nextBarSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs);
while (!sawInputEOS && nextBarSampleIndex == barSampleIndex) { while (!sawInputEOS && nextBarSampleIndex == barSampleIndex) {
sawInputEOS = !extractor.advance(); sawInputEOS = !extractor.advance();
@ -207,7 +204,6 @@ public final class AudioWaveForm {
} }
} }
} }
}
int outputBufferIndex; int outputBufferIndex;
do { do {

View File

@ -168,7 +168,7 @@ public final class AudioView extends FrameLayout implements AudioSlidePlayer.Lis
} }
waveFormView.setWaveData(data.getWaveForm()); waveFormView.setWaveData(data.getWaveForm());
}, },
e -> waveFormView.setWaveMode(false)); () -> waveFormView.setWaveMode(false));
} else { } else {
waveFormView.setWaveMode(false); waveFormView.setWaveMode(false);
if (duration != null) { if (duration != null) {