Added the trunk/branches/tags directories at repository root, and moved previous root into trunk/

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1002 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
laurentgom 2009-01-28 16:18:34 +00:00
commit 2f524481c1
974 changed files with 295448 additions and 0 deletions

View file

@ -0,0 +1,36 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.all;
public import
dsfml.audio.listener,
dsfml.audio.music,
dsfml.audio.sound,
dsfml.audio.soundbuffer,
dsfml.audio.soundbufferrecorder,
dsfml.audio.soundrecorder,
dsfml.audio.soundstatus,
dsfml.audio.soundstream;

View file

@ -0,0 +1,184 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.listener;
import dsfml.system.common;
import dsfml.system.vector3;
/**
* Listener is a global interface for defining the audio
* listener properties ; the audio listener is the point in
* the scene from where all the sounds are heard.
*
* See_Also:
* SFML Tutorial for sound spatialization example.
*/
class Listener
{
/**
* Change the global volume of all the sounds.
* The default volume is 100
*
* Params:
* volume = New global volume, in the range [0, 100]
*/
static void setGlobalVolume(float volume)
in
{
assert (volume >= 0.f && volume <= 100.f);
}
body
{
sfListener_SetGlobalVolume(volume);
}
/**
* Get the current value of the global volume of all the sounds
*
* Returns:
* Current global volume, in the range [0, 100]
*/
static float getGlobalVolume()
{
return sfListener_GetGlobalVolume();
}
/**
* Change the position of the listener.
* The default position is (0, 0, 0)
*
* Params:
* posX = X position of the listener in the world
* posY = Y position of the listener in the world
* posZ = Z position of the listener in the world
*/
static void setPosition(float posX, float posY, float posZ)
{
sfListener_SetPosition(posX, posY, posZ);
}
/**
* Change the position of the listener.
* The default position is (0, 0, 0)
*
* Params:
* position = new position
*/
static void setPosition(Vector3f position)
{
sfListener_SetPosition(position.x, position.y, position.z);
}
/**
* Get the current position of the listener
*
* Returns:
* Current position
*/
static Vector3f getPosition()
{
Vector3f ret;
sfListener_GetPosition(&ret.x, &ret.y, &ret.z);
return ret;
}
/**
* Change the orientation of the listener (the point
* he must look at).
* The default target is (0, 0, -1)
*
* Params:
* targetX = X position of the point the listener must look at
* targetY = X position of the point the listener must look at
* targetZ = X position of the point the listener must look at
*/
static void setTarget(float targetX, float targetY, float targetZ)
{
sfListener_SetTarget(targetX, targetY, targetZ);
}
/**
* Change the orientation of the listener (the point
* he must look at).
* The default target is (0, 0, -1)
*
* Params:
* target = Position of the point the listener must look at
*/
static void setTarget(Vector3f position)
{
sfListener_SetTarget(position.x, position.y, position.z);
}
/**
* Get the current orientation of the listener (the point
* he's looking at)
*
* Returns:
* Position of the point the listener is looking at
*/
static Vector3f getTarget()
{
Vector3f ret;
sfListener_GetTarget(&ret.x, &ret.y, &ret.z);
return ret;
}
private:
// External ====================================================================
extern (C)
{
typedef void function(float) pf_sfListener_SetGlobalVolume;
typedef float function() pf_sfListener_GetGlobalVolume;
typedef void function(float, float, float) pf_sfListener_SetPosition;
typedef void function(float*, float*, float*) pf_sfListener_GetPosition;
typedef void function(float, float, float) pf_sfListener_SetTarget;
typedef void function(float*, float*, float*) pf_sfListener_GetTarget;
static pf_sfListener_SetGlobalVolume sfListener_SetGlobalVolume;
static pf_sfListener_GetGlobalVolume sfListener_GetGlobalVolume;
static pf_sfListener_SetPosition sfListener_SetPosition;
static pf_sfListener_GetPosition sfListener_GetPosition;
static pf_sfListener_SetTarget sfListener_SetTarget;
static pf_sfListener_GetTarget sfListener_GetTarget;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfListener_SetGlobalVolume = cast(pf_sfListener_SetGlobalVolume)dll.getSymbol("sfListener_SetGlobalVolume");
sfListener_GetGlobalVolume = cast(pf_sfListener_GetGlobalVolume)dll.getSymbol("sfListener_GetGlobalVolume");
sfListener_SetPosition = cast(pf_sfListener_SetPosition)dll.getSymbol("sfListener_SetPosition");
sfListener_GetPosition = cast(pf_sfListener_GetPosition)dll.getSymbol("sfListener_GetPosition");
sfListener_SetTarget = cast(pf_sfListener_SetTarget)dll.getSymbol("sfListener_SetTarget");
sfListener_GetTarget = cast(pf_sfListener_GetTarget)dll.getSymbol("sfListener_GetTarget");
}
}

View file

