Skip to content

Conversation

@mariovisic
Copy link
Contributor

@mariovisic mariovisic commented Jul 3, 2021

Implements #196

The code is based off the example here: https://discourse.libsdl.org/t/time-length-of-sdl-mixer-chunks/12852/2

I couldn't find any code to get the length of Mix_Music objects, so in that
case we create a temporary Mix_Chunk object to determine it's length.

NOTE This code returned the correct values for the test media files but will need more manual testing to ensure that it returns accurate information given the comments in the forum post

The code is based off the example here: https://discourse.libsdl.org/t/time-length-of-sdl-mixer-chunks/12852/2

I couldn't find any code to get the length of Mix_Music objects, so in that
case we create a temporary Mix_Chunk object to determine it's length.
CI does not have the ability to open audio or music tracks, so we can't
test length. These tests do run locally and pass however :)
On CI we get the following warning:
> sound.c:60:34: warning: passing argument 2 of 'Mix_QuerySpec' from incompatible pointer type [-Wincompatible-pointer-types]

This is because we're using a default (32-bit) integer pointer when the
function expects a pointer to a 16-bit integer.
This was a copy and paste error, the code works regardless, but this will fix
the warning.
@mariovisic
Copy link
Contributor Author

I wasn't sure if this would work for variable bitrate tracks, so I did a test, it works great! So I think this is ready to 🚢

Here's what I did to test:

I downloaded the 5MB track here which is 2 minutes 12 seconds: https://file-examples.com/index.php/sample-audio-files/sample-mp3-download/ and checked the length in ruby2d and it returns 133 seconds which is correct (it's rounding up, where as my OS display is rounding down I think, the track is 02:12.21 exactly). So that looks good!

I then converted it to a VBR track by running the command I got from here: https://trac.ffmpeg.org/wiki/Encode/MP3

ffmpeg -i example.mp3 -codec:a libmp3lame -qscale:a 2 vbr_example.mp3

Ruby2d reports 133 for the new VBR track as well, presumably because the track reports its average bit rate and so dividing the file size by it still gives the length of the track :)

Let me know if you can think of anything else to test, but i'm confident that this works great :)

@blacktm blacktm merged commit c12b1c2 into ruby2d:main Jul 12, 2021
@blacktm
Copy link
Member

blacktm commented Jul 12, 2021

Awesome work, @mariovisic! 🔊 Thanks so much for working on this.

@mariovisic mariovisic deleted the audio_length branch July 13, 2021 04:25
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.

2 participants