7 Replies Latest reply on Apr 19, 2006 6:16 PM by Newsgroup_User

    setInterval is inconsistant

    kyugawa
      I am trying to develop a simple drum synth in Flash 8 using multiple setInterval functions to play short drum samples in unison. My problem is that the setInterval plays inconsistantly for example, I have the system play one sample every 300ms and another at 600ms. Theoretically one should play twice as fast as the other and match a beat every 600ms, but it doesn't it seems that the interval is going either two fast or two slow. I am working on a new IMac G5 with a gig of ram so I highly doubt its the computer performance. If anyone as any ideas or suggestions on how to lock down a consistant interval please let me know. Thanks a lot!
        • 1. setInterval is inconsistant
          Kagada
          Look this in the help files in the setInterval description;

          "If interval is less than the SWF file's frame rate (for example, 10 frames per second [fps] is equal to 100 millisecond intervals), the interval function is called as close in time to the value of interval as possible. Executing long, memory-intensive scripts during an interval causes delays."

          How many fps your movie have ?
          • 2. Re: setInterval is inconsistant
            kyugawa Level 1
            I'm running 30fps. Thanks, I'll try increasing the frame rate.
            • 3. Re: setInterval is inconsistant
              Level 7
              "kyugawa" <webforumsuser@macromedia.com> wrote in message news:e0sm9i$j2e$1@forums.macromedia.com...
              >I am trying to develop a simple drum synth in Flash 8 using multiple
              > setInterval functions to play short drum samples in unison. My problem is that
              > the setInterval plays inconsistantly for example, I have the system play one
              > sample every 300ms and another at 600ms. Theoretically one should play twice as
              > fast as the other and match a beat every 600ms, but it doesn't it seems that
              > the interval is going either two fast or two slow. I am working on a new IMac
              > G5 with a gig of ram so I highly doubt its the computer performance. If anyone
              > as any ideas or suggestions on how to lock down a consistant interval please
              > let me know. Thanks a lot!
              >

              setInterval is not that accurate because it is tied to the frame rate. I find that if I want higher precision I need to use a
              setInterval of 1ms and then use getTimer to see how much time has elapsed (and also a frame rate of 30). With a goal of 300ms I get
              301 to 302 ms

              In this example my G4 note and my B4 note play within 3ms of each other.

              this.createEmptyMovieClip("mc1", 1);
              this.createEmptyMovieClip("mc2", 2);

              snd1 = new Sound(mc1);
              snd1.attachSound("b4");

              snd2 = new Sound(mc2);
              snd2.attachSound("g4");

              function timerThing()
              {
              nowTime = getTimer(); // current time

              // play the b4 note once every 300ms
              b4time = nowTime - startTimeB4; // calculate elapsed time
              if(b4time >= 300)
              {
              boxb4.text = b4time;
              startTimeB4 += 300; // for next time
              snd1.stop(); // in case still playing
              snd1.start();
              }

              // play the g4 note once every 300ms
              g4time = nowTime - startTimeG4; // calculate elapsed time
              if(g4time >= 300)
              {
              boxg4.text = g4time;
              startTimeG4 += 300; // for next time
              snd2.stop(); // in case still playing
              snd2.start();
              }
              }

              id1 = setInterval(timerThing, 1); // 1 ms interval (but it can't do it that fast)

              I don't think precision greater than about 2ms is possible with Flash and slower computers probably do worse than fast ones.
              tralfaz




              • 4. Re: setInterval is inconsistant
                Level 7
                never rely on onEnterFrame or on setInterval .. both are unreliable.

                However, if you do all your calcs based on actual elapsed time sine last
                call (by comparing getTimer() values), then it won't matter which you use.
                --
                Jeckyl


                • 5. Re: setInterval is inconsistant
                  kyugawa Level 1
                  Thanks a lot for all the help. I'm currently using that method (getTimer()) and it seems to be within 1-3 ms accurate. I'm assuming that there isn't any other way in flash to get pinpoint accuracy on playing notes.
                  • 6. Re: setInterval is inconsistant
                    sneakyimp Level 2
                    Unless your tempo (beats per second) is an integer multiple of your frame rate (frames per second). Then you will never ever get it to work properly in actionscript. Even if your tempo *is* then it is likely you will still have problems.

                    • 7. Re: setInterval is inconsistant
                      Level 7
                      > Unless your tempo (beats per second) is an integer multiple of your frame
                      > rate
                      > (frames per second). Then you will never ever get it to work properly in
                      > actionscript. Even if your tempo *is* then it is likely you will still
                      > have
                      > problems.

                      If you use a setInterval with a very short interval and use getTimer to
                      adjust your timing, then it should be possible to get VERY close to what you
                      want. Won't be perfect though. It just depends on what level of
                      imperfection you're happy with
                      --
                      Jeckyl