Search code examples
c++audiodirectx-11directsoundxaudio2

Playing a sound in DirectX Windows 8 app


How do I go about loading an mp3 sound file and playing it from within my Windows 8 app? I cannot find a tutorial that helps me enough to understand what it is I must do?

All I have managed to do so far is:

Sound.h

#pragma once

#include <xaudio2.h>

class Sound
{
    Sound( );
    void Initialize();
    void Play( wchar_t fileName );
private:
    interface IXAudio2* audioEngine;
    interface IXAudio2MasteringVoice* masteringVoice;
    IXAudio2SourceVoice* sourceVoice;
    WAVEFORMATEX* format;
};

Sound.cpp

#include "pch.h"
#include "Sound.h"

Sound::Sound()
{}

void Sound::Initialize()
{
    // Create the XAudio2 Engine
    UINT32 flags = 0;

    XAudio2Create( &audioEngine, flags );

    // Create the mastering voice
    audioEngine->CreateMasteringVoice(
        &masteringVoice,
        XAUDIO2_DEFAULT_CHANNELS,
        48000
        );

    //
    // Create the source voice
    //
    audioEngine->CreateSourceVoice(
        &sourceVoice,
        format,
        0,
        XAUDIO2_DEFAULT_FREQ_RATIO,
        nullptr,
        nullptr,
        nullptr
        );
}

void Sound::Play( wchar_t fileName )
{
    // To do:
    // Load sound file and play it
}

I do not even know if what I have done is right...


Solution

  • SoundEffect.h

     #pragma once 
    
        ref class SoundEffect 
    { 
    internal: 
        SoundEffect(); 
    
        void Initialize( 
            _In_ IXAudio2*              masteringEngine, 
            _In_ WAVEFORMATEX*          sourceFormat, 
            _In_ Platform::Array<byte>^ soundData 
            ); 
    
        void PlaySound(_In_ float volume); 
    
    protected private: 
        bool                    m_audioAvailable; 
        IXAudio2SourceVoice*    m_sourceVoice; 
        Platform::Array<byte>^  m_soundData; 
    };
    

    SoundEffect.cpp

        #include "pch.h" 
    #include "SoundEffect.h" 
    
    SoundEffect::SoundEffect(): 
        m_audioAvailable(false) 
    { 
    } 
    
    //---------------------------------------------------------------------- 
    
    void SoundEffect::Initialize( 
        _In_ IXAudio2 *masteringEngine, 
        _In_ WAVEFORMATEX *sourceFormat, 
        _In_ Platform::Array<byte>^ soundData) 
    { 
        m_soundData = soundData; 
    
        if (masteringEngine == nullptr) 
        { 
            // Audio is not available so just return. 
            m_audioAvailable = false; 
            return; 
        } 
    
        // Create a source voice for this sound effect. 
        DX::ThrowIfFailed( 
            masteringEngine->CreateSourceVoice( 
                &m_sourceVoice, 
                sourceFormat 
                ) 
            ); 
        m_audioAvailable = true; 
    } 
    
    //---------------------------------------------------------------------- 
    
    void SoundEffect::PlaySound(_In_ float volume) 
    { 
        XAUDIO2_BUFFER buffer = {0}; 
    
        if (!m_audioAvailable) 
        { 
            // Audio is not available so just return. 
            return; 
        } 
    
        // Interrupt sound effect if it is currently playing. 
        DX::ThrowIfFailed( 
            m_sourceVoice->Stop() 
            ); 
        DX::ThrowIfFailed( 
            m_sourceVoice->FlushSourceBuffers() 
            ); 
    
        // Queue the memory buffer for playback and start the voice. 
        buffer.AudioBytes = m_soundData->Length; 
        buffer.pAudioData = m_soundData->Data; 
        buffer.Flags = XAUDIO2_END_OF_STREAM; 
    
        DX::ThrowIfFailed( 
            m_sourceVoice->SetVolume(volume) 
            ); 
        DX::ThrowIfFailed( 
            m_sourceVoice->SubmitSourceBuffer(&buffer) 
            ); 
        DX::ThrowIfFailed( 
            m_sourceVoice->Start() 
            ); 
    }