Advertisement
Kitomas

part of wip xaudio2 wrapper

Jul 18th, 2023 (edited)
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 KB | Source Code | 0 0
  1. HRESULT _kit_audioX2_CreateVoice(_kit_audioX2_Engine* engine, const WAVEFORMATEX* source_format, unsigned int* index_p){
  2.   HRESULT returnStatus=S_OK; int locked=0, slot=-1;
  3.   _IF_ERROR(engine==NULL,E_INVALIDARG,;);
  4.   _IF_ERROR(source_format==NULL,E_INVALIDARG,;);
  5.   _IF_ERROR(index_p==NULL,E_INVALIDARG,;);
  6.   EnterCriticalSection(&engine->lock); locked=1;
  7.  
  8.   //if no voices have been added, then allocate space for first pointer
  9.   if(!engine->sources_len){
  10.     engine->sources=(IXAudio2SourceVoice**)CoTaskMemRealloc(engine->sources, sizeof(IXAudio2SourceVoice*));
  11.     _IF_ERROR(engine->sources==NULL,E_OUTOFMEMORY,;);
  12.     engine->sources[0]=NULL;
  13.     engine->sources_len=1;
  14.   }
  15.  
  16.   //try to find first free space
  17.   unsigned int sources_len=engine->sources_len;
  18.   for(unsigned int i=0; i<sources_len; ++i){
  19.     if(engine->sources[i]==NULL){
  20.       returnStatus=engine->object->CreateSourceVoice(&engine->sources[i], source_format);
  21.       _IF_GOTO_ERROR(FAILED(returnStatus),;);
  22.       slot=i; break;
  23.     }
  24.   }
  25.  
  26.   //if no free space was found, append new voice to source voice list
  27.   if(slot == -1){
  28.     engine->sources=(IXAudio2SourceVoice**)CoTaskMemRealloc(engine->sources, sources_len*sizeof(IXAudio2SourceVoice*));
  29.     _IF_ERROR(engine->sources==NULL,E_OUTOFMEMORY,;);
  30.     returnStatus=engine->object->CreateSourceVoice(&engine->sources[sources_len], source_format);
  31.     _IF_GOTO_ERROR(FAILED(returnStatus),;);
  32.     slot=engine->sources_len++;
  33.   }
  34.  
  35.   _error_:
  36.   if((engine!=NULL) && locked) LeaveCriticalSection(&engine->lock);
  37.   if(slot != -1) *index_p=slot;
  38.   return returnStatus;
  39. }
  40.  
  41.  
  42. HRESULT _kit_audioX2_DestroyVoice(_kit_audioX2_Engine* engine, unsigned int index){
  43.   HRESULT returnStatus=S_OK; int locked=0;
  44.   _IF_ERROR(engine==NULL,E_INVALIDARG,;);
  45.   EnterCriticalSection(&engine->lock); locked=1;
  46.   _IF_ERROR(index>=engine->sources_len,E_INVALIDARG,;);
  47.   _IF_GOTO_ERROR(engine->sources[index]==NULL,;);
  48.  
  49.  
  50.   engine->sources[index]=NULL;
  51.  
  52.   //reallocate if index was the last element
  53.   unsigned int last_source=engine->sources_len-1;
  54.   if(index==last_source){
  55.     if(last_source!=0){
  56.  
  57.     }
  58.     --engine->sources_len;
  59.   }
  60.  
  61.   _error_:
  62.   if((engine!=NULL) && locked) LeaveCriticalSection(&engine->lock);
  63.   return returnStatus;
  64. }
  65.  
  66.  
  67.  
  68. //--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
  69.  
  70.  
  71. HRESULT _kit_audioX2_New(_kit_audioX2_Engine** engine_p, const WAVEFORMATEX* source_format){
  72.   HRESULT returnStatus=S_OK; ULONG numRefs=1; int critical_section=0;
  73.   _IF_ERROR(engine_p==NULL,E_INVALIDARG,;);
  74.   _IF_ERROR(source_format==NULL,E_INVALIDARG,;);
  75.   //allocate memory for struct
  76.   _kit_audioX2_Engine* engine=(_kit_audioX2_Engine*)CoTaskMemAlloc(sizeof(_kit_audioX2_Engine));
  77.   _IF_ERROR(engine==NULL,E_OUTOFMEMORY,;);
  78.   for(char i=0,*ch_p=(char*)engine; i<sizeof(_kit_audioX2_Engine); ++i) ch_p[i]=0;
  79.  
  80.   InitializeCriticalSection(&engine->lock); critical_section=1;
  81.  
  82.   //create xaudio2 object
  83.   returnStatus=XAudio2Create(&engine->object, 0, XAUDIO2_DEFAULT_PROCESSOR);
  84.   _IF_GOTO_ERROR(FAILED(returnStatus),;);
  85.  
  86.   //create mastering voice
  87.   returnStatus=engine->object->CreateMasteringVoice(&engine->master);
  88.   _IF_GOTO_ERROR(FAILED(returnStatus),;);
  89.  
  90.   //create the first source voice
  91.   unsigned int slot;
  92.   returnStatus=_kit_audioX2_AddVoice(engine, source_format, &slot);
  93.   _IF_GOTO_ERROR(FAILED(returnStatus),;);
  94.  
  95.   _error_:
  96.   if(SUCCEEDED(returnStatus)){
  97.     *engine_p=engine;
  98.   } else {
  99.     if(engine != NULL){
  100.       if(engine->sources != NULL){
  101.         _KIT_COM_CALL_S(engine->sources[slot],DestroyVoice);
  102.         CoTaskMemFree(engine->sources);
  103.       }
  104.       _KIT_COM_CALL_S(engine->master,DestroyVoice);
  105.       _KIT_COM_RELEASE_S(engine->object);
  106.       if(critical_section) DeleteCriticalSection(&engine->lock);
  107.       CoTaskMemFree(engine); engine=NULL;
  108.     }
  109.   }
  110.   return returnStatus;
  111. }
  112.  
  113.  
  114. HRESULT _kit_audioX2_Destroy(_kit_audioX2_Engine** engine_p){
  115.   HRESULT returnStatus=S_OK; ULONG numRefs=1; int locked=0;
  116.   _kit_audioX2_Engine* engine=NULL;
  117.   _IF_ERROR(engine_p==NULL,E_INVALIDARG,;);
  118.   _IF_ERROR((engine=*engine_p)==NULL,E_INVALIDARG,;);
  119.   EnterCriticalSection(&engine->lock); locked=1;
  120.  
  121.   _KIT_COM_CALL_S(engine->object,StopEngine);
  122.  
  123.   unsigned int sources_len=engine->sources_len;
  124.   for(unsigned int i=0; i<sources_len; ++i)
  125.     _KIT_COM_CALL_S(engine->sources[i],DestroyVoice);
  126.  
  127.   _KIT_COM_CALL_S(engine->master,DestroyVoice);
  128.  
  129.   _KIT_COM_RELEASE_S(engine->object);
  130.   LeaveCriticalSection(&engine->lock); locked=0;
  131.   DeleteCriticalSection(&engine->lock);
  132.   CoTaskMemFree(engine); *engine_p=NULL;
  133.  
  134.   _error_:
  135.   if((engine!=NULL) && locked) LeaveCriticalSection(&engine->lock);
  136.   return returnStatus;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement