Пример #1
0
void Audio::CreateResources()
{
    do
    {	
        if (FAILED(XAudio2Create(&m_musicEngine)))
        {
            m_engineExperiencedCriticalError = true;
            break;
        }

#if defined(_DEBUG)
        XAUDIO2_DEBUG_CONFIGURATION debugConfig = {0};
        debugConfig.BreakMask = XAUDIO2_LOG_ERRORS;
        debugConfig.TraceMask = XAUDIO2_LOG_ERRORS;
        m_musicEngine->SetDebugConfiguration(&debugConfig);
#endif

        m_musicEngineCallback.Initialize(this);
        m_musicEngine->RegisterForCallbacks(&m_musicEngineCallback);

	    // This sample plays the equivalent of background music, which we tag on the mastering voice as AudioCategory_GameMedia.
	    // In ordinary usage, if we were playing the music track with no effects, we could route it entirely through
	    // Media Foundation. Here we are using XAudio2 to apply a reverb effect to the music, so we use Media Foundation to
	    // decode the data then we feed it through the XAudio2 pipeline as a separate Mastering Voice, so that we can tag it
	    // as Game Media.
        // We default the mastering voice to 2 channels to simplify the reverb logic.
        if(FAILED(m_musicEngine->CreateMasteringVoice(&m_musicMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameMedia)))
        {
            m_engineExperiencedCriticalError = true;
            break;
        }

        // Create a separate engine and mastering voice for sound effects in the sample
	    // Games will use many voices in a complex graph for audio, mixing all effects down to a
	    // single mastering voice.
	    // We are creating an entirely new engine instance and mastering voice in order to tag
	    // our sound effects with the audio category AudioCategory_GameEffects.
        if(FAILED(XAudio2Create(&m_soundEffectEngine)))
        {
            m_engineExperiencedCriticalError = true;
            break;
        }
    
        m_soundEffectEngineCallback.Initialize(this);
        m_soundEffectEngine->RegisterForCallbacks(&m_soundEffectEngineCallback);

        // We default the mastering voice to 2 channels to simplify the reverb logic.
        if(FAILED(m_soundEffectEngine->CreateMasteringVoice(&m_soundEffectMasteringVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, nullptr, nullptr, AudioCategory_GameEffects)))
        {
            m_engineExperiencedCriticalError = true;
            break;
        }
    } while (false);
}
Пример #2
0
Audio::Audio()
{
	HRESULT result;

	result = XAudio2Create(&pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR);
	result = pXAudio2->CreateMasteringVoice(&pMasterVoice);
	pMusicVoice = nullptr;
	ZeroMemory(&musicBuffer, sizeof(musicBuffer));
	musicBuffer.LoopCount = XAUDIO2_LOOP_INFINITE;

	masterVolume = System::GetOptions()->GetMasterVolume();
	musicVolume = System::GetOptions()->GetMusicVolume();
	soundEffectsVolume = System::GetOptions()->GetSoundEffectVolume();

	finished = false;
	for (int i = 0; i < MAX_SOUNDS; i++)
	{
		voices[i].active = false;
		time(&voices[i].lastUsed);
		ZeroMemory(&voices[i].Buffer, sizeof(voices[i].Buffer));
		voices[i].wfx = { 0 };
		voices[i].filename.clear();
	}

	std::thread(&Audio::FindFinishedVoices, this).detach();
}
Пример #3
0
bool XAudioInterface::Init( )
{
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
    //CoInitialize(NULL);

    HRESULT hr;
    UINT32 xAudioCreationFlag = 0;
#ifdef _DEBUG
    //xAudioCreationFlag = XAUDIO2_DEBUG_ENGINE;
#endif

    if ( FAILED(hr = XAudio2Create( &m_XAudio2, xAudioCreationFlag, XAUDIO2_DEFAULT_PROCESSOR ) ))
    {
        return false;
    }

    // If we ever have to look for multiple device, it can be done through the following code:
    // http://msdn.microsoft.com/en-us/library/bb669173(VS.85).aspx
    // for now, we are going to use the default device since Win7 games will not require
    // advance features.

    if ( FAILED(hr = m_XAudio2->CreateMasteringVoice( &m_MasterVoice, XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, 0, NULL ) ) )
    {
        return false;
    }

    return true;
}
Пример #4
0
int SoundDevice::CreateSoundDevice( U_INT speakers )
{
#ifdef _DEBUG
	static const UINT32		flags		= XAUDIO2_DEBUG_ENGINE;
#else
	static const UINT32		flags		= 0;
#endif

	if( FAILED( CoInitializeEx( NULL, COINIT_MULTITHREADED ) ) )
	{
		return -1;
	}

	if( FAILED( XAudio2Create( &xAudio, flags ) ) )
	{
		CoUninitialize();
		return -2;
	}
	if( FAILED( xAudio->CreateMasteringVoice( &masterVoice ) ) )
	{
		CoUninitialize();
		return -3;
	}

	return 0;
}
Пример #5
0
// 初期化
HRESULT IXAudioDevice::Initialize(){
	// エラー処理用
	HRESULT hr;

	// COMの初期化
	CoInitializeEx( NULL, COINIT_MULTITHREADED );

	// IXAudio2の初期化
	UINT32 flags = 0;
	
	#ifdef _DEBUG
		flags |= XAUDIO2_DEBUG_ENGINE;
	#endif

	hr = XAudio2Create( &m_pXAudio2, flags );
	if( FAILED( hr ) ){
		return E_FAIL;
	}

	// IXAudio2MasteringVoiceの初期化
	hr = m_pXAudio2->CreateMasteringVoice( &m_pMasteringVoice );
	if( FAILED( hr ) ){
		return E_FAIL;
	}

	return S_OK;
}
XAudio2SoundDevice::XAudio2SoundDevice() :
    SoundDevice(SOUNDDEVICE_XAUDIO2), Device_(0), MasterVoice_(0)
{
    /* Initialize XAudio2 */
    #ifndef SP_PLATFORM_XBOX
    CoInitializeEx(0, COINIT_MULTITHREADED);
    #endif
    
    /* Create XAudio2 device */
    UINT32 Flags = 0;
    
    #ifdef _DEBUG
    Flags |= XAUDIO2_DEBUG_ENGINE;
    #endif
    
    if (XAudio2Create(&Device_, Flags) != S_OK)
    {
        io::Log::error("Could not create XAudio2 device");
        return;
    }
    
    /* Create mastering voice */
    if (Device_->CreateMasteringVoice(&MasterVoice_) != S_OK)
    {
        io::Log::error("Could not create mastering voice");
        return;
    }
}
Пример #7
0
XAudio2Player::XAudio2Player(void)
{
	
	currentBufferNumber = 0;
	
	CoInitializeEx( NULL, COINIT_MULTITHREADED );
	
	pXAudio2 = NULL;
	
	HRESULT hr;
	if( FAILED(hr = XAudio2Create( &pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) )
		return;
	IXAudio2MasteringVoice* pMasterVoice = NULL;
	if ( FAILED(hr = pXAudio2->CreateMasteringVoice( &pMasterVoice ) ) )
		return;
	
	wf.wFormatTag = WAVE_FORMAT_PCM;
    wf.nChannels = 2;
    wf.nSamplesPerSec = 44100;
    wf.nAvgBytesPerSec = 44100 * 4;
    wf.nBlockAlign = 4;
    wf.wBitsPerSample = 16;
    wf.cbSize = 0;

	if( FAILED(hr = pXAudio2->CreateSourceVoice( &pSourceVoice, &wf, 0, 1.0f, &voiceContext ) ) ) 
		return;
	if ( FAILED(hr = pSourceVoice->Start( 0, 0 ) ) )
		return;
	
	doUpdate = true;
	
	t = std::thread(&XAudio2Player::Play, this);
	
}
Пример #8
0
XAudio2Thread::XAudio2Thread()
	: m_xaudio2_instance(nullptr)
	, m_master_voice(nullptr)
	, m_source_voice(nullptr)
{
	HRESULT hr = S_OK;

	hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
	if (FAILED(hr))
	{
		LOG_ERROR(GENERAL, "XAudio2Thread : CoInitializeEx() failed(0x%08x)", (u32)hr);
		Emu.Pause();
		return;
	}

	hr = XAudio2Create(&m_xaudio2_instance, 0, XAUDIO2_DEFAULT_PROCESSOR);
	if (FAILED(hr))
	{
		LOG_ERROR(GENERAL, "XAudio2Thread : XAudio2Create() failed(0x%08x)", (u32)hr);
		Emu.Pause();
		return;
	}

	hr = m_xaudio2_instance->CreateMasteringVoice(&m_master_voice);
	if (FAILED(hr))
	{
		LOG_ERROR(GENERAL, "XAudio2Thread : CreateMasteringVoice() failed(0x%08x)", (u32)hr);
		m_xaudio2_instance->Release();
		Emu.Pause();
	}
}
Пример #9
0
bool XAudio2_7::Start()
{
	HRESULT hr;

	// callback doesn't seem to run on a specific cpu anyways
	IXAudio2* xaudptr;
	if (FAILED(hr = XAudio2Create(&xaudptr, 0, XAUDIO2_DEFAULT_PROCESSOR)))
	{
		PanicAlertT("XAudio2_7 init failed: %#X", hr);
		Stop();
		return false;
	}
	m_xaudio2 = std::unique_ptr<IXAudio2, Releaser>(xaudptr);

	// XAudio2 master voice
	// XAUDIO2_DEFAULT_CHANNELS instead of 2 for expansion?
	if (FAILED(hr = m_xaudio2->CreateMasteringVoice(&m_mastering_voice, 2, m_mixer->GetSampleRate())))
	{
		PanicAlertT("XAudio2_7 master voice creation failed: %#X", hr);
		Stop();
		return false;
	}

	// Volume
	m_mastering_voice->SetVolume(m_volume);

	m_voice_context = std::unique_ptr<StreamingVoiceContext2_7>
		(new StreamingVoiceContext2_7(m_xaudio2.get(), m_mixer, m_sound_sync_event));

	return true;
}
Пример #10
0
bool CAudioManager::Init()
{
	HRESULT hr;
	
	hr = XAudio2Create(&m_pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR);
	if(FAILED(hr))
	{
		LogErrorHr("Failed to create XAudio2", hr);
		return false;
	}

	hr = m_pXAudio2->CreateMasteringVoice(&m_pDevice);
	if(FAILED(hr))
	{
		LogErrorHr("Failed to create mastering voice", hr);
		return false;
	}

	for(int iTrackIndex = 0; iTrackIndex < g_iAudioTrackCount; iTrackIndex++)
	{
		if(!m_tracks[iTrackIndex].Init(m_pXAudio2, m_pDevice))
		{
			return false;
		}
	}

	m_bInited = true;

	return true;
}
Пример #11
0
XAudio2Player::XAudio2Player(IXAudio2* pxa, int &status)
{
	
	currentBufferNumber = 0;
	
	pXAudio2 = pxa;
	
	HRESULT hr;
	if( FAILED(hr = XAudio2Create( &pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR ) ) )
		status = SA_XAUDIO2_ERROR;
	IXAudio2MasteringVoice* pMasterVoice = NULL;
	if ( FAILED(hr = pXAudio2->CreateMasteringVoice( &pMasterVoice ) ) )
		status = SA_XAUDIO2_ERROR;

	wf.wFormatTag = WAVE_FORMAT_PCM;
    wf.nChannels = 2;
    wf.nSamplesPerSec = 44100;
    wf.nAvgBytesPerSec = 44100 * 4;
    wf.nBlockAlign = 4;
    wf.wBitsPerSample = 16;
    wf.cbSize = 0;

	if( FAILED(hr = pXAudio2->CreateSourceVoice( &pSourceVoice, &wf, 0, 1.0f, &voiceContext ) ) ) 
		status = SA_XAUDIO2_ERROR;
	if ( FAILED(hr = pSourceVoice->Start( 0, 0 ) ) )
		status = SA_XAUDIO2_ERROR;
	
	doUpdate = true;

	t = std::thread(&XAudio2Player::Play, this);
	
}
Пример #12
0
	void EngineService::AcceptVisitor( JobFiber& /*fiber*/, const ConfigurationBroadcastVisitor ) {
		enum : UINT32 {
			XAudio2CreateFlags = (ETIsDebugModeEnabled() ? XAUDIO2_DEBUG_ENGINE : 0)
		};

		ComPointer<IXAudio2>	audio;

		_log( MessageSeverity::Message, "Creating XAudio2 instance." ET_UTF8_NEWLINE_LITERAL );

		if( FAILED( XAudio2Create( audio.GetInterfacePointer(), XAudio2CreateFlags, XAUDIO2_DEFAULT_PROCESSOR ) ) ) {
			_log( MessageSeverity::Error, "Unable to create XAudio2 instance!" ET_UTF8_NEWLINE_LITERAL );

			return;
		}

		if( FAILED( audio->RegisterForCallbacks( this ) ) ) {
			_log( MessageSeverity::Error, "Unable to register XAudio2 device callbacks!" ET_UTF8_NEWLINE_LITERAL );
			return;
		}

		if( FAILED( audio->StartEngine() ) ) {
			_log( MessageSeverity::Error, "Unable to start XAudio2 engine!" ET_UTF8_NEWLINE_LITERAL );
			return;
		}

		_log( MessageSeverity::Message, "Created XAudio2 instance." ET_UTF8_NEWLINE_LITERAL );

	//	Commit changes to the service.
		_audio = eastl::move( audio );
	}
Пример #13
0
static int
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
{
    /* XAudio2Create() is a macro that uses COM; we don't load the .dll */
    IXAudio2 *ixa2 = NULL;
    if (FAILED(WIN_CoInitialize())) {
        SDL_SetError("XAudio2: CoInitialize() failed");
        return 0;
    }

    if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
        WIN_CoUninitialize();
        SDL_SetError("XAudio2: XAudio2Create() failed");
        return 0;  /* not available. */
    }
    IXAudio2_Release(ixa2);

    /* Set the function pointers */
    impl->DetectDevices = XAUDIO2_DetectDevices;
    impl->OpenDevice = XAUDIO2_OpenDevice;
    impl->PlayDevice = XAUDIO2_PlayDevice;
    impl->WaitDevice = XAUDIO2_WaitDevice;
    impl->WaitDone = XAUDIO2_WaitDone;
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
    impl->CloseDevice = XAUDIO2_CloseDevice;
    impl->Deinitialize = XAUDIO2_Deinitialize;

    return 1;   /* this audio target is available. */
}
Пример #14
0
static void
XAUDIO2_DetectDevices(void)
{
    IXAudio2 *ixa2 = NULL;
    UINT32 devcount = 0;
    UINT32 i = 0;

    if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
        SDL_SetError("XAudio2: XAudio2Create() failed at detection.");
        return;
    } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
        IXAudio2_Release(ixa2);
        return;
    }

    for (i = 0; i < devcount; i++) {
        XAUDIO2_DEVICE_DETAILS details;
        if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
            char *str = WIN_StringToUTF8(details.DisplayName);
            if (str != NULL) {
                SDL_AddAudioDevice(SDL_FALSE, str, (void *) ((size_t) i+1));
                SDL_free(str);  /* SDL_AddAudioDevice made a copy of the string. */
            }
        }
    }

    IXAudio2_Release(ixa2);
}
Пример #15
0
/* Constructor
 */
