Why Same code of SDL C++ works fine in windows but in Linux it is producing delay

@cmakeshift Sorry for misguide you . It is a button created by a graphics library . That button have handler . When I am clicking that push button that handler will run.Inside that handler have a SDL code . It runs and play sound which is just a beep sound .Some time when I click second button after first button the audio was mixed . But SDL_mixer is better .

Just for fun, I’ve done some calculations:

  • 1 meter of speaker/headphone cable introduces a delay of about 4 nanoseconds (at 0.8 * speed_of_light which IIRC is the usual speed of electrons in a cable)
  • more importantly, based on a speed of sound of 343.2 m/s:
    • 1 meter between your speaker and your ear introduces a delay of almost 3mio nanoseconds (3 milliseconds, 2914 microseconds)
    • 1 millimeter between your speaker/headphone and your ear introduces a delay of about 2914 nanoseconds (about 3 microseconds)

This means if you move your head just one millimeter further from the speaker, you get almost 3000 nanoseconds additional delay.

So yeah, I very much doubt you can get below microsecond accuracy when measuring sound delay, and more practically, even a few milliseconds of delay when playing a sound should be fine

@Daniel_Gibson : Thank you for that delightful information :slight_smile:

@cmakeshift : Suppose I pressed button and sound starts playing ok . But suddenly again I pressed that button just after 1st press , The sound was just playing but then second sound comes , In that scenario SDL mixing both sounds but Can we stop previously playing sound and start playing new sound . Is that possible in SDL_mixer ?

Please check out https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_34.html#SEC34
You can call it with the argument -1 to halt all sound. Do that before Mix_PlayChannel(…).

Stopping short of RTFM, I urge you, please, look at the documentation of the library you are trying to use. It might be a bit too much to take in a all at once, but if you read the overview you can get a nice picture of what everything does: what channels are, what chunks are, etc etc… Then you’ll know what to look for.

In the end you might realize SDL_mixer is really conceptually well-thought out and does everything you seem to need and more.

@cmakeshift Thank you for that information and sharing that documentation of SDL_mixer.