test_player.py 2.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3

# ============================================================================
# This script allows you to play music using a chorizo (or whatever food you
# like) keyboard ! See README for more info and links.

# As all the other scripts in this repository, I release it under a very
# permissive license. To make a long story short : do whatever you want with
# this script (but try to have fun :), I don't mind. It would be cool to quote
# the origin of the script if you reuse it, but you don't have to. I'd like to
# be noticed of what you did cool with it (if you think it's worth). :)
# Ah, I almost forgot : If by chance we ever meet and you think this script is
# worth, you can buy me a soda :)
#
#                                                                   Phyks
# =============================================================================

from sound4python import sound
from multiprocessing import Process
import math

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()

getch = _Getch()

def play_wave(frequency=440, nb_secs=1.):
    sine_wave = []
    for i in range(math.ceil(nb_secs*framerate)+1):
        sine_wave.append(int(16384*math.sin(2*math.pi*frequency*i/framerate)))

    sound(sine_wave)

framerate = 16000
processes = []
frequency = 440

print("Running... Press q to quit.")
running = True
while running:
    char = getch()
    if char == "q":
        running = False
        continue
    elif char == "a":
        frequency = 440
    elif char == "b":
        frequency = 493.88
    elif char == "c":
        frequency = 523.25
    elif char == "d":
        frequency = 587.33
    elif char == "e":
        frequency = 659.26
    elif char == "f":
        frequency = 698.46
    elif char == "g":
        frequency = 783.99
    else:
        continue

    print("Playing "+char.upper())
    processes.append(Process(target=play_wave, args=(frequency, 0.2)))
    processes[-1].start()

for i in processes:
    i.join()