Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Demo/scripts/morse.py

    r2 r391  
     1#! /usr/bin/env python
     2
    13# DAH should be three DOTs.
    24# Space between DOTs and DAHs should be one DOT.
     
    3739        'Y': '-.--',            'y': '-.--',
    3840        'Z': '--..',            'z': '--..',
    39         '0': '-----',
    40         '1': '.----',
    41         '2': '..---',
    42         '3': '...--',
    43         '4': '....-',
    44         '5': '.....',
    45         '6': '-....',
    46         '7': '--...',
    47         '8': '---..',
    48         '9': '----.',
    49         ',': '--..--',
    50         '.': '.-.-.-',
    51         '?': '..--..',
    52         ';': '-.-.-.',
    53         ':': '---...',
    54         "'": '.----.',
    55         '-': '-....-',
    56         '/': '-..-.',
    57         '(': '-.--.-',
    58         ')': '-.--.-',
    59         '_': '..--.-',
    60         ' ': ' '
     41        '0': '-----',           ',': '--..--',
     42        '1': '.----',           '.': '.-.-.-',
     43        '2': '..---',           '?': '..--..',
     44        '3': '...--',           ';': '-.-.-.',
     45        '4': '....-',           ':': '---...',
     46        '5': '.....',           "'": '.----.',
     47        '6': '-....',           '-': '-....-',
     48        '7': '--...',           '/': '-..-.',
     49        '8': '---..',           '(': '-.--.-',
     50        '9': '----.',           ')': '-.--.-',
     51        ' ': ' ',               '_': '..--.-',
    6152}
     53
     54nowave = '\0' * 200
    6255
    6356# If we play at 44.1 kHz (which we do), then if we produce one sine
     
    6659# appears to be a nice one for playing morse code.
    6760def mkwave(octave):
    68     global sinewave, nowave
    6961    sinewave = ''
    7062    for i in range(100):
    71         val = int(math.sin(math.pi * float(i) * octave / 50.0) * 30000)
    72         sinewave = sinewave + chr((val >> 8) & 255) + chr(val & 255)
    73     nowave = '\0' * 200
     63        val = int(math.sin(math.pi * i * octave / 50.0) * 30000)
     64        sinewave += chr((val >> 8) & 255) + chr(val & 255)
     65    return sinewave
    7466
    75 mkwave(OCTAVE)
     67defaultwave = mkwave(OCTAVE)
    7668
    7769def main():
    78     import getopt, string
     70    import getopt
    7971    try:
    8072        opts, args = getopt.getopt(sys.argv[1:], 'o:p:')
    8173    except getopt.error:
    8274        sys.stderr.write('Usage ' + sys.argv[0] +
    83                          ' [ -o outfile ] [ args ] ...\n')
     75                         ' [ -o outfile ] [ -p octave ] [ words ] ...\n')
    8476        sys.exit(1)
    8577    dev = None
     78    wave = defaultwave
    8679    for o, a in opts:
    8780        if o == '-o':
     
    9285            dev.setnchannels(1)
    9386        if o == '-p':
    94             mkwave(string.atoi(a))
     87            wave = mkwave(int(a))
    9588    if not dev:
    9689        import audiodev
     
    10295        dev.writeframesraw = dev.writeframes
    10396    if args:
    104         line = string.join(args)
     97        source = [' '.join(args)]
    10598    else:
    106         line = sys.stdin.readline()
    107     while line:
     99        source = iter(sys.stdin.readline, '')
     100    for line in source:
    108101        mline = morse(line)
    109         play(mline, dev)
     102        play(mline, dev, wave)
    110103        if hasattr(dev, 'wait'):
    111104            dev.wait()
    112         if not args:
    113             line = sys.stdin.readline()
    114         else:
    115             line = ''
    116105    dev.close()
    117106
     
    122111    for c in line:
    123112        try:
    124             res = res + morsetab[c] + '\001'
     113            res += morsetab[c] + '\001'
    125114        except KeyError:
    126115            pass
     
    128117
    129118# Play a line of morse code.
    130 def play(line, dev):
     119def play(line, dev, wave):
    131120    for c in line:
    132121        if c == '.':
    133             sine(dev, DOT)
     122            sine(dev, DOT, wave)
    134123        elif c == '-':
    135             sine(dev, DAH)
     124            sine(dev, DAH, wave)
    136125        else:                   # space
    137126            pause(dev, DAH + DOT)
    138127        pause(dev, DOT)
    139128
    140 def sine(dev, length):
     129def sine(dev, length, wave):
    141130    for i in range(length):
    142         dev.writeframesraw(sinewave)
     131        dev.writeframesraw(wave)
    143132
    144133def pause(dev, length):
     
    146135        dev.writeframesraw(nowave)
    147136
    148 if __name__ == '__main__' or sys.argv[0] == __name__:
     137if __name__ == '__main__':
    149138    main()
Note: See TracChangeset for help on using the changeset viewer.