Client LuaCsForBarotrauma
VideoSound.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using OpenAL;
7 using Microsoft.Xna.Framework;
8 using System.Runtime.InteropServices;
9 using System.Threading;
10 using Barotrauma.Media;
11 
12 namespace Barotrauma.Sounds
13 {
14  class VideoSound : Sound
15  {
16  private readonly object mutex;
17  private Queue<short[]> sampleQueue;
18 
19  private SoundChannel soundChannel;
20  private Video video;
21 
22  public VideoSound(SoundManager owner, string filename, int sampleRate, int channelCount, Video vid) : base(owner, filename, true, false)
23  {
24  ALFormat = channelCount == 2 ? Al.FormatStereo16 : Al.FormatMono16;
25  SampleRate = sampleRate;
26 
27  sampleQueue = new Queue<short[]>();
28  mutex = new object();
29 
30  soundChannel = null;
31 
32  video = vid;
33  }
34 
35  public override float GetAmplitudeAtPlaybackPos(int playbackPos)
36  {
37  throw new NotImplementedException();
38  }
39 
40  public override bool IsPlaying()
41  {
42  bool retVal = false;
43  lock (mutex)
44  {
45  retVal = soundChannel != null && soundChannel.IsPlaying;
46  }
47  return retVal;
48  }
49 
50  public void Enqueue(short[] buf)
51  {
52  lock (mutex)
53  {
54  sampleQueue.Enqueue(buf);
55  }
56  }
57 
58  public override SoundChannel Play(float gain, float range, Vector2 position, bool muffle = false)
59  {
60  throw new InvalidOperationException();
61  }
62 
63  public override SoundChannel Play(Vector3? position, float gain, float freqMult = 1.0f, bool muffle = false)
64  {
65  throw new InvalidOperationException();
66  }
67 
68  public override SoundChannel Play(float gain)
69  {
70  SoundChannel chn = null;
71  lock (mutex)
72  {
73  if (soundChannel != null)
74  {
75  soundChannel.Dispose();
76  soundChannel = null;
77  }
78  }
79  chn = new SoundChannel(this, gain, null, 1.0f, 1.0f, 3.0f, "video", false);
80  lock (mutex)
81  {
82  soundChannel = chn;
83  }
84  return chn;
85  }
86 
87  public override SoundChannel Play()
88  {
89  return Play(BaseGain);
90  }
91 
92  public override int FillStreamBuffer(int samplePos, short[] buffer)
93  {
94  if (!video.IsPlaying) return -1;
95 
96  short[] buf;
97  int readAmount = 0;
98  lock (mutex)
99  {
100  while (readAmount<buffer.Length)
101  {
102  if (sampleQueue.Count == 0) break;
103  buf = sampleQueue.Peek();
104  if (readAmount + buf.Length >= buffer.Length) break;
105  buf = sampleQueue.Dequeue();
106  buf.CopyTo(buffer, readAmount);
107  readAmount += buf.Length;
108  }
109  }
110  return readAmount;
111  }
112 
113  public override void Dispose()
114  {
115  lock (mutex)
116  {
117  soundChannel?.Dispose();
118  base.Dispose();
119  }
120  }
121  }
122 }
override SoundChannel Play(float gain)
Definition: VideoSound.cs:68
override int FillStreamBuffer(int samplePos, short[] buffer)
Definition: VideoSound.cs:92
VideoSound(SoundManager owner, string filename, int sampleRate, int channelCount, Video vid)
Definition: VideoSound.cs:22
override SoundChannel Play(float gain, float range, Vector2 position, bool muffle=false)
Definition: VideoSound.cs:58
override SoundChannel Play(Vector3? position, float gain, float freqMult=1.0f, bool muffle=false)
Definition: VideoSound.cs:63
void Enqueue(short[] buf)
Definition: VideoSound.cs:50
override SoundChannel Play()
Definition: VideoSound.cs:87
override bool IsPlaying()
Definition: VideoSound.cs:40
override float GetAmplitudeAtPlaybackPos(int playbackPos)
Definition: VideoSound.cs:35
Definition: Al.cs:38
const int FormatStereo16
Definition: Al.cs:86
const int FormatMono16
Definition: Al.cs:84
Definition: Al.cs:36