Actor/Ballistic Actor/nhPlayer

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
audio-streams
Niclas Thobaben 2017-05-06 01:20:13 +02:00
parent a95ec67a31
commit 928d7c0da4
10 changed files with 216 additions and 129 deletions

View File

@ -8,6 +8,7 @@ using OpenTK;
using OpenTK.Audio.OpenAL;
using OpenTK.Audio;
using nhengine.Audio;
using ImageSharp;
using ImageSharp.Processing;
@ -38,6 +39,7 @@ namespace nhengine
public BootStrap()
{
bootGraphics();
bootAudio();
bootMap();
}
@ -54,9 +56,19 @@ namespace nhengine
}
public void bootAudio(){
AL.Enable(ALCapability.Invalid);
AudioDelegate.init();
AudioDelegate.DistanceModel("exponential");
nhBuffers.addSearchPath("./sounds");
nhBuffers.loadSound("HowFire", "HowHit", "st1");
//AudioDelegate.test();
}
public void bootMap(){
ImageSharp.Image i = ImageSharp.Image.Load( new FileStream("M3-wip.png",FileMode.Open));
i.Flip(ImageSharp.Processing.FlipType.Vertical);
@ -81,6 +93,7 @@ namespace nhengine
public void run(){
glWindow.MakeCurrent();
glWindow.Run(25,25);
}

View File

@ -202,6 +202,7 @@ namespace nhengine
private void fireBallistic(){
Console.WriteLine("Fire Ballistic...");
BallisticActor ba = new BallisticActor(0);
ba.fire();
ba.Position = BootStrap.instance().SquaredMap.ground(new Vector2(10,10));
ba.Velocity = new Vector3(1,1,1);// + (Matrix4.CreateRotationZ(arcLeftRight) * Vector4.UnitY).Xyz;

View File

@ -65,8 +65,12 @@
<Compile Include="TextureManager.cs" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<None Include="OpenTK.dll.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="shader\simple_fragment.shader" />
<None Include="shader\simple_vertex.shader" />
<None Include="M3-wip.bmp">
@ -82,6 +86,21 @@
<None Include="models\ballistisch.obj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="sounds\seq03.wav" />
<None Include="sounds\seq02.wav" />
<None Include="sounds\seq01.wav" />
<None Include="sounds\st2.wav" />
<None Include="sounds\st1.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="sounds\sfx2.wav" />
<None Include="sounds\sfx1.wav" />
<None Include="sounds\HowHit.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="sounds\HowFire.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\org.niclasundharald.engine\org.niclasundharald.engine.csproj">
@ -89,5 +108,8 @@
<Name>org.niclasundharald.engine</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="sounds\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -11,8 +11,8 @@ namespace org.niclasundharald.engine
{
public class Actor : WorldObject
{
private static List<Actor> activeActors = new List<Actor>();
private static List<Actor> finishedActors = new List<Actor>();
public static List<Actor> activeActors = new List<Actor>();
public static List<Actor> finishedActors = new List<Actor>();
public static void updateAll(float timespan)
@ -40,21 +40,22 @@ namespace org.niclasundharald.engine
this.id = id;
activeActors.Add(this);
_player = new nhPlayer(("Actor" + id), Position);
_player = new nhPlayer();
activePlayers.Add(_player);
}
public void destroy(){
finishedActors.Add(this);
finishedPlayers.Add(_player);
_player.deletePlayer();
}
public Vector3 Heading { get; set; }
public Vector3 Velocity { get; set; }
public float Weight { get; set; }
protected virtual void update(float timespan){
public virtual void update(float timespan){
}
public void setHeading(Vector3 heading,Vector3 top){
@ -85,12 +86,12 @@ namespace org.niclasundharald.engine
//>>>>>>>>AUDIO
private static List<nhPlayer> activePlayers = new List<nhPlayer>();
private static List<nhPlayer> finishedPlayers = new List<nhPlayer>();
private static List<int> aBuffers = new List<int>();
private static Dictionary<string,int> aBuffers = new Dictionary<string,int>();
private nhPlayer _player;
public bool isPlaying = false;
public float rollOf { get; set; }
public float refDistance { get; set; }
public float maxDistance { get; set; }
@ -105,19 +106,28 @@ namespace org.niclasundharald.engine
public void setBuffers(params string[] buffers)
{
for (int i = 0; i < buffers.Length; i++)
foreach (string i in buffers)
{
aBuffers.Add(nhBuffers.buffers[buffers[i]]);
if(!aBuffers.ContainsKey(i))
aBuffers.Add(i, nhBuffers.buffers[i]);
}
}
public void playSound(string sound, float gain, bool loop)
public void playSound(string sound, float gain = 1.0f, bool loop = false)
{
_player.play(sound, gain, loop);
_player.position = this.Position;
_player.velocity = Velocity;
_player.play(aBuffers[sound], gain, loop);
}
public void updateSoundState()
{
isPlaying = _player.state();
}
}
}

View File

@ -4,6 +4,8 @@ using org.niclasundharald.engine.graphics;
using OpenTK;
namespace org.niclasundharald.engine
{
public class BallisticActor : Actor
@ -12,8 +14,11 @@ namespace org.niclasundharald.engine
:base(id)
{
this.Model3D = ModelManager.instance.findModel("ballistisch");
setBuffers("HowFire", "HowHit", "st1");
}
int hit = 0;
public override void update(float timespan)
{
base.update(timespan);
@ -26,18 +31,44 @@ namespace org.niclasundharald.engine
Console.WriteLine("BallisticActor update: {0} / {1}",Position,Velocity);
updateSoundState();
Console.WriteLine("Playing:" + isPlaying);
try{
Vector3 ground = SquaredMap.activeMap.ground(Position.Xy);
if (Position.Z <= ground.Z){
destroy();
if(hit != 1)
{
playSound("HowHit");
updateSoundState();
hit = 1;
}
if(!isPlaying)
{
destroy();
}
}
} catch (OutOfWorldException e){
} catch (OutOfWorldException e){
if(!isPlaying)
destroy();
}
}
public void fire()
{
playSound("HowFire");
setDistanceAttenuation(2, 0, 50);
}
}
}

View File

@ -17,11 +17,11 @@ namespace nhengine.Audio
{
private static bool debug = true;
//private static bool debug = true;
public static AudioContext context = new AudioContext();
public static EffectsExtension efx = new EffectsExtension();
//public static EffectsExtension efx = new EffectsExtension();
//PLAYERS
public static readonly List<nhPlayer> players = new List<nhPlayer>();
@ -35,27 +35,23 @@ namespace nhengine.Audio
nhBuffers.init();
nhListener.init();
nhPlayer.init(efx);
//nhMixer.init();
DistanceModel();
//nhAux reverb = new nhAux();
for (int i = 0; i < nhBuffers.buffers.Count; i++)
{
addPlayer("testplayer", new Vector3(0.0f, 0.0f, 0.0f));
}
Console.WriteLine("nhengine.Audio initialized.");
}
public static void test()
{
nhPlayer testPlayer = new nhPlayer();
testPlayer.play(nhBuffers.buffers["st1"]);
}
/* <<<<<<<<<<<MAIN>>>>>>>>>>> */
/*
public static void Main()
{
@ -75,7 +71,7 @@ namespace nhengine.Audio
}
*/
public static void DistanceModel(string distanceModel = "exponential", bool clamped = false)
{
@ -128,7 +124,7 @@ namespace nhengine.Audio
return _index;
}
/*
public static void play(string playerName, string buffer, float gain = 1.0f, bool loop = false)
{
@ -153,7 +149,7 @@ namespace nhengine.Audio
}
}
*/
public static void setPlayerPosition(string playerName, Vector3 position)
{
@ -197,10 +193,10 @@ namespace nhengine.Audio
}
}
/*
public static void addPlayer(string name, Vector3 position, float rollOf = 2.0f, float refDist = 0.0f, float maxDist = 100.0f)
{
nhPlayer _player = new nhPlayer(name, position, rollOf, refDist, maxDist);
nhPlayer _player = new nhPlayer(name, position);
players.Add(_player);
playerNames.Add(_player.name);
playerNumbers.Add(_player.number);
@ -214,6 +210,10 @@ namespace nhengine.Audio
players.RemoveAt(_index);
playerNames.RemoveAt(_index);
playerNumbers.RemoveAt(_index);
}
*/
}
}

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Collections.Generic;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using org.niclasundharald.engine;
@ -14,51 +15,58 @@ 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 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 string[] _folder;
private static List<string> _searchPaths = new List<string>();
//private static string filepath;
public static void init()
{
buffers.Clear();
buffersNames.Clear();
_files.Clear();
_openFiles();
//_searchPaths = new List<string>();
_searchPaths.Add(".");
}
private static void _openFiles()
public static void addSearchPath(string sPath)
{
_searchPaths.Add(sPath);
}
//_files.Add(filepath);
_folder = Directory.GetFiles(Path.Combine("Data", "Audio", "Sounds"), "*.wav");
for (int i = 0; i < _folder.Length; i++)
public static void loadSound(params string[] sName)
{
foreach(string a in sName)
{
_createBuffer(_folder[i]);
buffersNames.Add(Path.GetFileName(_folder[i]));
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)
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));
@ -67,7 +75,6 @@ namespace nhengine.Audio
{
//RIFF HEADER
string signature = new string(reader.ReadChars(4));
if (signature != "RIFF")
@ -127,7 +134,7 @@ namespace nhengine.Audio
int buffer = AL.GenBuffer();
AL.BufferData(buffer, fileFormat, soundData, soundData.Length, sample_rate);
buffers.Add(buffer);
buffers.Add(name, buffer);
}
@ -136,19 +143,6 @@ namespace nhengine.Audio
public static string getBufferName(int bufferID){
return buffersNames[bufferID];
}
public static int getBufferID(string bufferName){
int _index = buffersNames.IndexOf(bufferName);
return buffers[_index];
}

