~ubuntu-branches/ubuntu/quantal/python-pyo/quantal

« back to all changes in this revision

Viewing changes to pyolib/midi.py

  • Committer: Package Import Robot
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2012-07-03 23:45:41 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120703234541-jh5jg00lvljnwq8m
Tags: 0.6.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
You should have received a copy of the GNU General Public License
28
28
along with pyo.  If not, see <http://www.gnu.org/licenses/>.
29
29
"""
 
30
import sys
30
31
from _core import *
31
32
from _maps import *
32
33
 
102
103
        return ['ctlnumber', 'minscale', 'maxscale', 'channel', 'mul', 'add']
103
104
 
104
105
    def out(self, chnl=0, inc=1, dur=0, delay=0):
105
 
        return self
 
106
        return self.play(dur, delay)
106
107
 
107
108
    def setCtlNumber(self, x):
108
109
        """
259
260
    """
260
261
    def __init__(self, function, toprint=True):
261
262
        PyoObject.__init__(self)
 
263
        if not callable(function):
 
264
            print >> sys.stderr, 'TypeError: "function" argument of %s must be callable.\n' % self.__class__.__name__
 
265
            exit()
262
266
        self._function = function
263
267
        self._toprint = toprint
264
268
        self._base_objs = [CtlScan_base(self._function, self._toprint)]
267
271
        return []
268
272
 
269
273
    def out(self, chnl=0, inc=1, dur=0, delay=0):
270
 
        return self
 
274
        return self.play(dur, delay)
 
275
 
 
276
    def setMul(self, x):
 
277
        pass
 
278
 
 
279
    def setAdd(self, x):
 
280
        pass
 
281
 
 
282
    def setSub(self, x):
 
283
        pass
 
284
 
 
285
    def setDiv(self, x):
 
286
        pass
 
287
 
 
288
    def setFunction(self, x):
 
289
        """
 
290
        Replace the `function` attribute.
 
291
 
 
292
        Parameters:
 
293
 
 
294
        x : Python function
 
295
            new `function` attribute.
 
296
 
 
297
        """
 
298
        self._function = x
 
299
        x, lmax = convertArgsToLists(x)
 
300
        [obj.setFunction(wrap(x,i)) for i, obj in enumerate(self._base_objs)]
 
301
 
 
302
    def setToprint(self, x):
 
303
        """
 
304
        Replace the `toprint` attribute.
 
305
 
 
306
        Parameters:
 
307
 
 
308
        x : int
 
309
            new `toprint` attribute.
 
310
 
 
311
        """
 
312
        self._toprint = x
 
313
        x, lmax = convertArgsToLists(x)
 
314
        [obj.setToprint(wrap(x,i)) for i, obj in enumerate(self._base_objs)]
 
315
 
 
316
    def ctrl(self, map_list=None, title=None, wxnoserver=False):
 
317
        self._map_list = []
 
318
        PyoObject.ctrl(self, map_list, title, wxnoserver)
 
319
 
 
320
    @property
 
321
    def function(self): return self._function
 
322
    @function.setter
 
323
    def function(self, x): self.setFunction(x)   
 
324
    @property
 
325
    def toprint(self): return self._toprint
 
326
    @toprint.setter
 
327
    def toprint(self, x): self.setToprint(x)   
 
328
 
 
329
class CtlScan2(PyoObject):
 
330
    """
 
331
    Scan the Midi channel and controller number in input.
 
332
 
 
333
    Scan the Midi channel and controller number in input and send them 
 
334
    to a standard python `function`. Useful to implement a MidiLearn 
 
335
    algorithm.
 
336
 
 
337
    Parentclass: PyoObject
 
338
 
 
339
    Parameters:
 
340
 
 
341
    function : Python function
 
342
        Function to be called. The function must be declared
 
343
        with two arguments, one for the controller number and 
 
344
        one for the midi channel. Ex.: 
 
345
        def ctl_scan(ctlnum, midichnl):
 
346
            print ctlnum, midichnl
 
347
    toprint : boolean, optional
 
348
        If True, controller number and value will be print to 
 
349
        the console.
 
350
 
 
351
    Methods:
 
352
 
 
353
    setFunction(x) : Replace the `function` attribute.
 
354
    setToprint(x) : Replace the `toprint` attribute.
 
355
 
 
356
    Attributes:
 
357
 
 
358
    function : Python function. Function to be called.
 
359
    toprint : boolean. If True, print values to the console.
 
360
 
 
361
    Notes:
 
362
 
 
363
    The out() method is bypassed. CtlScan2's signal can not be sent 
 
364
    to audio outs.
 
