-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathProgram.cs
108 lines (95 loc) · 3.36 KB
/
Program.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSCore;
using CSCore.Codecs;
using CSCore.SoundOut;
using NVorbis;
namespace NVorbisIntegration
{
class Program
{
[STAThread]
static void Main(string[] args)
{
//Register the new codec.
CodecFactory.Instance.Register("ogg-vorbis", new CodecFactoryEntry(s => new NVorbisSource(s).ToWaveSource(), ".ogg"));
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Vorbis file (*.ogg)|*.ogg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
using (var source = CodecFactory.Instance.GetCodec(openFileDialog.FileName))
{
using (WasapiOut soundOut = new WasapiOut())
{
soundOut.Initialize(source);
soundOut.Play();
Console.ReadKey();
soundOut.Stop();
}
}
}
}
}
public sealed class NVorbisSource : ISampleSource
{
private readonly Stream _stream;
private readonly VorbisReader _vorbisReader;
private readonly WaveFormat _waveFormat;
private bool _disposed;
public NVorbisSource(Stream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
if(!stream.CanRead)
throw new ArgumentException("Stream is not readable.", "stream");
_stream = stream;
_vorbisReader = new VorbisReader(stream, false);
_waveFormat = new WaveFormat(_vorbisReader.SampleRate, 32, _vorbisReader.Channels, AudioEncoding.IeeeFloat);
}
public bool CanSeek
{
get { return _stream.CanSeek; }
}
public WaveFormat WaveFormat
{
get { return _waveFormat; }
}
//got fixed through workitem #17, thanks for reporting @rgodart.
public long Length
{
get { return CanSeek ? (long)(_vorbisReader.TotalTime.TotalSeconds * _waveFormat.SampleRate * _waveFormat.Channels) : 0; }
}
//got fixed through workitem #17, thanks for reporting @rgodart.
public long Position
{
get
{
return CanSeek ? (long)(_vorbisReader.DecodedTime.TotalSeconds * _vorbisReader.SampleRate * _vorbisReader.Channels) : 0;
}
set
{
if(!CanSeek)
throw new InvalidOperationException("NVorbisSource is not seekable.");
if (value < 0 || value > Length)
throw new ArgumentOutOfRangeException("value");
_vorbisReader.DecodedTime = TimeSpan.FromSeconds((double)value / _vorbisReader.SampleRate / _vorbisReader.Channels);
}
}
public int Read(float[] buffer, int offset, int count)
{
return _vorbisReader.ReadSamples(buffer, offset, count);
}
public void Dispose()
{
if(!_disposed)
_vorbisReader.Dispose();
else
throw new ObjectDisposedException("NVorbisSource");
_disposed = true;
}
}
}