Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ASoundEffectBasedAudioTrack.cs
Go to the documentation of this file.
1using System;
2using Microsoft.Xna.Framework.Audio;
3
4namespace Terraria.Audio;
5
7{
8 protected const int bufferLength = 4096;
9
10 protected const int bufferCountPerSubmit = 2;
11
12 protected const int buffersToCoverFor = 8;
13
14 protected byte[] _bufferToSubmit = new byte[4096];
15
16 protected float[] _temporaryBuffer = new float[2048];
17
18 private int _sampleRate;
19
20 private AudioChannels _channels;
21
22 protected DynamicSoundEffectInstance _soundEffectInstance;
23
24 public bool IsPlaying => _soundEffectInstance.State == SoundState.Playing;
25
26 public bool IsStopped => _soundEffectInstance.State == SoundState.Stopped;
27
28 public bool IsPaused => _soundEffectInstance.State == SoundState.Paused;
29
31 {
32 }
33
34 protected void CreateSoundEffect(int sampleRate, AudioChannels channels)
35 {
36 _sampleRate = sampleRate;
37 _channels = channels;
38 _soundEffectInstance = new DynamicSoundEffectInstance(_sampleRate, _channels);
39 }
40
41 private void _soundEffectInstance_BufferNeeded(object sender, EventArgs e)
42 {
44 }
45
46 public void Update()
47 {
48 if (IsPlaying && _soundEffectInstance.PendingBufferCount < 8)
49 {
51 }
52 }
53
54 protected void ResetBuffer()
55 {
56 for (int i = 0; i < _bufferToSubmit.Length; i++)
57 {
58 _bufferToSubmit[i] = 0;
59 }
60 }
61
62 protected void PrepareBuffer()
63 {
64 for (int i = 0; i < 2; i++)
65 {
67 }
68 }
69
70 public void Stop(AudioStopOptions options)
71 {
72 _soundEffectInstance.Stop(options == AudioStopOptions.Immediate);
73 }
74
75 public void Play()
76 {
79 }
80
81 public void Pause()
82 {
84 }
85
86 public void SetVariable(string variableName, float value)
87 {
88 switch (variableName)
89 {
90 case "Volume":
91 {
92 float volume = ReMapVolumeToMatchXact(value);
93 _soundEffectInstance.Volume = volume;
94 break;
95 }
96 case "Pitch":
97 _soundEffectInstance.Pitch = value;
98 break;
99 case "Pan":
100 _soundEffectInstance.Pan = value;
101 break;
102 }
103 }
104
105 private float ReMapVolumeToMatchXact(float musicVolume)
106 {
107 double num = 31.0 * (double)musicVolume - 25.0 - 11.94;
108 return (float)Math.Pow(10.0, num / 20.0);
109 }
110
111 public void Resume()
112 {
113 _soundEffectInstance.Resume();
114 }
115
116 protected virtual void PrepareToPlay()
117 {
118 ResetBuffer();
119 }
120
121 public abstract void Reuse();
122
123 protected virtual void ReadAheadPutAChunkIntoTheBuffer()
124 {
125 }
126
127 public abstract void Dispose();
128}
static double Pow(double x, double y)
void _soundEffectInstance_BufferNeeded(object sender, EventArgs e)
void CreateSoundEffect(int sampleRate, AudioChannels channels)
void SetVariable(string variableName, float value)