summaryrefslogtreecommitdiffstats
path: root/simple_turing.py
blob: 9704c6fd59fb78e9df190737519e46131baf9d28 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python

import sys
import wave
import random
import struct

RATE = 22000
SAMPLE_LEN = RATE * 30
TSIZE = 10000 # gets doubled

class Machine():
    def __init__(self, prog=None, verbose=False):
        self.verbose = verbose
        self.tape = map(lambda x: 0, range(TSIZE*2))
        self.top = 0
        self.bottom =  0
        self.index = 0
        self.state = 'A'
        if prog:
            self.prog= prog
        else:
            self.prog = {
                'A': {}
            }

    def run(self):
        while self.state != 'H':
            self.step()

    def step(self, n=1):
        if self.state == 'H':
            raise Exception("In halt state!")
        instr = self.prog[self.state][self.tape[self.index+TSIZE]]
        self.tape[self.index+TSIZE] = instr['write']
        self.index += instr['dir']
        if self.index > self.top:
            self.top = self.index
        if self.index < self.bottom:
            self.bottom = self.index
        self.state = instr['next']
        if n>1:
            self.step(n=n-1)

    def __str__(self):
        return "S=%s, [%d:%d]=%s" % (self.state,
                                     self.bottom,
                                     self.top,
                                     ''.join(map(lambda x: str(x),
                                                 self.tape[TSIZE+self.bottom:
                                                           TSIZE+self.top])))

# right is 1, left is -1
bb_2_2 = {
    'A': { 0: {'next': 'B',
               'write': 1,
               'dir': 1},
           1: {'next': 'B',
               'write': 1,
               'dir': -1}},
    'B': { 0: {'next': 'A',
               'write': 1,
               'dir': -1},
           1: {'next': 'H',
               'write': 1,
               'dir': 1}},
}

bb_6_2 = {
    'A': { 0: {'next': 'B',
               'write': 1,
               'dir': 1},
           1: {'next': 'E',
               'write': 1,
               'dir': -1}},
    'B': { 0: {'next': 'C',
               'write': 1,
               'dir': 1},
           1: {'next': 'F',
               'write': 1,
               'dir': 1}},
    'C': { 0: {'next': 'D',
               'write': 1,
               'dir': -1},
           1: {'next': 'B',
               'write': 0,
               'dir': 1}},
    'D': { 0: {'next': 'E',
               'write': 1,
               'dir': 1},
           1: {'next': 'C',
               'write': 0,
               'dir': -1}},
    'E': { 0: {'next': 'A',
               'write': 1,
               'dir': -1},
           1: {'next': 'D',
               'write': 0,
               'dir': 1}},
    'F': { 0: {'next': 'H',
               'write': 1,
               'dir': -1},
           1: {'next': 'C',
               'write': 1,
               'dir': 1}},
}

def test_fsm():
    m = Machine(prog=bb_2_2)
    print m
    m.run()
    print m
    return m

def scale(val, r_bottom, r_top, amp=32767):
    f = 1.0 * (val - r_bottom) / (r_top - r_bottom)
    return int(amp * 2.0 * f) - amp

def go(downsample = 1, prog=bb_6_2, seconds=10, rate=22000):
    noise_output = wave.open('turing_test.wav', 'w')
    noise_output.setparams((1, 1, RATE, 0, 'NONE', 'not compressed'))
    samples = rate * seconds

    m = Machine(bb_6_2)
    m.step()
    for i in range(0, samples):
            #value = random.randint(-32767, 32767)
            if i % downsample == 0:
                m.step()
            value = scale(m.index, m.bottom, m.top, 32767)
            packed_value = struct.pack('h', value)
            noise_output.writeframes(packed_value)
            #noise_output.writeframes(packed_value)

    noise_output.close()
    return (m.bottom, m.top)

if __name__ == '__main__':
    out = ''
    if len(sys.argv) > 2:
        out = go(seconds=int(sys.argv[1]), downsample = int(sys.argv[2]))
    elif len(sys.argv) > 1:
        out = go(seconds=int(sys.argv[1]))
    else:
        out = go()
    print out