XAudioSoundSystem::XAudioSoundSystem(void)
{
    HRESULT hr;
    mXAudio2 = NULL;	
	mMasteringVoice = NULL;

    UINT32 flags = 0;

	//
    // Initialize XAudio2
    //
    CoInitializeEx( NULL, COINIT_MULTITHREADED );

	if( FAILED( hr = XAudio2Create( &mXAudio2, flags ) ) )
    {
        wprintf( L"Failed to init XAudio2 engine: %#X\n", hr );
		return;
    }	

    //
    // Create a mastering voice
    //
    if( FAILED( hr = mXAudio2->CreateMasteringVoice( &mMasteringVoice ) ) )
    {
        wprintf( L"Failed creating mastering voice: %#X\n", hr );
        SAFE_RELEASE( mXAudio2 );
		return;
    }
}
Пример #16
0
static void
XAUDIO2_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
{
    IXAudio2 *ixa2 = NULL;
    UINT32 devcount = 0;
    UINT32 i = 0;
    void *ptr = NULL;

    if (iscapture) {
        SDL_SetError("XAudio2: capture devices unsupported.");
        return;
    } else if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
        SDL_SetError("XAudio2: XAudio2Create() failed.");
        return;
    } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) {
        SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed.");
        IXAudio2_Release(ixa2);
        return;
    }

    for (i = 0; i < devcount; i++) {
        XAUDIO2_DEVICE_DETAILS details;
        if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) {
            char *str = utf16_to_utf8(details.DisplayName);
            if (str != NULL) {
                addfn(str);
                SDL_free(str);  /* addfn() made a copy of the string. */
            }
        }
    }

    IXAudio2_Release(ixa2);
}
Пример #17
0
	//----------------------------------------------------------------------------------------------------
	bool EEMusic::InitializeMusic()
	{
		if (!s_isMusicInitialized)
		{
			av_register_all();

			// CoInitializeEx(NULL, COINIT_MULTITHREADED);
			if (FAILED(XAudio2Create(&s_XAudio2, 0)))
			{
				MessageBoxW(NULL, L"Create XAudio2 failed!", L"ERROR", MB_OK);
				// CoUninitialize();
				return false;
			}
			if (FAILED(s_XAudio2->CreateMasteringVoice(&s_masteringVoice)))
			{
				MessageBoxW(NULL, L"Create mastering voice failed!", L"ERROR", MB_OK);
				s_XAudio2->Release();
				s_XAudio2 = NULL;
				// CoUninitialize();
				return false;
			}

			s_isMusicInitialized = true;
		}

		return true;
	}
