Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Determine sound duration.

Author
Message
Hubert BAYRE
6
Years of Service
User Offline
Joined: 21st Oct 2017
Location: France
Posted: 19th Sep 2019 10:59
Hello !
Loadsound... playsound...
Is there a way to determine an .ogg file sound duration (seconds) ?
Many thanks
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 19th Sep 2019 23:09 Edited at: 19th Sep 2019 23:09
if you just need to determine if the sound is playing or not there's a command for that
Quote: "GetSoundInstancePlaying( iID )"

otherwise you can get the duration by first turning the sound into a memblock
Quote: "CreateMemblockFromSound( soundID )"

then work out the duration from the info in the memblock file
Quote: "The duration of the sound can be calculated with the following formula, numFrames/samplesPerSecond"

full info in the help docs
Quote: "Creates a memblock from a loaded sound file. The sound file must have been loaded with LoadSound and is unaffected by this command. The first 2 bytes of the memlbock store the number of channels (1 or 2 supported), the next 2 bytes store the bits per sample (8 or 16 supported), the next 4 bytes store the samples per second, for example 44100. The next 4 bytes are the number of frames in the sound data, the size of the sound data in bytes can be calculated from this with the formula numFrames*(bitsPerSample/8)*channels. In a 2 channel sound a frame contains 2 samples, one for each channel. In a mono sound a frame contains 1 sample. The rest of the memblock is raw sound data, for example in an 8bit stereo sound with 3 frames the raw data would look like this. byte offset 0 = Frame 1, left channel data byte offset 1 = Frame 1, right channel data byte offset 2 = Frame 2, left channel data byte offset 3 = Frame 2, right channel data byte offset 4 = Frame 3, left channel data byte offset 5 = Frame 3, right channel data The duration of the sound can be calculated with the following formula, numFrames/samplesPerSecond. This command does not work with sound instance IDs, only sound file IDs. Returns an ID that can be used to reference this memblock in other commands"
life's one big game
spec= i5 4ghz, 16gb ram, Nvidia 1070ti gpu
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 20th Sep 2019 22:25 Edited at: 20th Sep 2019 22:27
for whatever reason, i assumed you wanted to know duration before loading an ogg... regardless, i googled parsing .ogg files for duration and it's a chore (compared to smallg's memblock suggestion?). since it's VBR and meant to stream, it doesn't store duration/length, etc..



so there's no quick math (my first thought was to quickly retrieve filesize, subtract header length, then divide by bitrate before i understood the file structure better).

i have no insight as to whether it would be quicker to parse the file than convert to memblock as above but since AppGameKit obviously "handles" ogg, maybe there's an easier way to tap into specs like that? (ie, agk feature addition).

programs like audacity can obviously determine duration (only after its fully loaded for editing?) but, for example, "length" for .ogg shows "nothing" for me on windows (while mp3s do since it's in the metadata?).

not much help, here, i know

fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 20th Sep 2019 22:58 Edited at: 20th Sep 2019 23:08
using small's algorithm above
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 20th Sep 2019 23:28
You can use LoadMusicOGG() then you can use GetMusicDuration() to get the length of the audio sample.
Conjured Entertainment
AGK Developer
18
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 21st Sep 2019 03:19

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 21st Sep 2019 05:18 Edited at: 21st Sep 2019 05:21
Quote: "You can use LoadMusicOGG() then you can use GetMusicDuration() "

didn't think about the music commands but GetMusicDurationOGG() would do it properly (where GetMusicDuration is deprecated? and states "seconds" and "variable bit rate files this command will return an approximation. " while a quick test with OGG seems pretty accurate (returns float)).

Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 21st Sep 2019 09:10
Its accurate enough for the files ive played from it and alot easier than trying to figure out a file length.

I still use the old functions to play mp3s as background music in an old version.

Its also possible to use GetMusicPositionOGG() to get the current play position rather than use a timer like conjured is doing above.
Hubert BAYRE
6
Years of Service
User Offline
Joined: 21st Oct 2017
Location: France
Posted: 24th Sep 2019 20:21
Many thanks for your help and your ideas.

Login to post a reply

Server time is: 2024-04-25 15:06:02
Your offset time is: 2024-04-25 15:06:02