2017-05-05 20:42:00 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
//using System.Threading.Tasks;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Audio;
|
|
|
|
|
using OpenTK.Audio.OpenAL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-09 23:41:50 +02:00
|
|
|
|
namespace org.budnhead.audio
|
2017-05-05 20:42:00 +02:00
|
|
|
|
{
|
|
|
|
|
public class nhPlayer
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private int _source = AL.GenSource();
|
|
|
|
|
public int source
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{return _source;}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static int _playerNumber;
|
|
|
|
|
public int number
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{ return _playerNumber;}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
//private static EffectsExtension _efx;
|
|
|
|
|
//private int _filter;
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private float _gain = 1.0f;
|
|
|
|
|
public float gain
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value <= 0.0f)
|
|
|
|
|
{
|
|
|
|
|
_gain = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
else if (value >= 1.0f)
|
|
|
|
|
{
|
|
|
|
|
_gain = 1.0f;
|
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
_gain = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
get
|
|
|
|
|
{return _gain;}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector3 position = new Vector3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
public Vector3 velocity = new Vector3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
//public bool loop = false;
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
private float _rollOf = 2.0f;
|
|
|
|
|
private float _refDist = 0.0f;
|
|
|
|
|
private float _maxDist = 100.0f;
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
private int _state;
|
|
|
|
|
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
//private int[] _seqBuffer = { };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
public nhPlayer()
|
2017-05-05 20:42:00 +02:00
|
|
|
|
{
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
//_filter = _efx.GenFilter();
|
|
|
|
|
//_efx.BindFilterToSource(_source, _filter);
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
AL.Source(_source, ALSourcef.RolloffFactor, _rollOf);
|
|
|
|
|
AL.Source(_source, ALSourcef.ReferenceDistance, _refDist);
|
|
|
|
|
AL.Source(_source, ALSourcef.MaxDistance, _maxDist);
|
2017-05-06 01:20:13 +02:00
|
|
|
|
//AL.Source(_source, ALSourcei.EfxDirectFilter, _filter);
|
2017-05-05 20:42:00 +02:00
|
|
|
|
//AL.Source(_source, ALSourcei.EfxDirectFilter, )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//_filterEfx.Filter(_filter, EfxFilteri.FilterType, EfxFilterType.Lowpass);
|
2017-05-06 01:20:13 +02:00
|
|
|
|
//_efx.Filter(_filter, EfxFilterf.LowpassGainHF, 0.5f);
|
2017-05-05 20:42:00 +02:00
|
|
|
|
Interlocked.Increment(ref _playerNumber);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//>>>>>>>>>>>>>>>>>METHODS
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
public bool state()
|
2017-05-05 20:42:00 +02:00
|
|
|
|
{
|
2017-05-06 01:20:13 +02:00
|
|
|
|
AL.GetSource(_source, ALGetSourcei.SourceState, out _state);
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
if ((ALSourceState)_state == ALSourceState.Playing)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}else if((ALSourceState)_state == ALSourceState.Stopped)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
}
|
|
|
|
|
public void play(int buffer = 0, float gain = 1.0f, bool loop = false)
|
2017-05-05 20:42:00 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
_gain = gain;
|
2017-05-06 18:12:21 +02:00
|
|
|
|
AL.Source(_source, ALSourcef.RolloffFactor, _rollOf);
|
|
|
|
|
AL.Source(_source, ALSourcef.ReferenceDistance, _refDist);
|
|
|
|
|
AL.Source(_source, ALSourcef.MaxDistance, _maxDist);
|
2017-05-06 01:20:13 +02:00
|
|
|
|
|
|
|
|
|
if (buffer != 0)
|
|
|
|
|
{
|
|
|
|
|
AL.Source(_source, ALSourcei.Buffer, buffer);
|
|
|
|
|
}else{
|
|
|
|
|
throw new NotSupportedException("Not existing Buffer.");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-05 20:42:00 +02:00
|
|
|
|
AL.Source(_source, ALSourceb.Looping, loop);
|
|
|
|
|
AL.SourcePlay(_source);
|
|
|
|
|
AL.Source(_source, ALSourcef.Gain, _gain);
|
|
|
|
|
AL.Source(_source, ALSource3f.Position, position.X, position.Y, position.Z);
|
|
|
|
|
AL.Source(_source, ALSource3f.Velocity, velocity.X, velocity.Y, velocity.Z);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void stop()
|
|
|
|
|
{
|
|
|
|
|
AL.SourceStop(_source);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-06 18:12:21 +02:00
|
|
|
|
public void setDistanceAttenuation(float rollOf = 2.0f, float refDistance = 1.0f, float maxDistance = 100.0f)
|
2017-05-06 01:20:13 +02:00
|
|
|
|
{
|
|
|
|
|
_rollOf = rollOf;
|
|
|
|
|
_refDist = refDistance;
|
|
|
|
|
_maxDist = maxDistance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPosition(Vector3 position)
|
|
|
|
|
{
|
|
|
|
|
this.position = position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setVelocity(Vector3 velocity)
|
|
|
|
|
{
|
|
|
|
|
this.velocity = velocity;
|
|
|
|
|
}
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void playSeq(int buffer01, int buffer02, int buffer03)
|
|
|
|
|
{
|
|
|
|
|
_seqBuffer[0] = buffer01;
|
|
|
|
|
_seqBuffer[1] = buffer02;
|
|
|
|
|
_seqBuffer[2] = buffer03;
|
|
|
|
|
|
|
|
|
|
Thread _playThread = new Thread(_playSeq);
|
|
|
|
|
_playThread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void _playSeq()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int _buffersToProcess = 3;
|
|
|
|
|
|
|
|
|
|
AL.Source(_source, ALSourcei.Buffer, _buffer);
|
|
|
|
|
AL.Source(_source, ALSourceb.Looping, loop);
|
|
|
|
|
|
|
|
|
|
AL.SourceQueueBuffers(_source, 3, _seqBuffer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AL.SourcePlay(_source);
|
|
|
|
|
//_isplaying = true;
|
|
|
|
|
|
|
|
|
|
AL.Source(_source, ALSourcef.Gain, _gain);
|
|
|
|
|
AL.Source(_source, ALSource3f.Position, position[0], position[1], position[2]);
|
|
|
|
|
AL.Source(_source, ALSource3f.Velocity, velocity[0], velocity[1], velocity[2]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(16);
|
|
|
|
|
AL.GetSource(_source, ALGetSourcei.SourceState, out _state);
|
|
|
|
|
AL.GetSource(_source, ALGetSourcei.BuffersProcessed, out _buffersToProcess);
|
|
|
|
|
|
|
|
|
|
while(_buffersToProcess != 0)
|
|
|
|
|
{
|
|
|
|
|
//AL.SourceUnqueueBuffers(_source, 1, );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
while ((ALSourceState)_state == ALSourceState.Playing);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AL.SourceStop(_source);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void deletePlayer()
|
|
|
|
|
{
|
|
|
|
|
AL.DeleteSource(_source);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-06 01:20:13 +02:00
|
|
|
|
|
2017-05-05 20:42:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|