Пример #18
0
static int
XAUDIO2_Init(SDL_AudioDriverImpl * impl)
{
#ifndef SDL_XAUDIO2_HAS_SDK
    SDL_SetError("XAudio2: SDL was built without XAudio2 support (old DirectX SDK).");
    return 0;  /* no XAudio2 support, ever. Update your SDK! */
#else
    /* XAudio2Create() is a macro that uses COM; we don't load the .dll */
    IXAudio2 *ixa2 = NULL;
    if (FAILED(WIN_CoInitialize())) {
        SDL_SetError("XAudio2: CoInitialize() failed");
        return 0;
    }

    if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) {
        WIN_CoUninitialize();
        SDL_SetError("XAudio2: XAudio2Create() failed");
        return 0;  /* not available. */
    }
    IXAudio2_Release(ixa2);

    /* Set the function pointers */
    impl->DetectDevices = XAUDIO2_DetectDevices;
    impl->OpenDevice = XAUDIO2_OpenDevice;
    impl->PlayDevice = XAUDIO2_PlayDevice;
    impl->WaitDevice = XAUDIO2_WaitDevice;
    impl->WaitDone = XAUDIO2_WaitDone;
    impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf;
    impl->CloseDevice = XAUDIO2_CloseDevice;
    impl->Deinitialize = XAUDIO2_Deinitialize;

    return 1;   /* this audio target is available. */
