Client LuaCsForBarotrauma
Sound.cs
1 using System;
2 using OpenAL;
3 using Microsoft.Xna.Framework;
4 using Barotrauma.IO;
5 using System.Xml.Linq;
6 
7 namespace Barotrauma.Sounds
8 {
9  abstract class Sound : IDisposable
10  {
11  protected bool disposed;
12  public bool Disposed
13  {
14  get { return disposed; }
15  }
16 
17  public readonly SoundManager Owner;
18 
19  public readonly string Filename;
20 
21  public readonly ContentXElement XElement;
22 
23  public readonly bool Stream;
24 
25  public readonly bool StreamsReliably;
26 
27  public bool Loading { get; protected set; }
28 
29  private readonly SoundManager.SourcePoolIndex sourcePoolIndex = SoundManager.SourcePoolIndex.Default;
31  {
32  get
33  {
34  return sourcePoolIndex;
35  }
36  }
37 
38  protected SoundBuffers buffers;
40  {
41  get { return !Stream ? buffers : null; }
42  }
43 
44  public int ALFormat
45  {
46  get;
47  protected set;
48  }
49 
50  public int SampleRate
51  {
52  get;
53  protected set;
54  }
55 
59  public int MaxSimultaneousInstances = 5;
60 
61  public float BaseGain;
62  public float BaseNear;
63  public float BaseFar;
64 
65  public Sound(SoundManager owner, string filename, bool stream, bool streamsReliably, ContentXElement xElement = null, bool getFullPath = true)
66  {
67  Owner = owner;
68  Filename = getFullPath ? Path.GetFullPath(filename.CleanUpPath()).CleanUpPath() : filename;
69  Stream = stream;
70  StreamsReliably = streamsReliably;
71  XElement = xElement;
72  sourcePoolIndex = XElement?.GetAttributeEnum("sourcepool", SoundManager.SourcePoolIndex.Default) ?? SoundManager.SourcePoolIndex.Default;
73 
74  BaseGain = 1.0f;
75  BaseNear = 100.0f;
76  BaseFar = 200.0f;
77  }
78 
79  public override string ToString()
80  {
81  return GetType().ToString() + " (" + Filename + ")";
82  }
83 
84  public virtual bool IsPlaying()
85  {
86  return Owner.IsPlaying(this);
87  }
88 
90  {
91  if (Loading)
92  {
93  if (Level.Loaded is not { Generating: true })
94  {
95  DebugConsole.AddWarning($"Attempted to play the sound {this} while it was still loading.");
96  }
97  return true;
98  }
99  return false;
100  }
101 
102  public virtual SoundChannel Play(float gain, float range, Vector2 position, bool muffle = false)
103  {
105  return new SoundChannel(this, gain, new Vector3(position.X, position.Y, 0.0f), 1.0f, range * 0.4f, range, "default", muffle);
106  }
107 
108  public virtual SoundChannel Play(float gain, float range, float freqMult, Vector2 position, bool muffle = false)
109  {
111  return new SoundChannel(this, gain, new Vector3(position.X, position.Y, 0.0f), freqMult, range * 0.4f, range, "default", muffle);
112  }
113 
114  public virtual SoundChannel Play(Vector3? position, float gain, float freqMult = 1.0f, bool muffle = false)
115  {
117  return new SoundChannel(this, gain, position, freqMult, BaseNear, BaseFar, "default", muffle);
118  }
119 
120  public virtual SoundChannel Play(float gain)
121  {
122  return Play(null, gain);
123  }
124 
125  public virtual SoundChannel Play()
126  {
127  return Play(BaseGain);
128  }
129 
130  public virtual SoundChannel Play(float? gain, string category)
131  {
132  if (Owner.CountPlayingInstances(this) >= MaxSimultaneousInstances) { return null; }
133  return new SoundChannel(this, gain ?? BaseGain, null, 1.0f, BaseNear, BaseFar, category);
134  }
135 
136  static protected void CastBuffer(float[] inBuffer, short[] outBuffer, int length)
137  {
138  for (int i = 0; i < length; i++)
139  {
140  outBuffer[i] = ToolBox.FloatToShortAudioSample(inBuffer[i]);
141  }
142  }
143 
144  public abstract int FillStreamBuffer(int samplePos, short[] buffer);
145 
146  public abstract float GetAmplitudeAtPlaybackPos(int playbackPos);
147 
148  public virtual void InitializeAlBuffers() { }
149 
150  public virtual void FillAlBuffers() { }
151 
152  public virtual void DeleteAlBuffers()
153  {
154  Owner.KillChannels(this);
155  buffers?.Dispose();
156  }
157 
158  public virtual void Dispose()
159  {
160  if (disposed) { return; }
161 
162  DeleteAlBuffers();
163 
164  Owner.RemoveSound(this);
165  disposed = true;
166  }
167  }
168 }
169 
static void CastBuffer(float[] inBuffer, short[] outBuffer, int length)
Definition: Sound.cs:136
virtual SoundManager.SourcePoolIndex SourcePoolIndex
Definition: Sound.cs:31
int MaxSimultaneousInstances
How many instances of the same sound clip can be playing at the same time
Definition: Sound.cs:59
override string ToString()
Definition: Sound.cs:79
virtual SoundChannel Play()
Definition: Sound.cs:125
virtual void FillAlBuffers()
Definition: Sound.cs:150
Sound(SoundManager owner, string filename, bool stream, bool streamsReliably, ContentXElement xElement=null, bool getFullPath=true)
Definition: Sound.cs:65
readonly ContentXElement XElement
Definition: Sound.cs:21
virtual SoundChannel Play(float gain)
Definition: Sound.cs:120
bool LogWarningIfStillLoading()
Definition: Sound.cs:89
abstract int FillStreamBuffer(int samplePos, short[] buffer)
virtual void InitializeAlBuffers()
Definition: Sound.cs:148
virtual void Dispose()
Definition: Sound.cs:158
readonly bool StreamsReliably
Definition: Sound.cs:25
SoundBuffers buffers
Definition: Sound.cs:38
readonly bool Stream
Definition: Sound.cs:23
readonly string Filename
Definition: Sound.cs:19
virtual void DeleteAlBuffers()
Definition: Sound.cs:152
SoundBuffers? Buffers
Definition: Sound.cs:40
abstract float GetAmplitudeAtPlaybackPos(int playbackPos)
readonly SoundManager Owner
Definition: Sound.cs:17
virtual SoundChannel Play(float gain, float range, float freqMult, Vector2 position, bool muffle=false)
Definition: Sound.cs:108
virtual bool IsPlaying()
Definition: Sound.cs:84
virtual SoundChannel Play(float gain, float range, Vector2 position, bool muffle=false)
Definition: Sound.cs:102
virtual SoundChannel Play(float? gain, string category)
Definition: Sound.cs:130
virtual SoundChannel Play(Vector3? position, float gain, float freqMult=1.0f, bool muffle=false)
Definition: Sound.cs:114
int CountPlayingInstances(Sound sound)
void RemoveSound(Sound sound)
void KillChannels(Sound sound)
Definition: Al.cs:36