From f4c8bd70d0d7bcce73a789271fe04582ba456087 Mon Sep 17 00:00:00 2001 From: alcexhim Date: Wed, 16 Apr 2014 16:01:41 -0400 Subject: [PATCH] Various fixes and updates, added plugins, etc. --- .../MainWindow.cs | 62 +++++++++- .../Accessors/StreamAccessor.cs | 2 +- .../Waveform/WaveformAudioObjectModel.cs | 30 ++--- .../Multimedia/Audio/BGM/BGMDataFormat.cs | 110 ++++++++++++++++++ 4 files changed, 189 insertions(+), 15 deletions(-) diff --git a/CSharp/Applications/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/MainWindow.cs b/CSharp/Applications/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/MainWindow.cs index 527a4a6a..e1c8a112 100644 --- a/CSharp/Applications/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/MainWindow.cs +++ b/CSharp/Applications/UniversalEditor.UserInterface.WindowsForms.DesktopApplication/MainWindow.cs @@ -912,7 +912,67 @@ namespace UniversalEditor.UserInterface.WindowsForms } private bool SaveDocumentAs(Document doc, string FileName = null) { - throw new NotImplementedException(); + DataFormatReference dfr = null; + + retrySaveFileAs: + if (FileName == null) + { + SaveFileDialog sfd = new SaveFileDialog(); + + List list = new List(); + sfd.Filter = UniversalEditor.Common.Dialog.GetCommonDialogFilter(doc.ObjectModel.MakeReference(), out list); + + if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) + { + return false; + } + + if (sfd.FilterIndex > 1 && sfd.FilterIndex <= list.Count + 1) + { + dfr = list[sfd.FilterIndex - 2]; + } + FileName = sfd.FileName; + } + + if (dfr == null) + { + DataFormatReference[] dfrs = UniversalEditor.Common.Reflection.GetAvailableDataFormats(FileName, doc.ObjectModel.MakeReference()); + if (dfrs.Length == 0) + { + if (MessageBox.Show("Could not determine the data format to use to save the file. Please check to see that you typed the file extension correctly.", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry) + { + FileName = null; + goto retrySaveFileAs; + } + else + { + return false; + } + } + dfr = dfrs[0]; + } + + DataFormat df = dfr.Create(); + DataFormatOptionsDialog.ShowDialog(ref df, DataFormatOptionsDialogType.Export); + + #region Save Code + NotifySaving(doc); + + if (FileName == doc.Title && doc.Accessor != null) + { + doc.Accessor.Close(); + } + + + doc.DataFormat = df; + + doc.OutputAccessor = new FileAccessor(FileName, true, true); + doc.Save(); + + dcc.SelectedWindow.Title = System.IO.Path.GetFileName(FileName); + doc.IsSaved = true; + #endregion + return true; } /* diff --git a/CSharp/Libraries/UniversalEditor.Core/Accessors/StreamAccessor.cs b/CSharp/Libraries/UniversalEditor.Core/Accessors/StreamAccessor.cs index 71cbbda1..1d79f5e5 100644 --- a/CSharp/Libraries/UniversalEditor.Core/Accessors/StreamAccessor.cs +++ b/CSharp/Libraries/UniversalEditor.Core/Accessors/StreamAccessor.cs @@ -69,7 +69,7 @@ namespace UniversalEditor.Accessors public override void Close() { - throw new NotImplementedException(); + mvarBaseStream.Close(); } } } diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Multimedia/ObjectModels/Multimedia/Audio/Waveform/WaveformAudioObjectModel.cs b/CSharp/Plugins/UniversalEditor.Plugins.Multimedia/ObjectModels/Multimedia/Audio/Waveform/WaveformAudioObjectModel.cs index 167b7119..0ddc56ee 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Multimedia/ObjectModels/Multimedia/Audio/Waveform/WaveformAudioObjectModel.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.Multimedia/ObjectModels/Multimedia/Audio/Waveform/WaveformAudioObjectModel.cs @@ -46,19 +46,23 @@ namespace UniversalEditor.ObjectModels.Multimedia.Audio.Waveform try { short sample = 0; - switch (mvarHeader.BitsPerSample) - { - case 8: - { - sample = br.ReadByte(); - break; - } - case 16: - { - sample = br.ReadInt16(); - break; - } - } + switch (mvarHeader.BitsPerSample) + { + case 8: + { + sample = br.ReadByte(); + break; + } + case 16: + { + sample = br.ReadInt16(); + break; + } + default: + { + throw new ArgumentException("Bad BitsPerSample value: " + mvarHeader.BitsPerSample.ToString() + "; must be 8 or 16!"); + } + } samples.Add(sample); } catch (System.IO.IOException ex) diff --git a/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs index 253d52c4..458fd8fe 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs +++ b/CSharp/Plugins/UniversalEditor.Plugins.OSLib/DataFormats/Multimedia/Audio/BGM/BGMDataFormat.cs @@ -24,15 +24,125 @@ namespace UniversalEditor.DataFormats.Multimedia.Audio.BGM protected override void LoadInternal(ref ObjectModel objectModel) { IO.Reader reader = base.Accessor.Reader; + reader.Accessor.Position = 0; + string header = reader.ReadFixedLengthString(11); if (header != "OSLBGM v01\0") throw new InvalidDataFormatException(); + byte unknown1 = reader.ReadByte(); int format = reader.ReadInt32(); // always 1 int sampleRate = reader.ReadInt32(); // sampling rate byte nbChannels = reader.ReadByte(); // mono or stereo byte[] reserved = reader.ReadBytes(32); // reserved + byte unknown2 = reader.ReadByte(); + byte[] sampleData = reader.ReadToEnd(); + short[] samples = new short[sampleData.Length / 2]; + for (int i = 0; i < samples.Length; i++) + { + short sample = BitConverter.ToInt16(new byte[] { sampleData[(i * 2)], sampleData[(i * 2) + 1] }, 0); + samples[i] = sample; + } + short[] usamples = OSLDecode(samples); + + WaveformAudioObjectModel wave = (objectModel as WaveformAudioObjectModel); + wave.RawSamples = usamples; + wave.Header.BitsPerSample = 16; + wave.Header.ChannelCount = 1; + wave.Header.SampleRate = sampleRate; + wave.Header.BlockAlignment = 2; + wave.Header.DataRate = sampleRate; + } + + private readonly sbyte[] ima9_step_indices /*[16]*/ = + { + -1, -1, -1, -1, 2, 4, 7, 12, + -1, -1, -1, -1, 2, 4, 7, 12 + }; + private readonly short[] ima_step_table /*[89]*/ = + { + 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, + 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, + 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, + 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, + 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, + 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, + 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, + 5894, 6484, 7132, 7845, 8630, 9493,10442,11487,12635,13899, + 15289,16818,18500,20350,22385,24623,27086,29794,32767 + }; + + private static int ima9_rescale(int step, uint code) + { + /* 0,1,2,3,4,5,6,9 */ + int diff = step >> 3; + if ((code & 1) != 0) diff += step >> 2; + if ((code & 2) != 0) diff += step >> 1; + if ((code & 4) != 0) diff += step; + if ((code & 7) == 7) diff += step >> 1; + if ((code & 8) != 0) diff = -diff; + return diff; + } + + private short[] OSLDecode(int sampleCount, byte[] data) + { + int index = 0; + int len = data.Length; + int si = 0; + short[] usamples = new short[sampleCount]; + uint by = 0; + + while (len > 0) + { + int step, diff; + uint code; + + if (index < 0) + { + index = 0; + } + if (index > 88) + { + index = 88; + } + step = ima_step_table[index]; + + if ((len & 1) != 0) + { + code = by >> 4; + } + else + { + by = (uint)(samples[si]); + code = by & 0x0f; + } + + diff = ima9_rescale(step, code); + index += ima9_step_indices[code & 0x07]; + + usamples[si] = (short)(samples[si] + diff); + if (samples[si] < -32768) + { + usamples[si] = -32768; + } + if (usamples[si] > 32767) + { + usamples[si] = 32767; + } + + /* + //Deux échantillons sur 2 voies + for (int i = 0; i < samples.Length; i++) + { + *dst++ += samples[si]; + } + */ + + len--; + si++; + } + return usamples; } protected override void SaveInternal(ObjectModel objectModel)