#endif
}
Пример #19
0
static xaudio2_t *xaudio2_new(unsigned samplerate, unsigned channels,
      size_t size, unsigned device)
{
   xaudio2_t *handle = NULL;
   WAVEFORMATEX wfx  = {0};

#if defined(__cplusplus) && !defined(CINTERFACE)
   handle = new xaudio2;
#else
   handle = (xaudio2_t*)calloc(1, sizeof(*handle));
#endif

   if (!handle)
      goto error;

#if !defined(__cplusplus) || defined(CINTERFACE)
   handle->lpVtbl = &voice_vtable;
#endif

   if (FAILED(XAudio2Create(&handle->pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR)))
      goto error;

#if (_WIN32_WINNT >= 0x0602 /*_WIN32_WINNT_WIN8*/)
   if (FAILED(IXAudio2_CreateMasteringVoice(handle->pXAudio2, &handle->pMasterVoice, channels, samplerate, 0, (LPCWSTR)(uintptr_t)device, NULL, AudioCategory_GameEffects)))
      goto error;
#else
   if (FAILED(IXAudio2_CreateMasteringVoice(handle->pXAudio2, &handle->pMasterVoice, channels, samplerate, 0, device, NULL)))
      goto error;
#endif

   xaudio2_set_wavefmt(&wfx, channels, samplerate);

   if (FAILED(IXAudio2_CreateSourceVoice(handle->pXAudio2,
               &handle->pSourceVoice, &wfx,
               XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO,
               (IXAudio2VoiceCallback*)handle, 0, 0)))
      goto error;

   handle->hEvent  = CreateEvent(0, FALSE, FALSE, 0);
   if (!handle->hEvent)
      goto error;

   handle->bufsize = size / MAX_BUFFERS;
   handle->buf     = (uint8_t*)calloc(1, handle->bufsize * MAX_BUFFERS);
   if (!handle->buf)
      goto error;

   if (FAILED(IXAudio2SourceVoice_Start(handle->pSourceVoice, 0,
               XAUDIO2_COMMIT_NOW)))
      goto error;

   return handle;

error:
   xaudio2_free(handle);
   return NULL;
}
Пример #20
0
BOOL XAudio2SoundDriverLegacy::Setup()
{
	if (dllInitialized == true) return true;
	dllInitialized = true;
	bufferLength[0] = bufferLength[1] = bufferLength[2] = bufferLength[3] = bufferLength[4] = bufferLength[5] = 0;
	bufferLength[6] = bufferLength[7] = bufferLength[8] = bufferLength[9] = 0;
	audioIsPlaying = false;
	writeBuffer = 0;
	readBuffer = 0;
	filledBuffers = 0;
	bufferBytes = 0;
	lastLength = 1;

	cacheSize = 0;
	interrupts = 0;

	hMutex = CreateMutex(NULL, FALSE, NULL);
	if (FAILED(XAudio2Create(&g_engine)))
	{
		CoUninitialize();
		return -1;
	}

	if (FAILED(g_engine->CreateMasteringVoice(&g_master)))
	{
		g_engine->Release();
		CoUninitialize();
		return -2;
	}
	canPlay = true;

	// Load Wave File

	WAVEFORMATEX wfm;

	memset(&wfm, 0, sizeof(WAVEFORMATEX));

	wfm.wFormatTag = WAVE_FORMAT_PCM;
	wfm.nChannels = 2;
	wfm.nSamplesPerSec = 44100;
	wfm.wBitsPerSample = 16; // TODO: Allow 8bit audio...
	wfm.nBlockAlign = wfm.wBitsPerSample / 8 * wfm.nChannels;
	wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nBlockAlign;


	if (FAILED(g_engine->CreateSourceVoice(&g_source, &wfm, 0, XAUDIO2_DEFAULT_FREQ_RATIO, &voiceCallback, NULL, NULL)))
	{
		g_engine->Release();
		CoUninitialize();
		return -3;
	}

	g_source->Start();
	SetVolume(Configuration::getVolume());

	return FALSE;
}
Пример #21
0
bool Player::Create()
{
	if (FAILED(XAudio2Create(&AudioDriver, 0, XAUDIO2_DEFAULT_PROCESSOR)))
		return false;

	if (FAILED(AudioDriver->CreateMasteringVoice(&AudioMVoice)))
		return false;

	return true;
}
Пример #22
0
	void Audio::Initialize(bool music, bool fx)
	{
		m = music;
		f = fx;
		HRESULT hr = XAudio2Create(&s_pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR);
		assert(SUCCEEDED(hr));

		hr = s_pXAudio2->CreateMasteringVoice(&s_pMasterVoice);
		assert(SUCCEEDED(hr));
	}
Пример #23
0
	virtual const char* open( void * hwnd, unsigned sample_rate, unsigned nch, unsigned max_samples_per_frame, unsigned num_frames )
	{
		this->hwnd = hwnd;
		this->sample_rate = sample_rate;
		this->nch = nch;
		this->max_samples_per_frame = max_samples_per_frame;
		this->num_frames = num_frames;

#ifdef HAVE_KS_HEADERS
		WAVEFORMATEXTENSIBLE wfx;
		wfx.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
		wfx.Format.nChannels = nch; //1;
		wfx.Format.nSamplesPerSec = sample_rate;
		wfx.Format.nBlockAlign = 2 * nch; //2;
		wfx.Format.nAvgBytesPerSec = wfx.Format.nSamplesPerSec * wfx.Format.nBlockAlign;
		wfx.Format.wBitsPerSample = 16;
		wfx.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
		wfx.Samples.wValidBitsPerSample = 16;
		wfx.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
		wfx.dwChannelMask = nch == 2 ? KSAUDIO_SPEAKER_STEREO : KSAUDIO_SPEAKER_MONO;
#else
		WAVEFORMATEX wfx;
		wfx.wFormatTag = WAVE_FORMAT_PCM;
		wfx.nChannels = nch; //1;
		wfx.nSamplesPerSec = sample_rate;
		wfx.nBlockAlign = 2 * nch; //2;
		wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
		wfx.wBitsPerSample = 16;
		wfx.cbSize = 0;
#endif
        HRESULT hr = XAudio2Create( &xaud, 0 );
		if (FAILED(hr)) return "Creating XAudio2 interface";
		hr = xaud->CreateMasteringVoice(
			&mVoice,
			nch,
			sample_rate,
			0,
			NULL,
			NULL );
		if (FAILED(hr)) return "Creating XAudio2 mastering voice";
		hr = xaud->CreateSourceVoice( &sVoice, &wfx, 0, 4.0f, &notify );
		if (FAILED(hr)) return "Creating XAudio2 source voice";
		hr = sVoice->Start( 0 );
		if (FAILED(hr)) return "Starting XAudio2 voice";
		hr = sVoice->SetFrequencyRatio((float)1.0f);
		if (FAILED(hr)) return "Setting XAudio2 voice frequency ratio";
		buffered_count = 0;
		buffer_read_cursor = 0;
		buffer_write_cursor = 0;
		samples_played = 0;
		sample_buffer = new int16_t[ max_samples_per_frame * num_frames ];
		samples_in_buffer = new UINT64[ num_frames ];
		memset( samples_in_buffer, 0, sizeof( UINT64 ) * num_frames );
		return NULL;
	}
