-
Notifications
You must be signed in to change notification settings - Fork 8
/
AudioUtil.cs
116 lines (101 loc) · 2.99 KB
/
AudioUtil.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
using System.Collections.Generic;
using FMODUnity;
using UnityEngine;
namespace RomenH.CommonLib
{
public static class AudioUtil
{
private static readonly Dictionary<string, FMOD.Sound> sounds = new Dictionary<string, FMOD.Sound>();
public static bool LoadSound(string key, string soundFile, bool looping = false, bool oneAtATime = false)
{
FMOD.MODE mode = FMOD.MODE._2D | FMOD.MODE._3D | FMOD.MODE._3D_WORLDRELATIVE | FMOD.MODE.CREATESAMPLE;
if (looping)
mode |= FMOD.MODE.LOOP_NORMAL;
if (oneAtATime)
mode |= FMOD.MODE.UNIQUE;
FMOD.RESULT r1 = RuntimeManager.CoreSystem.createSound(soundFile, mode, out FMOD.Sound sound);
if (r1 == FMOD.RESULT.OK)
{
sounds.Add(key, sound);
return true;
}
else
{
Debug.LogError($"AutoUtil: Failed to create sound. (key={key}, error={r1})");
return false;
}
}
public static int PlaySound(string key, Vector3 position, float volume = 1f)
{
if (sounds.TryGetValue(key, out FMOD.Sound sound))
{
FMOD.RESULT r1 = RuntimeManager.CoreSystem.getMasterChannelGroup(out FMOD.ChannelGroup cg);
if (r1 == FMOD.RESULT.OK)
{
FMOD.RESULT r2 = RuntimeManager.CoreSystem.playSound(sound, cg, true, out FMOD.Channel channel);
if (r2 == FMOD.RESULT.OK)
{
FMOD.VECTOR pos = CameraController.Instance.GetVerticallyScaledPosition(position, false).ToFMODVector();
FMOD.VECTOR vel = new FMOD.VECTOR();
channel.set3DAttributes(ref pos, ref vel);
channel.setVolume(volume);
channel.setPaused(false);
channel.getIndex(out int index);
return index;
}
else
{
Debug.LogError($"AutoUtil: Failed to create sound instance. (key={key}, error={r2})");
}
}
else
{
Debug.LogError($"AutoUtil: Failed to get master channel group. (key={key}, error={r1})");
}
}
else
{
Debug.LogWarning($"AudioUtil: Tried to play sound that does not exist. (key={key})");
}
return -1;
}
public static FMOD.Channel CreateSound(string key)
{
if (sounds.TryGetValue(key, out FMOD.Sound sound))
{
FMOD.RESULT r1 = RuntimeManager.CoreSystem.getMasterChannelGroup(out FMOD.ChannelGroup cg);
if (r1 == FMOD.RESULT.OK)
{
FMOD.RESULT r2 = RuntimeManager.CoreSystem.playSound(sound, cg, true, out FMOD.Channel channel);
if (r2 == FMOD.RESULT.OK)
{
return channel;
}
else
{
Debug.LogError($"AutoUtil: Failed to create sound instance. (key={key}, error={r2})");
}
}
else
{
Debug.LogError($"AutoUtil: Failed to get master channel group. (key={key}, error={r1})");
}
}
else
{
Debug.LogWarning($"AudioUtil: Tried to play sound that does not exist. (key={key})");
}
return new FMOD.Channel();
}
public static void StopSound(int channelID)
{
if (channelID < 0) return;
FMOD.RESULT r1 = RuntimeManager.CoreSystem.getChannel(channelID, out FMOD.Channel channel);
if (r1 == FMOD.RESULT.OK)
{
channel.stop();
channel.clearHandle();
}
}
}
}