#Timing Issue
1 messages · Page 1 of 1 (latest)
I am tasked with playing a song and switching 12 relays to different configurations during the song
I'm using a pi 3 A+, although it's overkill.
I went thru the song and carefully marked the time that the lyrics change, down to the ms.
I'll upload the code so people can see it
one sec, I have to log in to discord on my pi, it's taking a bit
a bit was an understatement!
I feel like I've gone back in time 20 years to the dial up days
Unfortunately my wrinkles didn't disappear
haha yeah discord is a bit of a rough program for the pi
hmm
I could type out my code, but there is a huuge dictionary in there. I suppose I could type out just the important bits
That would work, but make sure either before or after you highlight the problem, it'll make it easier to understand what to look for while reading your code
Ah Ill do that now
So, my code opens the mp3 fine, plays it fine, but all the transitions seem to be off by one?
I've also written a short program to test the tlc59711 that I am using to trigger relays (testing setup is just driving LEDS), and I know it's working fine
here's the important bits
#imports and creation of large ordered dictionary. Keys are time in ms, values are lists of 12 #0's or 1s representing states of the relays
def sequencer(values):
values = [x * 65535 for x in values]
tlc59711[3] = (values[11],values[10],values[9])
tlc59711[2] = (values[8],values[7],values[6])
tlc59711[1] = (values[5],values[4],values[3])
tlc59711[0] = (values[2],values[1],values[0])
tlc59711.show()
while True:
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
values = [0,0,0,0,0,0,0,0,0,0,0,0]
for key in time_stamp_dict.keys():
if pygame.mixer.music.get_pos() <= key:
values = time_stamp_dict[key]
break
sequencer(values)
any syntax errors are from typing it out, it runs fine
dictionaries are not ordered
and I don't think that is the problem looking at it closer
I thought I did timing this way successfully in another project, but now I'm questioning that
I'm gonna try it with a slew of if-elif statements
it'll be gross but that's OK
Are you off by one ahead or behind?
Uhhh let me check again and see
I think I'm one ahead, the LEDs are lighting up early
Could just be a slight timing issue
yeah I wanted to avoid going thru and timestamping again, hoping it was some simple coding error
it took me houuuuurs
but if it's gotta be done, it's gotta be done
Thanks for your help, I'll post here again if I find I just timestamped wrong
You can upload your code file(s) with the + at the left, no reason to retype it here
Oh, I'm writing it on the pi, but I couldn't get discord to load on the pi
Oh wait....usb drive!
you could enable ssh server on the Pi and use pscp (comes with Putty) to copy files back and forth
Oh cool!
get the music play time
get_pos() -> time
This gets the number of milliseconds that the music has been playing for. The returned time only represents how long the music has been playing; it does not take into account any starting position offsets.```
From the looks of the docs, it might be calculating current ms based on time difference from the play() command, not the actual music start.
hmm
I could see your code being late if latency was an issue, but if it's early it's most likely some sort of offset problem
And if the offset is consistent enough, maybe all it takes is to insert a fixed offset into your key comparison
But I'd need to figure out the offset, if that is the problem?
You could also just calibrate it manually, test a couple of numbers and see if it's still off
Assuming the offset is consistent
I know it's common in rhythm games like DDR to set a global offset to align arrows on the screen with the actual audio
heres a screenshot of the shell, you can see the transition in real time. the ms are from get_pos()
so the transition seems to be happening at the right time??
compared to my dict
but it feels off when you listen to it?
yeah, I can take a video. The first LED is supposed to light up when "A partridge in a pear tree" starts
since I'm getting my phone out, I'll also reward you lovely folks with a picture of my cute dog
No, I believe you. How was the dict written?
No, but how did you come up with the timings for each entry? Did you write it yourself?
Oh, I see, you're off by a sample
THe first light isn't supposed to come on until 11690?
Oh nvm
My video just finished uploading to drive, let me pull it down
the key is end time not start
uhhh I think so
I wrote this near the end of my day, so my brain wasn't 100%
the first blue light is supposed to come on when "A partridge in a pear tree" starts, and turn off when "On the second day of christmas" starts
I will go timestamp it again and re-write the dictionary and see if that fixes it
If it does I will be only mildly ashamed
I think your dict timestamps are supposed to be start times.
Your code is switching to the sample after it exceeds the previous value, so I think all the keys in your dict need to shift up one entry...
hmm, can that be solved with code?
Yes it can, but I can't say it's a clean solution
if you saw the mechanical system this was controlling, you wouldn't be concerned about cleanliness 🙂
for key in time_stamp_dict.keys():
if key > pygame.mixer.music.get_pos():
break
values = time_stamp_dict[key]
Ah wait
Fixed
Try that?
oh just a fun tidbit, if you add py after the three ` marks it will do python syntax highlighting
or python
```py
sorry the first three back ticks
woot!
lol np
That seems to have solved it!!!!
I'm going to credit you and the rest of the adafruit folks in the manual for this
let me get that dog picture too
@dapper thunder Dog breath thanks you!
Cute
She loves my work from home days