365
 
 
366
    Examples:
 
367
 
 
368
    >>> s = Server()
 
369
    >>> s.setMidiInputDevice(0) # enter your device number (see pm_list_devices())
 
370
    >>> s.boot()
 
371
    >>> s.start()
 
372
    >>> def ctl_scan(ctlnum, midichnl):
 
373
    ...     print ctlnum, midichnl
 
374
    >>> a = CtlScan2(ctl_scan)
 
375
 
 
376
    """
 
377
    def __init__(self, function, toprint=True):
 
378
        PyoObject.__init__(self)
 
379
        if not callable(function):
 
380
            print >> sys.stderr, 'TypeError: "function" argument of %s must be callable.\n' % self.__class__.__name__
 
381
            exit()
 
382
        self._function = function
 
383
        self._toprint = toprint
 
384
        self._base_objs = [CtlScan2_base(self._function, self._toprint)]
 
385
 
 
386
    def __dir__(self):
 
387
        return []
 
388
 
 
389
    def out(self, chnl=0, inc=1, dur=0, delay=0):
 
390
        return self.play(dur, delay)
271
391
 
272
392
    def setMul(self, x):
273
393
        pass
405
525
    def __dir__(self):
406
526
        return ['channel', 'mul', 'add']
407
527
 
408
 
    def __del__(self):
409
 
        if self._pitch_dummy:
410
 
            [obj.deleteStream() for obj in self._pitch_dummy]
411
 
        if self._velocity_dummy:
412
 
            [obj.deleteStream() for obj in self._velocity_dummy]
413
 
        self._pitch_dummy = []
414
 
        self._velocity_dummy = []
415
 
        for obj in self._base_objs:
416
 
            obj.deleteStream()
417
 
            del obj
418
 
        self._base_handler.deleteStream()
419
 
        del self._base_handler
420
 
 
421
528
    def __getitem__(self, str):
422
529
        if str == 'pitch':
423
530
            self._pitch_dummy.append(Dummy([self._base_objs[i*2] for i in range(self._poly)]))
482
589
                        
483
590
    def play(self, dur=0, delay=0):
484
591
        self._base_handler.play()
485
 
        dur, delay, lmax = convertArgsToLists(dur, delay)
486
 
        self._base_objs = [obj.play(wrap(dur,i), wrap(delay,i)) for i, obj in enumerate(self._base_objs)]
487
 
        return self
 
592
        return PyoObject.play(self, dur, delay)
488
593
 
489
594
    def out(self, chnl=0, inc=1, dur=0, delay=0):
490
 
        return self
491
 
    
 
595
        return self.play(dur, delay)
 
596
 
492
597
    def stop(self):
493
598
        self._base_handler.stop()
494
 
        [obj.stop() for obj in self._base_objs]
495
 
        return self
 
599
        return PyoObject.stop(self)
496
600
 
497
601
    def ctrl(self, map_list=None, title=None, wxnoserver=False):
498
602
        self._map_list = []
567
671
        return ['brange', 'scale', 'channel', 'mul', 'add']
568
672
 
569
673
    def out(self, chnl=0, inc=1, dur=0, delay=0):
570
 
        return self
 
674
        return self.play(dur, delay)
571
675
 
572
676
    def setBrange(self, x):
573
677
        """
699
803
        return ['minscale', 'maxscale', 'channel', 'mul', 'add']
700
804
 
701
805
    def out(self, chnl=0, inc=1, dur=0, delay=0):
702
 
        return self
 
806
        return self.play(dur, delay)
703
807
 
704
808
    def setMinScale(self, x):
705
809
        """
812
916
        return ['channel', 'mul', 'add']
813
917
 
814
918
    def out(self, chnl=0, inc=1, dur=0, delay=0):
815
 
        return self
 
919
        return self.play(dur, delay)
816
920
 
817
921
    def setChannel(self, x):
818
922
        """
924
1028
        return ['input', 'attack', 'decay', 'sustain', 'release', 'mul', 'add']
925
1029
 
926
1030
    def out(self, chnl=0, inc=1, dur=0, delay=0):
927
 
        return self
 
1031
        return self.play(dur, delay)
928
1032
 
929
1033
    def setInput(self, x, fadetime=0.05):
930
1034
        """
1123
1227
        return ['input', 'delay', 'attack', 'decay', 'sustain', 'release', 'mul', 'add']
1124
1228
 
1125
1229
    def out(self, chnl=0, inc=1, dur=0, delay=0):
1126
 
        return self
 
1230
        return self.play(dur, delay)
1127
1231
 
1128
1232
    def setInput(self, x, fadetime=0.05):
1129
1233
        """