From 1bdc9d5218b3266172c442bc8b9c15e6b8a4a9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sun, 25 Dec 2022 11:40:25 +0100 Subject: [PATCH] Added #1917 Better error message for sound import on unsupported sampling rate --- CHANGELOG.md | 4 ++ .../decompiler/flash/tags/DefineSoundTag.java | 7 ++-- .../flash/tags/base/SoundImportException.java | 29 +++++++++++++ .../decompiler/flash/tags/base/SoundTag.java | 2 +- .../UnsupportedSamplingRateException.java | 41 +++++++++++++++++++ .../console/CommandLineArgumentParser.java | 18 +++++++- .../jpexs/decompiler/flash/gui/MainPanel.java | 9 ++++ .../flash/gui/locales/MainFrame.properties | 4 ++ .../flash/gui/locales/MainFrame_cs.properties | 4 ++ 9 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundImportException.java create mode 100644 libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/UnsupportedSamplingRateException.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 29c7c1f97..bfd7213d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Added +- [#1917] Better error message for sound import on unsupported sampling rate + ### Fixed - [#1914] DropShadow filter - [#1916] Translation tool did not load up @@ -2790,6 +2793,7 @@ All notable changes to this project will be documented in this file. [alpha 9]: https://github.com/jindrapetrik/jpexs-decompiler/compare/alpha8...alpha9 [alpha 8]: https://github.com/jindrapetrik/jpexs-decompiler/compare/alpha7...alpha8 [alpha 7]: https://github.com/jindrapetrik/jpexs-decompiler/releases/tag/alpha7 +[#1917]: https://www.free-decompiler.com/flash/issues/1917 [#1914]: https://www.free-decompiler.com/flash/issues/1914 [#1916]: https://www.free-decompiler.com/flash/issues/1916 [#1231]: https://www.free-decompiler.com/flash/issues/1231 diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineSoundTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineSoundTag.java index 19b76ffcf..45718ec49 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineSoundTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineSoundTag.java @@ -21,6 +21,7 @@ import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.SWFOutputStream; import com.jpexs.decompiler.flash.tags.base.CharacterTag; import com.jpexs.decompiler.flash.tags.base.SoundTag; +import com.jpexs.decompiler.flash.tags.base.UnsupportedSamplingRateException; import com.jpexs.decompiler.flash.types.BasicType; import com.jpexs.decompiler.flash.types.annotations.EnumValue; import com.jpexs.decompiler.flash.types.annotations.SWFType; @@ -213,7 +214,7 @@ public class DefineSoundTag extends CharacterTag implements SoundTag { } @Override - public boolean setSound(InputStream is, int newSoundFormat) { + public boolean setSound(InputStream is, int newSoundFormat) throws UnsupportedSamplingRateException { int newSoundRate = -1; boolean newSoundSize = false; boolean newSoundType = false; @@ -242,7 +243,7 @@ public class DefineSoundTag extends CharacterTag implements SoundTag { newSoundRate = 3; break; default: - return false; + throw new UnsupportedSamplingRateException(newSoundRate, new int[]{5512,11025,22050,44100}); } } catch (UnsupportedAudioFileException | IOException ex) { return false; @@ -284,7 +285,7 @@ public class DefineSoundTag extends CharacterTag implements SoundTag { newSoundRate = 3; break; default: - return false; + throw new UnsupportedSamplingRateException(newSoundRate, new int[]{11025,22050,44100}); } newSoundSize = true; diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundImportException.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundImportException.java new file mode 100644 index 000000000..d72a669c6 --- /dev/null +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundImportException.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2010-2022 JPEXS, All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package com.jpexs.decompiler.flash.tags.base; + +/** + * + * @author JPEXS + */ +public class SoundImportException extends Exception { + + public SoundImportException(String message) { + super(message); + } + +} diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundTag.java index d0425451a..b9cca2ec3 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/SoundTag.java @@ -33,7 +33,7 @@ public interface SoundTag extends TreeItem { public boolean importSupported(); - public boolean setSound(InputStream is, int newSoundFormat); + public boolean setSound(InputStream is, int newSoundFormat) throws SoundImportException; public int getSoundRate(); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/UnsupportedSamplingRateException.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/UnsupportedSamplingRateException.java new file mode 100644 index 000000000..71d1883db --- /dev/null +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/base/UnsupportedSamplingRateException.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2010-2022 JPEXS, All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + */ +package com.jpexs.decompiler.flash.tags.base; + +/** + * + * @author JPEXS + */ +public class UnsupportedSamplingRateException extends SoundImportException { + private final int soundRate; + private final int[] supportedRates; + + public UnsupportedSamplingRateException(int soundRate, int[] supportedRates) { + super("Unsupported sound rate: " + soundRate); + this.soundRate = soundRate; + this.supportedRates = supportedRates; + } + + public int[] getSupportedRates() { + return supportedRates; + } + + public int getSoundRate() { + return soundRate; + } + +} diff --git a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java index 451c78559..3094ef27d 100644 --- a/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java +++ b/src/com/jpexs/decompiler/flash/console/CommandLineArgumentParser.java @@ -222,6 +222,8 @@ import com.jpexs.decompiler.flash.gui.translator.Translator; import com.jpexs.decompiler.flash.importers.SymbolClassImporter; import com.jpexs.decompiler.flash.tags.base.HasSeparateAlphaChannel; import com.jpexs.decompiler.flash.tags.base.RenderContext; +import com.jpexs.decompiler.flash.tags.base.SoundImportException; +import com.jpexs.decompiler.flash.tags.base.UnsupportedSamplingRateException; import com.jpexs.decompiler.flash.timeline.Timeline; import com.jpexs.helpers.SerializableImage; import gnu.jpdf.PDFGraphics; @@ -3185,7 +3187,21 @@ public class CommandLineArgumentParser { if (repFile.toLowerCase(Locale.ENGLISH).endsWith(".mp3")) { soundFormat = SoundFormat.FORMAT_MP3; } - boolean ok = st.setSound(new ByteArrayInputStream(data), soundFormat); + + boolean ok = false; + + try { + ok = st.setSound(new ByteArrayInputStream(data), soundFormat); + } catch (UnsupportedSamplingRateException usre) { + List supportedRatesStr = new ArrayList<>(); + for (int i : usre.getSupportedRates()) { + supportedRatesStr.add("" + i); + } + System.err.println("Import FAILED. Input file has unsupported sampling rate ("+usre.getSoundRate()+"). Supported rates for this sound format: "+String.join(", ", supportedRatesStr)+"."); + System.exit(2); + } catch (SoundImportException sie) { + ok = false; + } if (!ok) { System.err.println("Import FAILED. Maybe unsuppoted media type? Only MP3 and uncompressed WAV are available."); System.exit(1); diff --git a/src/com/jpexs/decompiler/flash/gui/MainPanel.java b/src/com/jpexs/decompiler/flash/gui/MainPanel.java index 9998f6dc0..10e3ac1df 100644 --- a/src/com/jpexs/decompiler/flash/gui/MainPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/MainPanel.java @@ -171,6 +171,7 @@ import com.jpexs.decompiler.flash.tags.base.SoundTag; import com.jpexs.decompiler.flash.tags.base.SymbolClassTypeTag; import com.jpexs.decompiler.flash.tags.base.TextImportErrorHandler; import com.jpexs.decompiler.flash.tags.base.TextTag; +import com.jpexs.decompiler.flash.tags.base.UnsupportedSamplingRateException; import com.jpexs.decompiler.flash.tags.gfx.DefineExternalImage2; import com.jpexs.decompiler.flash.tags.gfx.DefineSubImage; import com.jpexs.decompiler.flash.tags.text.TextParseException; @@ -4002,6 +4003,14 @@ public final class MainPanel extends JPanel implements TreeSelectionListener, Se ds.getSwf().clearSoundCache(); } catch (IOException ex) { //ignore + } catch (UnsupportedSamplingRateException ex) { + String samplingRateKhz = "" + (ex.getSoundRate() / 1000.0) + " kHz"; + List supportedRatesKhz = new ArrayList<>(); + for (int rate:ex.getSupportedRates()) { + supportedRatesKhz.add("" + (rate / 1000.0) + " kHz"); + } + ViewMessages.showMessageDialog(this, translate("error.sound.rate").replace("%saplingRate%", samplingRateKhz).replace("%supportedRates%", String.join(", ", supportedRatesKhz)), translate("error"), JOptionPane.ERROR_MESSAGE); + return; } if (!ok) { diff --git a/src/com/jpexs/decompiler/flash/gui/locales/MainFrame.properties b/src/com/jpexs/decompiler/flash/gui/locales/MainFrame.properties index 5d88a533b..71c556295 100644 --- a/src/com/jpexs/decompiler/flash/gui/locales/MainFrame.properties +++ b/src/com/jpexs/decompiler/flash/gui/locales/MainFrame.properties @@ -1083,3 +1083,7 @@ transform.clipboard.copy = Copy matrix to clipboard transform.clipboard.paste = Paste matrix from clipboard status.editing.autosave = You are in the EDIT mode. Make changes, then press Save button. Or discard changes with Cancel button. If you switch to other tag, current editation will be automatically saved. + +error.sound.rate = The input sound has unsupported sampling rate: %saplingRate%.\r\n\ + You should convert it to one of supported rates (%supportedRates%)\r\n\ + with some sound editor before importing. \ No newline at end of file diff --git a/src/com/jpexs/decompiler/flash/gui/locales/MainFrame_cs.properties b/src/com/jpexs/decompiler/flash/gui/locales/MainFrame_cs.properties index cc4cffa9b..e1a1cbabc 100644 --- a/src/com/jpexs/decompiler/flash/gui/locales/MainFrame_cs.properties +++ b/src/com/jpexs/decompiler/flash/gui/locales/MainFrame_cs.properties @@ -1068,3 +1068,7 @@ transform.clipboard.copy = Kop\u00edrovat matici do schr\u00e1nky transform.clipboard.paste = Vlo\u017eit matici ze schr\u00e1nky status.editing.autosave = Jste v EDITA\u010cN\u00cdM re\u017eimu. Prove\u010fte zm\u011bny a stiskn\u011bte tla\u010d\u00edtko Ulo\u017eit. Nebo zru\u0161te zm\u011bny tla\u010d\u00edtkem Storno. Pokud p\u0159epnete na jin\u00fd tag, aktu\u00e1ln\u00ed editace bude automaticky ulo\u017eena. + +error.sound.rate = Vstupn\u00ed zvuk m\u00e1 nepodporovanou vzorkovac\u00ed frekvenci: %saplingRate%.\r\n\ + P\u0159ed importem je t\u0159eba zvuk zkonvertovat n\u011bjak\u00fdm editorem zvuku\r\n\ + do jedn\u00e9 z podporovan\u00fdch frekvenc\u00ed (%supportedRates%). \ No newline at end of file