Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play audio in cutscenes #288

Closed
wants to merge 14 commits into from
Closed

Conversation

madebr
Copy link
Collaborator

@madebr madebr commented Feb 15, 2023

  • Update miniaudio to 0.11.11
  • Play sounds when playing the smacker cutscenes
  • Because I noticed a lag between video and audio, I created a mini player at tools/minismackerplay.c using libsmacker, miniaudio and SDL2. This shows the overhead/timing/sleeps/... of dethrace should be improved

@madebr madebr force-pushed the cutscenes-with-audio branch from c84fb24 to de53b06 Compare February 15, 2023 01:01
@madebr madebr force-pushed the cutscenes-with-audio branch from de53b06 to 2289311 Compare February 20, 2023 00:34
Copy link
Owner

@dethrace-labs dethrace-labs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@madebr madebr requested a review from dethrace-labs April 26, 2023 18:13
Copy link
Collaborator Author

@madebr madebr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dethrace-labs
Can you please review and/or take over? Thanks.

Comment on lines 277 to 278
/* FIXME: get rid of bodge term */
if (now < user->smack_last_frame_time + smack->MSPerFrame - 5) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to avoid stuttering.
When sleeping too long, the next audio frame is not ready soon enough.

Comment on lines 233 to 234
int SmackSoundUseDirectSound(void* dd) {
// TODO: do some miniaudio init
// FIXME: is this a more appropriate place to init miniaudio? What is type of argument?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the arguent of SmackSoundUseDirectSound was NULL, I did the audio initialization in SmackOpen.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. The NULL argument is an optional pointer to a DIRECTSOUND instance, (NULL apparently means "create a new one")

https://github.com/TheNitesWhoSay/lawine/blob/master/lawine/misc/smack.c#L34

@@ -43,7 +43,7 @@ typedef struct SmackTag {
unsigned long addr32;

// added by dethrace
void* smk_handle;
void* user;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved all user data to a private struct, that is also allocated.


uint32_t smack_last_frame_time;
extern ma_engine miniaudio_engine;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-uses miniaudio_engine, so the separation between smackw32 and S3 is not perfect.

@dethrace-labs
Copy link
Owner

@dethrace-labs Can you please review and/or take over? Thanks.

Sure, thanks for bringing it up to latest main!

@@ -0,0 +1,290 @@
#define SDL_MAIN_HANDLED
Copy link
Collaborator Author

@madebr madebr Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the long run, it would be nice to convert this tool to using smackw32 + brender.
Perhaps worth a fixme :)

@dethrace-labs
Copy link
Owner

I still experience stuttering with this. I started to experiment by trying to feed the audio 1 frame in front of the video so we always have the next audio page to consume before we run out, but its not quite working yet

@PierreMarieBaty
Copy link

Hello @dethrace-labs

Congratulations for this impressive work. I rewrote your smackw32 files to add sound playback support to videos using the supplied miniaudio backend. I started with @madebr's code, figured out the problem (sort of) with sound cracklings/stuttering, and moved it to smackw32 where I think it belongs.

There were three problems. One of them was that your frame duration measurement was incorrect. The time measurement shall be done exactly once per loop, not twice (begin / end). Because of this little mistake, the video playback speed wasn't exactly on par with the audio consumption rate, leading to frequent sound buffer starvation, causing arbitrary seek/restarts producing audible pops. The second problem, as you guessed, was that more than one audio page needs to be precached in miniaudio's linked list to avoid sound starvation at the end of the first page. The third problem, once the others were solved, appeared in so that even playing the videos with audio without any stuttering and both audio and video playing at their designated speeds, a time shift could be observed between audio and video that turned out to be simply proportional to the video length. It was very noticeable in the intro video (the longest), when the roadsign shortly spins at the end. Around 25 pages of audio needed to be precached (and the sound playback start delayed by this amount) for audio and video to become accurately synced. On the other hand, on shorter videos only 1 or 2 audio pages needed to be precached to achieve good sync. It's as if the player code was supposed to precache a certain amount of audio pages that's directly proportional to the length of the video. By trial and error I figured out that the value (total number of frames / 100) would give the exact number of pages to precache for each video. Don't ask me why it works this way... perhaps it's by design ?

As I have no write access on this repo, here are the files if you want to review them. The only files changed are smackw32.c and smackw32.h. There are a few comments and I tried to adopt your coding style. I hope you find this contribution useful: https://www.pmbaty.com/smackw32.zip

@madebr
Copy link
Collaborator Author

madebr commented Dec 17, 2023

Feel free to create a new PR with your code. I will happily close this PR.

Or you can create a PR on my fork to the cutscenes-with-audio branch that I can merge into this pr.

@dethrace-labs
Copy link
Owner

Thanks @PierreMarieBaty for your analysis and code changes! As @madebr said, are you able to send the changes as a pull request?

@PierreMarieBaty
Copy link

PierreMarieBaty commented Dec 19, 2023 via email

madebr and others added 2 commits December 19, 2023 18:28
Co-Authored-By: PierreMarie Baty <PierreMarieBaty@users.noreply.github.com>
@madebr
Copy link
Collaborator Author

madebr commented Dec 19, 2023

I copied smackw32.c from the zip archive, but I still hear the audio too soon.

@PierreMarieBaty
Copy link

PierreMarieBaty commented Dec 20, 2023 via email

@madebr
Copy link
Collaborator Author

madebr commented Dec 21, 2023

I run a x64 Fedora 38 Linux system.

The video I'm seeing time synchronization issues with is GARAGE1.SMK.
That is the video that plays when starting a new game.
The video features a driver pressing some buttons, giving some synchronization references.

When I compare the video played through dethrace and through minismackerplay, the first one is not synchronized.

@PierreMarieBaty
Copy link

PierreMarieBaty commented Dec 22, 2023 via email

@PierreMarieBaty
Copy link

PierreMarieBaty commented Dec 22, 2023 via email

@dethrace-labs
Copy link
Owner

Thank you! I love it when a bunch of complicated work goes in and the end result is a simple small change :). I'll make a PR with the changes.

@dethrace-labs
Copy link
Owner

@PierreMarieBaty I can't access the zip file - could you check your website, or share the zip somewhere else?

@madebr
Copy link
Collaborator Author

madebr commented Jan 16, 2024

Weird, I can still download it.
This is the zip from #288 (comment):
https://dropmeafile.com/#e6f48d3f01

@PierreMarieBaty
Copy link

PierreMarieBaty commented Jan 17, 2024 via email

@dethrace-labs
Copy link
Owner

Closing this and will reopen shortly

@dethrace-labs dethrace-labs mentioned this pull request Jul 9, 2024
@madebr madebr deleted the cutscenes-with-audio branch July 19, 2024 20:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants