This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
AudioDeviceCollection.cs
151 lines (136 loc) · 5.27 KB
/
AudioDeviceCollection.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Accord Audio Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.DirectSound
{
using System;
using System.Collections.Generic;
using SharpDX.DirectSound;
/// <summary>
/// Audio Device Category.
/// </summary>
///
public enum AudioDeviceCategory
{
/// <summary>
/// Capture audio device, such as a microphone or audio-in.
/// </summary>
///
Capture,
/// <summary>
/// Output audio device, such as speakers or headphone jacks.
/// </summary>
///
Output,
}
/// <summary>
/// Audio Device Collection
/// </summary>
///
/// <remarks>
/// Objects of this class may be used to iterate through available audio
/// devices present in the system. For example, by creating a <see cref=
/// "AudioDeviceCollection"/> specifying <see cref="AudioDeviceCategory.Output">
/// AudioDeviceCategory.Output</see> to its constructor, it will be possible
/// to iterate through all available output devices detected by DirectSound.
/// To list capture devices, use <see cref="AudioDeviceCategory.Capture">
/// AudioDeviceCategory.Capture</see> instead.</remarks>
///
/// <example>
/// <para>The source code below shows a typical usage of AudioDeviceCollection.</para>
///
/// <code>
/// // Create a new AudioDeviceCollection to list output devices:
/// var collection = new AudioDeviceCollection(AudioDeviceCategory.Output);
///
/// // Print all devices available in the system
/// foreach (var device in collection)
/// Console.WriteLine(device.ToString());
///
/// // Get the default audio device in the system
/// var defaultDevice = collection.Default;
/// </code>
/// </example>
///
public class AudioDeviceCollection : IEnumerable<AudioDeviceInfo>
{
/// <summary>
/// Gets the default audio device of the chosen
/// <see cref="Category">category</see>.
/// </summary>
///
/// <value>The default audio device of the chosen <see cref="Category">category</see>.</value>
///
public AudioDeviceInfo Default { get; private set; }
/// <summary>
/// Gets the <see cref="AudioDeviceCategory">category</see>
/// of the audio devices listed by this collection.
/// </summary>
///
public AudioDeviceCategory Category { get; private set; }
/// <summary>
/// Creates a <see cref="AudioDeviceCollection"/> class containing
/// devices of the given <see cref="AudioDeviceCategory">category</see>.
/// </summary>
///
/// <param name="category">The category of the devices.</param>
///
public AudioDeviceCollection(AudioDeviceCategory category)
{
this.Category = category;
// Set default device
DeviceInformation info = new DeviceInformation(Guid.Empty,
Category == AudioDeviceCategory.Output ? "Primary Sound Driver" : "Primary Sound Capture Driver",
String.Empty);
this.Default = new AudioDeviceInfo(info);
}
/// <summary>
/// Returns an enumerator that iterates through the device collection.
/// </summary>
///
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that
/// can be used to iterate through the collection.
/// </returns>
///
public IEnumerator<AudioDeviceInfo> GetEnumerator()
{
var devices = (Category == AudioDeviceCategory.Capture) ?
DirectSoundCapture.GetDevices() :
DirectSound.GetDevices();
foreach (DeviceInformation info in devices)
yield return new AudioDeviceInfo(info);
}
/// <summary>
/// Returns an enumerator that iterates through the device collection.
/// </summary>
///
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that
/// can be used to iterate through the collection.
/// </returns>
///
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return (System.Collections.IEnumerator)GetEnumerator();
}
}
}