Author Topic: [C++/SDL/OpenGL/BASS] Help with Synchronizing video and audio  (Read 6385 times)

0 Members and 1 Guest are viewing this topic.

Offline S0lll0s

  • ZX 81
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
I am starting my first (2d) demo now, and I get along pretty well except for major synchronization problems.

I do not have a graphics timer, I base all my graphics on the current audio time as I only play a mp3 for my first demo.

The libraries I use are SDL with OpenGL and BASS as an Audio Library. I am creating my own engine from scratch.
The music for my demo is Awolnation - Sail, so you should go ahead and obtain a mp3 of that song (legally of course) and put it as sail.mp3 in the binary folder if you want to test it.

I know that Sail is 119 BPM and my Syncing works pretty well for the first ~50seconds, but then it slowly drifts off and gets really bad. It should draw a green Hexagon every when the kick starts (and every 2nd bar before that too).

This is how I check if the kick just kick'd:
Code: [Select]
       int millis = (int)( time * 1000.0f ); //time is the playback pos in seconds (double)
        int everyX = (int)( 119.0 / 60.0f * 1000.0f ); //everyX is the time it takes for a bar to pass
       
        if ( ( millis + everyX / 4 + everyX ) % ( everyX * 2 ) < 80 ) //The song starts with an offset and I want every 2nd bar
            return true;

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Quote
my Syncing works pretty well for the first ~50seconds, but then it slowly drifts off and gets really bad.

Hi S0lll0s,

I picked the track from youtube (this) and opened it in some random wave editor.
The track starts at 0.175s.
Between 33.424 and 65.692 are exactly 8 sequences, makes 4.0335 seconds per sequence.
Let's assume 8 beats per sequence (which results in 119.003 bpm).
Now I simplified your main loop to the following:
Code: [Select]
  float nextBeat= 0.175f; // start of the track
  float secondsPerBeat= 4.0335f / 8.0;

  while( true )
  {
    ...
   
    glClear( GL_COLOR_BUFFER_BIT );
     
    musicTime = BASS_ChannelBytes2Seconds( sailMusic, BASS_ChannelGetPosition( sailMusic, BASS_POS_BYTE ) );

    if (musicTime >= nextBeat)
    {
         nextBeat += secondsPerBeat;
         callback2();
    }

    SDL_GL_SwapBuffers();
  }

And a green hexagon is flashing to the beat.
Should be possible to get the rest working from here on.


how do I draw "thick" lines in OpenGL?
You call glLineWidth with the desired width.


 ???  ???

and make them grow?

 ;D  ;D
« Last Edit: April 07, 2013 by hellfire »
Challenge Trophies Won:

Offline S0lll0s

  • ZX 81
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
Quote
my Syncing works pretty well for the first ~50seconds, but then it slowly drifts off and gets really bad.

Hi S0lll0s,

[...]
And a green hexagon is flashing to the beat.
Should be possible to get the rest working from here on.
Thank you, I moved on with the graphics but synchronization still didn't work, I will try this now.

how do I draw "thick" lines in OpenGL?
You call glLineWidth with the desired width.
I meant curves that look smooth, like the grey in this one:


and make them grow?

 ;D  ;D

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
I meant curves that look smooth, like the grey in this one:

Curves are usually drawn by subdividing the round parts into many small line segments.
The more segments you use, the smoother it looks.
In your case it's probably enough to keep a bit of space between two lines and connect them with a quarter of a circle.
You might want to look up splines, too.

by growing I mean starting very short and progressively get longer.
Well, I guess you're already able to draw a short line and a long line.
So I'm pretty sure you can draw any length in between, too...
And what you're asking for is varying the length over time.
Challenge Trophies Won:

Offline S0lll0s

  • ZX 81
  • *
  • Posts: 6
  • Karma: 0
    • View Profile
by growing I mean starting very short and progressively get longer.
Well, I guess you're already able to draw a short line and a long line.
So I'm pretty sure you can draw any length in between, too...
And what you're asking for is varying the length over time.
[/quote]

Anyways, thanks for that link. I tried to find a good introduction to splines and this seems worth the read, I could only find spline librarys and extremely mathematical info.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Maybe you find this helpful for a start.
Challenge Trophies Won: