~ubuntu-branches/ubuntu/trusty/python-sfml/trusty

« back to all changes in this revision

Viewing changes to python/src/SoundBuffer.cpp

  • Committer: Package Import Robot
  • Author(s): James Cowgill
  • Date: 2013-12-09 17:50:52 UTC
  • mfrom: (1.1.3) (8.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131209175052-11v6drpb6g3yksst
Tags: 1.5.1.is.1.3+dfsg-1
* New upstream version 1.3 (from python-sfml.org)
  - This is a complete rewrite of the Python bindings for SFML2, and
    the new maintainer is using a different version numbering scheme.
* Added myself to the list of uploaders
* Change package priority from extra to optional
* Bumped standards version (to 3.9.5) and debhelper compat (to 9)
* Added Python 3 and documentation packages
* Improve package description for debug packages

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////
2
 
//
3
 
// PySFML - Python binding for SFML (Simple and Fast Multimedia Library)
4
 
// Copyright (C) 2007, 2008 Rémi Koenig (remi.k2620@gmail.com)
5
 
//
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.
8
 
//
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:
12
 
//
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.
17
 
//
18
 
// 2. Altered source versions must be plainly marked as such,
19
 
//    and must not be misrepresented as being the original software.
20
 
//
21
 
// 3. This notice may not be removed or altered from any source distribution.
22
 
//
23
 
////////////////////////////////////////////////////////////
24
 
 
25
 
#include "SoundBuffer.hpp"
26
 
 
27
 
#include "compat.hpp"
28
 
 
29
 
 
30
 
static void
31
 
PySfSoundBuffer_dealloc(PySfSoundBuffer *self)
32
 
{
33
 
        delete self->obj;
34
 
        free_object(self);
35
 
}
36
 
 
37
 
static PyObject *
38
 
PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
39
 
 
40
 
static PyObject*
41
 
PySfSoundBuffer_LoadFromFile(PySfSoundBuffer *self, PyObject *args)
42
 
{
43
 
        load_from_file(self, args);
44
 
}
45
 
 
46
 
static PyObject *
47
 
PySfSoundBuffer_LoadFromMemory(PySfSoundBuffer* self, PyObject *args)
48
 
{
49
 
        unsigned int SizeInBytes;
50
 
        char *Data;
51
 
 
52
 
        if (!PyArg_ParseTuple(args, "s#:SoundBuffer.LoadFromMemory", &Data, &SizeInBytes))
53
 
                return NULL; 
54
 
 
55
 
        return PyBool_FromLong(self->obj->LoadFromMemory(Data, (std::size_t) SizeInBytes));
56
 
}
57
 
 
58
 
static PyObject *
59
 
PySfSoundBuffer_LoadFromSamples(PySfSoundBuffer* self, PyObject *args)
60
 
{
61
 
        unsigned int SizeInBytes, ChannelsCount, SampleRate;
62
 
        char *Data;
63
 
 
64
 
        if (!PyArg_ParseTuple(args, "s#II:SoundBuffer.LoadFromSamples", &Data, &SizeInBytes, &ChannelsCount, &SampleRate))
65
 
                return NULL; 
66
 
 
67
 
        return PyBool_FromLong(self->obj->LoadFromSamples((const sf::Int16*)Data, (std::size_t) SizeInBytes/2, ChannelsCount, SampleRate));
68
 
}
69
 
 
70
 
static PyObject*
71
 
PySfSoundBuffer_GetSamples(PySfSoundBuffer *self)
72
 
{
73
 
#ifdef IS_PY3K
74
 
        return PyBytes_FromStringAndSize((const char *)(self->obj->GetSamples()), self->obj->GetSamplesCount()*2);
75
 
#else
76
 
        return PyString_FromStringAndSize((const char *)(self->obj->GetSamples()), self->obj->GetSamplesCount()*2);
77
 
#endif
78
 
}
79
 
 
80
 
static PyObject*
81
 
PySfSoundBuffer_SaveToFile(PySfSoundBuffer *self, PyObject *args)
82
 
{
83
 
        save_to_file(self, args);
84
 
}
85
 
 
86
 
static PyObject*
87
 
PySfSoundBuffer_GetDuration(PySfSoundBuffer *self)
88
 
{
89
 
        return PyFloat_FromDouble((double)(self->obj->GetDuration()));
90
 
}
91
 
 
92
 
static PyObject*
93
 
PySfSoundBuffer_GetChannelsCount(PySfSoundBuffer *self)
94
 
{
95
 
        return PyLong_FromUnsignedLong(self->obj->GetChannelsCount());
96
 
}
97
 
 
98
 
static PyObject*
99
 
PySfSoundBuffer_GetSampleRate(PySfSoundBuffer *self)
100
 
{
101
 
        return PyLong_FromUnsignedLong(self->obj->GetSampleRate());
102
 
}
103
 
 
104
 
static PyObject*
105
 
PySfSoundBuffer_GetSamplesCount(PySfSoundBuffer *self)
106
 
{
107
 
        return PyLong_FromUnsignedLong(self->obj->GetSamplesCount());
108
 
}
109
 
 
110
 
 
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 */
125
 
};
126
 
 
127
 