@ -0,0 +1,396 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.music;
import dsfml.audio.soundstatus;
import dsfml.system.common;
import dsfml.system.exception;
import dsfml.system.stringutil;
import dsfml.system.vector3;
/**
* Music defines a big sound played using streaming,
* so usually what we call a music :)
*/
class Music : DSFMLObject
{
/**
* Open a music file (doesn't play it -- call Play for that)
*
* Params:
* filename = Path of the file to open
*
*/
this(char[] filename)
{
if (filename is null || filename.length == 0)
throw new LoadingException("LoadingException : Filename is invalid.");
super(sfMusic_CreateFromFile(toStringz(filename)));
}
/**
* Open a music file from memory (doesn't play it -- call Play() for that)
*
* Params:
* data = file data in memory
*
*/
this(byte[] data)
{
if (data is null || data.length == 0)
throw new Exception("LoadingException : Memory stream is invalid.");
super(m_ptr = sfMusic_CreateFromMemory(data.ptr, data.length));
}
override void dispose()
{
sfMusic_Destroy(m_ptr);
}
/**
* Start playing the audio stream
*/
void play()
{
sfMusic_Play(m_ptr);
}
/**
* Stop playing the audio stream
*/
void stop()
{
sfMusic_Stop(m_ptr);
}
/**
* Pause the audio stream
*/
void pause()
{
sfMusic_Pause(m_ptr);
}
/**
* Return the number of channels (1 = mono, 2 = stereo)
*
* Returns:
* Number of channels
*/
uint getChannelsCount()
{
return sfMusic_GetChannelsCount(m_ptr);
}
/**
* Get the stream sample rate
*
* Returns:
* Stream frequency (number of samples per second)
*/
uint getSampleRate()
{
return sfMusic_GetSampleRate(m_ptr);
}
/**
* Get the music duration
*
* Returns:
* Music duration, in seconds
*/
float getDuration()
{
return sfMusic_GetDuration(m_ptr);
}
/**
* Get the status of the stream (stopped, paused, playing)
*
* Returns:
* Current status of the sound
*/
SoundStatus getStatus()
{
return sfMusic_GetStatus(m_ptr);
}
/**
* Tell whether or not the music is looping
*
* Returns:
* True if the music is looping, false otherwise
*/
bool getLoop()
{
return cast(bool)sfMusic_GetLoop(m_ptr);
}
/**
* Get the pitch
*
* Returns:
* Pitch value
*/
float getPitch()
{
return sfMusic_GetPitch(m_ptr);
}
/**
* Get the volume
*
* Returns:
* Volume value (in range [1, 100])
*/
float getVolume()
{
return sfMusic_GetVolume(m_ptr);
}
/**
* Get the sound position
*
* Returns:
* Current position of the music.
*/
Vector3f getPosition()
{
Vector3f ret;
sfMusic_GetPosition(m_ptr, &ret.x, &ret.y, &ret.z);
return ret;
}
/**
* Get the minimum distance
*
* Returns:
* Minimum distance for the sound
*/
float getMinDistance()
{
return sfMusic_GetMinDistance(m_ptr);
}
/**
* Get the attenuation factor
*
* Returns:
* Attenuation factor of the sound
*
*/
float getAttenuation()
{
return sfMusic_GetAttenuation(m_ptr);
}
/**
* Set the music loop state.
* This parameter is disabled by default
*
* Params:
* loop = True to play in loop, false to play once
*/
void setLoop(bool loop)
{
sfMusic_SetLoop(m_ptr, loop);
}
/**
* Set the sound pitch.
* The default pitch is 1
*
* Params:
* pitch = New pitch
*
*/
void setPitch(float pitch)
{
sfMusic_SetPitch(m_ptr, pitch);
}
/**
* Set the sound volume.
* The default volume is 100
*
* Params:
* volume = Volume (in range [0, 100])
*
*/
void setVolume(float volume)
in
{
assert (volume >= 0.f && volume <= 100.f);
}
body
{
sfMusic_SetVolume(m_ptr, volume);
}
/**
* Set the sound position.
* The default position is (0, 0, 0)
*
* Params:
* x = X position of the sound in the world
* y = Y position of the sound in the world
* z = Z position of the sound in the world
*
*/
void setPosition(float x, float y, float z)
{
sfMusic_SetPosition(m_ptr, x, y, z);
}
/**
* Set the sound position.
* The default position is (0, 0, 0)
*
* Params:
* position = new position
*
*/
void setPosition(Vector3f position)
{
sfMusic_SetPosition(m_ptr, position.x, position.y, position.z);
}
/**
* Set the minimum distance - closer than thsi distance
* the listener will hear the sound at its maximum volume.
* The default distance is 1.0
*
* Params:
* minDistance = new minimum distance for the sound
*/
void setMinDistance(float minDistance)
{
sfMusic_SetMinDistance(m_ptr, minDistance);
}
/**
* Set the attenuation factor - the higher the attenuation, the
* more the sound will be attenuated with distance from listener.
* The default attenuation factor 1.0
*
* Params:
* attenuation = new attenuation factor for the sound
*/
void setAttenuation(float attenuation)
{
sfMusic_SetAttenuation(m_ptr, attenuation);
}
private:
// External ====================================================================
extern (C)
{
typedef void* function(char*) pf_sfMusic_CreateFromFile;
typedef void* function(byte*, size_t) pf_sfMusic_CreateFromMemory;
typedef void function(void*) pf_sfMusic_Destroy;
typedef void function(void*, int) pf_sfMusic_SetLoop;
typedef bool function(void*) pf_sfMusic_GetLoop;
typedef float function(void*) pf_sfMusic_GetDuration;
typedef void function(void*) pf_sfMusic_Play;
typedef void function(void*) pf_sfMusic_Pause;
typedef void function(void*) pf_sfMusic_Stop;
typedef uint function(void*) pf_sfMusic_GetChannelsCount;
typedef uint function(void*) pf_sfMusic_GetSampleRate;
typedef SoundStatus function(void*) pf_sfMusic_GetStatus;
typedef void function(void*, float) pf_sfMusic_SetPitch;
typedef void function(void*, float) pf_sfMusic_SetVolume;
typedef void function(void*, float, float, float) pf_sfMusic_SetPosition;
typedef float function(void*) pf_sfMusic_GetPitch;
typedef float function(void*) pf_sfMusic_GetVolume;
typedef void function(void*, float*, float*, float*) pf_sfMusic_GetPosition;
typedef float function(void*) pf_sfMusic_GetMinDistance;
typedef float function(void*) pf_sfMusic_GetAttenuation;
typedef void function(void*, float) pf_sfMusic_SetMinDistance;
typedef void function(void*, float) pf_sfMusic_SetAttenuation;
static pf_sfMusic_CreateFromFile sfMusic_CreateFromFile;
static pf_sfMusic_CreateFromMemory sfMusic_CreateFromMemory;
static pf_sfMusic_Destroy sfMusic_Destroy;
static pf_sfMusic_SetLoop sfMusic_SetLoop;
static pf_sfMusic_GetLoop sfMusic_GetLoop;
static pf_sfMusic_GetDuration sfMusic_GetDuration;
static pf_sfMusic_Play sfMusic_Play;
static pf_sfMusic_Pause sfMusic_Pause;
static pf_sfMusic_Stop sfMusic_Stop;
static pf_sfMusic_GetChannelsCount sfMusic_GetChannelsCount;
static pf_sfMusic_GetSampleRate sfMusic_GetSampleRate;
static pf_sfMusic_GetStatus sfMusic_GetStatus;
static pf_sfMusic_SetPitch sfMusic_SetPitch;
static pf_sfMusic_SetVolume sfMusic_SetVolume;
static pf_sfMusic_SetPosition sfMusic_SetPosition;
static pf_sfMusic_GetPitch sfMusic_GetPitch;
static pf_sfMusic_GetVolume sfMusic_GetVolume;
static pf_sfMusic_GetPosition sfMusic_GetPosition;
static pf_sfMusic_GetMinDistance sfMusic_GetMinDistance;
static pf_sfMusic_GetAttenuation sfMusic_GetAttenuation;
static pf_sfMusic_SetMinDistance sfMusic_SetMinDistance;
static pf_sfMusic_SetAttenuation sfMusic_SetAttenuation;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfMusic_CreateFromFile = cast(pf_sfMusic_CreateFromFile)dll.getSymbol("sfMusic_CreateFromFile");
sfMusic_CreateFromMemory = cast(pf_sfMusic_CreateFromMemory)dll.getSymbol("sfMusic_CreateFromMemory");
sfMusic_Destroy = cast(pf_sfMusic_Destroy)dll.getSymbol("sfMusic_Destroy");
sfMusic_SetLoop = cast(pf_sfMusic_SetLoop)dll.getSymbol("sfMusic_SetLoop");
sfMusic_GetLoop = cast(pf_sfMusic_GetLoop)dll.getSymbol("sfMusic_GetLoop");
sfMusic_GetDuration = cast(pf_sfMusic_GetDuration)dll.getSymbol("sfMusic_GetDuration");
sfMusic_Play = cast(pf_sfMusic_Play)dll.getSymbol("sfMusic_Play");
sfMusic_Pause = cast(pf_sfMusic_Pause)dll.getSymbol("sfMusic_Pause");
sfMusic_Stop = cast(pf_sfMusic_Stop)dll.getSymbol("sfMusic_Stop");
sfMusic_GetChannelsCount = cast(pf_sfMusic_GetChannelsCount)dll.getSymbol("sfMusic_GetChannelsCount");
sfMusic_GetSampleRate = cast(pf_sfMusic_GetSampleRate)dll.getSymbol("sfMusic_GetSampleRate");
sfMusic_GetStatus = cast(pf_sfMusic_GetStatus)dll.getSymbol("sfMusic_GetStatus");
sfMusic_SetPitch = cast(pf_sfMusic_SetPitch)dll.getSymbol("sfMusic_SetPitch");
sfMusic_SetVolume = cast(pf_sfMusic_SetVolume)dll.getSymbol("sfMusic_SetVolume");
sfMusic_SetPosition = cast(pf_sfMusic_SetPosition)dll.getSymbol("sfMusic_SetPosition");
sfMusic_GetPitch = cast(pf_sfMusic_GetPitch)dll.getSymbol("sfMusic_GetPitch");
sfMusic_GetVolume = cast(pf_sfMusic_GetVolume)dll.getSymbol("sfMusic_GetVolume");
sfMusic_GetPosition = cast(pf_sfMusic_GetPosition)dll.getSymbol("sfMusic_GetPosition");
sfMusic_GetMinDistance = cast(pf_sfMusic_GetMinDistance)dll.getSymbol("sfMusic_GetMinDistance");
sfMusic_GetAttenuation = cast(pf_sfMusic_GetAttenuation)dll.getSymbol("sfMusic_GetAttenuation");
sfMusic_SetMinDistance = cast(pf_sfMusic_SetMinDistance)dll.getSymbol("sfMusic_SetMinDistance");
sfMusic_SetAttenuation = cast(pf_sfMusic_SetAttenuation)dll.getSymbol("sfMusic_SetAttenuation");
}
}

