-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathOutputDevice.cs
More file actions
84 lines (69 loc) · 2.4 KB
/
OutputDevice.cs
File metadata and controls
84 lines (69 loc) · 2.4 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
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
using Molten.Collections;
using Silk.NET.OpenAL;
namespace Molten.Audio.OpenAL;
internal unsafe class OutputDevice : AudioDevice, IAudioOutput
{
Context* _context;
ThreadedList<ISoundSource> _sources;
internal OutputDevice(AudioServiceAL service, string specifier, bool isDefault) :
base(service, specifier, isDefault, AudioDeviceType.Output)
{
_sources = new ThreadedList<ISoundSource>();
}
protected override unsafe bool OnOpen()
{
Ptr = Service.Alc.OpenDevice(Name);
ContextError result = Service.Alc.GetError(Ptr);
if (CheckAlcError($""))
return false;
_context = Service.Alc.CreateContext(Ptr, null);
result = Service.Alc.GetError(Ptr);
if (result == ContextError.NoError)
{
Service.Alc.MakeContextCurrent(_context);
Service.Alc.ProcessContext(_context);
result = Service.Alc.GetError(Ptr);
}
return true;
}
protected override void OnClose()
{
Context* curContext = Service.Alc.GetCurrentContext();
if (CheckAlcError($"Unable to retrieve the current context for '{Name}'"))
return;
if (curContext == _context)
{
Service.Alc.MakeContextCurrent(null);
if (CheckAlcError($"Failed to unset '{Name}' as a the current context"))
return;
}
Service.Alc.SuspendContext(_context);
Service.Alc.DestroyContext(_context);
if (CheckAlcError($"Failed to destroy the context for '{Name}'"))
return;
Service.Alc.CloseDevice(Ptr);
if (CheckAlcError($"Failed to close AL device for '{Name}'"))
return;
}
protected override void OnTransferTo(AudioDevice other)
{
// TODO recreate all _sources and their instances on the target device.
}
protected override void OnUpdate(Timing time)
{
for(int i = _sources.Count - 1; i >=0; i--)
{
SoundSource src = _sources[i] as SoundSource;
for (int s = src.InstanceCount - 1; s >= 0; s--)
src.Instances[s].Update();
}
}
public ISoundSource CreateSoundSource(AudioBuffer dataBuffer = null)
{
SoundSource source = new SoundSource(this);
_sources.Add(source);
if(dataBuffer != null)
source.CommitBuffer(dataBuffer);
return source;
}
}