public static SoundEffect Load(string path) { SoundEffect soundEffect; using (MemoryStream stream = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(stream)) { OggDecoder decoder = new OggDecoder(); decoder.Initialize(File.OpenRead(path)); byte[] data = decoder.SelectMany(chunk => chunk.Bytes.Take(chunk.Length)).ToArray(); WriteWave(writer, decoder.Stereo ? 2 : 1, decoder.SampleRate, data); stream.Position = 0; soundEffect = SoundEffect.FromStream(stream); } return(soundEffect); }
public static SoundEffect Convert(string path) { string wavPath = path.Replace(".ogg", ".wav"); SoundEffect soundEffect = null; using (FileStream stream = new FileStream(wavPath, FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(stream)) { OggDecoder decoder = new OggDecoder(); decoder.Initialize(File.OpenRead(path)); byte[] data = decoder.SelectMany(chunk => chunk.Bytes.Take(chunk.Length)).ToArray(); WriteWave(writer, decoder.Stereo ? 2 : 1, decoder.SampleRate, data); } using (FileStream stream = new FileStream(wavPath, FileMode.Open)) soundEffect = SoundEffect.FromStream(stream); return(soundEffect); }
public OggVorbisFile(string name, string filepath, Sound soundType) { Name = name; string cachefile = SystemInfo.DecodedMusicCache + SystemInfo.PathSeparator.ToString () + soundType.ToString () + "_" + name.GetHashCode ().ToString () + ".wav"; Log.BlockList (id: 33, before: " - ", after: "", begin: "Load ogg audio files:", end: ""); Log.BlockList (id: 34, before: " - ", after: "", begin: "Decode ogg audio files:", end: ""); byte[] data; try { Log.ListElement (33, "[", soundType, "] ", name); data = File.ReadAllBytes (cachefile); } catch (Exception) { Log.ListElement (34, "[", soundType, "] ", name); OggDecoder decoder = new OggDecoder (); decoder.Initialize (TitleContainer.OpenStream (filepath)); data = decoder.SelectMany (chunk => chunk.Bytes.Take (chunk.Length)).ToArray (); using (MemoryStream stream = new MemoryStream ()) using (BinaryWriter writer = new BinaryWriter (stream)) { WriteWave (writer, decoder.Stereo ? 2 : 1, decoder.SampleRate, data); stream.Position = 0; data = stream.ToArray (); } File.WriteAllBytes (cachefile, data); } using (MemoryStream stream = new MemoryStream (data)) { stream.Position = 0; SoundEffect soundEffect = SoundEffect.FromStream (stream); internalFile = new SoundEffectFile (name, soundEffect, soundType); } }
public static void CompileOggs(string path = null) { if (path == null) { path = Path.GetDirectoryName(Engine.Assembly.Location); } else if (path.StartsWith(".")) { path = (Path.GetDirectoryName(Engine.Assembly.Location) + path.Substring(1)); } if (!Directory.Exists(path)) { return; } var files = Engine.DirSearch(path, ".ogg"); foreach (var file in files) { var directoryName = Path.GetDirectoryName(file); var newFile = (directoryName + "\\" + Path.GetFileNameWithoutExtension(file) + ".wav"); if (File.Exists(newFile)) { continue; } using (var fs = new FileStream(newFile, FileMode.CreateNew)) { var decoder = new OggDecoder(); decoder.Initialize(new FileStream(file, FileMode.Open)); using (var bw = new BinaryWriter(fs)) { WriteWave(bw, (decoder.Stereo ? 2 : 1), decoder.SampleRate, decoder.SelectMany(chunk => chunk.Bytes.Take(chunk.Length)).ToArray()); bw.Close(); } fs.Close(); } } }
public SoundEffect Convert(string path) { string wavPath = path.Replace(".ogg", ".wav"); SoundEffect soundEffect = null; int c = 0; Exception le = null; bool done = false; while (!done && c < ti) { FileStream stream = new FileStream(wavPath, FileMode.Create); try { using (BinaryWriter writer = new BinaryWriter(stream)) { OggDecoder decoder = new OggDecoder(); decoder.Initialize(File.OpenRead(path)); byte[] data = decoder.SelectMany(chunk => chunk.Bytes.Take(chunk.Length)).ToArray(); WriteWave(writer, decoder.Stereo ? 2 : 1, decoder.SampleRate, data); } } catch (Exception e) { le = e; c++; Thread.Sleep(slp); } done = true; stream.Close(); stream.Dispose(); } int d = 0; while (soundEffect == null && d < ti) { FileStream stream = stream = new FileStream(wavPath, FileMode.Open); try { soundEffect = SoundEffect.FromStream(stream); } catch (Exception e) { le = e; d++; Thread.Sleep(slp); } stream.Close(); stream.Dispose(); } /*if(soundEffect == null) * Monitor.Log(path + ":" + le.Message + ":" + le.StackTrace, LogLevel.Trace);*/ return(soundEffect); }