View file

@ -0,0 +1,415 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.sound;
import dsfml.audio.soundbuffer;
import dsfml.audio.soundstatus;
import dsfml.system.common;
import dsfml.system.exception;
import dsfml.system.vector3;
/**
* Sound defines the properties of the sound such as position,
* volume, pitch, etc.
*/
class Sound : DSFMLObject
{
/**
* Default constructor
*/
this()
{
super(sfSound_Create());
}
/**
* Construct the sound from its parameters
*
* Params:
* buffer = Sound buffer to play
* loop = Loop flag (false by default)
* pitch = Value of the pitch (1 by default)
* volume = Volume (100 by default)
* x = X position (0 by default)
* y = Y position (0 by default)
* z = Z position (0 by default)
*
* Throws:
* NullParameterException if buffer is null
*/
this(SoundBuffer buffer, bool loop = false, float pitch = 1.f, float volume = 100.f, float x = 0.f, float y = 0.f, float z = 0.f)
{
if (buffer is null)
throw new NullParameterException("NullParameterException : SoundBuffer is null.");
super(sfSound_Create());
sfSound_SetBuffer(m_ptr, buffer.getNativePointer());
sfSound_SetLoop(m_ptr, loop);
sfSound_SetPitch(m_ptr, pitch);
sfSound_SetVolume(m_ptr, volume);
sfSound_SetPosition(m_ptr, x, y, z);
}
override void dispose()
{
sfSound_Destroy(m_ptr);
}
/**
* Play the sound
*/
void play()
{
sfSound_Play(m_ptr);
}
/**
* Pause the sound
*/
void pause()
{
sfSound_Pause(m_ptr);
}
/**
* Stop the sound
*/
void stop()
{
sfSound_Stop(m_ptr);
}
/**
* Set the source buffer
*
* Params:
* buffer = New sound buffer to bind to the sound
*/
void setBuffer(SoundBuffer buffer)
{
if (buffer is null)
throw new NullParameterException("NullParameterException : SoundBuffer is null.");
m_buffer = buffer;
sfSound_SetBuffer(m_ptr, buffer.getNativePointer);
}
/**
* Set the sound loop state.
* This parameter is disabled by default
*
* Params:
* loop = True to play in loop, false to play once
*/
void setLoop(bool loop)
{
sfSound_SetLoop(m_ptr, loop);
}
/**
* Set the sound pitch.
* The default pitch is 1
*
* Params:
* pitch = New pitch
*/
void setPitch(float pitch)
{
sfSound_SetPitch(m_ptr, pitch);
}
/**
* Set the sound volume.
* The default volume is 100
*
* Params:
* volume = Volume (in range [0, 100])
*/
void setVolume(float volume)
in
{
assert(volume >= 0 && volume <= 100);
}
body
{
sfSound_SetVolume(m_ptr, volume);
}
/**
* Set the sound position.
* The default position is (0, 0, 0)
*
* Params:
* x = X position of the sound in the world
* y = Y position of the sound in the world
* z = Z position of the sound in the world
*/
void setPosition(float x, float y, float z)
{
sfSound_SetPosition(m_ptr, x, y, z);
}
/**
* Set the sound position.
* The default position is (0, 0, 0)
*
* Params :
* position = new position
*/
void setPosition(Vector3f position)
{
sfSound_SetPosition(m_ptr, position.x, position.y, position.z);
}
/**
* Set the minimum distance - closer than this distance
* the listener will hear the sound at its maximum volume.
* The default distance is 1.0
*
* Params:
* minDistance = new minimum distance for the sound
*/
void setMinDistance(float minDistance)
{
sfSound_SetMinDistance(m_ptr, minDistance);
}
/**
* Set the attenuation factor - the higher the attenuation, the
* more the sound will be attenuated with distance from listener.
* The default attenuation factor 1.0
*
* Params:
* attenuation = new attenuation factor for the sound
*/
void setAttenuation(float attenuation)
{
sfSound_SetAttenuation(m_ptr, attenuation);
}
/**
* Set the current playing offset of a sound
*
* Params:
* offset = new playing position, expressed in seconds
*/
void setPlayingOffset(float offset)
{
sfSound_SetPlayingOffset(m_ptr, offset);
}
/**
* Get the source buffer
*
* Returns:
* Sound buffer bound to the sound (can be NULL)
*/
SoundBuffer getBuffer()
{
return m_buffer;
}
/**
* Tell whether or not the sound is looping
*
* Returns:
* True if the sound is looping, false otherwise
*/
bool getLoop()
{
return cast(bool)(sfSound_GetLoop(m_ptr));
}
/**
* Get the pitch
*
* Returns:
* Pitch value
*/
float getPitch()
{
return sfSound_GetPitch(m_ptr);
}
/**
* Get the volume
*
* Returns:
* Volume value (in range [1, 100])
*/
float getVolume()
{
return sfSound_GetVolume(m_ptr);
}
/**
* Get the sound position
*
* Params:
* x = X position of the sound in the world
* y = Y position of the sound in the world
* z = Z position of the sound in the world
*/
Vector3f getPosition()
{
Vector3f ret;
sfSound_GetPosition(m_ptr, &ret.x, &ret.y, &ret.z);
return ret;
}
/**
* Get the minimum distance
*
* Returns:
* Minimum distance for the sound
*/
float getMinDistance()
{
return sfSound_GetMinDistance(m_ptr);
}
/**
* Get the attenuation factor
*
* Returns:
* Attenuation factor of the sound
*
*/
float getAttenuation()
{
return sfSound_GetAttenuation(m_ptr);
}
/**
* Get the status of the sound (stopped, paused, playing)
*
* Returns:
* Current status of the sound
*/
SoundStatus getStatus()
{
return sfSound_GetStatus(m_ptr);
}
/**
* Get the current playing position of the sound
*
* Returns:
* Current playing position, expressed in seconds
*/
float getPlayingOffset()
{
return sfSound_GetPlayingOffset(m_ptr);
}
private:
SoundBuffer m_buffer;
// External ====================================================================
extern (C)
{
typedef void* function() pf_sfSound_Create;
typedef void function(void*) pf_sfSound_Destroy;
typedef void function(void*) pf_sfSound_Play;
typedef void function(void*) pf_sfSound_Pause;
typedef void function(void*) pf_sfSound_Stop;
typedef void function(void*, void*) pf_sfSound_SetBuffer;
typedef void* function(void*) pf_sfSound_GetBuffer;
typedef void function(void*, int) pf_sfSound_SetLoop;
typedef int function(void*) pf_sfSound_GetLoop;
typedef SoundStatus function(void*) pf_sfSound_GetStatus;
typedef void function(void*, float) pf_sfSound_SetPitch;
typedef void function(void*, float) pf_sfSound_SetVolume;
typedef void function(void*, float, float, float) pf_sfSound_SetPosition;
typedef float function(void*) pf_sfSound_GetPitch;
typedef float function(void*) pf_sfSound_GetVolume;
typedef void function(void*, float*, float*, float*) pf_sfSound_GetPosition;
typedef float function(void*) pf_sfSound_GetPlayingOffset;
typedef float function(void*) pf_sfSound_GetMinDistance;
typedef float function(void*) pf_sfSound_GetAttenuation;
typedef void function(void*, float) pf_sfSound_SetMinDistance;
typedef void function(void*, float) pf_sfSound_SetAttenuation;
typedef void function(void*, float) pf_sfSound_SetPlayingOffset;
static pf_sfSound_Create sfSound_Create;
static pf_sfSound_Destroy sfSound_Destroy;
static pf_sfSound_Play sfSound_Play;
static pf_sfSound_Pause sfSound_Pause;
static pf_sfSound_Stop sfSound_Stop;
static pf_sfSound_SetBuffer sfSound_SetBuffer;
static pf_sfSound_GetBuffer sfSound_GetBuffer;
static pf_sfSound_SetLoop sfSound_SetLoop;
static pf_sfSound_GetLoop sfSound_GetLoop;
static pf_sfSound_GetStatus sfSound_GetStatus;
static pf_sfSound_SetPitch sfSound_SetPitch;
static pf_sfSound_SetVolume sfSound_SetVolume;
static pf_sfSound_SetPosition sfSound_SetPosition;
static pf_sfSound_GetPitch sfSound_GetPitch;
static pf_sfSound_GetVolume sfSound_GetVolume;
static pf_sfSound_GetPosition sfSound_GetPosition;
static pf_sfSound_GetPlayingOffset sfSound_GetPlayingOffset;
static pf_sfSound_GetMinDistance sfSound_GetMinDistance;
static pf_sfSound_GetAttenuation sfSound_GetAttenuation;
static pf_sfSound_SetMinDistance sfSound_SetMinDistance;
static pf_sfSound_SetAttenuation sfSound_SetAttenuation;
static pf_sfSound_SetPlayingOffset sfSound_SetPlayingOffset;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfSound_Create = cast(pf_sfSound_Create)dll.getSymbol("sfSound_Create");
sfSound_Destroy = cast(pf_sfSound_Destroy)dll.getSymbol("sfSound_Destroy");
sfSound_Play = cast(pf_sfSound_Play)dll.getSymbol("sfSound_Play");
sfSound_Pause = cast(pf_sfSound_Pause)dll.getSymbol("sfSound_Pause");
sfSound_Stop = cast(pf_sfSound_Stop)dll.getSymbol("sfSound_Stop");
sfSound_SetBuffer = cast(pf_sfSound_SetBuffer)dll.getSymbol("sfSound_SetBuffer");
sfSound_GetBuffer = cast(pf_sfSound_GetBuffer)dll.getSymbol("sfSound_GetBuffer");
sfSound_SetLoop = cast(pf_sfSound_SetLoop)dll.getSymbol("sfSound_SetLoop");
sfSound_GetLoop = cast(pf_sfSound_GetLoop)dll.getSymbol("sfSound_GetLoop");
sfSound_GetStatus = cast(pf_sfSound_GetStatus)dll.getSymbol("sfSound_GetStatus");
sfSound_SetPitch = cast(pf_sfSound_SetPitch)dll.getSymbol("sfSound_SetPitch");
sfSound_SetVolume = cast(pf_sfSound_SetVolume)dll.getSymbol("sfSound_SetVolume");
sfSound_SetPosition = cast(pf_sfSound_SetPosition)dll.getSymbol("sfSound_SetPosition");
sfSound_GetPitch = cast(pf_sfSound_GetPitch)dll.getSymbol("sfSound_GetPitch");
sfSound_GetVolume = cast(pf_sfSound_GetVolume)dll.getSymbol("sfSound_GetVolume");
sfSound_GetPosition = cast(pf_sfSound_GetPosition)dll.getSymbol("sfSound_GetPosition");
sfSound_GetPlayingOffset = cast(pf_sfSound_GetPlayingOffset)dll.getSymbol("sfSound_GetPlayingOffset");
sfSound_GetMinDistance = cast(pf_sfSound_GetMinDistance)dll.getSymbol("sfSound_GetMinDistance");
sfSound_GetAttenuation = cast(pf_sfSound_GetAttenuation)dll.getSymbol("sfSound_GetAttenuation");
sfSound_SetMinDistance = cast(pf_sfSound_SetMinDistance)dll.getSymbol("sfSound_SetMinDistance");
sfSound_SetAttenuation = cast(pf_sfSound_SetAttenuation)dll.getSymbol("sfSound_SetAttenuation");
sfSound_SetPlayingOffset = cast(pf_sfSound_SetPlayingOffset)dll.getSymbol("sfSound_SetPlayingOffset");
}
}

