#Timing Issue

1 messages · Page 1 of 1 (latest)

lone trellis
#

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

wintry badge
#

haha yeah discord is a bit of a rough program for the pi

lone trellis
#

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

wintry badge
#

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

lone trellis
#

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

fresh lion
#

dictionaries are not ordered

lone trellis
#

oops it's an ordered dict

#

I'll edit above to indicate

fresh lion
#

and I don't think that is the problem looking at it closer

lone trellis
#

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

fresh lion
#

Are you off by one ahead or behind?

lone trellis
#

Uhhh let me check again and see

#

I think I'm one ahead, the LEDs are lighting up early

fresh lion
#

Could just be a slight timing issue

lone trellis
#

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

mystic sage
#

You can upload your code file(s) with the + at the left, no reason to retype it here

lone trellis
#

Oh, I'm writing it on the pi, but I couldn't get discord to load on the pi

#

Oh wait....usb drive!

mystic sage
#

what is the other computer you have?

#

Mac, Windows, Linux?

lone trellis
#

Windows

mystic sage
#

you could enable ssh server on the Pi and use pscp (comes with Putty) to copy files back and forth

lone trellis
#

Oh cool!

dapper thunder
#
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.
lone trellis
#

hmm

dapper thunder
#

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

lone trellis
#

so...use onboard RTC?

#

hmm

lone trellis
dapper thunder
#

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

lone trellis
#

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

dapper thunder
#

but it feels off when you listen to it?

lone trellis
#

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

dapper thunder
#

No, I believe you. How was the dict written?

lone trellis
#

I posted my code above

#

it's a bit long so may need to download it

dapper thunder
#

No, but how did you come up with the timings for each entry? Did you write it yourself?

lone trellis
#

oh yes

#

I went thru the song in audacity and timestamped everything

dapper thunder
#

Oh, I see, you're off by a sample

#

THe first light isn't supposed to come on until 11690?

#

Oh nvm

lone trellis
#

My video just finished uploading to drive, let me pull it down

dapper thunder
#

the key is end time not start

lone trellis
#

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

dapper thunder
#

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...

lone trellis
#

hmm, can that be solved with code?

dapper thunder
#

Yes it can, but I can't say it's a clean solution

lone trellis
#

if you saw the mechanical system this was controlling, you wouldn't be concerned about cleanliness 🙂

dapper thunder
#
        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?

lone trellis
#

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!

dapper thunder
#

lol np

lone trellis
#

It's a nice feature

#

Ill try that thanks! one sec

lone trellis
#

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!

dapper thunder
#

Cute

lone trellis
#

She loves my work from home days