-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsounds-of-fibonacci-with-cache.txt
56 lines (45 loc) · 1.19 KB
/
sounds-of-fibonacci-with-cache.txt
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
define :convertNumberToTone do |n, maxNValue|
beginSound = 40.0
lastSound = 115.0
result = beginSound + n.to_f * ((lastSound - beginSound) / maxNValue)
return result
end
define :playNumberWithoutDelay do |n, maxNValue|
tone = convertNumberToTone n, maxNValue
play tone, release: 0.1
end
define :playNumberWithDelay do |n, maxNValue|
playNumberWithoutDelay n, maxNValue
sleep 0.03
end
define :fibWithoutSound do |n, cache|
fibInternal n, false, nil, cache
end
define :fib do |n|
cache1 = []
cache2 = []
maxNValue = fibWithoutSound n, cache1
fibInternal n, true, maxNValue, cache2
end
define :fibInternal do |n, playsound, maxNValue, cache|
if cache[n - 1] != nil
return cache[n - 1]
end
if n == 1 || n == 2
if playsound
playNumberWithDelay n, maxNValue
end
cache[n - 1] = 1
return 1
else
fibNMinusOne = fibInternal (n - 1), playsound, maxNValue, cache
fibNMinusTwo = fibInternal (n - 2), playsound, maxNValue, cache
if playsound
playNumberWithoutDelay fibNMinusOne, maxNValue
playNumberWithDelay fibNMinusTwo, maxNValue
end
result = fibNMinusOne + fibNMinusTwo
cache[n - 1] = result
end
end
fib(200)