1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
bool SoundOgg::OpenOgg( char* filename )
{
if (m_bFileOpened)
Close();
FILE *f;
f = fopen(filename , "rb");
if (!f) return false;
ov_open(f, &VorbisFile, NULL, 0);
// ok now the tricky part
// the vorbis_info struct keeps the most of the interesting format info
vorbis_info *vorbisInfo = ov_info(&VorbisFile,-1);
// set the wave format
WAVEFORMATEX wfm;
memset(&wfm, 0, sizeof(wfm));
wfm.cbSize = sizeof(wfm);
wfm.nChannels = vorbisInfo->channels;
wfm.wBitsPerSample = 16; // ogg vorbis is always 16 bit
wfm.nSamplesPerSec = vorbisInfo->rate;
wfm.nAvgBytesPerSec = wfm.nSamplesPerSec*wfm.nChannels*2;
wfm.nBlockAlign = 2*wfm.nChannels;
wfm.wFormatTag = 1;
// set up the buffer
DSBUFFERDESC desc;
desc.dwSize = sizeof(desc);
desc.dwFlags = 0;
desc.lpwfxFormat = &wfm;
desc.dwReserved = 0;
desc.dwBufferBytes = BUFSIZE*2;
pDS->CreateSoundBuffer(&desc, &pDSB, NULL );
// fill the buffer
DWORD pos = 0;
int sec = 0;
int ret = 1;
DWORD size = BUFSIZE*2;
char *buf;
pDSB->Lock(0, size, (LPVOID*)&buf, &size, NULL, NULL, DSBLOCK_ENTIREBUFFER);
// now read in the bits
while(ret && pos<size)
{
ret = ov_read(&VorbisFile, buf+pos, size-pos, 0, 2, 1, &sec);
pos += ret;
}
pDSB->Unlock( buf, size, NULL, NULL );
m_nCurSection = 0;
m_nLastSection = 0;
return m_bFileOpened = true;
} |
Partager