PyTypeObject PySfSoundBufferType = {
128
 
        head_init
129
 
        "SoundBuffer",                  /*tp_name*/
130
 
        sizeof(PySfSoundBuffer), /*tp_basicsize*/
131
 
        0,                                              /*tp_itemsize*/
132
 
        (destructor)PySfSoundBuffer_dealloc, /*tp_dealloc*/
133
 
        0,                                              /*tp_print*/
134
 
        0,                                              /*tp_getattr*/
135
 
        0,                                              /*tp_setattr*/
136
 
        0,                                              /*tp_compare*/
137
 
        0,                                              /*tp_repr*/
138
 
        0,                                              /*tp_as_number*/
139
 
        0,                                              /*tp_as_sequence*/
140
 
        0,                                              /*tp_as_mapping*/
141
 
        0,                                              /*tp_hash */
142
 
        0,                                              /*tp_call*/
143
 
        0,                                              /*tp_str*/
144
 
        0,                                              /*tp_getattro*/
145
 
        0,                                              /*tp_setattro*/
146
 
        0,                                              /*tp_as_buffer*/
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 */
151
 
        0,                                              /* tp_traverse */
152
 
        0,                                              /* tp_clear */
153
 
        0,                                              /* tp_richcompare */
154
 
        0,                                              /* tp_weaklistoffset */
155
 
        0,                                              /* tp_iter */
156
 
        0,                                              /* tp_iternext */
157
 
        PySfSoundBuffer_methods, /* tp_methods */
158
 
        0,                                              /* tp_members */
159
 
        0,                                              /* tp_getset */
160
 
        0,                                              /* tp_base */
161
 
        0,                                              /* tp_dict */
162
 
        0,                                              /* tp_descr_get */
163
 
        0,                                              /* tp_descr_set */
164
 
        0,                                              /* tp_dictoffset */
165
 
        0,                                              /* tp_init */
166
 
        0,                                              /* tp_alloc */
167
 
        PySfSoundBuffer_new,    /* tp_new */
168
 
};
169
 
 
170
 
static PyObject *
171
 
PySfSoundBuffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
172
 
{
173
 
        PySfSoundBuffer *self;
174
 
        self = (PySfSoundBuffer *)type->tp_alloc(type, 0);
175
 
        if (self != NULL)
176
 
        {
177
 
                PySfSoundBuffer *Copy=NULL;
178
 
                if (PyArg_ParseTuple(args, "O!:SoundBuffer.__init__", &PySfSoundBufferType, &Copy))
179
 
                {
180
 
                        self->obj = new sf::SoundBuffer(*(Copy->obj));
181
 
                        return (PyObject *)self;
182
 
                }
183
 
                PyErr_Clear();
184
 
                self->obj = new sf::SoundBuffer();
185
 
        }
186
 
        return (PyObject *)self;
187
 
}
188
 
 
189
 
PySfSoundBuffer *
190
 
GetNewPySfSoundBuffer()
191
 
{
192
 
        return PyObject_New(PySfSoundBuffer, &PySfSoundBufferType);
193
 
}
194