Funni Audio Added >:]


I added audio today and decided to post this as a tech discussion that's going to be a little different from my normal tech discussions.

First and foremost, before you even think about doing anything audio on Windows, you're going to need to use one of their audio apis. I personally am using WASAPI whoose code looks like this

#define DX12IDPTR(data) __uuidof((data)), (void**)&(data)

IMMDevice* AudioOutputDevice = NULL;
IAudioClient* WasapiAudioClient = NULL;
IAudioRenderClient* WasapiAudioRenderClient = NULL;
DWORD SampleRateInSeconds = 0;
WORD NumberOfAudioChannels = 0;


if(SUCCEEDED(CoInitializeEx(0, COINIT_SPEED_OVER_MEMORY)))

        {

            IMMDeviceEnumerator* Enumerator;

            if(SUCCEEDED(CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, DX12IDPTR(Enumerator))))

            {

                if(SUCCEEDED(Enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &AudioOutputDevice)))

                {

                    if(SUCCEEDED(AudioOutputDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (LPVOID*)&WasapiAudioClient)))

                    {

                        WAVEFORMATEX* DefaultWaveFormat = NULL;

                        WasapiAudioClient->GetMixFormat(&DefaultWaveFormat);

                        if(DefaultWaveFormat)

                        {

                            SampleRateInSeconds = DefaultWaveFormat->nSamplesPerSec;

                            NumberOfAudioChannels = DefaultWaveFormat->nChannels;

                            WAVEFORMATEXTENSIBLE WaveFormat;

                            WaveFormat.Format.cbSize = sizeof(WaveFormat);

                            WaveFormat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;

                            WaveFormat.Format.wBitsPerSample = sizeof(s16)*8;

                            WaveFormat.Format.nChannels = DefaultWaveFormat->nChannels;

                            WaveFormat.Format.nSamplesPerSec = DefaultWaveFormat->nSamplesPerSec; 

                            WaveFormat.Format.nBlockAlign = (WORD)(WaveFormat.Format.nChannels * WaveFormat.Format.wBitsPerSample / 8);

                            WaveFormat.Format.nAvgBytesPerSec = WaveFormat.Format.nSamplesPerSec * WaveFormat.Format.nBlockAlign;

                            WaveFormat.Samples.wValidBitsPerSample = 16;

                            WaveFormat.dwChannelMask = KSAUDIO_SPEAKER_STEREO;

                            WaveFormat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;

                            REFERENCE_TIME BufferDuration = 10000000ULL * SampleRateInSeconds / SampleRateInSeconds;

                            if(SUCCEEDED(WasapiAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST, BufferDuration, 0, &WaveFormat.Format, NULL)))

                            {

                                if(SUCCEEDED(WasapiAudioClient->GetService( __uuidof(IAudioRenderClient),  (void**)&WasapiAudioRenderClient)))

                                {

                                    //Assert(SUCCEEDED(WasapiAudioClient->Start()), "Windows: WASAPI: Failed to start audio rendering in WASAPI!"); //Finalize setup stuff and go!

                                    WasapiAudioClient->Start();

                                }

                            }

                        }

                    }

                }

            }

        }

Basically Assert() just means to check the condition and if zero, crash and print out the error. So in other word, use that as your final error check to see if audio playing actually started. I forgot the exact libs you need to link with to use WASAPI but if you look up any of the called functions above on Microsoft Docs, they'll tell you on the bottom of the page as of 10/22/21.

The annoying thing beyond just having to setup the WASAPI code. I also used Audacity where it would always crash every time I pressed a button on my keyboard unless I deleted the temp data it made in my Windows user Roaming folder. This also appears to be a bug that's been known since march of this year https://forum.audacityteam.org/viewtopic.php?t=116720 so yeee.

After all that though. It all went well. Please let me know what you think of the game as of today's build! Thank you and have an awesome day!!!

Files

Funmi_Zip.7z 2 MB
Oct 22, 2021

Get Funmi

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.