View File

@ -20,25 +20,6 @@ namespace nhengine.Audio
}
private int _buffer;
public string buffer
{
set
{_buffer = nhBuffers.getBufferID(value);}
get
{return _buffer.ToString();}
}
private string _name;
public string name
{
set
{_name = value + ".p" + _playerNumber ;}
get
{return _name;}
}
private static int _playerNumber;
public int number
{
@ -46,8 +27,8 @@ namespace nhengine.Audio
{ return _playerNumber;}
}
private static EffectsExtension _efx;
private int _filter;
//private static EffectsExtension _efx;
//private int _filter;
@ -76,40 +57,33 @@ namespace nhengine.Audio
public Vector3 velocity = new Vector3(0.0f, 0.0f, 0.0f);
//public bool loop = false;
private float _rollOf;
private float _refDist;
private float _maxDist;
private float _rollOf = 2.0f;
private float _refDist = 0.0f;
private float _maxDist = 100.0f;
//private int _state;
private int _state;
//private int[] _seqBuffer = { };
public nhPlayer(string name, Vector3 position, float rollOf = 2.0f, float refDist = 0.0f, float maxDist = 100.0f)
public nhPlayer()
{
this.name = name;
_rollOf = rollOf;
_refDist = refDist;
_maxDist = maxDist;
this.position = position;
_filter = _efx.GenFilter();
_efx.BindFilterToSource(_source, _filter);
//_filter = _efx.GenFilter();
//_efx.BindFilterToSource(_source, _filter);
AL.Source(_source, ALSourcef.RolloffFactor, _rollOf);
AL.Source(_source, ALSourcef.ReferenceDistance, _refDist);
AL.Source(_source, ALSourcef.MaxDistance, _maxDist);
AL.Source(_source, ALSourcei.EfxDirectFilter, _filter);
//AL.Source(_source, ALSourcei.EfxDirectFilter, _filter);
//AL.Source(_source, ALSourcei.EfxDirectFilter, )
//_filterEfx.Filter(_filter, EfxFilteri.FilterType, EfxFilterType.Lowpass);
_efx.Filter(_filter, EfxFilterf.LowpassGainHF, 0.5f);
printInfo();
//_efx.Filter(_filter, EfxFilterf.LowpassGainHF, 0.5f);
Interlocked.Increment(ref _playerNumber);
}
@ -117,26 +91,40 @@ namespace nhengine.Audio
//>>>>>>>>>>>>>>>>>METHODS
public static void init(EffectsExtension effectsExtension)
public bool state()
{
AL.GetSource(_source, ALGetSourcei.SourceState, out _state);
if ((ALSourceState)_state == ALSourceState.Playing)
{
return true;
}else if((ALSourceState)_state == ALSourceState.Stopped)
{
return false;
}else
{
return false;
}
_efx = effectsExtension;
}
public void play(string buffer = null, float gain = 1.0f, bool loop = false)
public void play(int buffer = 0, float gain = 1.0f, bool loop = false)
{
_gain = gain;
this.buffer = buffer;
AL.Source(_source, ALSourcei.Buffer, _buffer);
if (buffer != 0)
{
AL.Source(_source, ALSourcei.Buffer, buffer);
}else{
throw new NotSupportedException("Not existing Buffer.");
}
AL.Source(_source, ALSourceb.Looping, loop);
AL.SourcePlay(_source);
//_isplaying = true;
AL.Source(_source, ALSourcef.Gain, _gain);
AL.Source(_source, ALSource3f.Position, position.X, position.Y, position.Z);
@ -150,6 +138,22 @@ namespace nhengine.Audio
AL.SourceStop(_source);
}
public void setDistanceAttenuation(float rollOf = 2.0f, float refDistance = 0.0f, float maxDistance = 100.0f)
{
_rollOf = rollOf;
_refDist = refDistance;
_maxDist = maxDistance;
}
public void setPosition(Vector3 position)
{
this.position = position;
}
public void setVelocity(Vector3 velocity)
{
this.velocity = velocity;
}
/*
@ -218,13 +222,7 @@ namespace nhengine.Audio
AL.DeleteSource(_source);
}
public void printInfo()
{
Console.WriteLine("<<<<PLAYER #" + _playerNumber + ">>>>");
Console.WriteLine("Name:" + _name);
Console.WriteLine("Buffer:" + _buffer);
Console.WriteLine("-----------");
}
}

View File

@ -3,6 +3,7 @@
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;
using OpenTK;
using nhengine.Audio;
namespace org.niclasundharald.engine.graphics
{
@ -15,6 +16,8 @@ namespace org.niclasundharald.engine.graphics
vView,
vTop;
public GLCamera()
{
fov = MathHelper.PiOver2;
@ -53,18 +56,18 @@ namespace org.niclasundharald.engine.graphics
public Vector3 Position
{
get { return new Vector3(vPosition); }
set { this.vPosition = new Vector3(value); buildMatrices(); }
set { this.vPosition = new Vector3(value); buildMatrices(); setListener();}
}
public Vector3 View
{
get { return new Vector3(vView); }
set { this.vView = new Vector3(value).Normalized(); buildMatrices(); }
set { this.vView = new Vector3(value).Normalized(); buildMatrices(); setListener(); }
}
public Vector3 Top
{
get { return new Vector3(vTop); }
set { this.vTop = new Vector3(value).Normalized(); buildMatrices(); }
set { this.vTop = new Vector3(value).Normalized(); buildMatrices(); setListener();}
}
private void buildMatrices(){
@ -87,6 +90,14 @@ namespace org.niclasundharald.engine.graphics
_mCamera = mTranslation * mRotation;
}
private void setListener()
{
nhListener.setPosition(vPosition);
nhListener.setOrientation(vTop, vView);
}
}
}

View File

@ -68,11 +68,17 @@
<Compile Include="Physics.cs" />
<Compile Include="graphics\SquaredMap.cs" />
<Compile Include="exceptions\OutOfWorldException.cs" />
<Compile Include="audio\nhPlayer.cs" />
<Compile Include="audio\AudioDelegate.cs" />
<Compile Include="audio\nhBuffers.cs" />
<Compile Include="audio\nhListener.cs" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<None Include="Gameplay Object Tree.txt" />
<None Include="audio\nhAux.cs" />
<None Include="audio\nhMixer.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="graphics\" />
@ -81,6 +87,7 @@
<Folder Include="contrib\ImageSharp.dll">
</Folder>
<Folder Include="exceptions\" />
<Folder Include="audio\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>