public bool Export(IExportContainer container, Object asset, string path) { AudioClip audioClip = (AudioClip)asset; if (!audioClip.CheckAssetIntegrity()) { Logger.Log(LogType.Warning, LogCategory.Export, $"Can't export '{audioClip.Name}' because resources file hasn't been found"); return(false); } if (IsSupported(audioClip)) { byte[] data = ExportAudio(audioClip); if (data == null) { Logger.Log(LogType.Warning, LogCategory.Export, $"Unable to convert '{audioClip.ValidName}' to wav"); return(false); } using (Stream fileStream = FileUtils.CreateVirtualFile(path)) { fileStream.Write(data, 0, data.Length); } } else { using (Stream fileStream = FileUtils.CreateVirtualFile(path)) { audioClip.ExportBinary(container, fileStream); } } return(true); }
public static bool ExportAudio(IExportContainer container, AudioClip audioClip, Stream exportStream) { using (MemoryStream memStream = new MemoryStream()) { audioClip.ExportBinary(container, memStream); if (memStream.Length == 0) { return(false); } byte[] data = memStream.ToArray(); return(AudioConverter.ConvertToWav(data, exportStream)); } }
public void Export(IExportContainer container, Object asset, string path, Action <IExportContainer, Object, string> callback) { AudioClip audioClip = (AudioClip)asset; using (Stream fileStream = FileUtils.CreateVirtualFile(path)) { if (IsSupported(audioClip)) { ExportAudioClip(container, fileStream, audioClip); } else { audioClip.ExportBinary(container, fileStream); } } callback?.Invoke(container, asset, path); }
public void Export(IExportContainer container, Object asset, string path, Action <IExportContainer, Object, string> callback) { AudioClip audioClip = (AudioClip)asset; using (FileStream fileStream = new FileStream(path, FileMode.CreateNew, FileAccess.Write)) { if (IsSupported(audioClip)) { ExportAudioClip(container, fileStream, audioClip); } else { audioClip.ExportBinary(container, fileStream); } } callback?.Invoke(container, asset, path); }
public void Export(IExportContainer container, Object asset, string path, Action <IExportContainer, Object, string> callback) { AudioClip audioClip = (AudioClip)asset; using (Stream fileStream = FileUtils.CreateVirtualFile(path)) { if (IsSupported(audioClip)) { bool result = ExportAudio(container, audioClip, fileStream); if (!result) { Logger.Log(LogType.Warning, LogCategory.Export, $"Unable to convert '{audioClip.Name}' to wav"); } } else { audioClip.ExportBinary(container, fileStream); } } callback?.Invoke(container, asset, path); }
public override bool Export(IAssetsExporter exporter, IExportCollection collection, string dirPath) { AssetExportCollection asset = (AssetExportCollection)collection; AudioClip audioClip = (AudioClip)asset.Asset; exporter.File = audioClip.File; string subFolder = audioClip.ClassID.ToString(); string subPath = Path.Combine(dirPath, subFolder); string fileName = GetUniqueFileName(audioClip, subPath); if (IsSupported(audioClip)) { fileName = $"{Path.GetFileNameWithoutExtension(fileName)}.wav"; } string filePath = Path.Combine(subPath, fileName); if (!Directory.Exists(subPath)) { Directory.CreateDirectory(subPath); } exporter.File = audioClip.File; using (FileStream fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write)) { if (IsSupported(audioClip)) { ExportAudioClip(fileStream, audioClip); } else { Logger.Instance.Log(LogType.Warning, LogCategory.Export, $"AudioClip type {GetAudioType(audioClip)} isn't supported"); audioClip.ExportBinary(exporter, fileStream); } } ExportMeta(exporter, asset, filePath); return(IsSupported(audioClip)); }
private static void ExportAudioClip(IExportContainer container, Stream fileStream, AudioClip clip) { CREATESOUNDEXINFO exinfo = new CREATESOUNDEXINFO(); FMOD.System system = null; Sound sound = null; Sound subsound = null; try { RESULT result = Factory.System_Create(out system); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't create factory for AudioClip {clip.Name}"); return; } result = system.init(1, INITFLAGS.NORMAL, IntPtr.Zero); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't init system for AudioClip {clip.Name}"); return; } byte[] data; using (MemoryStream memStream = new MemoryStream()) { clip.ExportBinary(container, memStream); data = memStream.ToArray(); } if (data.Length == 0) { return; } exinfo.cbsize = Marshal.SizeOf(exinfo); exinfo.length = (uint)data.Length; result = system.createSound(data, MODE.OPENMEMORY, ref exinfo, out sound); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't create sound for AudioClip {clip.Name}"); return; } result = sound.getSubSound(0, out subsound); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't get subsound for AudioClip {clip.Name}"); return; } result = subsound.getFormat(out SOUND_TYPE type, out SOUND_FORMAT format, out int numChannels, out int bitsPerSample); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't get format for AudioClip {clip.Name}"); return; } result = subsound.getDefaults(out float frequency, out int priority); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't get defaults for AudioClip {clip.Name}"); return; } int sampleRate = (int)frequency; result = subsound.getLength(out uint length, TIMEUNIT.PCMBYTES); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't get length for AudioClip {clip.Name}"); return; } result = subsound.@lock(0, length, out IntPtr ptr1, out IntPtr ptr2, out uint len1, out uint len2); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't lock for AudioClip {clip.Name}"); return; } using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(Encoding.UTF8.GetBytes("RIFF")); writer.Write(len1 + 36); writer.Write(Encoding.UTF8.GetBytes("WAVEfmt ")); writer.Write(16); writer.Write((short)1); writer.Write((short)numChannels); writer.Write(sampleRate); writer.Write(sampleRate * numChannels * bitsPerSample / 8); writer.Write((short)(numChannels * bitsPerSample / 8)); writer.Write((short)bitsPerSample); writer.Write(Encoding.UTF8.GetBytes("data")); writer.Write(len1); for (int i = 0; i < len1; i++) { byte value = Marshal.ReadByte(ptr1, i); writer.Write(value); } } result = subsound.unlock(ptr1, ptr2, len1, len2); if (result != RESULT.OK) { Logger.Instance.Log(LogType.Error, LogCategory.Export, $"Can't unlock for AudioClip {clip.Name}"); } } finally { if (subsound != null) { subsound.release(); } if (sound != null) { sound.release(); } if (system != null) { system.release(); } } }