2009-01-29 00:18:34 +08:00
////////////////////////////////////////////////////////////
//
// PySFML - Python binding for SFML (Simple and Fast Multimedia Library)
// Copyright (C) 2007, 2008 Rémi Koenig (remi.k2620@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.
//
////////////////////////////////////////////////////////////
# include "SoundBuffer.hpp"
2009-02-26 20:36:06 +08:00
# include "compat.hpp"
2009-01-29 00:18:34 +08:00
static void
PySfSoundBuffer_dealloc ( PySfSoundBuffer * self )
{
delete self - > obj ;
2009-02-26 20:36:06 +08:00
free_object ( self ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
2009-03-15 06:28:16 +08:00
PySfSoundBuffer_new ( PyTypeObject * type , PyObject * args , PyObject * kwds ) ;
2009-01-29 00:18:34 +08:00
static PyObject *
PySfSoundBuffer_LoadFromFile ( PySfSoundBuffer * self , PyObject * args )
{
2009-02-26 20:36:06 +08:00
load_from_file ( self , args ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
PySfSoundBuffer_LoadFromMemory ( PySfSoundBuffer * self , PyObject * args )
{
unsigned int SizeInBytes ;
char * Data ;
2009-02-26 20:36:06 +08:00
if ( ! PyArg_ParseTuple ( args , " s#:SoundBuffer.LoadFromMemory " , & Data , & SizeInBytes ) )
2009-01-29 00:18:34 +08:00
return NULL ;
2009-02-26 20:36:06 +08:00
return PyBool_FromLong ( self - > obj - > LoadFromMemory ( Data , ( std : : size_t ) SizeInBytes ) ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
PySfSoundBuffer_LoadFromSamples ( PySfSoundBuffer * self , PyObject * args )
{
unsigned int SizeInBytes , ChannelsCount , SampleRate ;
char * Data ;
2009-02-26 20:36:06 +08:00
if ( ! PyArg_ParseTuple ( args , " s#II:SoundBuffer.LoadFromSamples " , & Data , & SizeInBytes , & ChannelsCount , & SampleRate ) )
2009-01-29 00:18:34 +08:00
return NULL ;
2009-02-26 20:36:06 +08:00
return PyBool_FromLong ( self - > obj - > LoadFromSamples ( ( const sf : : Int16 * ) Data , ( std : : size_t ) SizeInBytes / 2 , ChannelsCount , SampleRate ) ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
PySfSoundBuffer_GetSamples ( PySfSoundBuffer * self )
{
2009-02-26 20:36:06 +08:00
# ifdef IS_PY3K
return PyBytes_FromStringAndSize ( ( const char * ) ( self - > obj - > GetSamples ( ) ) , self - > obj - > GetSamplesCount ( ) * 2 ) ;
# else
2009-01-29 00:18:34 +08:00
return PyString_FromStringAndSize ( ( const char * ) ( self - > obj - > GetSamples ( ) ) , self - > obj - > GetSamplesCount ( ) * 2 ) ;
2009-02-26 20:36:06 +08:00
# endif
2009-01-29 00:18:34 +08:00
}
static PyObject *
PySfSoundBuffer_SaveToFile ( PySfSoundBuffer * self , PyObject * args )
{
2009-02-26 20:36:06 +08:00
save_to_file ( self , args ) ;
2009-01-29 00:18:34 +08:00
}
static PyObject *
PySfSoundBuffer_GetDuration ( PySfSoundBuffer * self )
{
return PyFloat_FromDouble ( ( double ) ( self - > obj - > GetDuration ( ) ) ) ;
}
static PyObject *
PySfSoundBuffer_GetChannelsCount ( PySfSoundBuffer * self )
{
return PyLong_FromUnsignedLong ( self - > obj - > GetChannelsCount ( ) ) ;
}
static PyObject *
PySfSoundBuffer_GetSampleRate ( PySfSoundBuffer * self )
{
return PyLong_FromUnsignedLong ( self - > obj - > GetSampleRate ( ) ) ;
}
static PyObject *
PySfSoundBuffer_GetSamplesCount ( PySfSoundBuffer * self )
{
return PyLong_FromUnsignedLong ( self - > obj - > GetSamplesCount ( ) ) ;
}
static PyMethodDef PySfSoundBuffer_methods [ ] = {
{ " LoadFromFile " , ( PyCFunction ) PySfSoundBuffer_LoadFromFile , METH_O , " LoadFromFile(FileName) \n Load the sound buffer from a file. Returns True if loading has been successful. \n Filename : Path of the sound file to load " } ,
{ " SaveToFile " , ( PyCFunction ) PySfSoundBuffer_SaveToFile , METH_O , " SaveToFile(Filename) \n Save the sound buffer to a file. Returns True if saving has been successful. \n Filename : Path of the sound file to write " } ,
{ " LoadFromMemory " , ( PyCFunction ) PySfSoundBuffer_LoadFromMemory , METH_O , " LoadFromMemory(Data) \n Load the sound buffer from a string in memory. \n Data : string representing the file data in memory " } ,
{ " LoadFromSamples " , ( PyCFunction ) PySfSoundBuffer_LoadFromSamples , METH_VARARGS , " LoadFromSamples(Samples, ChannelsCount, SampleRate) \n Load the sound buffer from an array of samples - assumed format for samples is 16 bits signed integer. \n \
Samples : Pointer to the samples in memory \ n \
ChannelsCount : Number of channels ( 1 = mono , 2 = stereo , . . . ) \ n \
SampleRate : Frequency ( number of samples to play per second ) " },
{ " GetDuration " , ( PyCFunction ) PySfSoundBuffer_GetDuration , METH_NOARGS , " GetDuration() \n Get the sound duration. " } ,
{ " GetChannelsCount " , ( PyCFunction ) PySfSoundBuffer_GetChannelsCount , METH_NOARGS , " GetChannelsCount() \n Return the number of channels (1 = mono, 2 = stereo). " } ,
{ " GetSampleRate " , ( PyCFunction ) PySfSoundBuffer_GetSampleRate , METH_NOARGS , " GetSampleRate() \n Get the sound frequency (sample rate). " } ,
{ " GetSamplesCount " , ( PyCFunction ) PySfSoundBuffer_GetSamplesCount , METH_NOARGS , " GetSamplesCount() \n Return the samples count. " } ,
{ " GetSamples " , ( PyCFunction ) PySfSoundBuffer_GetSamples , METH_NOARGS , " GetSamples() \n Return the sound samples as a string. " } ,
{ NULL } /* Sentinel */
} ;
PyTypeObject PySfSoundBufferType = {
2009-02-26 20:36:06 +08:00
head_init
2009-01-29 00:18:34 +08:00
" SoundBuffer " , /*tp_name*/
sizeof ( PySfSoundBuffer ) , /*tp_basicsize*/
0 , /*tp_itemsize*/
( destructor ) PySfSoundBuffer_dealloc , /*tp_dealloc*/
0 , /*tp_print*/
0 , /*tp_getattr*/
0 , /*tp_setattr*/
0 , /*tp_compare*/
0 , /*tp_repr*/
0 , /*tp_as_number*/
0 , /*tp_as_sequence*/
0 , /*tp_as_mapping*/
0 , /*tp_hash */
0 , /*tp_call*/
0 , /*tp_str*/
0 , /*tp_getattro*/
0 , /*tp_setattro*/
0 , /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE , /*tp_flags*/
" sf.SoundBuffer is the low-level for loading and manipulating sound buffers. \n \
Default constructor : SoundBuffer ( ) \ n \
Copy constructor : SoundBuffer ( Copy ) where Copy is a sf . SoundBuffer instance . " , /* tp_doc */
0 , /* tp_traverse */
0 , /* tp_clear */
0 , /* tp_richcompare */
0 , /* tp_weaklistoffset */
0 , /* tp_iter */
0 , /* tp_iternext */
PySfSoundBuffer_methods , /* tp_methods */
2009-02-26 20:36:06 +08:00
0 , /* tp_members */
2009-01-29 00:18:34 +08:00
0 , /* tp_getset */
0 , /* tp_base */
0 , /* tp_dict */
0 , /* tp_descr_get */
0 , /* tp_descr_set */
0 , /* tp_dictoffset */
2009-03-15 06:28:16 +08:00
0 , /* tp_init */
2009-01-29 00:18:34 +08:00
0 , /* tp_alloc */
PySfSoundBuffer_new , /* tp_new */
} ;
2009-03-15 06:28:16 +08:00
static PyObject *
PySfSoundBuffer_new ( PyTypeObject * type , PyObject * args , PyObject * kwds )
2009-01-29 00:18:34 +08:00
{
2009-03-15 06:28:16 +08:00
PySfSoundBuffer * self ;
self = ( PySfSoundBuffer * ) type - > tp_alloc ( type , 0 ) ;
if ( self ! = NULL )
2009-01-29 00:18:34 +08:00
{
2009-03-15 06:28:16 +08:00
PySfSoundBuffer * Copy = NULL ;
if ( PyArg_ParseTuple ( args , " O!:SoundBuffer.__init__ " , & PySfSoundBufferType , & Copy ) )
{
self - > obj = new sf : : SoundBuffer ( * ( Copy - > obj ) ) ;
return ( PyObject * ) self ;
}
PyErr_Clear ( ) ;
2009-01-29 00:18:34 +08:00
self - > obj = new sf : : SoundBuffer ( ) ;
2009-03-15 06:28:16 +08:00
}
return ( PyObject * ) self ;
2009-01-29 00:18:34 +08:00
}
PySfSoundBuffer *
GetNewPySfSoundBuffer ( )
{
2009-03-15 06:28:16 +08:00
return PyObject_New ( PySfSoundBuffer , & PySfSoundBufferType ) ;
2009-01-29 00:18:34 +08:00
}