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

How to get duration of wav file? #35

Open
maxpain opened this issue Mar 29, 2023 · 3 comments
Open

How to get duration of wav file? #35

maxpain opened this issue Mar 29, 2023 · 3 comments

Comments

@maxpain
Copy link

maxpain commented Mar 29, 2023

No description provided.

@wxfred
Copy link

wxfred commented Aug 10, 2023

Try this

wav.fromBuffer(buffer)
const { data, fmt } = wav
const duration = data.chunkSize / fmt.numChannels / fmt.sampleRate / (fmt.bitsPerSample / 8)

@banagale
Copy link

banagale commented Oct 13, 2023

I wasn't able to get the above working for me.

But I was able to use @wxfred 's suggestion to get it working using the following:

    // Where wav is a correctly created WaveFile:
    const duration: number = wav.chunkSize / wav.fmt.numChannels / wav.fmt.sampleRate / (wav.fmt.bitsPerSample / 8)
    // Some debug to validate values are coming back as expected.
    console.log(
        'wav.chunkSize', wav.chunkSize,
        'wav.fmt.numChannels', wav.fmt.numChannels,
        'wav.fmt.sampleRate', wav.fmt.sampleRate,
        'wav.fmt.bitsPerSample', wav.fmt.bitsPerSample
    );

If you save the wave file, on MacOS you can validate the calculated duration of a final output using the built in utility:

afinfo filename.wav | grep duration

I found they were very close, but not exactly the same.

@coraxx
Copy link

coraxx commented Apr 24, 2024

I calculate it in ms like this:

var duration = wav.data.chunkSize * 1000 / wav.fmt.blockAlign / wav.fmt.sampleRate / wav.fmt.numChannels;

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

No branches or pull requests

4 participants