View file

@ -0,0 +1,226 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.soundbuffer;
import dsfml.system.common;
import dsfml.system.exception;
import dsfml.system.stringutil;
/**
* SoundBuffer is the low-level for loading and manipulating
* sound buffers
*/
class SoundBuffer : DSFMLObject
{
/**
* Load the sound buffer from a file
*
* Params:
* filename = Path of the sound file to load
*
* Throws:
* LoadingException on failure
*/
this(char[] filename)
{
if (filename is null || filename.length == 0)
throw new LoadingException("LoadingException : Filename is invalid.");
super(sfSoundBuffer_CreateFromFile(toStringz(filename)));
}
/**
* Load the sound buffer from a file in memory
*
* Params:
* data = Array of file data in memory
*
* Throws:
* LoadingException on failure
*/
this(byte[] data)
{
if (data is null || data.length == 0)
throw new Exception("LoadingException : Memory stream is invalid.");
super(sfSoundBuffer_CreateFromMemory(data.ptr, data.length));
}
/**
* Load the sound buffer from an array of samples - assumed format for
* samples is 16 bits signed integer
*
* Params:
* samples = Array of samples in memory
* channelsCount = Number of channels (1 = mono, 2 = stereo, ...)
* sampleRate = Frequency (number of samples to play per second)
*
* Throws:
* LoadingException on failure
*/
this(short[] samples, uint channelsCount, uint sampleRate)
{
if (samples is null || samples.length == 0)
throw new Exception("LoadingException : Samples array is invalid.");
super(sfSoundBuffer_CreateFromSamples(samples.ptr, samples.length, channelsCount, sampleRate));
}
override void dispose()
{
sfSoundBuffer_Destroy(m_ptr);
}
/**
* Save the sound buffer to a file
*
* Params:
* filename = Path of the sound file to write
*
* Returns:
* True if saving has been successful
*/
bool saveToFile(char[] filename)
{
if (filename !is null && filename.length > 0 )
{
return cast(bool)sfSoundBuffer_SaveToFile(m_ptr, toStringz(filename));
}
return false;
}
/**
* Return the sound samples
*
* Returns:
* Array of sound samples, in 16 bits signed integer format
*/
short[] getSamples()
{
short* temp = null;
temp = sfSoundBuffer_GetSamples(m_ptr);
return temp is null ? null : temp[0..getSamplesCount];
}
/**
* Return the samples count
*
* Returns:
* Number of samples
*/
size_t getSamplesCount()
{
return sfSoundBuffer_GetSamplesCount(m_ptr);
}
/**
* Get the sample rate
*
* Returns:
* Sound frequency (number of samples per second)
*/
uint getSampleRate()
{
return sfSoundBuffer_GetSampleRate(m_ptr);
}
/**
* Return the number of channels (1 = mono, 2 = stereo, ...)
*
* Returns:
* Number of channels
*/
uint getChannelsCount()
{
return sfSoundBuffer_GetChannelsCount(m_ptr);
}
/**
* Get the sound duration
*
* Returns:
* Sound duration, in seconds
*/
float getDuration()
{
return sfSoundBuffer_GetDuration(m_ptr);
}
package:
this(void* ptr)
{
super(ptr, true);
}
private:
// External ====================================================================
extern (C)
{
typedef void* function(char*) pf_sfSoundBuffer_CreateFromFile;
typedef void* function(byte*, size_t) pf_sfSoundBuffer_CreateFromMemory;
typedef void* function(short*, size_t, uint, uint) pf_sfSoundBuffer_CreateFromSamples;
typedef void function(void*) pf_sfSoundBuffer_Destroy;
typedef int function(void*, char*) pf_sfSoundBuffer_SaveToFile;
typedef short* function(void*) pf_sfSoundBuffer_GetSamples;
typedef size_t function(void*) pf_sfSoundBuffer_GetSamplesCount;
typedef uint function(void*) pf_sfSoundBuffer_GetSampleRate;
typedef uint function(void*) pf_sfSoundBuffer_GetChannelsCount;
typedef float function(void*) pf_sfSoundBuffer_GetDuration;
static pf_sfSoundBuffer_CreateFromFile sfSoundBuffer_CreateFromFile;
static pf_sfSoundBuffer_CreateFromMemory sfSoundBuffer_CreateFromMemory;
static pf_sfSoundBuffer_CreateFromSamples sfSoundBuffer_CreateFromSamples;
static pf_sfSoundBuffer_Destroy sfSoundBuffer_Destroy;
static pf_sfSoundBuffer_SaveToFile sfSoundBuffer_SaveToFile;
static pf_sfSoundBuffer_GetSamples sfSoundBuffer_GetSamples;
static pf_sfSoundBuffer_GetSamplesCount sfSoundBuffer_GetSamplesCount;
static pf_sfSoundBuffer_GetSampleRate sfSoundBuffer_GetSampleRate;
static pf_sfSoundBuffer_GetChannelsCount sfSoundBuffer_GetChannelsCount;
static pf_sfSoundBuffer_GetDuration sfSoundBuffer_GetDuration;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfSoundBuffer_CreateFromFile = cast(pf_sfSoundBuffer_CreateFromFile)dll.getSymbol("sfSoundBuffer_CreateFromFile");
sfSoundBuffer_CreateFromMemory = cast(pf_sfSoundBuffer_CreateFromMemory)dll.getSymbol("sfSoundBuffer_CreateFromMemory");
sfSoundBuffer_CreateFromSamples = cast(pf_sfSoundBuffer_CreateFromSamples)dll.getSymbol("sfSoundBuffer_CreateFromSamples");
sfSoundBuffer_Destroy = cast(pf_sfSoundBuffer_Destroy)dll.getSymbol("sfSoundBuffer_Destroy");
sfSoundBuffer_SaveToFile = cast(pf_sfSoundBuffer_SaveToFile)dll.getSymbol("sfSoundBuffer_SaveToFile");
sfSoundBuffer_GetSamples = cast(pf_sfSoundBuffer_GetSamples)dll.getSymbol("sfSoundBuffer_GetSamples");
sfSoundBuffer_GetSamplesCount = cast(pf_sfSoundBuffer_GetSamplesCount)dll.getSymbol("sfSoundBuffer_GetSamplesCount");
sfSoundBuffer_GetSampleRate = cast(pf_sfSoundBuffer_GetSampleRate)dll.getSymbol("sfSoundBuffer_GetSampleRate");
sfSoundBuffer_GetChannelsCount = cast(pf_sfSoundBuffer_GetChannelsCount)dll.getSymbol("sfSoundBuffer_GetChannelsCount");
sfSoundBuffer_GetDuration = cast(pf_sfSoundBuffer_GetDuration)dll.getSymbol("sfSoundBuffer_GetDuration");
}
}

