/// <summary> /// Initializes a new instance of AudioFileReader /// </summary> /// <param name="stream">The file to open</param> public AudioDataReader(Stream stream, SelectDecoder format, PreferredDecoder preferredDecoder) { readerStream = CreateReaderStream(stream, format, preferredDecoder); sourceBytesPerSample = (readerStream.WaveFormat.BitsPerSample / 8) * readerStream.WaveFormat.Channels; sampleChannel = new SampleChannel(readerStream, false); destBytesPerSample = 4 * sampleChannel.WaveFormat.Channels; length = SourceToDest(readerStream.Length); }
/// <summary> /// Creates the reader stream, supporting all filetypes in the core NAudio library, /// and ensuring we are in PCM format /// </summary> /// <param name="stream">File Name</param> WaveStream CreateReaderStream(Stream stream, SelectDecoder format, PreferredDecoder preferredDecoder) { WaveStream readerStream = null; switch (format) { case SelectDecoder.WAV: readerStream = new WaveFileReader(stream); if (readerStream.WaveFormat.Encoding != WaveFormatEncoding.Pcm && readerStream.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) { readerStream = WaveFormatConversionStream.CreatePcmStream(readerStream); readerStream = new BlockAlignReductionStream(readerStream); } break; case SelectDecoder.MP3: readerStream = new Mp3FileReader(stream, waveFormat => CreateMp3FrameDecoder(waveFormat, preferredDecoder)); break; case SelectDecoder.AIFF: readerStream = new AiffFileReader(stream); break; case SelectDecoder.Ogg: readerStream = new NVorbis.NAudioSupport.VorbisWaveReader(stream); break; case SelectDecoder.AutoDetect: UsedDecoder = UsedDecoder.None; // try to create reader for all formats foreach (var tryFormat in autoDetectFormatOrder) { stream.Seek(0, SeekOrigin.Begin); try { readerStream = CreateReaderStream(stream, tryFormat, preferredDecoder); } catch { } if (readerStream != null) { UsedDecoder = (UsedDecoder)tryFormat; break; } } if (readerStream == null) { throw new Exception("Failed to figure out " + typeof(SelectDecoder) + "."); } break; } return(readerStream); }
public static AudioClip Load( Stream dataStream, SelectDecoder audioFormat, string unityAudioClipName, LoadMethod loadType = LoadMethod.AllPartsInBackgroundThread, bool diposeDataStreamIfNotNeeded = true #if UNITY_4 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 , bool is3D = true #endif ) { var config = new AudioLoaderConfig() { DataStream = dataStream, AudioFormat = audioFormat, UnityAudioClipName = unityAudioClipName, LoadMethod = loadType, DisposeDataStreamIfNotNeeded = diposeDataStreamIfNotNeeded, PreferredDecoder = PreferredDecoder, #if UNITY_4 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 Is3D = is3D, #endif }; var audioLoader = new AudioLoader(config); audioLoader.OnLoadingDone += () => OnLoadingDonePrivate(audioLoader); audioLoader.OnLoadingAborted += () => OnLoadingAbortedPrivate(audioLoader); audioLoader.OnLoadingFailed += (exception) => OnLoadingFailedPrivate(audioLoader, exception); if (loadType == LoadMethod.StreamInUnityThread) { SetAudioClipLoadType(audioLoader.AudioClip, AudioClipLoadType.Streaming); } else { SetAudioClipLoadType(audioLoader.AudioClip, AudioClipLoadType.DecompressOnLoad); } SetAudioClipLoadState(audioLoader.AudioClip, audioLoader.LoadState); audioLoader.StartLoading(); SetAudioClipLoadState(audioLoader.AudioClip, audioLoader.LoadState); return(audioLoader.AudioClip); }
/// <summary> /// Loads an AudioClip from Stream, you need to manually supply AudioFormat. /// Use GetAudioFormat to get AudioFormat from filePath. /// Supported formats: waw, mp3, aiff, ogg. /// </summary> /// <param name="dataStream">Stream used to read the audio data</param> /// <param name="audioFormat">Use GetAudioFormat to get AudioFormat from filePath</param> /// <param name="unityAudioClipName">Used as Unity's name of the AudioClip</param> /// <param name="doStream">audioClip will be loaded and decoded on the fly on demand, use for long one time use clips</param> /// <param name="loadInBackground">if !doStream and loadInBackground then loading is done in own thread so it doesnt hang up caller's thread</param> /// <param name="diposeDataStreamIfNotNeeded">if !doStream will Close and Dispose the dataStream if/when it's no longer needed</param> /// <returns>null if error (i.e. unsupported format), may contain unknown data or only silence if clip is not yet loaded</returns> public static AudioClip Load( Stream dataStream, SelectDecoder audioFormat, string unityAudioClipName, bool doStream = false, bool loadInBackground = true, bool diposeDataStreamIfNotNeeded = true #if UNITY_4 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 , bool is3D = true #endif ) { LoadMethod loadType; if (doStream) { loadType = LoadMethod.StreamInUnityThread; } else if (loadInBackground) { loadType = LoadMethod.AllPartsInBackgroundThread; } else { loadType = LoadMethod.AllPartsInUnityThread; } return(Load(dataStream, audioFormat, unityAudioClipName, loadType, diposeDataStreamIfNotNeeded)); }