c++ - Loading WAV file with XAudio2 -


i'm writing program can load , play wav file, i'm using xaudio2 library, started writing msdn.microsoft.com, , have copied code msdn , still not work. don't know problem. when try play sound createsourcevoice have xaudio2_e_invalid_call error. i'll grateful help. code:

my temporary main:

int _tmain(int argc, _tchar* argv[]) { tchar * strfilename = _text("chimes.wav"); //creating instance od xaufio2 engine ixaudio2* pxaudio2; ixaudio2masteringvoice* pmasteringvoice; ixaudio2sourcevoice* psourcevoice; hresult hr;  if ( failed( hr = xaudio2create( &pxaudio2, 0, xaudio2_default_processor ) ) ){     cout << "xaudio2create failed!\n"; }  waveformatextensible wfx = { 0 }; xaudio2_buffer buffer = { 0 };  //open audio file createfile  handle hfile = createfile(     strfilename,     generic_read,     file_share_read,     null,     open_existing,     0,     null     );  dword dwchunkposition = 0; dword dwchunksize;   if (invalid_handle_value == hfile)     cout << "invalid handle value!\n";     //return hresult_from_win32(getlasterror()); if (invalid_set_file_pointer == setfilepointer(hfile, 0, null, file_begin)) {     cout << "invalid set file pointer!\n";     //return hresult_from_win32(getlasterror()); }  cout << "locating 'riff' , whole data size\n"; findchunk(hfile, fourccriff, dwchunksize, dwchunkposition);  cout << "checking filetype\n"; dword filetype; readchunkdata(hfile, &filetype, sizeof(dword), dwchunkposition);  if (filetype != fourccwave)     cout << "this isn't wave file!\n"; else cout << "wave format!\n";  cout << "locating fmt , filling out waveformatextensible structure\n"; findchunk(hfile, fourccfmt, dwchunksize, dwchunkposition); readchunkdata(hfile, &wfx, dwchunksize, dwchunkposition);  cout << "locating data\n"; findchunk(hfile, fourccdata, dwchunksize, dwchunkposition);  cout << "reading sound\n"; byte * pdatabuffer = new byte[dwchunksize]; readchunkdata(hfile, pdatabuffer, dwchunksize, dwchunkposition); buffer.audiobytes = dwchunksize; buffer.paudiodata = pdatabuffer; buffer.flags = xaudio2_end_of_stream;  //plaing preloaded sound; if (failed(hr = pxaudio2->createsourcevoice(&psourcevoice, (waveformatex*)&wfx)))     cout << "playing sound failed!\n";  system("pause"); return 0; } 

and functions searching chunks in wav file:

hresult findchunk(handle hfile, dword fourcc, dword & dwchunksize, dword & dwchunkdataposition) { hresult hr = s_ok; if (invalid_set_file_pointer == setfilepointer(hfile, 0, null, file_begin))     return hresult_from_win32(getlasterror());  dword dwchunktype; dword dwchunkdatasize; dword dwriffdatasize = 0; dword dwfiletype; dword bytesread = 0; dword dwoffset = 0;  while (hr == s_ok) {     dword dwread;     if (0 == readfile(hfile, &dwchunktype, sizeof(dword), &dwread, null))         hr = hresult_from_win32(getlasterror());      if (0 == readfile(hfile, &dwchunkdatasize, sizeof(dword), &dwread, null))         hr = hresult_from_win32(getlasterror());      switch (dwchunktype)     {     case fourccriff:         dwriffdatasize = dwchunkdatasize;         dwchunkdatasize = 4;         if (0 == readfile(hfile, &dwfiletype, sizeof(dword), &dwread, null))             hr = hresult_from_win32(getlasterror());         break;      default:         if (invalid_set_file_pointer == setfilepointer(hfile, dwchunkdatasize, null, file_current))             return hresult_from_win32(getlasterror());     }      dwoffset += sizeof(dword)* 2;      if (dwchunktype == fourcc)     {         dwchunksize = dwchunkdatasize;         dwchunkdataposition = dwoffset;         return s_ok;     }      dwoffset += dwchunkdatasize;      if (bytesread >= dwriffdatasize) return s_false;  }  return s_ok;  }  hresult readchunkdata(handle hfile, void * buffer, dword buffersize, dword bufferoffset) {     hresult hr = s_ok; if (invalid_set_file_pointer == setfilepointer(hfile, bufferoffset, null, file_begin))     return hresult_from_win32(getlasterror()); dword dwread; if (0 == readfile(hfile, buffer, buffersize, &dwread, null))     hr = hresult_from_win32(getlasterror()); return hr; } 

where instantiate mastering voice (pmasteringvoice)? xaudio2 graph expects source voice -> mastering voice -> xaudio2.

add following after xaudio2create call

ixaudio2masteringvoice* pmasteringvoice = null; if ( failed(hr = pxaudio2->createmasteringvoice( &pmasteringvoice ) ) )     return hr; 

http://msdn.microsoft.com/en-us/library/windows/desktop/ee415779(v=vs.85).aspx


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -