Start recording output and stop recording output?

in your project click add c++ file. select the type called blueprint function library Name it AudioFunctions click create class.

then it will open up visual studio 2017

in there you should see AudioFunctions.h and AudioFunctions.cpp
these 2 functions will load a wav file from a folder/filename and allow it to be played… below is the code used to load/play the wav

inside your AudioFunctions.h delete all code in it and add this code:
before you delete the code make a copy of the line that says the folowing. yours will be different because the name of your game will be there instead of the words EDITME

UCLASS()
class EDITME_API UAudioFunctions : public UBlueprintFunctionLibrary




// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "AudioFunctions.generated.h"

/**
 *
 */
UCLASS()
class EDITME_API UAudioFunctions : public UBlueprintFunctionLibrary  //* EDIT THIS LINE TO MATCH THE ONE FOR YOUR GAME (EDITME_API)
{
    GENERATED_BODY()

    //* Declare blueprint functions here?
        UFUNCTION(BlueprintCallable, Category = "AudioFunctions", meta = (DisplayName = "Get Sound Wave from Wave file", Keywords = "Get Sound Wave from Wave file"))
        static class USoundWave* GetSoundFromWaveFile(const FString& filePath, bool& Success);


    /** Obtain all files in a provided directory, with optional extension filter. All files are returned if Ext is left blank. Returns false if operation could not occur. */
        UFUNCTION(BlueprintPure, Category = "AudioFunctions")
        static bool GetALLFilesInFolder(TArray<FString>& Files, FString RootFolderFullPath, FString Ext);

};



then inside your AudioFunctions.cpp delete all code and replace that with all of this code:




// Fill out your copyright notice in the Description page of Project Settings.
#include "AudioFunctions.h"
#include "Sound/SoundWave.h"
#include "Misc/FileHelper.h"
#include "Kismet/GameplayStatics.h"


class USoundWave* UAudioFunctions::GetSoundFromWaveFile(const FString& filePath, bool& Success)
{
    if (filePath == "") { Success = false; return nullptr; }

    USoundWave* sw = NewObject<USoundWave>(USoundWave::StaticClass());
    if (!sw) { Success = false; return nullptr; }

    TArray < uint8 > rawFile;

    FFileHelper::LoadFileToArray(rawFile, filePath.GetCharArray().GetData());
    FWaveModInfo WaveInfo;

    if (WaveInfo.ReadWaveInfo(rawFile.GetData(), rawFile.Num()))
    {

        // - catching not supported bit depth
        if (*WaveInfo.pBitsPerSample != 16) { Success = false;  return nullptr; }

        sw->InvalidateCompressedData();

        sw->RawData.Lock(LOCK_READ_WRITE);
        void* LockedData = sw->RawData.Realloc(rawFile.Num());
        FMemory::Memcpy(LockedData, rawFile.GetData(), rawFile.Num());
        sw->RawData.Unlock();

        int32 DurationDiv = *WaveInfo.pChannels * *WaveInfo.pBitsPerSample * *WaveInfo.pSamplesPerSec;
        if (DurationDiv)
        {
            sw->Duration = *WaveInfo.pWaveDataSize * 8.0f / DurationDiv;
        }
        else
        {
            sw->Duration = 0.0f;
        }
        sw->SetSampleRate(*WaveInfo.pSamplesPerSec);
        sw->NumChannels = *WaveInfo.pChannels;
        sw->RawPCMDataSize = WaveInfo.SampleDataSize;
        sw->SoundGroup = ESoundGroup::SOUNDGROUP_Default;

    }
    else {
        Success = false;
        return nullptr;
    }

// - Baking PCM Data from file into SoundWave memory
const int32 NumSamples = sw->RawPCMDataSize / sizeof(Audio::FResamplerResults);

sw->RawPCMData = (uint8*)FMemory::Malloc(sw->RawPCMDataSize);
FMemory::Memcpy(sw->RawPCMData, WaveInfo.SampleDataStart, NumSamples * sizeof(Audio::FResamplerResults));

if (!sw) { Success = false; return nullptr; }

Success = true;
return sw;
}
//~~~~~~
// File IO
//~~~~~~
bool UAudioFunctions::GetALLFilesInFolder(TArray<FString>& Files, FString RootFolderFullPath, FString Ext)
{
    if (RootFolderFullPath.Len() < 1) return false;

    FPaths::NormalizeDirectoryName(RootFolderFullPath);

    IFileManager& FileManager = IFileManager::Get();

    if (!Ext.Contains(TEXT("*")))
    {
        if (Ext == "")
        {
            Ext = "*.*";
        }
        else
        {
            Ext = (Ext.Left(1) == ".") ? "*" + Ext : "*." + Ext;
        }
    }

    FString FinalPath = RootFolderFullPath + "/" + Ext;

    FileManager.FindFiles(Files, *FinalPath, true, false);
    return true;
}



Changes in code for 4.24.3 highlighted in green

after you add the code click on your project name ------> over there
and click build/build solution. once it is done
you should have 2 new BP nodes.

GetALLFilesInFolder <---- can be used to load an array with wave files to a STRING
GetSoundFromWaveFile<---- Gets the Usoundwave from the folder/name STRING

Inside a widget(not needed) I added the following nodes to load all files in the folder
into an array on EVENT CONSTRUCT I call the GetFilesInFolder EVENT and put the array in a combo box to display all the wav files to the player
so they can choose what one to play. then on the button PLAY(button_134) i get the projectpath, + {path}Recordings
this is the folder in the same path as my content folder config, intermediate and saved etc. in there i have a folder named Recordings. you can name it whatever you want.
whatever name you name the folder make sure you change the name in the format text to match your folder name. this it the path data. the combo box will get the wav file NAME as a string
so you need to feed the PATH and FILENAME into the format text using the append STRING. then use the GetSoundFromWaveFile to convert the STRING by loading up the actual file from the STRING into a UsoundWav format for it to be hooked up to the play sound at location(if looping needed) use Spawn sound at location (if you don’t need Looping)
Click Compile/saveall etc. Save everything then build for windows 64 and it should be able to load/play the file.

THEN for recording i used this code:

Once you are done and you compile your game into a folder go into the windowsnoeditor folder
you will see an engine folder
an exe of your game and another folder with the name of your game

Open the folder with the same name as your game and Create the folder in there where the files will be loaded from.
mine is a folder named Recordings. then after you record something, when you look in that folder you will see the file
with a name like 01_22_20_01_55.WAV this is the recorded file.

2 Likes