tags:

views:

50

answers:

3

Hi, I'd like to play a sound and have some way of reliably telling how much of it has thus far been played. I've looked at several sound libraries but they are all horribly underdocumented and only seem to export a "PlaySound, no questions asked" routine.

I.e, I want this:

a = Sound(filename)  
PlaySound(a);  
while true:  
    print a.miliseconds_elapsed, a.length  
    sleep(1)  

C, C++ or Python solutions preferred. Thank you.

A: 

This is most likely going to be both hardware-dependent (sound card etc) and OS-dependent (size of buffers used by OS etc).

Maybe it would help if you said a little more about what you're really trying to achieve and also whether we can make any assumptions about what hardware and OS this will run on ?

One possible solution: assume that the sound starts playing more or less immediately and then use a reasonably accurate timer to determine how much of the sound has played (since it will have a known, fixed sample rate).

Paul R
I'm really looking for a high-level API. It should be independent of OS/Sound card as much as possible. I mean you wouldn't want to ship a product that only runs sound on *some* audio cards, now would you :) I'm thinking of something like SDL_Mixer (or even some obscure function call in SDL_Mixer itself).
x10
Depending on what platforms you want to support, you could use something like Apple's QuickTime, which has this kind of support built in. That would limit you to Mac OS X and Windows though.
Paul R
A: 

I use BASS Audio Library (http://www.un4seen.com/)

BASS is an audio library for use in Windows and Mac OSX software. Its purpose is to provide developers with powerful and efficient sample, stream (MP3, MP2, MP1, OGG, WAV, AIFF, custom generated, and more via add-ons), MOD music (XM, IT, S3M, MOD, MTM, UMX), MO3 music (MP3/OGG compressed MODs), and recording functions. All in a tiny DLL, under 100KB in size.*

A C program using BASS is as simple as

HSTREAM str;
BASS_Init(-1,44100,0,0,NULL);
BASS_Start();
str=BASS_StreamCreateFile(FALSE,filename,0,0,0);
BASS_ChannelPlay(str,FALSE);
while (BASS_ChannelIsActive(str)==BASS_ACTIVE_PLAYING) {
  pos=BASS_ChannelGetPosition(str,BASS_POS_BYTE);
}
BASS_Stop();
BASS_Free();
PA
A: 

Hi, I'm also looking for a nice Audiolibrary, where i can directly write on the Soundcards Buffer. I didn't have time yet to have a look at it myself, but pyAudio looks pretty nice. If you scroll down on the page you see an example similar like yours.

With help of the buffersize, number of channels and sample rate you can easily calculate the time each loop-step lasts and print it out.

eL