-
Notifications
You must be signed in to change notification settings - Fork 2
/
SoundEffect.cs
51 lines (43 loc) · 1.47 KB
/
SoundEffect.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
using SFML.Audio;
using System.IO;
using System;
using System.Collections.Generic;
namespace Nitemare3D
{
public class SoundEffect
{
const int sampleRate = 10989;
const int volumeModifier = 120;
Sound sound;
public const int soundOffset = 34;//everything before that is midi or blank(idk why there's blank entries)
static List<SoundEffect> effects = new List<SoundEffect>();
public static void PlaySound(int id)
{
if(effects[id - soundOffset].sound == null){return;}
if(effects[id - soundOffset].sound.Status == SoundStatus.Playing){return;}
effects[id - soundOffset].Play();
}
public static void LoadSounds()
{
for(int i = soundOffset; i < Dat.Snd.Count; i++)
{
SoundEffect sound = new SoundEffect(i);
effects.Add(sound);
}
}
public SoundEffect(int index)
{
//todo: proper VOC support
var reader = new BinaryReader(new MemoryStream(Dat.Snd[index]));
if(reader.BaseStream.Length <= 0){return;}
var samples = reader.ReadBytes((int)reader.BaseStream.Length);
var b = Array.ConvertAll(samples, c => (short)(c * volumeModifier));
sound = new Sound(new SoundBuffer(b, 1, sampleRate));
reader.BaseStream.Close();
}
public void Play()
{
sound?.Play();
}
}
}