Skip to content

Commit

Permalink
Fix waveform array out of bounds.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-signal authored and greyson-signal committed Jun 4, 2020
1 parent 26a9dd9 commit 91b142e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Expand Up @@ -54,19 +54,19 @@ public AudioWaveForm(@NonNull Context context, @NonNull AudioSlide slide) {
private static final Executor AUDIO_DECODER_EXECUTOR = new SerialExecutor(SignalExecutors.BOUNDED);

@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();
Attachment attachment = slide.asAttachment();

if (uri == null) {
Log.w(TAG, "No uri");
Util.runOnMain(() -> onFailure.accept(null));
Util.runOnMain(onFailure);
return;
}

if (!(attachment instanceof DatabaseAttachment)) {
Log.i(TAG, "Not yet in database");
Util.runOnMain(() -> onFailure.accept(null));
Util.runOnMain(onFailure);
return;
}

Expand Down Expand Up @@ -110,9 +110,9 @@ public void getWaveForm(@NonNull Consumer<AudioFileInfo> onSuccess, @NonNull Con

WAVE_FORM_CACHE.put(cacheKey, fileInfo);
Util.runOnMain(() -> onSuccess.accept(fileInfo));
} catch (IOException e) {
} catch (Throwable e) {
Log.w(TAG, "Failed to create audio wave form for " + cacheKey, e);
Util.runOnMain(() -> onFailure.accept(e));
Util.runOnMain(onFailure);
}
});
}
Expand All @@ -126,9 +126,8 @@ public void getWaveForm(@NonNull Consumer<AudioFileInfo> onSuccess, @NonNull Con
@RequiresApi(api = 23)
private @NonNull AudioFileInfo generateWaveForm(@NonNull Uri uri) throws IOException {
try (MediaInput dataSource = DecryptableUriMediaInput.createForUri(context, uri)) {
long[] wave = new long[BAR_COUNT];
int[] waveSamples = new int[BAR_COUNT];
int[] inputSamples = new int[BAR_COUNT * SAMPLES_PER_BAR];
long[] wave = new long[BAR_COUNT];
int[] waveSamples = new int[BAR_COUNT];

MediaExtractor extractor = dataSource.createExtractor();

Expand Down Expand Up @@ -194,15 +193,12 @@ public void getWaveForm(@NonNull Consumer<AudioFileInfo> onSuccess, @NonNull Con

if (!sawInputEOS) {
int barSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs);
inputSamples[barSampleIndex]++;
sawInputEOS = !extractor.advance();
if (inputSamples[barSampleIndex] > 0) {
int nextBarSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs);
while (!sawInputEOS && nextBarSampleIndex == barSampleIndex) {
sawInputEOS = !extractor.advance();
if (!sawInputEOS) {
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) {
sawInputEOS = !extractor.advance();
if (!sawInputEOS) {
nextBarSampleIndex = (int) (SAMPLES_PER_BAR * (wave.length * extractor.getSampleTime()) / totalDurationUs);
}
}
}
Expand Down
Expand Up @@ -168,7 +168,7 @@ public void setAudio(final @NonNull AudioSlide audio,
}
waveFormView.setWaveData(data.getWaveForm());
},
e -> waveFormView.setWaveMode(false));
() -> waveFormView.setWaveMode(false));
} else {
waveFormView.setWaveMode(false);
if (duration != null) {
Expand Down

0 comments on commit 91b142e

Please sign in to comment.