From e3b3dae016e3a60f55b9788063ae0089c52b187b Mon Sep 17 00:00:00 2001 From: miaozaiye Date: Sun, 1 Oct 2017 21:06:40 +0800 Subject: [PATCH 1/6] add sound in G-ball & clock --- .idea/workspace.xml | 80 ++++++++++++++++++++++++--------------------- miaozaiye/G-ball.py | 23 +++++++++++-- miaozaiye/clock.py | 14 +++++++- 3 files changed, 75 insertions(+), 42 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2cd47ba..525260a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -32,7 +32,7 @@ - + @@ -72,21 +72,23 @@ - + - - - + + + + + - + - - + + @@ -200,8 +202,8 @@ @@ -311,7 +313,7 @@ - + - + - + - + @@ -919,16 +921,6 @@ - - - - - - - - - - @@ -939,47 +931,59 @@ - + - + - + - + - + - + - + - - + + + + - - + + + + + + + + + + + + \ No newline at end of file diff --git a/miaozaiye/G-ball.py b/miaozaiye/G-ball.py index 5ac964a..101b409 100644 --- a/miaozaiye/G-ball.py +++ b/miaozaiye/G-ball.py @@ -1,7 +1,7 @@ #有重力效果的球 - -from stdpackage import stddraw +import math +from stdpackage import stddraw,stdarray,stdaudio stddraw.setXscale(-1.0,1.0) stddraw.setYscale(-1.0,1.0) @@ -16,14 +16,31 @@ f = 0.005 position1 = [(r1x,r1y)] +SPS = 44100 +CONCERT_A = 330.0 +duration = 0.1 +n = int(duration*SPS) +pitch = 1 +hz = CONCERT_A*(2**(pitch/12.0)) + +samples = stdarray.create1D(n+1,0.0) +for i in range(n+1): + samples[i] = math.sin(2.0*math.pi*i*hz/SPS) + changepoint = [] + while True: - if abs(r1x+v1x)+RADIUS>1.0: v1x = -v1x + if abs(r1x+v1x)+RADIUS>1.0: + v1x = -v1x + + stdaudio.playSamples(samples) + v1y +=g if abs(r1y + v1y)+RADIUS >1.0: v1y = -v1y + stdaudio.playSamples(samples) r1x = r1x+v1x r1y = r1y + v1y diff --git a/miaozaiye/clock.py b/miaozaiye/clock.py index 9531def..60d6c09 100644 --- a/miaozaiye/clock.py +++ b/miaozaiye/clock.py @@ -9,11 +9,22 @@ ''' from stdpackage import stddraw -from stdpackage import stdarray +from stdpackage import stdarray,stdaudio import math import sys +SPS = 44100 +CONCERT_A = 330.0 +duration = 0.1 +n = int(duration*SPS) +pitch = 1 +hz = CONCERT_A*(2**(pitch/12.0)) + +samples = stdarray.create1D(n+1,0.0) +for i in range(n+1): + samples[i] = math.sin(2.0*math.pi*i*hz/SPS) + def draw_clock(H = 0,M = 0, S = 0): stddraw.setXscale(-1.5,1.5) stddraw.setYscale(-1.5,1.5) @@ -91,6 +102,7 @@ def draw_clock(H = 0,M = 0, S = 0): stddraw.setPenRadius(0.005) stddraw.line(0,0,Sec_L*math.cos(-angle_s),Sec_L*math.sin(-angle_s)) + stdaudio.playSamples(samples) stddraw.show(1000) def main(): From a6dd51c78f31d3578af1a9e04644c7e68b84a873 Mon Sep 17 00:00:00 2001 From: miaozaiye Date: Sat, 7 Oct 2017 22:33:16 +0800 Subject: [PATCH 2/6] add tune in dragon_line --- .idea/workspace.xml | 249 ++++++++++++++++++--------------------- miaozaiye/dragon_line.py | 28 ++++- 2 files changed, 140 insertions(+), 137 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 525260a..17f2e57 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,8 @@ - - + + @@ -28,35 +28,37 @@ - - + + - - + + - + - - + + - - + + + + - - + + - + - + @@ -72,11 +74,11 @@ - + - - + + @@ -96,43 +98,41 @@ - - + + - + - - + + - - - - + + - - + + - + - - + + - + @@ -197,13 +197,13 @@ @@ -213,7 +213,7 @@ true - - - + + - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -563,7 +545,7 @@ - + @@ -631,13 +613,6 @@ - - - - - - - @@ -813,13 +788,6 @@ - - - - - - - @@ -827,13 +795,6 @@ - - - - - - - @@ -869,84 +830,100 @@ - + - - - - + - + - - + - + - - + - + - - + - + - - - - + - + - - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -955,32 +932,32 @@ - + - + - + - + - + - + - + - - + + - + diff --git a/miaozaiye/dragon_line.py b/miaozaiye/dragon_line.py index 53eac96..b77d514 100644 --- a/miaozaiye/dragon_line.py +++ b/miaozaiye/dragon_line.py @@ -25,6 +25,20 @@ from stdpackage import stddraw import math +from stdpackage import stdarray +from stdpackage import stdaudio + + +SPS = 44100 +CONCERT_A = 440.0 + +pitch0 = 3 +duration = 0.5 + +n = int(SPS*duration) + + + class DragonLine: def __init__(self): @@ -46,6 +60,17 @@ def turn_flow(self,wrap): dragonline = DragonLine() +def play_tune(x,y): + pitch = (pitch0) * math.sqrt(x*x+y*y) + + + hz = CONCERT_A*(2**(pitch/12.0)) + samples = stdarray.create1D(n+1,0.0) + for i in range(n+1): + samples[i] = math.sin(2.0*math.pi*i*hz/SPS) + stdaudio.playSamples(samples) + + def draw_line(dragonline,turn): stddraw.setXscale(-5,5) stddraw.setYscale(-5,5) @@ -75,9 +100,10 @@ def draw_line(dragonline,turn): stddraw.setPenColor(stddraw.BLACK) stddraw.line(x_list[index],y_list[index],x_list[index+1],y_list[index+1]) stddraw.setPenColor(stddraw.RED) + play_tune(x,y) # stddraw.text(x_list[index],y_list[index],'({0},{1}) to ({2},{3})'.format(x_list[index],y_list[index],x_list[index+1],y_list[index+1])) # stddraw.text(x_list[index],y_list[index],str(index)) stddraw.show(50) -draw_line(dragonline,30) +draw_line(dragonline,20) From e1a5236e2377367709d2ff8102b471bf0a951e80 Mon Sep 17 00:00:00 2001 From: miaozaiye Date: Sat, 7 Oct 2017 22:44:17 +0800 Subject: [PATCH 3/6] add tune in circle.py --- .idea/workspace.xml | 157 ++++++++++++++++++++------------------- miaozaiye/1-26 circle.py | 25 +++++++ 2 files changed, 106 insertions(+), 76 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 17f2e57..fbd402f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,7 @@ - + @@ -28,11 +28,11 @@ - + - - + + @@ -40,80 +40,80 @@ - - + + - - - - + + - - + + - + - + - - + + - - + + - - + + - + - + - - + + - + - + - - + + - + - - + + - - + + + + @@ -196,7 +196,6 @@ @@ -295,7 +295,7 @@ - + - + - + - + @@ -546,17 +546,17 @@ - + - + - + @@ -823,13 +823,6 @@ - - - - - - - @@ -851,13 +844,6 @@ - - - - - - - @@ -865,13 +851,6 @@ - - - - - - - @@ -942,10 +921,18 @@ + + + + + + + + - + @@ -954,13 +941,31 @@ - - + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/miaozaiye/1-26 circle.py b/miaozaiye/1-26 circle.py index 2d819ec..029cf28 100644 --- a/miaozaiye/1-26 circle.py +++ b/miaozaiye/1-26 circle.py @@ -4,12 +4,35 @@ import random import optparse from stdpackage import stddraw +from stdpackage import stdarray +from stdpackage import stdaudio +import math + +SPS = 44100 +CONCERT_A = 440.0 + +pitch0 = 6 + + + + n = 200 p = 0.3 min = 0.05 max = 0.2 +def play_tune(x,y,r): + pitch = (pitch0) * math.sqrt(x*x+y*y)/10 + duration = float(r)*3 + n1 = int(SPS*duration) + hz = CONCERT_A*(2**(pitch/12.0)) + samples = stdarray.create1D(n1+1,0.0) + for i in range(n+1): + samples[i] = math.sin(2.0*math.pi*i*hz/SPS) + stdaudio.playSamples(samples) + + def main(): @@ -45,6 +68,8 @@ def main(): stddraw.filledCircle(x,y,r) i +=1 + + play_tune(x,y,r) stddraw.show(10) From 7101894198c9c9e5111622352bba3de872f0e65f Mon Sep 17 00:00:00 2001 From: miaozaiye Date: Thu, 12 Oct 2017 17:59:25 +0800 Subject: [PATCH 4/6] update spirograph with sound --- .idea/workspace.xml | 103 +++++++++++++++++------------------ miaozaiye/1-31-Spirograph.py | 20 ++++++- 2 files changed, 70 insertions(+), 53 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index fbd402f..2b33e7e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,7 @@ - + @@ -32,7 +32,7 @@ - + @@ -65,9 +65,11 @@ - - - + + + + + @@ -198,12 +200,12 @@ @@ -213,8 +215,8 @@ true - @@ -545,28 +547,28 @@ - + - + - - + - + + @@ -855,9 +857,6 @@ - - - @@ -865,7 +864,6 @@ - @@ -873,59 +871,54 @@ - - - - - - - - - - + - + - + - + - + - + - - + + + + - + - - + + - + - + - - + + + + @@ -939,30 +932,36 @@ - + - - - - + + - + - + - + - + - - + + + + + + + + + + diff --git a/miaozaiye/1-31-Spirograph.py b/miaozaiye/1-31-Spirograph.py index 760081f..0fb4ba6 100644 --- a/miaozaiye/1-31-Spirograph.py +++ b/miaozaiye/1-31-Spirograph.py @@ -7,7 +7,24 @@ import sys import math -from stdpackage import stddraw,stdarray +from stdpackage import stddraw,stdarray,stdaudio + +SPS = 44100 +CONCERT_A = 220.0 + +pitch0 = 3 +duration = 0.5 + +n = int(SPS*duration) + + +def play_tune(x,y): + pitch = (pitch0) * (x+y)/2 + hz = CONCERT_A*(2**(pitch/12.0)) + samples = stdarray.create1D(n+1,0.0) + for i in range(n+1): + samples[i] = math.sin(2.0*math.pi*i*hz/SPS) + stdaudio.playSamples(samples) def draw_spirograph(R,r,a): stddraw.setXscale(-40,40) @@ -32,6 +49,7 @@ def draw_spirograph(R,r,a): stddraw.line(x[t],y[t],x[t+1],y[t+1]) + play_tune(x[t],y[t]) stddraw.show(5) stddraw.show() From 9d68d6200adcfa1d6b9c60c0298472e5cf1c6cd9 Mon Sep 17 00:00:00 2001 From: miaozaiye Date: Thu, 12 Oct 2017 18:31:09 +0800 Subject: [PATCH 5/6] oscilloscope.py --- .idea/workspace.xml | 125 ++++++++++++++++++++------------------ miaozaiye/oscilloscope.py | 46 ++++++++++++++ 2 files changed, 111 insertions(+), 60 deletions(-) create mode 100644 miaozaiye/oscilloscope.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2b33e7e..f9abf0f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,8 @@ + - @@ -40,16 +40,6 @@ - - - - - - - - - - @@ -62,11 +52,11 @@ - + - - + + @@ -74,6 +64,18 @@ + + + + + + + + + + + + @@ -155,7 +157,6 @@ @@ -215,8 +217,8 @@ true - @@ -297,8 +299,8 @@ - - + + - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -547,28 +549,28 @@ - + - + + - + + - - @@ -587,13 +589,6 @@ - - - - - - - @@ -902,16 +897,6 @@ - - - - - - - - - - @@ -966,5 +951,25 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/miaozaiye/oscilloscope.py b/miaozaiye/oscilloscope.py new file mode 100644 index 0000000..c8e38de --- /dev/null +++ b/miaozaiye/oscilloscope.py @@ -0,0 +1,46 @@ +#示波器,程序模拟示波器的输入,产生李萨如模式 +''' + +当两个相互垂直的周期扰动同时发生时,结果产生该模式图案 + +x(t) = A_x*sin(w_x*t+theta_x) +y(t) = A_y*sin(w_x*t+theta_y) + +程序带六个命令行参数: A_x,A_y,w_x,w_y,theta_x,theta_y +''' + +import math +import sys +from stdpackage import stddraw,stdaudio + +def draw(x1,x0,y1,y0): + stddraw.line(x0,y0,x1,y1) + pass + +def main(): + A_x,A_y,w_x,w_y,theta_x,theta_y = sys.argv[1:] + A_x = float(A_x) + A_y = float(A_y) + w_x = float(w_x) + w_y = float(w_y) + theta_x = float(theta_x) + theta_y = float(theta_y) + i = 0 + X = [] + Y = [] + stddraw.setXscale(-4,4) + stddraw.setYscale(-4,4) + while True: + x = A_x*math.sin(w_x*i+theta_x) + y = A_y*math.sin(w_y*i+theta_y) + X.append(x) + Y.append(y) + if i <1: + pass + else: + draw(X[i],X[i-1],Y[i],Y[i-1]) + stddraw.show(20) + i +=1 + + +main() \ No newline at end of file From 422725d8a8d43443990f88a03504b2e06ac7732d Mon Sep 17 00:00:00 2001 From: miaozaiye Date: Thu, 12 Oct 2017 18:50:56 +0800 Subject: [PATCH 6/6] update --- .idea/workspace.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f9abf0f..0550895 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -67,7 +67,7 @@ - + @@ -963,7 +963,7 @@ - +