private void PlaySound(int id) { this.sp.Stop(); sp_timer.Stop(); playing.Visible = false; stopButton.Visible = false; if (this.treeView.SelectedNode == null) { return; } Ultima.UOSound s = Ultima.Sounds.GetSound(id); if (s == null) { return; } using (MemoryStream m = new MemoryStream(s.buffer)) { this.sp.Stream = m; this.sp.Play(); playing.Value = 0; playing.Visible = true; stopButton.Visible = true; this.sp_timer_start = DateTime.Now; this.sp_timer_max = (int)(Ultima.Sounds.GetSoundLength(id) * 1000); sp_timer.Interval = 50; sp_timer.Start(); } }
public static void Add(int id, string name, string file) { using (FileStream wav = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] resultBuffer = new byte[wav.Length]; wav.Seek(0, SeekOrigin.Begin); wav.Read(resultBuffer, 0, (int)wav.Length); m_Cache[id] = new UOSound(name, id, resultBuffer); m_Removed[id] = false; } }
/// <summary> /// Returns <see cref="UOSound"/> of ID with bool translated in .def /// </summary> /// <param name="soundId"></param> /// <param name="translated"></param> /// <returns></returns> public static UOSound GetSound(int soundId, out bool translated) { translated = false; if (soundId < 0) { return(null); } if (_removed[soundId]) { return(null); } if (_cache[soundId] != null) { return(_cache[soundId]); } Stream stream = _fileIndex.Seek(soundId, out int length, out int _, out bool _); if (_fileIndex.Index[soundId].Lookup < 0 || length <= 0) { if (!_translations.TryGetValue(soundId, out soundId)) { return(null); } translated = true; stream = _fileIndex.Seek(soundId, out length, out int _, out bool _); } if (stream == null) { return(null); } length -= 32; int[] waveHeader = WaveHeader(length); var stringBuffer = new byte[32]; var buffer = new byte[length]; stream.Read(stringBuffer, 0, 32); stream.Read(buffer, 0, length); stream.Close(); var resultBuffer = new byte[buffer.Length + (waveHeader.Length << 2)]; Buffer.BlockCopy(waveHeader, 0, resultBuffer, 0, waveHeader.Length << 2); Buffer.BlockCopy(buffer, 0, resultBuffer, waveHeader.Length << 2, buffer.Length); string str = Encoding.ASCII.GetString(stringBuffer); // seems that the null terminator's not being properly recognized :/ if (str.IndexOf('\0') > 0) { str = str.Substring(0, str.IndexOf('\0')); } var sound = new UOSound(str, soundId, resultBuffer); if (!Files.CacheData) { return(sound); } if (!translated) // no .def definition { _cache[soundId] = sound; } return(sound); }
public static void Save(string path) { string idx = Path.Combine(path, "soundidx.mul"); string mul = Path.Combine(path, "sound.mul"); const int headerLength = 44; using (var fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write)) using (var fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) using (var binidx = new BinaryWriter(fsidx)) using (var binmul = new BinaryWriter(fsmul)) { for (int i = 0; i < _cache.Length; ++i) { UOSound sound = _cache[i]; if (sound == null && !_removed[i]) { sound = GetSound(i, out bool trans); if (!trans) { _cache[i] = sound; } else { sound = null; } } if (sound == null || _removed[i]) { binidx.Write(-1); // lookup binidx.Write(-1); // length binidx.Write(-1); // extra } else { binidx.Write((int)fsmul.Position); // lookup var length = (int)fsmul.Position; var b = new byte[32]; if (sound.Name != null) { byte[] bb = Encoding.Default.GetBytes(sound.Name); if (bb.Length > 32) { Array.Resize(ref bb, 32); } bb.CopyTo(b, 0); } binmul.Write(b); using (var m = new MemoryStream(sound.buffer)) { m.Seek(headerLength, SeekOrigin.Begin); var resultBuffer = new byte[m.Length - headerLength]; m.Read(resultBuffer, 0, (int)m.Length - headerLength); binmul.Write(resultBuffer); } length = (int)fsmul.Position - length; binidx.Write(length); binidx.Write(i + 1); } } } }
/// <summary> /// Returns <see cref="UOSound"/> of ID with bool translated in .def /// </summary> /// <param name="soundID"></param> /// <param name="translated"></param> /// <returns></returns> public static UOSound GetSound(int soundID, out bool translated) { translated = false; if (soundID < 0) { return(null); } if (m_Cache[soundID] != null) { return(m_Cache[soundID]); } int length, extra; bool patched; Stream stream = m_FileIndex.Seek(soundID, out length, out extra, out patched); if ((m_FileIndex.Index[soundID].lookup < 0) || (length <= 0)) { if (!m_Translations.TryGetValue(soundID, out soundID)) { return(null); } translated = true; stream = m_FileIndex.Seek(soundID, out length, out extra, out patched); } if (stream == null) { return(null); } length -= 32; int[] waveHeader = WaveHeader(length); byte[] stringBuffer = new byte[32]; byte[] buffer = new byte[length]; stream.Read(stringBuffer, 0, 32); stream.Read(buffer, 0, length); stream.Close(); byte[] resultBuffer = new byte[buffer.Length + (waveHeader.Length << 2)]; Buffer.BlockCopy(waveHeader, 0, resultBuffer, 0, (waveHeader.Length << 2)); Buffer.BlockCopy(buffer, 0, resultBuffer, (waveHeader.Length << 2), buffer.Length); string str = Encoding.ASCII .GetString(stringBuffer); // seems that the null terminator's not being properly recognized :/ if (str.IndexOf('\0') > 0) { str = str.Substring(0, str.IndexOf('\0')); } UOSound sound = new UOSound(str, soundID, resultBuffer); if (Files.CacheData) { if (!translated) // no .def definition { m_Cache[soundID] = sound; } } return(sound); }
public static void Save(string path) { string idx = Path.Combine(path, "soundidx.mul"); string mul = Path.Combine(path, "sound.mul"); int Headerlength = 44; using (FileStream fsidx = new FileStream(idx, FileMode.Create, FileAccess.Write, FileShare.Write), fsmul = new FileStream(mul, FileMode.Create, FileAccess.Write, FileShare.Write)) { using (BinaryWriter binidx = new BinaryWriter(fsidx), binmul = new BinaryWriter(fsmul)) { for (int i = 0; i < m_Cache.Length; ++i) { UOSound sound = m_Cache[i]; if ((sound == null) && (!m_Removed[i])) { bool trans; sound = GetSound(i, out trans); if (!trans) { m_Cache[i] = sound; } else { sound = null; } } if ((sound == null) || (m_Removed[i])) { binidx.Write((int)-1); // lookup binidx.Write((int)-1); // length binidx.Write((int)-1); // extra } else { binidx.Write((int)fsmul.Position); //lookup int length = (int)fsmul.Position; byte[] b = new byte[32]; if (sound.Name != null) { byte[] bb = Encoding.Default.GetBytes(sound.Name); if (bb.Length > 32) { Array.Resize(ref bb, 32); } bb.CopyTo(b, 0); } binmul.Write(b); using (MemoryStream m = new MemoryStream(sound.buffer)) { m.Seek(Headerlength, SeekOrigin.Begin); byte[] resultBuffer = new byte[m.Length - Headerlength]; m.Read(resultBuffer, 0, (int)m.Length - Headerlength); binmul.Write(resultBuffer); } length = (int)fsmul.Position - length; binidx.Write(length); binidx.Write(i + 1); } } } } }
/// <summary> /// Returns <see cref="UOSound"/> of ID with bool translated in .def /// </summary> /// <param name="soundID"></param> /// <param name="translated"></param> /// <returns></returns> public static UOSound GetSound(int soundID, out bool translated) { translated = false; if (soundID < 0) return null; if (m_Cache[soundID] != null) return m_Cache[soundID]; int length, extra; bool patched; Stream stream = m_FileIndex.Seek(soundID, out length, out extra, out patched); if ((m_FileIndex.Index[soundID].lookup < 0) || (length <= 0)) { if (!m_Translations.TryGetValue(soundID, out soundID)) return null; translated = true; stream = m_FileIndex.Seek(soundID, out length, out extra, out patched); } if (stream == null) return null; length -= 32; int[] waveHeader = WaveHeader(length); byte[] stringBuffer = new byte[32]; byte[] buffer = new byte[length]; stream.Read(stringBuffer, 0, 32); stream.Read(buffer, 0, length); stream.Close(); byte[] resultBuffer = new byte[buffer.Length + (waveHeader.Length << 2)]; Buffer.BlockCopy(waveHeader, 0, resultBuffer, 0, (waveHeader.Length << 2)); Buffer.BlockCopy(buffer, 0, resultBuffer, (waveHeader.Length << 2), buffer.Length); string str = Encoding.ASCII.GetString(stringBuffer); // seems that the null terminator's not being properly recognized :/ if (str.IndexOf('\0') > 0) str = str.Substring(0, str.IndexOf('\0')); UOSound sound = new UOSound(str, soundID, resultBuffer); if (Files.CacheData) { if (!translated) // no .def definition m_Cache[soundID] = sound; } return sound; }