View file

@ -0,0 +1,105 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.soundbufferrecorder;
import dsfml.system.common;
import dsfml.audio.soundbuffer;
import dsfml.audio.soundrecorder;
/**
* Specialized sfSoundRecorder which saves the captured
* audio data into a sound buffer
*/
class SoundBufferRecorder : SoundRecorder
{
/**
* Constructor
*/
this()
{
super(sfSoundBufferRecorder_Create());
}
override void dispose()
{
sfSoundBufferRecorder_Destroy(m_ptr);
}
/**
* Get the sound buffer containing the captured audio data
*
* Returns:
* SoundBuffer containing the captured audio data
*
*/
SoundBuffer getBuffer()
{
return new SoundBuffer(sfSoundBufferRecorder_GetBuffer(m_ptr));
}
protected:
override bool onStart()
{
return true;
}
override bool onProcessSamples(short[] s)
{
return true;
}
override void onStop()
{
}
private:
// External ====================================================================
extern (C)
{
typedef void* function() pf_sfSoundBufferRecorder_Create;
typedef void function(void*) pf_sfSoundBufferRecorder_Destroy;
typedef void* function(void*) pf_sfSoundBufferRecorder_GetBuffer;
static pf_sfSoundBufferRecorder_Create sfSoundBufferRecorder_Create;
static pf_sfSoundBufferRecorder_Destroy sfSoundBufferRecorder_Destroy;
static pf_sfSoundBufferRecorder_GetBuffer sfSoundBufferRecorder_GetBuffer;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfSoundBufferRecorder_Create = cast(pf_sfSoundBufferRecorder_Create)dll.getSymbol("sfSoundBufferRecorder_Create");
sfSoundBufferRecorder_Destroy = cast(pf_sfSoundBufferRecorder_Destroy)dll.getSymbol("sfSoundBufferRecorder_Destroy");
sfSoundBufferRecorder_GetBuffer = cast(pf_sfSoundBufferRecorder_GetBuffer)dll.getSymbol("sfSoundBufferRecorder_GetBuffer");
}
}

View file

