Commits

Anonymous committed cb8c400

making channel & sound play support keyword arguments and adding fade_ms argument for fading in

Comments (0)

Files changed (2)

 /* sound object methods */
 
 static PyObject*
-snd_play (PyObject* self, PyObject* args)
+snd_play (PyObject* self, PyObject* args, PyObject* kwargs)
 {
     Mix_Chunk* chunk = PySound_AsChunk (self);
     int channelnum = -1;
-    int loops = 0, playtime = -1;
+    int loops = 0, playtime = -1, fade_ms = 0;
 
-    if (!PyArg_ParseTuple (args, "|ii", &loops, &playtime))
-        return NULL;
-
-    channelnum = Mix_PlayChannelTimed (-1, chunk, loops, playtime);
+    char *kwids[] = { "loops", "maxtime", "fade_ms", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", kwids, &loops, &playtime, &fade_ms))
+       return NULL;
+    
+    if (fade_ms > 0)
+    {	
+    	channelnum = Mix_FadeInChannelTimed (-1, chunk, loops, fade_ms, playtime);    	
+    }
+    else
+    {
+    	channelnum = Mix_PlayChannelTimed (-1, chunk, loops, playtime);
+    }
     if (channelnum == -1)
         Py_RETURN_NONE;
 
 
 static PyMethodDef sound_methods[] =
 {
-    { "play", snd_play, METH_VARARGS, DOC_SOUNDPLAY },
+    { "play", (PyCFunction) snd_play, METH_KEYWORDS, DOC_SOUNDPLAY },
     { "get_num_channels", (PyCFunction) snd_get_num_channels, METH_NOARGS,
       DOC_SOUNDGETNUMCHANNELS },
     { "fadeout", snd_fadeout, METH_VARARGS, DOC_SOUNDFADEOUT },
 
 /* channel object methods */
 static PyObject*
-chan_play (PyObject* self, PyObject* args)
+chan_play (PyObject* self, PyObject* args, PyObject* kwargs)
 {
     int channelnum = PyChannel_AsInt (self);
     PyObject* sound;
     Mix_Chunk* chunk;
-    int loops = 0, playtime = -1;
+    int loops = 0, playtime = -1, fade_ms = 0;
 
-    if (!PyArg_ParseTuple (args, "O!|ii", &PySound_Type, &sound, &loops,
-                           &playtime))
-        return NULL;
+    char *kwids[] = { "Sound", "loops", "maxtime", "fade_ms", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|iii", kwids, &PySound_Type, &sound, 
+                                     &loops, &playtime, &fade_ms))
+       return NULL;
     chunk = PySound_AsChunk (sound);
 
-    channelnum = Mix_PlayChannelTimed (channelnum, chunk, loops, playtime);
+    if (fade_ms > 0)
+    {
+        channelnum = Mix_FadeInChannelTimed (channelnum, chunk, loops, fade_ms, playtime);
+    }
+    else
+    {
+        channelnum = Mix_PlayChannelTimed (channelnum, chunk, loops, playtime);
+    }
     if (channelnum != -1)
         Mix_GroupChannel (channelnum, (intptr_t)chunk);
 
 
 static PyMethodDef channel_builtins[] =
 {
-    { "play", chan_play, METH_VARARGS, DOC_CHANNELPLAY },
+    { "play", (PyCFunction) chan_play, METH_KEYWORDS, DOC_CHANNELPLAY },
     { "queue", chan_queue, METH_VARARGS, DOC_CHANNELQUEUE },
     { "get_busy", (PyCFunction) chan_get_busy, METH_NOARGS,
       DOC_CHANNELGETBUSY },
 
 play
 begin sound playback
-Sound.play(loops=0, maxtime=0): return Channel
+Sound.play(loops=0, maxtime=0, fade_ms=0): return Channel
 
 Begin playback of the Sound (i.e., on the computer's speakers) on an
 available Channel. This will forcibly select
 The maxtime argument can be used to stop playback after a given number of
 milliseconds.
 
+The fade_ms argument will make the sound start playing at 0 volume and 
+fade up to full volume over the time given. The sample may end before the
+fade-in is complete.
+ 
 This returns the Channel object for the channel that was selected.
 <END>
 
 
 play
 play a Sound on a specific Channel
-Channel.play(Sound, loops=0, time=0): return None
+Channel.play(Sound, loops=0, maxtime=0, fade_ms=0): return None
 
 This will begin playback of a Sound on a specific Channel. If the Channel
 is currently playing any other Sound it will be stopped.
 3, the sound will be played 4 times (the first time, then three more).
 If loops is -1 then the playback will repeat indefinitely.
 
-As in Sound.play(), the time argument can be used to
+As in Sound.play(), the maxtime argument can be used to
 stop playback of the Sound after a given number of milliseconds.
+
+As in Sound.play(), the fade_ms argument can be used fade in the sound.
 <END>
 
 stop