Пример #24
0
Error AudioDriverXAudio2::init() {

	active = false;
	thread_exited = false;
	exit_thread = false;
	pcm_open = false;
	samples_in = NULL;

	mix_rate = 48000;
	// FIXME: speaker_mode seems unused in the Xaudio2 driver so far
	speaker_mode = SPEAKER_MODE_STEREO;
	channels = 2;

	int latency = GLOBAL_DEF("audio/output_latency", 25);
	buffer_size = closest_power_of_2(latency * mix_rate / 1000);

	samples_in = memnew_arr(int32_t, buffer_size * channels);
	for (int i = 0; i < AUDIO_BUFFERS; i++) {
		samples_out[i] = memnew_arr(int16_t, buffer_size * channels);
		xaudio_buffer[i].AudioBytes = buffer_size * channels * sizeof(int16_t);
		xaudio_buffer[i].pAudioData = (const BYTE *)(samples_out[i]);
		xaudio_buffer[i].Flags = 0;
	}

	HRESULT hr;
	hr = XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
	if (hr != S_OK) {
		ERR_EXPLAIN("Error creating XAudio2 engine.");
		ERR_FAIL_V(ERR_UNAVAILABLE);
	}
	hr = xaudio->CreateMasteringVoice(&mastering_voice);
	if (hr != S_OK) {
		ERR_EXPLAIN("Error creating XAudio2 mastering voice.");
		ERR_FAIL_V(ERR_UNAVAILABLE);
	}

	wave_format.nChannels = channels;
	wave_format.cbSize = 0;
	wave_format.nSamplesPerSec = mix_rate;
	wave_format.wFormatTag = WAVE_FORMAT_PCM;
	wave_format.wBitsPerSample = 16;
	wave_format.nBlockAlign = channels * wave_format.wBitsPerSample >> 3;
	wave_format.nAvgBytesPerSec = mix_rate * wave_format.nBlockAlign;

	hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, &voice_callback);
	if (hr != S_OK) {
		ERR_EXPLAIN("Error creating XAudio2 source voice. " + itos(hr));
		ERR_FAIL_V(ERR_UNAVAILABLE);
	}

	mutex = Mutex::create();
	thread = Thread::create(AudioDriverXAudio2::thread_func, this);

	return OK;
};
void Sound_Engine::Internal::Init(){
	pXAudio2=0;
	pMasteringVoice=0;
	pSubmixVoice=0;
	pReverbEffect=0;
	nFrameToApply3DAudio=0;
	dwChannelMask=0;
	nChannels=0;
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	HR(XAudio2Create( &pXAudio2, 0 )) ;
	HR(pXAudio2->CreateMasteringVoice( &pMasteringVoice ) );

	// Check device details to make sure it's within our sample supported parameters
	XAUDIO2_DEVICE_DETAILS details;
	HR(pXAudio2->GetDeviceDetails( 0, &details ) );
	if( details.OutputFormat.Format.nChannels > OUTPUTCHANNELS ){
		RELEASECOM( pXAudio2 );
		assert(true);
	}
	dwChannelMask = details.OutputFormat.dwChannelMask;
	nChannels = details.OutputFormat.Format.nChannels;

	HR(XAudio2CreateReverb( &pReverbEffect, 0 ) ); // Create reverb effect


	// Create a submix voice
	// Performance tip: you need not run global FX with the sample number
	// of channels as the final mix.  For example, this sample runs
	// the reverb in mono mode, thus reducing CPU overhead.
	XAUDIO2_EFFECT_DESCRIPTOR effects[] = { { pReverbEffect, TRUE, 1 } };
	XAUDIO2_EFFECT_CHAIN effectChain = { 1, effects };
	HR(pXAudio2->CreateSubmixVoice( &pSubmixVoice, 1,details.OutputFormat.Format.nSamplesPerSec, 0, 0, NULL, &effectChain ) );

	// Set default FX params
	XAUDIO2FX_REVERB_PARAMETERS native;
	ReverbConvertI3DL2ToNative( &g_PRESET_PARAMS[0], &native );
	pSubmixVoice->SetEffectParameters( 0, &native, sizeof( native ) );

	//
	// Initialize X3DAudio
	//  Speaker geometry configuration on the final mix, specifies assignment of channels
	//  to speaker positions, defined as per WAVEFORMATEXTENSIBLE.dwChannelMask

	X3DAudioInitialize( details.OutputFormat.dwChannelMask, X3DAUDIO_SPEED_OF_SOUND, x3DInstance );
	CurrentListenerPos = D3DXVECTOR3( 0, 0, 0 );

	listener.Position = CurrentListenerPos;
	listener.OrientFront = D3DXVECTOR3( 0, 0, 1 );
	listener.OrientTop = D3DXVECTOR3( 0, 1, 0 );
	listener.pCone = (X3DAUDIO_CONE*)&Listener_DirectionalCone;

	dspSettings.SrcChannelCount = INPUTCHANNELS;
	dspSettings.DstChannelCount = nChannels;
	dspSettings.pMatrixCoefficients = matrixCoefficients;
}
Пример #26
0
void InitXAudio(Game* thegame)
{
	CoInitializeEx(NULL, COINIT_MULTITHREADED);
	HRESULT hr = XAudio2Create(&XAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR);
	if(FAILED(hr))
	    exit(EXIT_FAILURE);

	hr = XAudio2->CreateMasteringVoice(&MasterVoice);
	if(FAILED(hr))
		exit(EXIT_FAILURE);
}
Пример #27
0
void DeviceAudioDX11::restartAudio (void)
{
#if DT2_MULTITHREADED_AUDIO
    AutoSpinLockRecursive lock(&_lock);
#endif

    if (!_x_audio_2) {
        // Initialize Audio system
        HRESULT hr = XAudio2Create( &_x_audio_2, 0, XAUDIO2_DEFAULT_PROCESSOR );

        Assert(SUCCEEDED(hr));

        // Initialize Mastering Voice
        hr = _x_audio_2->CreateMasteringVoice( &_x_master_voice );

        Assert(SUCCEEDED(hr));

        // Initialize 3D audio
        XAUDIO2_VOICE_DETAILS voice_details;
        _x_master_voice->GetVoiceDetails(&voice_details);

        // Initialize 3D audio subsystem
        X3DAudioInitialize( voice_details.InputChannels, X3DAUDIO_SPEED_OF_SOUND, _x3_instance );

        // Initialize listener
        ::memset(&_x3_listener,0,sizeof(X3DAUDIO_LISTENER));

        // Initialize DSP parameters
        ::memset(&_x3_dsp,0,sizeof(X3DAUDIO_DSP_SETTINGS));

        _x3_dsp_matrix.resize(voice_details.InputChannels * NUM_CHANNELS);

        _x3_dsp_delays.resize(voice_details.InputChannels);

        _x3_dsp.pMatrixCoefficients = &_x3_dsp_matrix[0];
        _x3_dsp.pDelayTimes = &_x3_dsp_delays[0];
        _x3_dsp.DstChannelCount = voice_details.InputChannels;
        _x3_dsp.SrcChannelCount = 0; // Changed on the fly when calculating


        // Initialize rest of this class
        setNumberOfChannels(32);
    
        // Get all channels playing again
        for (DTuint c = 0; c < min2(_channels.size(),_save_channels.size()); ++c) {                
            playOnChannel (_channels[c], _save_channels[c]._source, _save_channels[c]._sound_loader);
            RELEASE(_save_channels[c]._source);
            RELEASE(_save_channels[c]._sound_loader);
        }
        
        _save_channels.clear();
    }
}
Пример #28
0
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void InitSound()
{
	HRESULT hr;
	
	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
	hr = XAudio2Create(&g_xaudio2, 0);
	hr = g_xaudio2->CreateMasteringVoice(&g_masteringVoice, 2, 44100);

	g_sound = EffekseerSound::Sound::Create(g_xaudio2, 16, 16);
	
	g_manager->SetSoundPlayer( g_sound->CreateSoundPlayer() );
	g_manager->SetSoundLoader( g_sound->CreateSoundLoader() );
}
Пример #29
0
void GameAudio::initialize()
{
	currentMusicPlaying = ENUM_MUSIC_NONE;	//// initially, current music is none
	
	bool ciFailed = FAILED(CoInitializeEx(0, COINIT_MULTITHREADED));
	UINT32 flags = 0;

	bool xa2cSuccess = SUCCEEDED(XAudio2Create(&xAudio2Engine));

	bool cmvSuccess = SUCCEEDED(xAudio2Engine->CreateMasteringVoice(&masterVoice,
		XAUDIO2_DEFAULT_CHANNELS, XAUDIO2_DEFAULT_SAMPLERATE, 0, 0, 0));
	
	//// -- still not sure if I need only one master voice or multiple(one master per one source voice)
}
Пример #30
0
xaudio2_t *xaudio2_new(unsigned samplerate, unsigned channels,
    size_t size, unsigned device)
{
   xaudio2_t *handle = (xaudio2_t*)calloc(1, sizeof(*handle));
   if (!handle)
      return NULL;

   handle->lpVtbl = &voice_vtable;
   CoInitializeEx(0, COINIT_MULTITHREADED);
   WAVEFORMATEX wfx = {0};

   if (FAILED(XAudio2Create(&handle->pXAudio2)))
      goto error;

   if (FAILED(IXAudio2_CreateMasteringVoice(handle->pXAudio2,
               &handle->pMasterVoice, channels, samplerate, 0, device, NULL)))
      goto error;

   wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
   wfx.nChannels = channels;
   wfx.nSamplesPerSec = samplerate;
   wfx.nBlockAlign = channels * sizeof(float);
   wfx.wBitsPerSample = sizeof(float) * 8;
   wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
   wfx.cbSize = 0;

   if (FAILED(IXAudio2_CreateSourceVoice(handle->pXAudio2,
               &handle->pSourceVoice, &wfx,
               XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO,
               (IXAudio2VoiceCallback*)handle, 0, 0)))
      goto error;

   handle->hEvent = CreateEvent(0, FALSE, FALSE, 0);
   if (!handle->hEvent)
      goto error;

   handle->bufsize = size / MAX_BUFFERS;
   handle->buf = (uint8_t*)calloc(1, handle->bufsize * MAX_BUFFERS);
   if (!handle->buf)
      goto error;

   if (FAILED(IXAudio2SourceVoice_Start(handle->pSourceVoice, 0, XAUDIO2_COMMIT_NOW)))
      goto error;

   return handle;

error:
   xaudio2_free(handle);
   return NULL;
}