1
////////////////////////////////////////////////////////////
3
// PySFML - Python binding for SFML (Simple and Fast Multimedia Library)
4
// Copyright (C) 2007, 2008 Rémi Koenig (remi.k2620@gmail.com)
6
// This software is provided 'as-is', without any express or implied warranty.
7
// In no event will the authors be held liable for any damages arising from the use of this software.
9
// Permission is granted to anyone to use this software for any purpose,
10
// including commercial applications, and to alter it and redistribute it freely,
11
// subject to the following restrictions:
13
// 1. The origin of this software must not be misrepresented;
14
// you must not claim that you wrote the original software.
15
// If you use this software in a product, an acknowledgment
16
// in the product documentation would be appreciated but is not required.
18
// 2. Altered source versions must be plainly marked as such,
19
// and must not be misrepresented as being the original software.
21
// 3. This notice may not be removed or altered from any source distribution.
23
////////////////////////////////////////////////////////////
25
#include "SoundBuffer.hpp"
31
PySfSoundBuffer_dealloc(PySfSoundBuffer *self)
38
PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
41
PySfSoundBuffer_LoadFromFile(PySfSoundBuffer *self, PyObject *args)
43
load_from_file(self, args);
47
PySfSoundBuffer_LoadFromMemory(PySfSoundBuffer* self, PyObject *args)
49
unsigned int SizeInBytes;
52
if (!PyArg_ParseTuple(args, "s#:SoundBuffer.LoadFromMemory", &Data, &SizeInBytes))
55
return PyBool_FromLong(self->obj->LoadFromMemory(Data, (std::size_t) SizeInBytes));
59
PySfSoundBuffer_LoadFromSamples(PySfSoundBuffer* self, PyObject *args)
61
unsigned int SizeInBytes, ChannelsCount, SampleRate;
64
if (!PyArg_ParseTuple(args, "s#II:SoundBuffer.LoadFromSamples", &Data, &SizeInBytes, &ChannelsCount, &SampleRate))
67
return PyBool_FromLong(self->obj->LoadFromSamples((const sf::Int16*)Data, (std::size_t) SizeInBytes/2, ChannelsCount, SampleRate));
71
PySfSoundBuffer_GetSamples(PySfSoundBuffer *self)
74
return PyBytes_FromStringAndSize((const char *)(self->obj->GetSamples()), self->obj->GetSamplesCount()*2);
76
return PyString_FromStringAndSize((const char *)(self->obj->GetSamples()), self->obj->GetSamplesCount()*2);
81
PySfSoundBuffer_SaveToFile(PySfSoundBuffer *self, PyObject *args)
83
save_to_file(self, args);
87
PySfSoundBuffer_GetDuration(PySfSoundBuffer *self)
89
return PyFloat_FromDouble((double)(self->obj->GetDuration()));
93
PySfSoundBuffer_GetChannelsCount(PySfSoundBuffer *self)
95
return PyLong_FromUnsignedLong(self->obj->GetChannelsCount());
99
PySfSoundBuffer_GetSampleRate(PySfSoundBuffer *self)
101
return PyLong_FromUnsignedLong(self->obj->GetSampleRate());
105
PySfSoundBuffer_GetSamplesCount(PySfSoundBuffer *self)
107
return PyLong_FromUnsignedLong(self->obj->GetSamplesCount());
111
static PyMethodDef PySfSoundBuffer_methods[] = {
112
{"LoadFromFile", (PyCFunction)PySfSoundBuffer_LoadFromFile, METH_O, "LoadFromFile(FileName)\nLoad the sound buffer from a file. Returns True if loading has been successful.\n Filename : Path of the sound file to load"},
113
{"SaveToFile", (PyCFunction)PySfSoundBuffer_SaveToFile, METH_O, "SaveToFile(Filename)\nSave the sound buffer to a file. Returns True if saving has been successful.\n Filename : Path of the sound file to write"},
114
{"LoadFromMemory", (PyCFunction)PySfSoundBuffer_LoadFromMemory, METH_O, "LoadFromMemory(Data)\nLoad the sound buffer from a string in memory.\n Data : string representing the file data in memory "},
115
{"LoadFromSamples", (PyCFunction)PySfSoundBuffer_LoadFromSamples, METH_VARARGS, "LoadFromSamples(Samples, ChannelsCount, SampleRate)\nLoad the sound buffer from an array of samples - assumed format for samples is 16 bits signed integer.\n\
116
Samples : Pointer to the samples in memory\n\
117
ChannelsCount : Number of channels (1 = mono, 2 = stereo, ...)\n\
118
SampleRate : Frequency (number of samples to play per second)"},
119
{"GetDuration", (PyCFunction)PySfSoundBuffer_GetDuration, METH_NOARGS, "GetDuration()\nGet the sound duration."},
120
{"GetChannelsCount", (PyCFunction)PySfSoundBuffer_GetChannelsCount, METH_NOARGS, "GetChannelsCount()\nReturn the number of channels (1 = mono, 2 = stereo)."},
121
{"GetSampleRate", (PyCFunction)PySfSoundBuffer_GetSampleRate, METH_NOARGS, "GetSampleRate()\nGet the sound frequency (sample rate)."},
122
{"GetSamplesCount", (PyCFunction)PySfSoundBuffer_GetSamplesCount, METH_NOARGS, "GetSamplesCount()\nReturn the samples count."},
123
{"GetSamples", (PyCFunction)PySfSoundBuffer_GetSamples, METH_NOARGS, "GetSamples()\nReturn the sound samples as a string."},
124
{NULL} /* Sentinel */
127
PyTypeObject PySfSoundBufferType = {
129
"SoundBuffer", /*tp_name*/
130
sizeof(PySfSoundBuffer), /*tp_basicsize*/
132
(destructor)PySfSoundBuffer_dealloc, /*tp_dealloc*/
139
0, /*tp_as_sequence*/
147
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
148
"sf.SoundBuffer is the low-level for loading and manipulating sound buffers.\n\
149
Default constructor : SoundBuffer()\n\
150
Copy constructor : SoundBuffer(Copy) where Copy is a sf.SoundBuffer instance.", /* tp_doc */
153
0, /* tp_richcompare */
154
0, /* tp_weaklistoffset */
157
PySfSoundBuffer_methods, /* tp_methods */
162
0, /* tp_descr_get */
163
0, /* tp_descr_set */
164
0, /* tp_dictoffset */
167
PySfSoundBuffer_new, /* tp_new */
171
PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
173
PySfSoundBuffer *self;
174
self = (PySfSoundBuffer *)type->tp_alloc(type, 0);
177
PySfSoundBuffer *Copy=NULL;
178
if (PyArg_ParseTuple(args, "O!:SoundBuffer.__init__", &PySfSoundBufferType, &Copy))
180
self->obj = new sf::SoundBuffer(*(Copy->obj));
181
return (PyObject *)self;
184
self->obj = new sf::SoundBuffer();
186
return (PyObject *)self;
190
GetNewPySfSoundBuffer()
192
return PyObject_New(PySfSoundBuffer, &PySfSoundBufferType);