Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python VPython, Scamp and Ableton

McDervish

New Coder
VPython and Ableton….

I’ve been using VPython to simulate a pendulum / pendulum wave. Now using scamp library and triggering audio in Ableton via IAC bus when pendulums/s extent of travel is at max. I have got it working but the program slows/stalls every time a pendulum reaches full extent; audio is triggered but VPython visuals stall until audio triggered. Tried threading with pendulum as one function and audio trigger as another but no joy…. Any ideas? Multiprocessing?
 
Can you provide us with some code please so we can have a look for you 😀
from vpython import *
import math as m
import threading as th
from scamp import *

# s = Session()
#theramin = s.new_midi_part("theramin", "IAC Bus 1", start_channel=0, num_channels=1)

lengths = [
0.3438,0.3307,0.31836,0.3067,
0.2956,0.28516,0.27524, 0.26583,
0.2569, 0.2484, 0.24033,0.23264,
0.2253, 0.21833, 0.21166, 0.2053
]

class Pendulum:

def init(self,length):
self.rod = cylinder(radius=0.001)
self.bob = sphere(radius=0.01)
self.length = length
self.theta = 7*m.pi/4
self.gravity = 0.003
self.theta_vel = 0
self.z = 0
self.s = Session()


def swing(self):
force = self.gravity * m.cos(self.theta)
theta_acc = (-1 * force) / self.length
self.theta_vel+=theta_acc
self.theta+=self.theta_vel

x = m.cos(self.theta)*self.length
y = m.sin(self.theta)*self.length
# +5000 moves the whole thing up on y axis
self.rod.axis=vector(x,y,self.z)
self.bob.pos=vector(x,y,self.z)
# self.bob.color=vector(1,0,0)#red
# self.bob.color=vector(1,0.5,0)#orange
# self.bob.color=vector(1,1,0)#yellow
# self.bob.color=vector(0,1,0)#green
# self.bob.color=vector(0,0,255)#blue
# self.bob.color=vector(0.29,0,0.51)#indigo
# self.bob.color=vector(0.5,0,1)#violet
if self.length == lengths[0] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(0,48,0.5,0.1)
print("hit")
elif self.length == lengths[1] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(1,53,0.5,0.1)
elif self.length == lengths[2] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(2,60,0.5,0.1)
elif self.length == lengths[3] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(3,65,0.5,0.1)
elif self.length == lengths[4] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(4,72,0.5,0.5)

def sound(self,MIDI_ch, MIDI_note,volume,duration):
#self.s.new_midi_part("theramin", "IAC Bus 1", start_channel=0, num_channels=1)
self.MIDI_ch = MIDI_ch
self.MIDI_note = MIDI_note
self.volume = volume
self.duration = duration
self.s.new_midi_part("theramin", "IAC Bus 1", start_channel=self.MIDI_ch, num_channels=1).play_note(self.MIDI_note,self.volume,self.duration)





pendulums = [Pendulum(length) for length in lengths]

# for pendulum in pendulums:
# threads = [th.Thread(target = Pendulum, args = length,) for length in lengths]
#
# for thread in threads:
# thread.start()
# thread.join()



while True:
rate(25)
for i in pendulums:
i.swing()
 
from vpython import *
import math as m
import threading as th
from scamp import *

# s = Session()
#theramin = s.new_midi_part("theramin", "IAC Bus 1", start_channel=0, num_channels=1)

lengths = [
0.3438,0.3307,0.31836,0.3067,
0.2956,0.28516,0.27524, 0.26583,
0.2569, 0.2484, 0.24033,0.23264,
0.2253, 0.21833, 0.21166, 0.2053
]

class Pendulum:

def init(self,length):
self.rod = cylinder(radius=0.001)
self.bob = sphere(radius=0.01)
self.length = length
self.theta = 7*m.pi/4
self.gravity = 0.003
self.theta_vel = 0
self.z = 0
self.s = Session()


def swing(self):
force = self.gravity * m.cos(self.theta)
theta_acc = (-1 * force) / self.length
self.theta_vel+=theta_acc
self.theta+=self.theta_vel

x = m.cos(self.theta)*self.length
y = m.sin(self.theta)*self.length
# +5000 moves the whole thing up on y axis
self.rod.axis=vector(x,y,self.z)
self.bob.pos=vector(x,y,self.z)
# self.bob.color=vector(1,0,0)#red
# self.bob.color=vector(1,0.5,0)#orange
# self.bob.color=vector(1,1,0)#yellow
# self.bob.color=vector(0,1,0)#green
# self.bob.color=vector(0,0,255)#blue
# self.bob.color=vector(0.29,0,0.51)#indigo
# self.bob.color=vector(0.5,0,1)#violet
if self.length == lengths[0] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(0,48,0.5,0.1)
print("hit")
elif self.length == lengths[1] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(1,53,0.5,0.1)
elif self.length == lengths[2] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(2,60,0.5,0.1)
elif self.length == lengths[3] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(3,65,0.5,0.1)
elif self.length == lengths[4] and (self.theta <= 5*m.pi/4 or self.theta >= 7*m.pi/4):
self.sound(4,72,0.5,0.5)

def sound(self,MIDI_ch, MIDI_note,volume,duration):
#self.s.new_midi_part("theramin", "IAC Bus 1", start_channel=0, num_channels=1)
self.MIDI_ch = MIDI_ch
self.MIDI_note = MIDI_note
self.volume = volume
self.duration = duration
self.s.new_midi_part("theramin", "IAC Bus 1", start_channel=self.MIDI_ch, num_channels=1).play_note(self.MIDI_note,self.volume,self.duration)





pendulums = [Pendulum(length) for length in lengths]

# for pendulum in pendulums:
# threads = [th.Thread(target = Pendulum, args = length,) for length in lengths]
#
# for thread in threads:
# thread.start()
# thread.join()



while True:
rate(25)
for i in pendulums:
i.swing()
sorry formatting was lost when I copy / past'd
 
Back
Top Bottom