How can I move an object with audio?

Hello…

I have a problem to solve. Thanks for your help.

I have an audio file. (2 min long)

when I tap on a button the music plays.

(and the time for the file star running… 0:00:00 - 0:00:01 etc)

I have a function

    totalTime = audio.getDuration( audio1 )
    print(totalTime)

the print time is 11494

I have an object, a rectangle.

I want to be able to move that object, where ever I need

when the song is on the first beat or the second measure.

    h = display.newRect(0, 0, 100, 100)     h.x = display.contentCenterX     h.y = display.contentCenterY     function moveHimage( event )         if totalTime == 5600 then             h.x = display.contentCenterX + 200         end     end     Runtime:addEventListener( "enterFrame", moveHimage )

I got this function but is not working

any idea how can I do this.

Thanks for your help

Victor

totalTime = audio.getDuration( audio1 ) h = display.newRect(0, 0, 100, 100) h.x = display.contentCenterX h.y = display.contentCenterY transition.to( h, { x = display.contentCenterX + 200, time = 0, delay = totalTime } )

Let me be the negative nancy here: you won’t be able to get a tight and secure indication of when the first or second beat falls, because audio is done on a best-effort basis. Lord knows I’ve tried to create plenty of rhythm based games.

Yeah, I didn’t want to mention that all this timing, while ms accurate, is still frame-related, so synchronizing sound and visuals is not easy, especially for a rhythm game that needs to know what sounds/beats are playing.

Thank you very much. You helped me a lot!!!

I managed to make it work (More or Less)

I was not expecting something perfect.

The transition (time = 0) help a lot.

I create this function for a 4 bar music sample

    function moveIndicator()     tempo = (totalTime/16) - (40)     transition.to( h, { x = display.contentCenterX - 400, time = 0, delay = tempo\*1 } )     transition.to( h, { x = display.contentCenterX - 300, time = 0, delay = tempo\*2 } )     transition.to( h, { x = display.contentCenterX - 250, time = 0, delay = tempo\*3 } )     transition.to( h, { x = display.contentCenterX - 128, time = 0, delay = tempo\*4 } )     transition.to( h, { x = display.contentCenterX - 100, time = 0, delay = tempo\*5 } )     transition.to( h, { x = display.contentCenterX - 86, time = 0, delay = tempo\*6 } )     transition.to( h, { x = display.contentCenterX - 10, time = 0, delay = tempo\*7 } )     transition.to( h, { x = display.contentCenterX + 10, time = 0, delay = tempo\*8 } )     transition.to( h, { x = display.contentCenterX + 52, time = 0, delay = tempo\*9 } )     transition.to( h, { x = display.contentCenterX + 85, time = 0, delay = tempo\*10 } )     transition.to( h, { x = display.contentCenterX + 150, time = 0, delay = tempo\*11 } )     transition.to( h, { x = display.contentCenterX + 200, time = 0, delay = tempo\*12 } )     transition.to( h, { x = display.contentCenterX + 250, time = 0, delay = tempo\*13 } )     transition.to( h, { x = display.contentCenterX + 328, time = 0, delay = tempo\*14 } )     transition.to( h, { x = display.contentCenterX + 400, time = 0, delay = tempo\*15 } )     transition.to( h, { x = display.contentCenterX + 500, time = 0, delay = tempo\*16 } )     end

I can change the “tempo” to make it go faster or slower

the - (40) is a little adjustment

And each of the transitions will give me 1 beat

I have 16 beats = 4 measures.

And I can adjust the CenterX - 300 or centerX - 317

until it looks more or less okay on top of the note.

Like this I can make the song look nice!!!

The only problem is if I have 100 songs…

imagine all the time it would take me to do this…

Is there a way I can optimize this function?

Like a formula to tell the program…

The image for the note is

image = 875 pixeles longX

divide that image into 4 so 875/4 = variableA

so variableA = 1 measure

divide 1 measure/4 = beat1

now CenterX = beat1

=beat1 = beat1 + 1

or something like that I get so confuse

Can you help me please…

or should I use this function 100 times?

Victor

Why not generate based on a table instead?

local function moveIndicator( h, values )    tempo = (totalTime/16) - (40)    for i = 1, #values do       transition.to( h, { x = display.contentCenterX + values[i], time = 0, delay = tempo \* i } )    end end

local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 } moveIndicator( h, song1 )

I think there is something missing I get this error

– Attempt to get length of local ‘values’ (a nil value) –

values is a table? Where do I create it?

See to correction to my code above:

local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 } moveIndicator( h, song1 )

Please forgive me roaminggamer…

I still don’t get it. I see the same code…

song1 is a table

when I call the function moveIndicator (  here I see song1 )

but inside is the #values

    local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 }     function moveIndicator( h, values )            tempo = (totalTime/16) - (40)            h.isVisible = true            for i = 1, #values do               transition.to( h, { x = display.contentCenterX + values[i], time = 0, delay = tempo \* i } )            end     end     moveIndicator( song1 )

Still gives me the same error

Attempt to get length of local ‘values’ (a nil value)

Sorry, but I don’t get it…

I need a little bit more help, please…

I am this close to make it work I just don’t get the

#values

please help me out

Thanks in advance

I don’t know what ‘h’ is from your original example, but you need to pass that in as I’ve been showing:

moveIndicator( h, song1 )

Thank you… I kept thinking and thinking…

I got it!!!

The problem was in two different names…