@ -0,0 +1,332 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.soundrecorder;
import dsfml.audio.soundbuffer;
import dsfml.system.alloc;
import dsfml.system.common;
import dsfml.system.sleep;
import dsfml.system.linkedlist;
import dsfml.system.mutex;
import dsfml.system.lock;
import dsfml.system.thread;
/**
* SoundRecorder is an interface for capturing sound data.
*
* $(B onProcessSamples and onStop will be called by a different thread, take care of synchronization issues.)
*
* Examples:
* -------
* class MySoundRecorder : SoundRecorder
* {
* this()
* {
*
* }
*
* protected bool onStart()
* {
* return true;
* }
*
* protected void onStop()
* {
*
* }
*
* protected bool onProcessSamples(out short[])
* {
* // Process data here
*
* return true; //return true to continue capture, else return false
* }
* }
* -------
*/
abstract class SoundRecorder : DSFMLObject
{
override void dispose()
{
if (m_flag)
stop();
m_instances.remove(m_id);
sfSoundRecorder_Destroy(m_ptr);
}
/**
* Start the capture.
*
* Only one capture can happen at the same time
*
* Params:
* sampleRate : Sound frequency (the more samples, the higher the quality)
* (44100 by default = CD quality)
*/
void start(uint sampleRate = 44100)
{
sfSoundRecorder_Start(m_ptr, sampleRate);
m_t = new Thread(&threadPoll);
m_t.launch();
}
/**
* Stop the capture
*/
void stop()
{
sfSoundRecorder_Stop(m_ptr);
m_flag = false;
m_t.wait();
m_t = null;
}
/**
* Get the sample rate
*
* Returns:
* Frequency, in samples per second
*/
uint getSampleRate()
{
return sfSoundRecorder_GetSampleRate(m_ptr);
}
/**
* Tell if the system supports sound capture.
* If not, this class won't be usable
*
* Returns:
* True if audio capture is supported
*
*/
static bool canCapture()
{
return cast(bool)sfSoundRecorder_CanCapture();
}
protected:
/**
* Protected constructor
*/
this()
{
m_id = ++seed;
m_instances[m_id] = this;
super(sfSoundRecorder_Create(&internalOnStart, &internalCallback, &internalOnStop, &m_id));
init(true);
}
this(void* ptr)
{
super(ptr);
init(false);
}
/**
* Start recording audio data
*
* Returns:
* False to abort recording audio data, true to start
*/
abstract bool onStart();
/**
* Stop recording audio data
*/
abstract void onStop();
/**
* callback function
*
* Parameters:
* samples = Array of samples
*
* Returns:
* true to continue recording, false to stop.
*/
abstract bool onProcessSamples(short[] samples);
bool m_disposed;
private:
/*
* an init function to initialize id of the object.
*/
void init(bool flag)
{
if (flag)
{
m_list = new LinkedList!(Samples)();
m_flag = true;
m_continue = true;
m_mutex = new Mutex();
}
}
void* m_userData;
int m_id;
static int seed = 0;
static SoundRecorder[int] m_instances;
/*
* Extern C callback function
*
* This function must be static for C interop. To retrieve the current
* instance, we retrieve id of the sender in the user data, and search associated instance
* in the associative array.
*
* We don't call delegate or derived class on that thread because GC is not aware of this thread
* instead we enqueue data informations in a queue and poll this queue with a managed thread.
*/
extern(C) static int internalCallback(short* s, size_t size, void* user)
{
int id;
// retrieve instance
if ((id = *cast(int*)(user)) in m_instances)
{
SoundRecorder temp = m_instances[id];
scope Lock l = new Lock(temp.m_mutex);
if (temp.m_continue)
// this new is allowed because Samples is an custom alloc class.
temp.m_list.enqueue(new Samples(s, size));
return temp.m_continue;
}
return false;
}
extern(C) static int internalOnStart(void* user)
{
int id;
bool ret = false;
if ((id = *cast(int*)(user)) in m_instances)
{
SoundRecorder temp = m_instances[id];
ret = temp.onStart();
}
return ret;
}
extern(C) static void internalOnStop(void* user)
{
// Nothing to do
}
/*
* Managed thread loop
*/
void threadPoll(void* user)
{
while (m_flag)
{
sleep(0.05f);
// if samples are available
if (!m_list.empty)
{
// Lock ressources
scope Lock l = new Lock(m_mutex);
Samples s = m_list.dequeue;
m_continue = this.onProcessSamples(s.data[0..s.length].dup);
delete s;
if (!m_continue)
{
// delete all samples left
foreach(Samples dummy; m_list)
delete dummy;
break;
}
}
}
onStop();
}
Mutex m_mutex;
bool m_flag;
bool m_continue = true;
LinkedList!(Samples) m_list;
Thread m_t;
// External ====================================================================
extern (C)
{
typedef void* function(int function(void*), int function(short*, size_t, void*), void function(void*), void*) pf_sfSoundRecorder_Create;
typedef void function(void*) pf_sfSoundRecorder_Destroy;
typedef void function(void*, uint SampleRate) pf_sfSoundRecorder_Start;
typedef void function(void*) pf_sfSoundRecorder_Stop;
typedef uint function(void*) pf_sfSoundRecorder_GetSampleRate;
typedef int function() pf_sfSoundRecorder_CanCapture;
static pf_sfSoundRecorder_Create sfSoundRecorder_Create;
static pf_sfSoundRecorder_Destroy sfSoundRecorder_Destroy;
static pf_sfSoundRecorder_Start sfSoundRecorder_Start;
static pf_sfSoundRecorder_Stop sfSoundRecorder_Stop;
static pf_sfSoundRecorder_GetSampleRate sfSoundRecorder_GetSampleRate;
static pf_sfSoundRecorder_CanCapture sfSoundRecorder_CanCapture;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfSoundRecorder_Create = cast(pf_sfSoundRecorder_Create)dll.getSymbol("sfSoundRecorder_Create");
sfSoundRecorder_Destroy = cast(pf_sfSoundRecorder_Destroy)dll.getSymbol("sfSoundRecorder_Destroy");
sfSoundRecorder_Start = cast(pf_sfSoundRecorder_Start)dll.getSymbol("sfSoundRecorder_Start");
sfSoundRecorder_Stop = cast(pf_sfSoundRecorder_Stop)dll.getSymbol("sfSoundRecorder_Stop");
sfSoundRecorder_GetSampleRate = cast(pf_sfSoundRecorder_GetSampleRate)dll.getSymbol("sfSoundRecorder_GetSampleRate");
sfSoundRecorder_CanCapture = cast(pf_sfSoundRecorder_CanCapture)dll.getSymbol("sfSoundRecorder_CanCapture");
}
}
// Use explicit alloc to allow instaciation by C thread
private class Samples
{
mixin Alloc;
this(short* data, size_t length)
{
this.data = data;
this.length = length;
}
public short* data;
public size_t length;
}

View file

@ -0,0 +1,34 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.soundstatus;
/// Enumeration of the sound states
enum SoundStatus
{
STOPPED, /// Sound / music is not playing
PAUSED, /// Sound / music is paused
PLAYING /// Sound / music is playing
}

View file

