Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36ddc9e

Browse files
committed
Initial revision
1 parent 6acc1b5 commit 36ddc9e

4 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lib/lib-stdwin/StripChart.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Module 'StripChart'
2+
3+
4+
import rect
5+
from Buttons import *
6+
from Resize import *
7+
8+
9+
class StripChart() = LabelAppearance(), NoReactivity(), NoResize():
10+
#
11+
def define(self, (win, bounds, scale)):
12+
self.init_appearance(win, bounds)
13+
self.init_reactivity()
14+
self.init_resize()
15+
self.ydata = []
16+
self.scale = scale
17+
self.resetbounds()
18+
return self
19+
#
20+
def setbounds(self, bounds):
21+
LabelAppearance.setbounds(self, bounds)
22+
self.resetbounds()
23+
#
24+
def resetbounds(self):
25+
(left, top), (right, bottom) = self.bounds
26+
self.width = right-left
27+
self.height = bottom-top
28+
excess = len(self.ydata) - self.width
29+
if excess > 0:
30+
del self.ydata[:excess]
31+
elif excess < 0:
32+
while len(self.ydata) < self.width:
33+
self.ydata.insert(0, 0)
34+
#
35+
def append(self, y):
36+
self.ydata.append(y)
37+
excess = len(self.ydata) - self.width
38+
if excess > 0:
39+
del self.ydata[:excess]
40+
if not self.limbo:
41+
self.win.scroll(self.bounds, (-excess, 0))
42+
if not self.limbo:
43+
(left, top), (right, bottom) = self.bounds
44+
i = len(self.ydata)
45+
area = (left+i-1, top), (left+i, bottom)
46+
self.draw(self.win.begindrawing(), area)
47+
#
48+
def draw(self, (d, area)):
49+
self.limbo = 0
50+
area = rect.intersect(area, self.bounds)
51+
if area = rect.empty:
52+
return
53+
d.cliprect(area)
54+
d.erase(self.bounds)
55+
(a_left, a_top), (a_right, a_bottom) = area
56+
(left, top), (right, bottom) = self.bounds
57+
height = bottom - top
58+
i1 = a_left - left
59+
i2 = a_right - left
60+
for i in range(max(0, i1), min(len(self.ydata), i2)):
61+
split = bottom-self.ydata[i]*height/self.scale
62+
d.paint((left+i, split), (left+i+1, bottom))
63+
if not self.enabled:
64+
self.flipenable(d)
65+
if self.hilited:
66+
self.fliphilite(d)
67+
d.noclip()

Lib/lib-stdwin/VUMeter.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Module VUMeter
2+
3+
import audio
4+
from StripChart import StripChart
5+
6+
K = 1024
7+
Rates = [0, 32*K, 16*K, 8*K]
8+
9+
class VUMeter() = StripChart():
10+
#
11+
# Override define() and timer() methods
12+
#
13+
def define(self, (win, bounds)):
14+
self = StripChart.define(self, (win, bounds, 128))
15+
self.sampling = 0
16+
self.rate = 3
17+
self.enable(0)
18+
return self
19+
#
20+
def timer(self):
21+
if self.sampling:
22+
chunk = audio.wait_recording()
23+
self.sampling = 0
24+
nums = audio.chr2num(chunk)
25+
ampl = max(abs(min(nums)), abs(max(nums)))
26+
self.append(ampl)
27+
if self.enabled and not self.sampling:
28+
audio.setrate(self.rate)
29+
size = Rates[self.rate]/10
30+
size = size/48*48
31+
audio.start_recording(size)
32+
self.sampling = 1
33+
if self.sampling:
34+
self.win.settimer(1)
35+
#
36+
# New methods: start() and stop()
37+
#
38+
def stop(self):
39+
if self.sampling:
40+
chunk = audio.stop_recording()
41+
self.sampling = 0
42+
self.enable(0)
43+
#
44+
def start(self):
45+
self.enable(1)
46+
self.timer()

