|
15 | 15 | using System.IO;
|
16 | 16 | using System.Threading.Tasks;
|
17 | 17 | using System.Windows.Input;
|
| 18 | +using OpenLoco.ObjectEditor.Types; |
| 19 | +using System.ComponentModel; |
| 20 | +using Core.Objects.Sound; |
| 21 | +using System.Collections.ObjectModel; |
| 22 | +using OpenLoco.ObjectEditor.Data; |
| 23 | +using NAudio.Wave; |
| 24 | +using System.Threading; |
18 | 25 |
|
19 | 26 | namespace AvaGui.ViewModels
|
20 | 27 | {
|
21 |
| - public class ImageTableViewModel : ReactiveObject |
| 28 | + public interface IExtraContentViewModel |
| 29 | + { } |
| 30 | + |
| 31 | + public record UiSoundObject(string SoundName) |
| 32 | + { |
| 33 | + public UiSoundObject(string soundName, RiffWavHeader header, byte[] data) : this(soundName) |
| 34 | + { |
| 35 | + Header = header; |
| 36 | + Data = data; |
| 37 | + Duration = $"{data.Length / (decimal)header.ByteRate:0.#}s"; |
| 38 | + } |
| 39 | + |
| 40 | + public string Duration { get; init; } |
| 41 | + |
| 42 | + public RiffWavHeader Header { get; set; } |
| 43 | + public byte[] Data { get; set; } |
| 44 | + } |
| 45 | + |
| 46 | + public class SoundViewModel : ReactiveObject, IExtraContentViewModel |
| 47 | + { |
| 48 | + ILocoObject parent; // currently not needed |
| 49 | + |
| 50 | + WaveOutEvent? CurrentWOEvent { get; set; } |
| 51 | + |
| 52 | + public SoundViewModel(ILocoObject parent) |
| 53 | + { |
| 54 | + if (parent.Object is not SoundObject soundObject) |
| 55 | + { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + this.parent = parent; |
| 60 | + |
| 61 | + var hdr = soundObject.SoundObjectData.PcmHeader; |
| 62 | + var text = parent.StringTable.Table["Name"][LanguageId.English_UK] ?? "<null>"; |
| 63 | + Sound = new UiSoundObject(text, SawyerStreamWriter.WaveFormatExToRiff(hdr, soundObject.PcmData.Length), soundObject.PcmData); |
| 64 | + |
| 65 | + PlaySoundCommand = ReactiveCommand.Create(PlaySound); |
| 66 | + PauseSoundCommand = ReactiveCommand.Create(() => CurrentWOEvent?.Pause()); |
| 67 | + StopSoundCommand = ReactiveCommand.Create(() => CurrentWOEvent?.Stop()); |
| 68 | + |
| 69 | + //ImportSoundCommand = ReactiveCommand.Create(ImportSound); |
| 70 | + //ExportSoundCommand = ReactiveCommand.Create(ExportSound); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public void PlaySound() |
| 75 | + { |
| 76 | + if (CurrentWOEvent != null) |
| 77 | + { |
| 78 | + if (CurrentWOEvent.PlaybackState == PlaybackState.Playing) |
| 79 | + { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (CurrentWOEvent.PlaybackState == PlaybackState.Paused) |
| 84 | + { |
| 85 | + CurrentWOEvent.Play(); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // do it asyncly to a) give user ui control and b) allow multiple sounds to play at once |
| 91 | + _ = Task.Run(() => |
| 92 | + { |
| 93 | + if (CurrentWOEvent?.PlaybackState == PlaybackState.Stopped) |
| 94 | + { |
| 95 | + Thread.Sleep(100); // give time to wait until previous sound is disposed |
| 96 | + } |
| 97 | + |
| 98 | + CurrentWOEvent?.Dispose(); |
| 99 | + |
| 100 | + using (var ms = new MemoryStream(Sound.Data)) |
| 101 | + using (var rs = new RawSourceWaveStream(ms, new WaveFormat((int)Sound.Header.SampleRate, Sound.Header.BitsPerSample, Sound.Header.NumberOfChannels))) |
| 102 | + using (CurrentWOEvent = new WaveOutEvent()) |
| 103 | + { |
| 104 | + CurrentWOEvent.Init(rs); |
| 105 | + CurrentWOEvent.Play(); |
| 106 | + |
| 107 | + // need to wait for music to finish |
| 108 | + while (CurrentWOEvent?.PlaybackState != PlaybackState.Stopped) |
| 109 | + { |
| 110 | + if (CurrentWOEvent == null) |
| 111 | + { |
| 112 | + break; |
| 113 | + } |
| 114 | + Thread.Sleep(50); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + CurrentWOEvent = null; |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + [Reactive] |
| 123 | + public UiSoundObject Sound { get; set; } |
| 124 | + |
| 125 | + [Reactive] |
| 126 | + public ICommand PlaySoundCommand { get; set; } |
| 127 | + |
| 128 | + [Reactive] |
| 129 | + public ICommand PauseSoundCommand { get; set; } |
| 130 | + |
| 131 | + [Reactive] |
| 132 | + public ICommand StopSoundCommand { get; set; } |
| 133 | + |
| 134 | + //public async Task ImportSound() |
| 135 | + //{ |
| 136 | + // var folders = await PlatformSpecific.OpenFolderPicker(); |
| 137 | + // var dir = folders.FirstOrDefault(); |
| 138 | + // if (dir == null) |
| 139 | + // { |
| 140 | + // return; |
| 141 | + // } |
| 142 | + |
| 143 | + // var dirPath = dir.Path.LocalPath; |
| 144 | + // if (Directory.Exists(dirPath) && Directory.EnumerateFiles(dirPath).Any()) |
| 145 | + // { |
| 146 | + // var files = Directory.GetFiles(dirPath); |
| 147 | + // var sorted = files.OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f).Split('-')[0])); |
| 148 | + |
| 149 | + // var g1Elements = new List<G1Element32>(); |
| 150 | + // var i = 0; |
| 151 | + // //foreach (var file in sorted) |
| 152 | + // //{ |
| 153 | + // // var img = SixLabors.ImageSharp.Image.Load<Rgb24>(file); |
| 154 | + // // var data = PaletteMap.ConvertRgb24ImageToG1Data(img); |
| 155 | + // // var hasTransparency = data.Any(b => b == 0); |
| 156 | + // // var oldImage = Parent.G1Elements[i++]; |
| 157 | + // // oldImage.ImageData = PaletteMap.ConvertRgb24ImageToG1Data(img); // simply overwrite existing pixel data |
| 158 | + // //} |
| 159 | + // } |
| 160 | + |
| 161 | + // //this.RaisePropertyChanged(nameof(Images)); |
| 162 | + //} |
| 163 | + |
| 164 | + //public async Task ExportSound() |
| 165 | + //{ |
| 166 | + // var folders = await PlatformSpecific.OpenFolderPicker(); |
| 167 | + // var dir = folders.FirstOrDefault(); |
| 168 | + // if (dir == null) |
| 169 | + // { |
| 170 | + // return; |
| 171 | + // } |
| 172 | + |
| 173 | + // var dirPath = dir.Path.LocalPath; |
| 174 | + // if (Directory.Exists(dirPath)) |
| 175 | + // { |
| 176 | + // var counter = 0; |
| 177 | + // //foreach (var image in Images) |
| 178 | + // //{ |
| 179 | + // // var imageName = counter++.ToString(); // todo: use GetImageName from winforms project |
| 180 | + // // var path = Path.Combine(dir.Path.LocalPath, $"{imageName}.png"); |
| 181 | + // // //logger.Debug($"Saving image to {path}"); |
| 182 | + // // image.Save(path); |
| 183 | + // //} |
| 184 | + // } |
| 185 | + //} |
| 186 | + |
| 187 | + //[Reactive] |
| 188 | + //public ICommand ImportSoundCommand { get; set; } |
| 189 | + |
| 190 | + //[Reactive] |
| 191 | + //public ICommand ExportSoundCommand { get; set; } |
| 192 | + } |
| 193 | + |
| 194 | + public class ImageTableViewModel : ReactiveObject, IExtraContentViewModel |
22 | 195 | {
|
23 | 196 | ILocoObject Parent;
|
24 | 197 |
|
|
0 commit comments