@ -0,0 +1,550 @@
/*
* DSFML - SFML Library binding in D language.
* Copyright (C) 2008 Julien Dagorn (sirjulio13@gmail.com)
*
* This software is provided 'as-is', without any express or
* implied warranty. In no event will the authors be held
* liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but
* is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* source distribution.
*/
module dsfml.audio.soundstream;
import dsfml.system.alloc;
import dsfml.system.common;
import dsfml.system.vector3;
import dsfml.system.linkedlist;
import dsfml.system.lock;
import dsfml.system.mutex;
import dsfml.system.sleep;
import dsfml.system.thread;
import dsfml.audio.sound;
import dsfml.audio.soundstatus;
/**
* SoundStream is a streamed sound, ie samples are acquired
* while the sound is playing. Use it for big sounds that would
* require hundreds of MB in memory, or for streaming sound from the network.
*
* SoundStream is a base class and cannot be instanciated directly.
*
* $(B onGetData override will be called by a different thread, take care of synchronization issues.) onStart is called by the thread which called .play().
*
* ------------------------
* class MySoundStream : SoundStream
* {
* this()
* {
* super(2, 44100); // you need to initialize the base class before any operation.
* }
* protected bool onGetData(out short[] data)
* {
* //You need to fill data array with some samples
*
* return true; //or false if you want to stop playback
* }
*
* protected bool onStart()
* {
* return true;
* }
* }
* ------------------------
*/
abstract class SoundStream : DSFMLObject
{
override void dispose()
{
stop();
sfSoundStream_Destroy(m_ptr);
s_instances.remove(m_id);
}
/**
* Start playing the stream
*/
void play()
{
m_flag = true;
sfSoundStream_Play(m_ptr);
if (getStatus() != SoundStatus.PAUSED)
{
m_t = new Thread(&threadPoll);
m_t.launch();
}
}
/**
* Pause the stream
*/
void pause()
{
sfSoundStream_Pause(m_ptr);
}
/**
* Stop the stream
*/
void stop()
{
m_flag = false;
sfSoundStream_Stop(m_ptr);
m_t.wait();
if (m_dummy !is null)
delete m_dummy;
}
/**
* Get number of channels of the stream
*
* Returns:
* number of channels
*/
uint getChannelsCount()
{
return m_channelsCount;
}
/**
* Get the sample rate of the stream
*
* Returns:
* sample rate
*/
uint getSampleRate()
{
return m_sampleRate;
}
/**
* Get the current status of the stream
*
* Returns:
* Current stream status
*/
SoundStatus getStatus()
{
return sfSoundStream_GetStatus(m_ptr);
}
/**
* Set the sound pitch.
* The default pitch is 1
*
* Params:
* pitch = New pitch
*/
void setPitch(float pitch)
{
sfSoundStream_SetPitch(m_ptr, pitch);
}
/**
* Set the sound volume.
* The default volume is 100
*
* Params:
* volume = Volume (in range [0, 100])
*/
void setVolume(float volume)
in
{
assert(volume >= 0.f && volume <= 100.f);
}
body
{
sfSoundStream_SetVolume(m_ptr, volume);
}
/*
* Set the sound position (take 3 values).
* The default position is (0, 0, 0)
*
* Params:
* x, y, z = Position of the sound in the world
*/
void setPosition(float x, float y, float z)
{
sfSoundStream_SetPosition(m_ptr, x, y, z);
}
/**
* Set the sound position (take 3 values).
* The default position is (0, 0, 0)
*
* Params:
* vec = Position of the sound in the world
*
*/
void setPosition(Vector3f vec)
{
sfSoundStream_SetPosition(m_ptr, vec.x, vec.y, vec.z);
}
/**
* Set the minimum distance - closer than this distance,
* the listener will hear the sound at its maximum volume.
* The default minimum distance is 1.0
*
* Params:
* minDistance = New minimum distance for the sound
*/
void setMinDistance(float minDistance)
{
sfSoundStream_SetMinDistance(m_ptr, minDistance);
}
/**
* Set the attenuation factor - the higher the attenuation, the
* more the sound will be attenuated with distance from listener.
* The default attenuation factor 1.0
*
* Params:
* attenuation = New attenuation factor for the sound
*
*/
void setAttenuation(float attenuation)
{
sfSoundStream_SetAttenuation(m_ptr, attenuation);
}
/**
* Get the pitch
*
* Returns:
* Pitch value
*/
float getPitch()
{
return sfSoundStream_GetPitch(m_ptr);
}
/**
* Get the volume
*
* Returns:
* Volume value (in range [1, 100])
*
*/
float getVolume()
{
return sfSoundStream_GetVolume(m_ptr);
}
/**
* Get the sound position
*
* Returns:
* Sound position
*/
Vector3f getPosition()
{
Vector3f vec;
sfSoundStream_GetPosition(m_ptr, &vec.x, &vec.y, &vec.z);
return vec;
}
/**
* Get the minimum distance
*
* Returns:
* Get the minimum distance of the sound
*/
float getMinDistance()
{
return sfSoundStream_GetMinDistance(m_ptr);
}
/**
* Get the attenuation
*
* Returns:
* Get the attenuation of the sound
*/
float getAttenuation()
{
return sfSoundStream_GetAttenuation(m_ptr);
}
/**
* Get the current playing offset of the stream
*
* Returns:
* current playing offset, in seconds.
*/
float getPlayingOffset()
{
return sfSoundStream_GetPlayingOffset(m_ptr);
}
/**
* Tell whether or not the stream is looping
*
* Returns:
* True if the music is looping, false otherwise
*/
bool getLoop()
{
if (m_ptr !is null)
return cast(bool)sfSoundStream_GetLoop(m_ptr);
return false;
}
/**
* Set the stream loop state.
*
* Disabled by default.
*
* Params:
* loop = true to play in loop, false to play once
*/
void setLoop(bool loop)
{
if (m_ptr !is null)
sfSoundStream_SetLoop(m_ptr, loop);
}
protected:
/**
* Protected constructor
*
* Params:
* channelsCount = number of channel
* sampleRate = sample rate of the stream
*
*/
this(uint channelsCount, uint sampleRate)
{
m_channelsCount = channelsCount;
m_sampleRate = sampleRate;
super(sfSoundStream_Create(&externalOnStart, &externalOnGetData, channelsCount, sampleRate, &m_id));
m_mutex = new Mutex();
m_samples = new LinkedList!(Data);
m_t = new Thread(&this.threadPoll);
m_id = ++s_seed;
s_instances[m_id] = this;
}
/**
* Called each time the stream restart
*
* Returns:
* false to abort the playback
*/
abstract bool onStart();
/**
* Called each time the stream needs new data.
* This method will be call by an other thread, take care of possible synchronisation issues.
*
* Params:
* data = array of samples to stream
*
* Returns:
* true to continue streaming, false to stop
*/
abstract bool onGetData(out short[] data);
private:
// Called sync when user calling play()
extern(C) static int externalOnStart(void* user)
{
int id;
if ((id = *cast(int*) user) in s_instances)
{
SoundStream temp = s_instances[id];
return (temp.m_flag = temp.onStart());
}
return true;
}
// C Thread callback (no allocation can be done)
extern (C) static int externalOnGetData(sfSoundStreamChunk* data, void* user)
{
int id, flag = false;
// Get calling soundStream
if ((id = *cast(int*) user) in s_instances)
{
SoundStream temp = s_instances[id];
//if no samples are available but streaming is not stopped, we sleep the thread
while (temp.m_samples.empty && temp.m_flag)
sleep(0.01f);
scope Lock l = new Lock(temp.m_mutex);
if (!temp.m_samples.empty)
{
if (temp.m_dummy !is null)
delete temp.m_dummy;
temp.m_dummy = temp.m_samples.dequeue;
if ((flag = temp.m_dummy.Flag) == true)
{
data.Samples = temp.m_dummy.Samples.ptr;
data.NbSamples = temp.m_dummy.Samples.length;
}
else
{
data.Samples = null;
data.NbSamples = 0;
}
}
}
return flag;
}
// Managed thread loop
void threadPoll(void* dummy)
{
short[] data;
bool ret = true;
// while streaming is active ...
while (ret && m_flag)
{
{
scope Lock l = new Lock(m_mutex);
// see how many samples are available (keep always 2 samples ready)
if (m_samples.getCount < 2)
{
// if we need new samples, lock and call derived class
ret = onGetData(data);
m_samples.enqueue(new Data(data, ret));
}
}
sleep(0.1f);
}
}
private class Data
{
short[] Samples;
bool Flag;
mixin Alloc;
this (short[] samples, bool flag)
{
this.Samples = samples;
this.Flag = flag;
}
}
Thread m_t;
Mutex m_mutex;
LinkedList!(Data) m_samples;
Data m_dummy;
bool m_flag;
uint m_channelsCount;
uint m_sampleRate;
int m_id;
static SoundStream[int] s_instances;
static int s_seed = 0;
// External ====================================================================
extern (C)
{
struct sfSoundStreamChunk{ short* Samples; uint NbSamples; }
alias int function(void*) sfSoundStreamStartCallback;
alias int function (sfSoundStreamChunk*, void*) sfSoundStreamGetDataCallback;
typedef void* function(sfSoundStreamStartCallback, sfSoundStreamGetDataCallback, uint, uint, void*) pf_sfSoundStream_Create;
typedef void function(void*) pf_sfSoundStream_Destroy;
typedef void function(void*) pf_sfSoundStream_Play;
typedef void function(void*) pf_sfSoundStream_Pause;
typedef void function(void*) pf_sfSoundStream_Stop;
typedef SoundStatus function(void*) pf_sfSoundStream_GetStatus;
typedef uint function(void*) pf_sfSoundStream_GetChannelsCount;
typedef uint function(void*) pf_sfSoundStream_GetSampleRate;
typedef void function(void*, float) pf_sfSoundStream_SetPitch;
typedef void function(void*, float) pf_sfSoundStream_SetVolume;
typedef void function(void*, float, float, float) pf_sfSoundStream_SetPosition;
typedef void function(void*, float) pf_sfSoundStream_SetMinDistance;
typedef void function(void*, float) pf_sfSoundStream_SetAttenuation;
typedef float function(void*) pf_sfSoundStream_GetPitch;
typedef float function(void*) pf_sfSoundStream_GetVolume;
typedef void function(void*, float*, float*, float*) pf_sfSoundStream_GetPosition;
typedef float function(void*) pf_sfSoundStream_GetMinDistance;
typedef float function(void*) pf_sfSoundStream_GetAttenuation;
typedef float function(void*) pf_sfSoundStream_GetPlayingOffset;
typedef int function(void*) pf_sfSoundStream_GetLoop;
typedef void function(void*, int) pf_sfSoundStream_SetLoop;
static pf_sfSoundStream_Create sfSoundStream_Create;
static pf_sfSoundStream_Destroy sfSoundStream_Destroy;
static pf_sfSoundStream_Play sfSoundStream_Play;
static pf_sfSoundStream_Pause sfSoundStream_Pause;
static pf_sfSoundStream_Stop sfSoundStream_Stop;
static pf_sfSoundStream_GetStatus sfSoundStream_GetStatus;
static pf_sfSoundStream_GetChannelsCount sfSoundStream_GetChannelsCount;
static pf_sfSoundStream_GetSampleRate sfSoundStream_GetSampleRate;
static pf_sfSoundStream_SetPitch sfSoundStream_SetPitch;
static pf_sfSoundStream_SetVolume sfSoundStream_SetVolume;
static pf_sfSoundStream_SetPosition sfSoundStream_SetPosition;
static pf_sfSoundStream_SetMinDistance sfSoundStream_SetMinDistance;
static pf_sfSoundStream_SetAttenuation sfSoundStream_SetAttenuation;
static pf_sfSoundStream_GetPitch sfSoundStream_GetPitch;
static pf_sfSoundStream_GetVolume sfSoundStream_GetVolume;
static pf_sfSoundStream_GetPosition sfSoundStream_GetPosition;
static pf_sfSoundStream_GetMinDistance sfSoundStream_GetMinDistance;
static pf_sfSoundStream_GetAttenuation sfSoundStream_GetAttenuation;
static pf_sfSoundStream_GetPlayingOffset sfSoundStream_GetPlayingOffset;
static pf_sfSoundStream_GetLoop sfSoundStream_GetLoop;
static pf_sfSoundStream_SetLoop sfSoundStream_SetLoop;
}
static this()
{
DllLoader dll = DllLoader.load("csfml-audio");
sfSoundStream_Create = cast(pf_sfSoundStream_Create)dll.getSymbol("sfSoundStream_Create");
sfSoundStream_Destroy = cast(pf_sfSoundStream_Destroy)dll.getSymbol("sfSoundStream_Destroy");
sfSoundStream_Play = cast(pf_sfSoundStream_Play)dll.getSymbol("sfSoundStream_Play");
sfSoundStream_Pause = cast(pf_sfSoundStream_Pause)dll.getSymbol("sfSoundStream_Pause");
sfSoundStream_Stop = cast(pf_sfSoundStream_Stop)dll.getSymbol("sfSoundStream_Stop");
sfSoundStream_GetStatus = cast(pf_sfSoundStream_GetStatus)dll.getSymbol("sfSoundStream_GetStatus");
sfSoundStream_GetChannelsCount = cast(pf_sfSoundStream_GetChannelsCount)dll.getSymbol("sfSoundStream_GetChannelsCount");
sfSoundStream_GetSampleRate = cast(pf_sfSoundStream_GetSampleRate)dll.getSymbol("sfSoundStream_GetSampleRate");
sfSoundStream_SetPitch = cast(pf_sfSoundStream_SetPitch)dll.getSymbol("sfSoundStream_SetPitch");
sfSoundStream_SetVolume = cast(pf_sfSoundStream_SetVolume)dll.getSymbol("sfSoundStream_SetVolume");
sfSoundStream_SetPosition = cast(pf_sfSoundStream_SetPosition)dll.getSymbol("sfSoundStream_SetPosition");
sfSoundStream_SetMinDistance = cast(pf_sfSoundStream_SetMinDistance)dll.getSymbol("sfSoundStream_SetMinDistance");
sfSoundStream_SetAttenuation = cast(pf_sfSoundStream_SetAttenuation)dll.getSymbol("sfSoundStream_SetAttenuation");
sfSoundStream_GetPitch = cast(pf_sfSoundStream_GetPitch)dll.getSymbol("sfSoundStream_GetPitch");
sfSoundStream_GetVolume = cast(pf_sfSoundStream_GetVolume)dll.getSymbol("sfSoundStream_GetVolume");
sfSoundStream_GetPosition = cast(pf_sfSoundStream_GetPosition)dll.getSymbol("sfSoundStream_GetPosition");
sfSoundStream_GetMinDistance = cast(pf_sfSoundStream_GetMinDistance)dll.getSymbol("sfSoundStream_GetMinDistance");
sfSoundStream_GetAttenuation = cast(pf_sfSoundStream_GetAttenuation)dll.getSymbol("sfSoundStream_GetAttenuation");
sfSoundStream_GetPlayingOffset = cast(pf_sfSoundStream_GetPlayingOffset)dll.getSymbol("sfSoundStream_GetPlayingOffset");
sfSoundStream_GetLoop = cast(pf_sfSoundStream_GetLoop)dll.getSymbol("sfSoundStream_GetLoop");
sfSoundStream_SetLoop = cast(pf_sfSoundStream_SetLoop)dll.getSymbol("sfSoundStream_SetLoop");
}
}