Repository navigation
Expand file tree
/
Copy pathAudioDecoder.cs
More file actions
36 lines (34 loc) · 1.47 KB
/
Copy pathAudioDecoder.cs
File metadata and controls
36 lines (34 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using NAudio.Vorbis;
using NAudio.Wave;
namespace aBookPlayer;
/// <summary>
/// Decodes the supported audio formats (<see cref="AudioFormats"/>). Apart from OGG (NVorbis, ~100 KB), decoding
/// relies on what is built into NAudio or Windows (Media Foundation), so extra formats add no weight to the app.
/// </summary>
static class AudioDecoder
{
/// <summary>Opens a seekable stream decoding to PCM/float. Can scan the whole file: call it off the UI thread.</summary>
public static WaveStream Open(string path)
{
switch (Path.GetExtension(path).ToLowerInvariant())
{
case ".mp3":
return new Mp3FileReader(path);
case ".ogg":
return new VorbisWaveReader(path);
case ".aiff" or ".aif":
return new AiffFileReader(path);
case ".wav":
var wav = new WaveFileReader(path);
if (wav.WaveFormat.Encoding is WaveFormatEncoding.Pcm or WaveFormatEncoding.IeeeFloat)
return wav;
// WAVE_FORMAT_EXTENSIBLE (24-bit/multichannel exports) is rejected by NAudio's sample
// converters, and compressed WAVs (ADPCM, …) need a codec: let Windows decode both
wav.Dispose();
return new MediaFoundationReader(path);
default:
// M4A/M4B/AAC/MP4/WMA/FLAC: Windows' built-in decoders
return new MediaFoundationReader(path);
}
}
}