This site requires JavaScript, please enable it in your browser!
Greenfoot back
Brantgarron
Brantgarron wrote ...

2018/3/20

Trying to sync object with music

Brantgarron Brantgarron

2018/3/20

#
Hey, so I am trying to sync object that appears to certain beats in the music. I am pretty sure there is some way to detect the number of frames that have passed during a scenario and equate that to time....30 frames per one second I think How would I use this info to once that scenario reaches, let's say 150 seconds, to then spawn one of my objects? Right now I have a paw class that randomly spawns paws but obviously, this is just temporary and I wanted to see if what I wanted to do worked, now I would like some help with the time and frames issue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Game here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Game extends World
{
    private int leftPawSpawnTimer;
    private int rightPawSpawnTimer;
    private int upPawSpawnTimer;
    private int downPawSpawnTimer;
    GreenfootSound redBone = new GreenfootSound("redBone.wav");
    /**
     * Constructor for objects of class Game.
     *
     */
    public Game()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        //addObject(new Pause(), 530, 1);
        //addObject(new LeftPaw(), 45, 500); //those are the correct coordinates for LeftPaw
        //addObject(new RightPaw(), 293, 700);
        //addObject(new UpPaw(), 125, 500);
        //addObject(new DownPaw(), 208, 500);
        prepare();
 
    }
    public void prepare()
    {
        addObject(new Scoreboard(), 500, 30);
    }
    public void act()
    {
        leftPawSpawnTimer();
        rightPawSpawnTimer();
        upPawSpawnTimer();
        downPawSpawnTimer();  
        redBone.play();
    }
    public void leftPawSpawnTimer()
    {
        leftPawSpawnTimer = (leftPawSpawnTimer+1)%100;
        if (leftPawSpawnTimer == 0)
        leftPawSpawn();
    }
    public void rightPawSpawnTimer()
    {
        rightPawSpawnTimer = (rightPawSpawnTimer+1)%350;
        if(rightPawSpawnTimer == 0)
        rightPawSpawn();
    }
    public void upPawSpawnTimer()
    {
        upPawSpawnTimer = (upPawSpawnTimer+1)%275;
        if(upPawSpawnTimer == 0)
        upPawSpawn();
    }
    public void downPawSpawnTimer()
    {
        downPawSpawnTimer = (downPawSpawnTimer+1)%310;
        if(downPawSpawnTimer == 0)
        downPawSpawn();
    }
    public void leftPawSpawn()
    {
        addObject(new LeftPaw(), 45, 500);
    }
    public void rightPawSpawn()
    {
        addObject( new RightPaw(), 293, 500);
    }
    public void upPawSpawn()
    {
        addObject(new UpPaw(), 125, 500);
    }
    public void downPawSpawn()
    {
        addObject(new DownPaw(), 208, 500);
    }
}
Vercility Vercility

2018/3/20

#
I dont fully understand your question to be honest. What is your main goal? You can measure the time with System.currentTimeMillis or sth like that. FPS is dependent of System and might fluctuate due to System settings or performance so thats not a good way to measure time If you just need to let 100 secs pass use something like this
1
2
3
4
5
long cur = System.currentTimeMillis();
 
if(cur - System.currentTimeMillis() => 100000) {
//
}
Note that you'll have to safe the initial time (cur) outside of your loop environment (probably act)
Brantgarron Brantgarron

2018/3/20

#
It is a game similar to guitar hero that I am trying to make. Therefore I would like incoming object appear over a target area that the user would then hit the correct key to receive points. The only problem is that I am having trouble with syncing the appearing object with the background music
Vercility wrote...
I dont fully understand your question to be honest. What is your main goal? You can measure the time with System.currentTimeMillis or sth like that. FPS is dependent of System and might fluctuate due to System settings or performance so thats not a good way to measure time
Vercility Vercility

2018/3/20

#
ehhh i meant ">=" not "=>"
Brantgarron Brantgarron

2018/3/20

#
Not quite what I want....I would rather press keys to know the time between and where they start in the simulation and then use those numbers in frames or seconds to add an object when the world reaches that given frame or time
Vercility Vercility

2018/3/20

#
I can't Really grasp the difference to that (partly due to your broken English, no offense) Having the objects appear at a certain time should be possible with that approach. Can you give an example?
danpost danpost

2018/3/21

#
Probably the easiest thing to do is to count how many beats (or measures -- to be multiplied by beats per measure) the entire song is and then divide the song length in seconds by the number of beats. That should give you a fairly accurate measure to go by. In this case, I strongly suggest using system time over an act counter as the continuous playing of a song is unaffected by the running of act cycles.
Brantgarron Brantgarron

2018/3/21

#
what do you mean by using system time over an act counter danpost?
danpost wrote...
Probably the easiest thing to do is to count how many beats (or measures -- to be multiplied by beats per measure) the entire song is and then divide the song length in seconds by the number of beats. That should give you a fairly accurate measure to go by. In this case, I strongly suggest using system time over an act counter as the continuous playing of a song is unaffected by the running of act cycles.
danpost danpost

2018/3/21

#
An act counter would be like this:
1
2
3
4
5
6
7
8
9
10
// field
private int timer;
 
// action code
if (timer == 200*40) return; else timer++; // do nothing if done; else run timer
if (timer%40 == 0)
{
    System.out.println("Beat = "+(timer/40)); // if interval complete, then new beat
    // whatever action to be performed
}
The timer is incremented every act cycle and is dependent on the act cycles which varies time-wise from machine to machine. The '40' suggests something like 3 beats in about every 2 seconds. The '200' would be the number of beats in the entire song. Using the system clock you would have something like this (using the same '200' and 3 beats about every 2 seconds:
1
2
3
4
5
6
7
8
9
10
11
12
// field
private long startTime;
private int beats;
 
// action code
if (beats == 200) return;
if (startTime == 0) startTime = System.currentTimeMillis();
if ((System.currentTimeMillis()-startTime)/670 > beats)
{
    System.out.println("Beat = "+(++beats));
    // whatever action to be performed
}
This way is not influenced by the speed of the scenario. Changing the scenario speed also does not change the playing of the music. But it will change the speed of the act counter above.
You need to login to post a reply.