#include "wav.h" NAMESPACE_UPP SampleFormat WavBitsToSampleFormat(int bits){ switch(bits){ case 8: return SND_UINT8; case 16: return SND_INT16; case 24: return SND_INT24; case 32: return SND_INT32; } return SND_UNKNOWN; } bool IsWavFile(const char* fn){ RiffFile f(fn); if(f.filep()==NULL) return false; return strcmp(f.subType(),"WAVE")==0; } bool PlayWav(const char* fn){ WaveFile f; if(!f.OpenRead(fn)) return false; if(f.IsCompressed()){ // TODO ... LOG("Compressed wave files not supported yet"); return false; } SoundStream s; SoundDevice dev=SoundSys().GetDefaultOutput(); SampleFormat format=WavBitsToSampleFormat(f.GetBitsPerChannel()); s.SetFlags(SND_NOCLIP) .SetFramesPerBuffer(512) .SetSampleRate(f.GetSampleRate()); s.Open(Null,StreamParameters(dev,f.GetNumChannels(),format,dev.LowOutputLatency)); s.Start(); if(s.IsError()){ LOG("Failed to open Sound Stream for file "< buf(len*bps); while(f.ReadRaw(~buf,n*bps)){ s.Write(~buf,n); // write n frames i-=n; Sleep(t); // we can wait a while, before filing the buffer again n=min(s.WriteAvailable(),len); } s.Write(~buf,i); // write the leftovers s.Close(); f.Close(); return true; } END_UPP_NAMESPACE