2
* $Id: Sound.c 14444 2008-04-16 22:40:48Z hos $
4
* ***** BEGIN GPL LICENSE BLOCK *****
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software Foundation,
18
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21
* All rights reserved.
23
* This is a new part of Blender.
25
* Contributor(s): Chris Keith
27
* ***** END GPL LICENSE BLOCK *****
30
#include "Sound.h" /*This must come first*/
32
#include "BKE_global.h"
34
#include "BLI_blenlib.h"
35
#include "BKE_sound.h"
36
#include "BKE_library.h"
37
#include "BIF_editsound.h"
38
#include "BKE_packedFile.h"
39
#include "mydevice.h" /* redraw defines */
40
#include "gen_utils.h"
41
#include "gen_library.h"
42
#include "DNA_space_types.h" /* for FILE_MAXDIR only */
44
/*****************************************************************************/
45
/* Python BPy_Sound defaults: */
46
/*****************************************************************************/
48
#define EXPP_SND_volume_MIN 0.0
49
#define EXPP_SND_volume_MAX 1.0
50
#define EXPP_SND_pitch_MIN -12.0
51
#define EXPP_SND_pitch_MAX 12.0
52
#define EXPP_SND_attenuation_MIN 0.0
53
#define EXPP_SND_attenuation_MAX 5.0
55
/*****************************************************************************/
56
/* Python API function prototypes for the Sound module. */
57
/*****************************************************************************/
58
static PyObject *M_Sound_Get( PyObject * self, PyObject * args );
59
static PyObject *M_Sound_Load( PyObject * self, PyObject * value );
61
/************************************************************************/
62
/* The following string definitions are used for documentation strings. */
63
/* In Python these will be written to the console when doing a */
64
/* Blender.Sound.__doc__ */
65
/************************************************************************/
66
static char M_Sound_doc[] = "The Blender Sound module\n\n";
68
static char M_Sound_Get_doc[] =
69
"(name) - return the sound with the name 'name', \
70
returns None if not found.\n If 'name' is not specified, \
71
it returns a list of all sounds in the\ncurrent scene.";
73
static char M_Sound_Load_doc[] =
74
"(filename) - return sound from file filename as a Sound Object,\n\
75
returns None if not found.";
77
/*****************************************************************************/
78
/* Python method structure definition for Blender.Sound module: */
79
/*****************************************************************************/
80
struct PyMethodDef M_Sound_methods[] = {
81
{"Get", M_Sound_Get, METH_VARARGS, M_Sound_Get_doc},
82
{"Load", M_Sound_Load, METH_O, M_Sound_Load_doc},
86
/*****************************************************************************/
87
/* Python Sound_Type callback function prototypes: */
88
/*****************************************************************************/
89
static int Sound_compare( BPy_Sound * a, BPy_Sound * b );
90
static PyObject *Sound_repr( BPy_Sound * self );
92
#define SOUND_FLOAT_METHODS(funcname, varname) \
93
static PyObject *Sound_get ## funcname(BPy_Sound *self) { \
94
return PyFloat_FromDouble(self->sound->varname); \
96
static PyObject *Sound_set ## funcname(BPy_Sound *self, PyObject *args) { \
98
if (!PyArg_ParseTuple(args, "f", &f)) \
99
return (EXPP_ReturnPyObjError (PyExc_TypeError, \
100
"expected float argument")); \
101
self->sound->varname = EXPP_ClampFloat(f, \
102
EXPP_SND_##varname##_MIN, EXPP_SND_##varname##_MAX); \
106
#define SOUND_FLOAT_METHOD_FUNCS(varname) \
107
{"get"#varname, (PyCFunction)Sound_get ## varname, METH_NOARGS, \
108
"() - Return Sound object "#varname}, \
109
{"set"#varname, (PyCFunction)Sound_set ## varname, METH_VARARGS, \
110
"(float) - Change Sound object "#varname},
113
/*****************************************************************************/
114
/* Python BPy_Sound methods declarations: */
115
/*****************************************************************************/
116
static PyObject *Sound_getName( BPy_Sound * self );
117
static PyObject *Sound_getFilename( BPy_Sound * self );
118
static PyObject *Sound_setName( BPy_Sound * self, PyObject * args );
119
static int Sound_setFilename( BPy_Sound * self, PyObject * args );
120
static PyObject *Sound_oldsetFilename( BPy_Sound * self, PyObject * args );
121
static PyObject *Sound_setCurrent( BPy_Sound * self );
122
static PyObject *Sound_play( BPy_Sound * self );
123
static PyObject *Sound_unpack( BPy_Sound * self, PyObject * args);
124
static PyObject *Sound_pack( BPy_Sound * self );
125
/*static PyObject *Sound_reload ( BPy_Sound * self );*/
126
SOUND_FLOAT_METHODS( Volume, volume )
127
SOUND_FLOAT_METHODS( Attenuation, attenuation )
128
SOUND_FLOAT_METHODS( Pitch, pitch )
129
/* these can't be set via interface, removed for now */
131
SOUND_FLOAT_METHODS( Panning, panning )
132
SOUND_FLOAT_METHODS( MinGain, min_gain )
133
SOUND_FLOAT_METHODS( MaxGain, max_gain )
134
SOUND_FLOAT_METHODS( Distance, distance )
137
/*****************************************************************************/
138
/* Python BPy_Sound methods table: */
139
/*****************************************************************************/
140
static PyMethodDef BPy_Sound_methods[] = {
141
/* name, method, flags, doc */
142
{"getName", ( PyCFunction ) Sound_getName, METH_NOARGS,
143
"() - Return Sound object name"},
144
{"getFilename", ( PyCFunction ) Sound_getFilename, METH_NOARGS,
145
"() - Return Sound object filename"},
146
{"setName", ( PyCFunction ) Sound_setName, METH_VARARGS,
147
"(name) - Set Sound object name"},
148
{"setFilename", ( PyCFunction ) Sound_oldsetFilename, METH_VARARGS,
149
"(filename) - Set Sound object filename"},
150
{"setCurrent", ( PyCFunction ) Sound_setCurrent, METH_NOARGS,
151
"() - make this the active sound in the sound buttons win (also redraws)"},
152
{"play", ( PyCFunction ) Sound_play, METH_NOARGS,
153
"() - play this sound"},
154
{"unpack", ( PyCFunction ) Sound_unpack, METH_VARARGS,
155
"(int) - Unpack sound. Uses one of the values defined in Blender.UnpackModes."},
156
{"pack", ( PyCFunction ) Sound_pack, METH_NOARGS,
157
"() Pack the sound"},
159
{"reload", ( PyCFunction ) Sound_setCurrent, METH_NOARGS,
160
"() - reload this Sound object's sample.\n\
161
This is only useful if the original sound file has changed."},
163
SOUND_FLOAT_METHOD_FUNCS( Volume )
164
SOUND_FLOAT_METHOD_FUNCS( Attenuation )
165
SOUND_FLOAT_METHOD_FUNCS( Pitch )
167
SOUND_FLOAT_METHOD_FUNCS( Panning )
168
SOUND_FLOAT_METHOD_FUNCS( MinGain )
169
SOUND_FLOAT_METHOD_FUNCS( MaxGain )
170
SOUND_FLOAT_METHOD_FUNCS( Distance )
172
{NULL, NULL, 0, NULL}
175
/* NOTE: these were copied and modified from image.h. To Be Done TBD:
176
* macro-ize them, or C++ templates eventually?
178
/****************************************************************************/
179
/* Function: M_Sound_Get */
180
/* Python equivalent: Blender.Sound.Get */
181
/* Description: Receives a string and returns the Sound object */
182
/* whose name matches the string. If no argument is */
183
/* passed in, a list of all Sound names in the */
184
/* current scene is returned. */
185
/****************************************************************************/
186
static PyObject *M_Sound_Get( PyObject * self, PyObject * args )
191
if( !PyArg_ParseTuple( args, "|s", &name ) )
192
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
193
"expected string argument (or nothing)" ) );
195
snd_iter = G.main->sound.first;
197
if( name ) { /* (name) - Search Sound by name */
199
BPy_Sound *wanted_Sound = NULL;
201
while( ( snd_iter ) && ( wanted_Sound == NULL ) ) {
202
if( strcmp( name, snd_iter->id.name + 2 ) == 0 ) {
205
PyObject_NEW( BPy_Sound, &Sound_Type );
207
wanted_Sound->sound = snd_iter;
211
snd_iter = snd_iter->id.next;
214
if( wanted_Sound == NULL ) { /* Requested Sound doesn't exist */
216
PyOS_snprintf( error_msg, sizeof( error_msg ),
217
"Sound \"%s\" not found", name );
218
return ( EXPP_ReturnPyObjError
219
( PyExc_NameError, error_msg ) );
222
return ( PyObject * ) wanted_Sound;
225
else { /* () - return a list of all Sounds in the scene */
227
PyObject *snd_list, *pyobj;
229
snd_list = PyList_New( BLI_countlist( &( G.main->sound ) ) );
231
if( snd_list == NULL )
232
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
233
"couldn't create PyList" ) );
236
pyobj = Sound_CreatePyObject( snd_iter );
240
return ( EXPP_ReturnPyObjError
242
"couldn't create PyObject" ) );
244
PyList_SET_ITEM( snd_list, index, pyobj );
246
snd_iter = snd_iter->id.next;
254
/*****************************************************************************/
255
/* Function: M_Sound_Load */
256
/* Python equivalent: Blender.Sound.Load */
257
/* Description: Receives a string and returns the Sound object */
258
/* whose filename matches the string. */
259
/*****************************************************************************/
260
static PyObject *M_Sound_Load( PyObject * self, PyObject * value )
262
char *fname = PyString_AsString(value);
267
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
268
"expected string argument" ) );
270
snd = ( BPy_Sound * ) PyObject_NEW( BPy_Sound, &Sound_Type );
273
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
274
"couldn't create PyObject Sound_Type" ) );
276
snd_ptr = sound_new_sound( fname );
280
G.ssound->sound = snd_ptr;
285
return ( EXPP_ReturnPyObjError( PyExc_IOError,
286
"not a valid sound sample" ) );
288
snd->sound = snd_ptr;
290
return ( PyObject * ) snd;
293
/*****************************************************************************/
294
/* Function: Sound_Init */
295
/*****************************************************************************/
296
PyObject *Sound_Init( void )
300
if( PyType_Ready( &Sound_Type ) < 0 )
304
Py_InitModule3( "Blender.Sound", M_Sound_methods,
307
return ( submodule );
310
/************************/
311
/*** The Sound PyType ***/
312
/************************/
315
/*****************************************************************************/
316
/* Function: Sound_CreatePyObject */
317
/* Description: This function will create a new BPy_Sound from an existing */
318
/* Blender Sound structure. */
319
/*****************************************************************************/
320
PyObject *Sound_CreatePyObject( bSound * snd )
324
py_snd = ( BPy_Sound * ) PyObject_NEW( BPy_Sound, &Sound_Type );
327
return EXPP_ReturnPyObjError( PyExc_MemoryError,
328
"couldn't create BPy_Sound object" );
332
return ( PyObject * ) py_snd;
335
/*****************************************************************************/
336
/* Function: Sound_FromPyObject */
337
/* Description: Returns the Blender Sound associated with this object */
338
/*****************************************************************************/
339
bSound *Sound_FromPyObject( PyObject * pyobj )
341
return ( ( BPy_Sound * ) pyobj )->sound;
344
/*****************************************************************************/
345
/* Python BPy_Sound methods: */
346
/*****************************************************************************/
347
static PyObject *Sound_getName( BPy_Sound * self )
349
return PyString_FromString( self->sound->id.name + 2 );
352
static PyObject *Sound_getFilename( BPy_Sound * self )
354
return PyString_FromString( self->sound->name );
357
static PyObject *Sound_getPacked( BPy_Sound * self )
359
if (!sound_sample_is_null(self->sound)) {
360
bSample *sample = sound_find_sample(self->sound);
361
if (sample->packedfile)
367
static PyObject *Sound_setName( BPy_Sound * self, PyObject * args )
371
if( !PyArg_ParseTuple( args, "s", &name ) ) {
372
return ( EXPP_ReturnPyObjError( PyExc_AttributeError,
373
"expected a String as argument" ) );
376
rename_id( &self->sound->id, name );
381
static int Sound_setFilename( BPy_Sound * self, PyObject * value )
385
/* max len is FILE_MAXDIR = 160 chars like in DNA_image_types.h */
386
name = PyString_AsString(value);
387
if (!name || strlen(name) > FILE_MAXDIR)
388
return ( EXPP_ReturnIntError( PyExc_ValueError,
389
"string argument is limited to 160 chars at most" ) );
391
strcpy( self->sound->name, name );
395
static PyObject *Sound_oldsetFilename( BPy_Sound * self, PyObject * args )
397
return EXPP_setterWrapper( (void *)self, args, (setter)Sound_setFilename );
401
static PyObject *Sound_play( BPy_Sound * self )
403
sound_play_sound( self->sound );
408
static PyObject *Sound_setCurrent( BPy_Sound * self )
410
bSound *snd_ptr = self->sound;
414
G.ssound->sound = snd_ptr;
418
EXPP_allqueue( REDRAWSOUND, 0 );
419
EXPP_allqueue( REDRAWBUTSLOGIC, 0 );
426
static PyObject *Sound_unpack( BPy_Sound * self, PyObject * args )
428
bSound *sound = self->sound;
430
if( !PyArg_ParseTuple( args, "i", &mode ) )
431
return EXPP_ReturnPyObjError( PyExc_TypeError,
432
"expected an integer from Blender.UnpackModes" );
434
if (!sound_sample_is_null(sound)) {
435
bSample *sample = sound_find_sample(sound);
436
if (sample->packedfile) {
437
if (unpackSample(sample, mode) == RET_ERROR)
438
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
439
"error unpacking sound");
442
return EXPP_ReturnPyObjError( PyExc_RuntimeError, "sound has no samples" );
449
static PyObject *Sound_pack( BPy_Sound * self )
451
bSound *sound = self->sound;
452
if (!sound_sample_is_null(sound))
454
bSample *sample = sound_find_sample(sound);
455
if (sample->packedfile )
456
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
457
"sound alredy packed" );
458
sound_set_packedfile(sample, newPackedFile(sample->name));
462
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
463
"sound has no samples" );
469
static PyObject *Sound_reload( BPy_Sound * self)
473
if (sound->snd_sound) {
474
SND_RemoveSound(ghSoundScene, sound->snd_sound);
475
sound->snd_sound = NULL;
484
/*****************************************************************************/
485
/* Function: Sound_compare */
486
/* Description: This is a callback function for the BPy_Sound type. It */
487
/* compares two Sound_Type objects. Only the "==" and "!=" */
488
/* comparisons are meaninful. Returns 0 for equality and -1 if */
489
/* they don't point to the same Blender Sound struct. */
490
/* In Python it becomes 1 if they are equal, 0 otherwise. */
491
/*****************************************************************************/
492
static int Sound_compare( BPy_Sound * a, BPy_Sound * b )
494
return ( a->sound == b->sound ) ? 0 : -1;
497
/*****************************************************************************/
498
/* Function: Sound_repr */
499
/* Description: This is a callback function for the BPy_Sound type. It */
500
/* builds a meaninful string to represent Sound objects. */
501
/*****************************************************************************/
502
static PyObject *Sound_repr( BPy_Sound * self )
504
return PyString_FromFormat( "[Sound \"%s\"]",
505
self->sound->id.name + 2 );
508
/*****************************************************************************/
509
/* Python attributes get/set structure: */
510
/*****************************************************************************/
511
static PyGetSetDef BPy_Sound_getseters[] = {
512
GENERIC_LIB_GETSETATTR,
513
{"filename", (getter)Sound_getFilename, (setter)Sound_setFilename,
514
"text filename", NULL},
515
{"packed", (getter)Sound_getPacked, (setter)NULL,
516
"text filename", NULL},
517
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
522
/*****************************************************************************/
523
/* Python Sound_Type structure definition: */
524
/*****************************************************************************/
525
PyTypeObject Sound_Type = {
526
PyObject_HEAD_INIT( NULL )
528
"Blender Sound", /* tp_name */
529
sizeof( BPy_Sound ), /* tp_basicsize */
532
NULL, /* tp_dealloc */
534
NULL, /* tp_getattr */
535
NULL, /* tp_setattr */
536
( cmpfunc ) Sound_compare, /* tp_compare */
537
( reprfunc ) Sound_repr, /* tp_repr */
539
/* Method suites for standard classes */
541
NULL, /* PyNumberMethods *tp_as_number; */
542
NULL, /* PySequenceMethods *tp_as_sequence; */
543
NULL, /* PyMappingMethods *tp_as_mapping; */
545
/* More standard operations (here for binary compatibility) */
547
( hashfunc ) GenericLib_hash, /* hashfunc tp_hash; */
548
NULL, /* ternaryfunc tp_call; */
549
NULL, /* reprfunc tp_str; */
550
NULL, /* getattrofunc tp_getattro; */
551
NULL, /* setattrofunc tp_setattro; */
553
/* Functions to access object as input/output buffer */
554
NULL, /* PyBufferProcs *tp_as_buffer; */
556
/*** Flags to define presence of optional/expanded features ***/
557
Py_TPFLAGS_DEFAULT, /* long tp_flags; */
559
NULL, /* char *tp_doc; Documentation string */
560
/*** Assigned meaning in release 2.0 ***/
561
/* call function for all accessible objects */
562
NULL, /* traverseproc tp_traverse; */
564
/* delete references to contained objects */
565
NULL, /* inquiry tp_clear; */
567
/*** Assigned meaning in release 2.1 ***/
568
/*** rich comparisons ***/
569
NULL, /* richcmpfunc tp_richcompare; */
571
/*** weak reference enabler ***/
572
0, /* long tp_weaklistoffset; */
574
/*** Added in release 2.2 ***/
576
NULL, /* getiterfunc tp_iter; */
577
NULL, /* iternextfunc tp_iternext; */
579
/*** Attribute descriptor and subclassing stuff ***/
580
BPy_Sound_methods, /* struct PyMethodDef *tp_methods; */
581
NULL, /* struct PyMemberDef *tp_members; */
582
BPy_Sound_getseters, /* struct PyGetSetDef *tp_getset; */
583
NULL, /* struct _typeobject *tp_base; */
584
NULL, /* PyObject *tp_dict; */
585
NULL, /* descrgetfunc tp_descr_get; */
586
NULL, /* descrsetfunc tp_descr_set; */
587
0, /* long tp_dictoffset; */
588
NULL, /* initproc tp_init; */
589
NULL, /* allocfunc tp_alloc; */
590
NULL, /* newfunc tp_new; */
591
/* Low-level free-memory routine */
592
NULL, /* freefunc tp_free; */
593
/* For PyObject_IS_GC */
594
NULL, /* inquiry tp_is_gc; */
595
NULL, /* PyObject *tp_bases; */
596
/* method resolution order */
597
NULL, /* PyObject *tp_mro; */
598
NULL, /* PyObject *tp_cache; */
599
NULL, /* PyObject *tp_subclasses; */
600
NULL, /* PyObject *tp_weaklist; */