928d7c0da4
Actor: +play Sound +select needed buffers by key +get playingState in update Ballistic Actor: +play Sound on fire +play Sound on Hit +destroy object after hit
151 lines
3 KiB
C#
151 lines
3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using OpenTK.Audio;
|
|
using OpenTK.Audio.OpenAL;
|
|
using org.niclasundharald.engine;
|
|
|
|
|
|
|
|
|
|
namespace nhengine.Audio
|
|
{
|
|
|
|
|
|
public static class nhBuffers
|
|
{
|
|
|
|
//public static List<int> buffers = new List<int>();
|
|
//public static List<string> buffersNames = new List<string>();
|
|
|
|
public static Dictionary<string, int> buffers = new Dictionary<string, int>();
|
|
|
|
private static List<string> _files = new List<string>();
|
|
private static List<string> _searchPaths = new List<string>();
|
|
//private static string filepath;
|
|
|
|
|
|
|
|
|
|
|
|
public static void init()
|
|
{
|
|
|
|
buffers.Clear();
|
|
_files.Clear();
|
|
//_searchPaths = new List<string>();
|
|
_searchPaths.Add(".");
|
|
|
|
}
|
|
|
|
|
|
public static void addSearchPath(string sPath)
|
|
{
|
|
_searchPaths.Add(sPath);
|
|
}
|
|
|
|
public static void loadSound(params string[] sName)
|
|
{
|
|
foreach(string a in sName)
|
|
{
|
|
string fullName = FileHelper.findFile(String.Format("{0}.wav", a), _searchPaths.ToArray());
|
|
Console.WriteLine("TEST: " + fullName);
|
|
if (fullName != null)
|
|
{
|
|
Console.WriteLine("Sound found: {0}", fullName);
|
|
_createBuffer(fullName, a);
|
|
}else
|
|
throw new FileNotFoundException("File not found.");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static void _createBuffer(string path, string sName)
|
|
{
|
|
|
|
Stream stream = File.Open(path, FileMode.Open);
|
|
string name = sName;
|
|
|
|
if (stream == null)
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
using (BinaryReader reader = new BinaryReader(stream))
|
|
{
|
|
|
|
|
|
//RIFF HEADER
|
|
string signature = new string(reader.ReadChars(4));
|
|
if (signature != "RIFF")
|
|
throw new NotSupportedException("File is not RIFF.");
|
|
|
|
reader.ReadInt32();
|
|
|
|
string format = new string(reader.ReadChars(4));
|
|
if (format != "WAVE")
|
|
throw new NotSupportedException("File is not WAVE.");
|
|
|
|
|
|
|
|
//WAVE HEADER
|
|
string format_sig = new string(reader.ReadChars(4));
|
|
if (format_sig != "fmt ")
|
|
throw new NotSupportedException("File is not supported.");
|
|
|
|
|
|
|
|
//Format Chunk Size
|
|
reader.ReadInt32();
|
|
//Format
|
|
reader.ReadInt16();
|
|
int channels = reader.ReadInt16();
|
|
int sample_rate = reader.ReadInt32();
|
|
//Byterate
|
|
reader.ReadInt32();
|
|
reader.ReadInt16();
|
|
//Block allign
|
|
int bits = reader.ReadInt16();
|
|
|
|
|
|
|
|
string data_sig = new string(reader.ReadChars(4));
|
|
if (data_sig != "data")
|
|
throw new NotSupportedException("File is not supported");
|
|
|
|
//Data chunk Size
|
|
reader.ReadInt32();
|
|
|
|
byte[] soundData = reader.ReadBytes((int)reader.BaseStream.Length);
|
|
|
|
ALFormat fileFormat;
|
|
|
|
switch (channels)
|
|
{
|
|
case 1:
|
|
fileFormat = bits == 8 ? ALFormat.Mono8 : ALFormat.Mono16;
|
|
break;
|
|
case 2:
|
|
fileFormat = bits == 8 ? ALFormat.Stereo8 : ALFormat.Stereo16;
|
|
break;
|
|
default: throw new NotSupportedException("File is not supported.");
|
|
}
|
|
|
|
|
|
int buffer = AL.GenBuffer();
|
|
AL.BufferData(buffer, fileFormat, soundData, soundData.Length, sample_rate);
|
|
buffers.Add(name, buffer);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|