Lib/stdwin/StripChart.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Module 'StripChart'
2+
3+
4+
import rect
5+
from Buttons import *
6+
from Resize import *
7+
8+
9+
class StripChart() = LabelAppearance(), NoReactivity(), NoResize():
10+
#
11+
def define(self, (win, bounds, scale)):
12+
self.init_appearance(win, bounds)
13+
self.init_reactivity()
14+
self.init_resize()
15+
self.ydata = []
16+
self.scale = scale
17+
self.resetbounds()
18+
return self
19+
#
20+
def setbounds(self, bounds):
21+
LabelAppearance.setbounds(self, bounds)
22+
self.resetbounds()
23+
#
24+
def resetbounds(self):
25+
(left, top), (right, bottom) = self.bounds
26+
self.width = right-left
27+
self.height = bottom-top
28+
excess = len(self.ydata) - self.width
29+
if excess > 0:
30+
del self.ydata[:excess]
31+
elif excess < 0:
32+
while len(self.ydata) < self.width:
33+
self.ydata.insert(0, 0)
34+
#
35+
def append(self, y):
36+
self.ydata.append(y)
37+
excess = len(self.ydata) - self.width
38+
if excess > 0:
39+
del self.ydata[:excess]
40+
if not self.limbo:
41+
self.win.scroll(self.bounds, (-excess, 0))
42+
if not self.limbo:
43+
(left, top), (right, bottom) = self.bounds
44+
i = len(self.ydata)
45+
area = (left+i-1, top), (left+i, bottom)
46+
self.draw(self.win.begindrawing(), area)
47+
#
48+
def draw(self, (d, area)):
49+
self.limbo = 0
50+
area = rect.intersect(area, self.bounds)
51+
if area = rect.empty:
52+
return
53+
d.cliprect(area)
54+
d.erase(self.bounds)
55+
(a_left, a_top), (a_right, a_bottom) = area
56+
(left, top), (right, bottom) = self.bounds
57+
height = bottom - top
58+
i1 = a_left - left
59+
i2 = a_right - left
60+
for i in range(max(0, i1), min(len(self.ydata), i2)):
61+
split = bottom-self.ydata[i]*height/self.scale
62+
d.paint((left+i, split), (left+i+1, bottom))
63+
if not self.enabled:
64+
self.flipenable(d)
65+
if self.hilited:
66+
self.fliphilite(d)
67+
d.noclip()

Lib/stdwin/VUMeter.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Module VUMeter
2+
3+
import audio
4+
from StripChart import StripChart
5+
6+
K = 1024
7+
Rates = [0, 32*K, 16*K, 8*K]
8+
9+
class VUMeter() = StripChart():
10+
#
11+
# Override define() and timer() methods
12+
#
13+
def define(self, (win, bounds)):
14+
self = StripChart.define(self, (win, bounds, 128))
15+
self.sampling = 0
16+
self.rate = 3
17+
self.enable(0)
18+
return self
19+
#
20+
def timer(self):
21+
if self.sampling:
22+
chunk = audio.wait_recording()
23+
self.sampling = 0
24+
nums = audio.chr2num(chunk)
25+
ampl = max(abs(min(nums)), abs(max(nums)))
26+
self.append(ampl)
27+
if self.enabled and not self.sampling:
28+
audio.setrate(self.rate)
29+
size = Rates[self.rate]/10
30+
size = size/48*48
31+
audio.start_recording(size)
32+
self.sampling = 1
33+
if self.sampling:
34+
self.win.settimer(1)
35+
#
36+
# New methods: start() and stop()
37+
#
38+
def stop(self):
39+
if self.sampling:
40+
chunk = audio.stop_recording()
41+
self.sampling = 0
42+
self.enable(0)
43+
#
44+
def start(self):
45+
self.enable(1)
46+
self.timer()

0 commit comments

Comments
 (0)