Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Mixer.cs
浏览该文件的文档.
1#if BROWSER
2using AL = Engine.Browser.AL;
3using ALContext = Engine.Browser.ALContext;
4using GetSourceInteger = Engine.Browser.AL.GetSourceInteger;
5using SourceState = Engine.Browser.AL.SourceState;
6using ListenerFloat = Engine.Browser.AL.ListenerFloat;
7using AudioError = Engine.Browser.AL.AudioError;
8#else
9using Silk.NET.OpenAL;
10#endif
11
12namespace Engine.Audio {
13 public static class Mixer {
14 public static AL AL;
15 public static ALContext m_audioContext;
16 static float m_masterVolume = 1f;
17 public static readonly List<Sound> m_soundsToStop = [];
18 public static HashSet<Sound> m_soundsToStopPoll = [];
19 public static bool m_isInitialized;
20
21 public static float MasterVolume {
22 get => m_masterVolume;
23 set {
24 value = MathUtils.Saturate(value);
25 if (value != m_masterVolume) {
26 m_masterVolume = value;
28 }
29 }
30 }
31
32 internal static void Initialize() {
33#if BROWSER
34 m_audioContext = new ALContext();
35 AL = new AL();
36 if (!CheckALErrorFull()) {
37 m_isInitialized = true;
38 }
39#else
40#if !MOBILE
41 //直接加载
42 string fullPath = Path.GetDirectoryName(
44 ); //路径备选方案
45 Environment.SetEnvironmentVariable("PATH", $"{fullPath};{RunPath.GetEnvironmentPath()}", EnvironmentVariableTarget.Process);
46#endif
47 m_audioContext = ALContext.GetApi();
48 AL = AL.GetApi();
49 unsafe {
50 Device* device = m_audioContext.OpenDevice("");
51 if (device == null) {
52 Log.Error("Could not create audio device");
53 return;
54 }
55 Context* c = m_audioContext.CreateContext(device, null);
56 m_audioContext.MakeContextCurrent(c);
57 }
58 if (!CheckALErrorFull()) {
59 m_isInitialized = true;
60 }
61#endif
62 }
63
64 internal static void Dispose() {
65 m_isInitialized = false;
66 m_audioContext?.Dispose();
67 }
68
69 internal static void BeforeFrame() {
70 foreach (Sound item in m_soundsToStopPoll) {
71 if (item.m_source != 0
72 && item.State == SoundState.Playing) {
73 AL.GetSourceProperty((uint)item.m_source, GetSourceInteger.SourceState, out int sourceState);
74 if (sourceState == (int)SourceState.Stopped) {
75 m_soundsToStop.Add(item);
76 }
77 }
78 }
79 foreach (Sound item2 in m_soundsToStop) {
80 item2.Stop();
81 }
82 m_soundsToStop.Clear();
83 }
84
85 internal static void AfterFrame() { }
86
87 internal static void InternalSetMasterVolume(float volume) {
88 if (m_isInitialized) {
89 AL.SetListenerProperty(ListenerFloat.Gain, volume);
90 }
91 }
92 /*
93 internal static void CheckALError()
94 {
95 ALError error = AL.GetError();
96 //if (error != 0)
97 //{
98 // throw new InvalidOperationException(AL.GetErrorString(error));
99 //}
100 }*/
101
102 public static AudioError CheckALError() {
103 AudioError error = AL.GetError();
104 if (error != AudioError.NoError) {
105 Log.Error($"OPENAL ERROR: {error}");
106 }
107 return error;
108 }
109
114 public static bool CheckALErrorFull() //注意返回值为是否出错
115 {
116 try {
117 AudioError error = AL.GetError();
118 if (error != AudioError.NoError) {
119 Log.Error($"OPENAL ERROR: {error}");
120 //throw new InvalidOperationException(AL.GetErrorString(error));
121 return true;
122 }
123 return false;
124 }
125 catch (Exception e) {
126 Log.Error($"Unable to load OPENAL: {e}");
127 return true;
128 }
129 }
130 }
131}
SharpDX.Direct3D11.Device Device
unsafe
定义 Main.cs:15
System.Environment Environment
static void InternalSetMasterVolume(float volume)
static void Initialize()
static HashSet< Sound > m_soundsToStopPoll
static bool m_isInitialized
static ALContext m_audioContext
static AudioError CheckALError()
static readonly List< Sound > m_soundsToStop
static void Dispose()
static bool CheckALErrorFull()
完整检查 OpenAL 是否可用和有无问题
static float MasterVolume
static float m_masterVolume
static void AfterFrame()
static void BeforeFrame()
static void Error(object message)
定义 Log.cs:80
static float Saturate(float x)
static string GetEntryPath()
获取运行入口路径(用命令行或者其他程序调用时调用者目录)
static string GetExecutablePath()
获取 EXE 或 dll 所在路径(包含文件自身路径)