diff --git a/arm9/source/main.cpp b/arm9/source/main.cpp index 4922b79..1e29a86 100644 --- a/arm9/source/main.cpp +++ b/arm9/source/main.cpp @@ -263,6 +263,7 @@ u8 dsmw_lastnotes[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; u8 dsmw_lastchannels[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; char last_themepath[SETTINGS_FILENAME_LEN + 1]; +char *preview_smp_path = NULL; bool fastscroll = false; bool multisamp_from_mapsamp = false; @@ -1972,6 +1973,61 @@ void handleSamplePreviewToggled(bool on) settings->setSamplePreview(on); } + + +u32 calcFileSize(const char *path) { + struct stat filestats; + int stat_res = stat(path, &filestats); + + if(stat_res != -1) { + return filestats.st_size; + } + + return 0; +} + +void previewWav(void) { + if (mb != NULL) + deleteMessageBox(); + + debugprintf("previewing\n"); + + // Load sample + bool success; + Sample *smp = new Sample(preview_smp_path, false, &success); + if(!success) + { + delete smp; + return; + } + + updateMemoryState(false); + + // Stop and delete previously playing preview sample + if(state->preview_sample) + CommandStopSample(0); + + // Wait until previously playing preview sample is deleted + while(state->preview_sample) + cothread_yield_irq(IRQ_VBLANK); + + // Play it + state->preview_sample = smp; + DC_FlushAll(); + CommandPlaySample(smp, 4*12, 255, 0); + + // When the sample has finished playing, the arm7 sends a signal, + // so the arm9 can delete the sample +} + +void confirmWavPreview(void) +{ + if (mb != 0) deleteMessageBox(); + mb = new MessageBox(&sub_vram, "preview large audio file?", 2, "preview", previewWav, "cancel", deleteMessageBox); + gui->registerOverlayWidget(mb, 0, SUB_SCREEN); + mb->reveal(); +} + void handleFileChange(File file) { if(!file.is_dir) @@ -1996,34 +2052,23 @@ void handleFileChange(File file) if(state->playing) pausePlay(); - debugprintf("previewing\n"); + if (preview_smp_path != NULL) + { + ntxm_free(preview_smp_path); + preview_smp_path = NULL; + } - // Load sample - bool success; - Sample *smp = new Sample(file.name_with_path.c_str(), false, &success); - if(!success) + preview_smp_path = ntxm_ustrdup(file.name_with_path.c_str()); + if (!preview_smp_path) { - delete smp; + showMessage("not enough ram free!", true); return; } - - updateMemoryState(false); - - // Stop and delete previously playing preview sample - if(state->preview_sample) - CommandStopSample(0); - - // Wait until previously playing preview sample is deleted - while(state->preview_sample) - cothread_yield_irq(IRQ_VBLANK); - - // Play it - state->preview_sample = smp; - DC_FlushAll(); - CommandPlaySample(smp, 4*12, 255, 0); - // When the sample has finished playing, the arm7 sends a signal, - // so the arm9 can delete the sample + if (calcFileSize(str) > 3 * 1024 * 1024 /* 3MiB */) + confirmWavPreview(); + else + previewWav(); } } }