/// <summary> /// Returns a fully initialized <see cref="IWaveSource" /> instance which is able to decode the specified file. If the /// specified file can not be decoded, this method throws an <see cref="NotSupportedException" />. /// </summary> /// <param name="filename">Filename of the specified file.</param> /// <returns>Fully initialized <see cref="IWaveSource" /> instance which is able to decode the specified file.</returns> /// <exception cref="NotSupportedException">The codec of the specified file is not supported.</exception> public IWaveSource GetCodec(string filename) { if (String.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename"); } if (!File.Exists(filename)) { throw new FileNotFoundException("File not found.", filename); } string extension = Path.GetExtension(filename).Remove(0, 1); //get the extension without the "dot". IWaveSource source = null; if (File.Exists(filename)) { Stream stream = File.OpenRead(filename); try { foreach (var codecEntry in _codecs) { try { if ( codecEntry.Value.FileExtensions.Any( x => x.Equals(extension, StringComparison.OrdinalIgnoreCase))) { source = codecEntry.Value.GetCodecAction(stream); if (source != null) { break; } } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } } } finally { if (source == null) { stream.Dispose(); } else { source = new DisposeFileStreamSource(source, stream); } } } if (source != null) { return(source); } return(Default(filename)); }
/// <summary> /// Returns a fully initialized <see cref="IWaveSource" /> instance which is able to decode the specified file. If the /// specified file can not be decoded, this method throws an <see cref="NotSupportedException" />. /// </summary> /// <param name="filename">Filename of the specified file.</param> /// <returns>Fully initialized <see cref="IWaveSource" /> instance which is able to decode the specified file.</returns> /// <exception cref="NotSupportedException">The codec of the specified file is not supported.</exception> public IWaveSource GetCodec(string filename) { if (String.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename"); } if (!File.Exists(filename)) { throw new FileNotFoundException("File not found.", filename); } string extension = Path.GetExtension(filename).Remove(0, 1); //get the extension without the "dot". IWaveSource source = null; if (File.Exists(filename)) { Log.Verbose("Processing {0}", filename); Stream stream = File.OpenRead(filename); try { // test for some predefined headers // to support audio files that have the wrong exension // i.e. aiff files that are really wav files etc. using (var reader = new BinaryReader(stream, Encoding.ASCII, true)) { var fileChunkId = new String(reader.ReadChars(4)); var lowerCaseExtension = extension.ToLowerInvariant(); // wav files have RIFF if (fileChunkId.Equals("RIFF") && !lowerCaseExtension.Contains("wav")) { extension = "wav"; } // aif files have FORM if (fileChunkId.Equals("FORM") && !lowerCaseExtension.Contains("aif")) { extension = "aif"; } // ogg files have OggS if (fileChunkId.Equals("OggS") && !lowerCaseExtension.Contains("ogg")) { extension = "ogg"; } // mp3 files have ID3 and end of text character (\u0003) if (fileChunkId.Equals("ID3\u0003") && !lowerCaseExtension.Contains("mp3")) { extension = "mp3"; } // flac files have fLaC if (fileChunkId.Equals("fLaC") && !lowerCaseExtension.Contains("fla")) { extension = "flac"; } stream.Position -= 4; } foreach (var codecEntry in _codecs) { try { if ( codecEntry.Value.FileExtensions.Any( x => x.Equals(extension, StringComparison.OrdinalIgnoreCase))) { source = codecEntry.Value.GetCodecAction(stream); if (source != null) { break; } } } catch (Exception ex) { Log.Warning(ex.Message); } } } finally { if (source == null) { stream.Dispose(); } else { source = new DisposeFileStreamSource(source, stream); } } } if (source != null) { return(source); } throw new ArgumentException("No codecs found that could process the source."); }