HomeNewsFeaturesLicensingDownloadsScreenshotsFAQRoadmap Contact Us
Search:
7 Online

Community

Discussion Topics Recent Postings User Contributions General Articles Example Documentation Credits

Licensed Developer

Programmer's Manual Artists Manual Tutorials and Articles

Programming: Game Code

Game Code Overview Server Side Game Code Client Side Game Code

Programming: System

Alphabetical Function List Renderer File System Collision & Ray Casting Low Level Audio Game Audio The Console Console Variable List Multiplayer Localisation Maths Library Memory Manager Model File Formats Texture Formats

Art: Overviews

Specifications Shaders Particle Systems Lens Flares Cipher console Cipher file types Tutorials Reference

Art: Tools

Shader Designer Particle Designer 3dsmax tools Model Conversion Font Generator

Cipher Engine
Game Development Search Engine
GameDev.net
You are not signed in - [sign in] [register]

Getting the length of a Wav/Ogg in seconds

Overview

Author: philk
Date Submitted: 2004-06-24 06:57:44

Source Code

/*
==========================
s_GetWaveLength

Returns the length of the sound in milliseconds
==========================
*/
int snd_GetLength(const tchar_t* filename)
{
	int		size;
	char	sSampleBuffer[16*1024];
	int		pcmlength = 0;
	int		bits, channels, freq;
	int		handle;
		
	handle = s_OpenWave(filename);
	if (0 == handle)
	{
		return 0;
	}
	
	do
	{
		size = s_ReadWave(handle, sSampleBuffer, sizeof(sSampleBuffer), &bits, &channels, &freq);
		pcmlength += size;
	}while (size);

	// close the file again
	s_CloseWave(handle);

	return pcmlength / (bits / 8) / channels / freq;
}


Description

I just thought I might share some small piece of code I recently added to the sound.c file. It calculates the duration of any sound in seconds.

This is not optimized though, since it was mainly written for calculating the length of Ogg musics. And due to the nature of the Ogg/Vorbis file format one cannot just read the length of the uncompressed pcm data from the header. So one optimization would be for WAVE files to just read the RIFF header and do some small calcs for the ADPCM files. A good place would be sound.c and the function

int s_UncompressedSampleSize(streamingwave_t* wavedata)


Maybe one of you can use it for a dynamic music manager or something like that.

Happy coding
Phil

[Recent Contributions] [Recent Source Code]



Register and Sign In to discuss this article