You put “values” inside the parenthesis, and also “values” and the loop for i – #values

and also inside the transition { + values[i] }

local function moveIndicator( h, values )    tempo = (totalTime/16) - (40)    for i = 1, #values do       transition.to( h, { x = display.contentCenterX + values[i], time = 0, delay = tempo \* i } )    end end

but when you call the function, you put song1 – ( h, song1 )

( h is just the rectangle to move )

local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 } moveIndicator( h, song1 )

So song1 and values should be the same…

i change that to only 1 variable

values = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 }     function moveIndicator( h, values )    tempo = (totalTime/16) - (40)    for i = 1, #values do       transition.to( h, { x = display.contentCenterX + values[i], time = 0, delay = tempo \* i } )    end end

moveIndicator( h, values )

And it works perfect…

I think you wanted me to say that all the information on the values table

was for the song1,

and another information for the song2 and so on

But i did not get it at first

Now thanks ti you it works really good

and I learn the “famous” “pass it to a function”

Thanks

roaminggamer

totalTime = audio.getDuration( audio1 ) h = display.newRect(0, 0, 100, 100) h.x = display.contentCenterX h.y = display.contentCenterY transition.to( h, { x = display.contentCenterX + 200, time = 0, delay = totalTime } )

Let me be the negative nancy here: you won’t be able to get a tight and secure indication of when the first or second beat falls, because audio is done on a best-effort basis. Lord knows I’ve tried to create plenty of rhythm based games.

Yeah, I didn’t want to mention that all this timing, while ms accurate, is still frame-related, so synchronizing sound and visuals is not easy, especially for a rhythm game that needs to know what sounds/beats are playing.

Thank you very much. You helped me a lot!!!

I managed to make it work (More or Less)

I was not expecting something perfect.

The transition (time = 0) help a lot.

I create this function for a 4 bar music sample

    function moveIndicator()     tempo = (totalTime/16) - (40)     transition.to( h, { x = display.contentCenterX - 400, time = 0, delay = tempo\*1 } )     transition.to( h, { x = display.contentCenterX - 300, time = 0, delay = tempo\*2 } )     transition.to( h, { x = display.contentCenterX - 250, time = 0, delay = tempo\*3 } )     transition.to( h, { x = display.contentCenterX - 128, time = 0, delay = tempo\*4 } )     transition.to( h, { x = display.contentCenterX - 100, time = 0, delay = tempo\*5 } )     transition.to( h, { x = display.contentCenterX - 86, time = 0, delay = tempo\*6 } )     transition.to( h, { x = display.contentCenterX - 10, time = 0, delay = tempo\*7 } )     transition.to( h, { x = display.contentCenterX + 10, time = 0, delay = tempo\*8 } )     transition.to( h, { x = display.contentCenterX + 52, time = 0, delay = tempo\*9 } )     transition.to( h, { x = display.contentCenterX + 85, time = 0, delay = tempo\*10 } )     transition.to( h, { x = display.contentCenterX + 150, time = 0, delay = tempo\*11 } )     transition.to( h, { x = display.contentCenterX + 200, time = 0, delay = tempo\*12 } )     transition.to( h, { x = display.contentCenterX + 250, time = 0, delay = tempo\*13 } )     transition.to( h, { x = display.contentCenterX + 328, time = 0, delay = tempo\*14 } )     transition.to( h, { x = display.contentCenterX + 400, time = 0, delay = tempo\*15 } )     transition.to( h, { x = display.contentCenterX + 500, time = 0, delay = tempo\*16 } )     end

I can change the “tempo” to make it go faster or slower

the - (40) is a little adjustment

And each of the transitions will give me 1 beat

I have 16 beats = 4 measures.

And I can adjust the CenterX - 300 or centerX - 317

until it looks more or less okay on top of the note.

Like this I can make the song look nice!!!

The only problem is if I have 100 songs…

imagine all the time it would take me to do this…

Is there a way I can optimize this function?

Like a formula to tell the program…

The image for the note is

image = 875 pixeles longX

divide that image into 4 so 875/4 = variableA

so variableA = 1 measure

divide 1 measure/4 = beat1

now CenterX = beat1

=beat1 = beat1 + 1

or something like that I get so confuse

Can you help me please…

or should I use this function 100 times?

Victor

Why not generate based on a table instead?

local function moveIndicator( h, values )    tempo = (totalTime/16) - (40)    for i = 1, #values do       transition.to( h, { x = display.contentCenterX + values[i], time = 0, delay = tempo \* i } )    end end

local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 } moveIndicator( h, song1 )

I think there is something missing I get this error

– Attempt to get length of local ‘values’ (a nil value) –

values is a table? Where do I create it?

See to correction to my code above:

local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 } moveIndicator( h, song1 )

Please forgive me roaminggamer…

I still don’t get it. I see the same code…

song1 is a table

when I call the function moveIndicator (  here I see song1 )

but inside is the #values

    local song1 = { -400, -300, -250, -128 -100, -86, -10, 10, 52, 85, 150, 200, 250, 328, 400, 500 }     function moveIndicator( h, values )            tempo = (totalTime/16) - (40)            h.isVisible = true            for i = 1, #values do               transition.to( h, { x = display.contentCenterX + values[i], time = 0, delay = tempo \* i } )            end     end     moveIndicator( song1 )

Still gives me the same error

Attempt to get length of local ‘values’ (a nil value)

Sorry, but I don’t get it…