Advertisement
Guest User

UE4 VoiceCapture

a guest
Jun 17th, 2018
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1.  
  2. // Fill out your copyright notice in the Description page of Project Settings.
  3.  
  4. #pragma once
  5.  
  6. #include "GameFramework/Character.h"
  7.  
  8.  
  9. #include "SlateApplication.h"
  10.  
  11. #include "Voice.h"
  12. #include "OnlineSubsystemUtils.h"
  13. #include "Sound/SoundWaveProcedural.h"
  14. #include "MyCharacter.generated.h"
  15.  
  16. UCLASS()
  17. class MYPROJECT_API AMyCharacter : public ACharacter
  18. {
  19.     GENERATED_BODY()
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. protected:
  27.     // Called when the game starts or when spawned
  28.     virtual void BeginPlay() override;
  29.  
  30. public:
  31.  
  32.     AMyCharacter();
  33.  
  34.     // Called every frame
  35.     virtual void Tick(float DeltaTime) override;
  36.  
  37.     // Called to bind functionality to input
  38.     virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
  39.  
  40.     UPROPERTY()
  41.         float VoiceCaptureVolume;
  42.  
  43.     UPROPERTY()
  44.         bool VoiceCaptureTest;
  45.     UPROPERTY()
  46.         bool PlayVoiceCaptureFlag;
  47.  
  48.     UPROPERTY()
  49.         FTimerHandle VoiceCaptureTickTimer;
  50.     UPROPERTY()
  51.         FTimerHandle PlayVoiceCaptureTimer;
  52.  
  53.     TSharedPtr<class IVoiceCapture> VoiceCapture;
  54.  
  55.     UPROPERTY()
  56.         USoundWaveProcedural* VoiceCaptureSoundWaveProcedural;
  57.  
  58.     UPROPERTY(VisibleAnywhere, BluePrintReadOnly)
  59.         UAudioComponent* VoiceCaptureAudioComponent;
  60.  
  61.     UPROPERTY()
  62.         TArray<uint8> VoiceCaptureBuffer;
  63.  
  64.     UFUNCTION()
  65.         void VoiceCaptureTick();
  66.  
  67.     UFUNCTION()
  68.         void PlayVoiceCapture();
  69.    
  70.    
  71. };
  72.  
  73.  
  74. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77.  
  78.  
  79.  
  80.  
  81.  
  82. #include "MyProject.h"
  83. #include "MyCharacter.h"
  84.  
  85.  
  86. // Sets default values
  87. AMyCharacter::AMyCharacter()
  88. {
  89.     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  90.     PrimaryActorTick.bCanEverTick = true;
  91.  
  92.  
  93.     VoiceCapture = FVoiceModule::Get().CreateVoiceCapture();
  94.     VoiceCapture->Start();
  95.  
  96.     VoiceCaptureAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("VoiceCaptureAudioComponent"));
  97.     VoiceCaptureAudioComponent->SetupAttachment(RootComponent);
  98.     VoiceCaptureAudioComponent->bAutoActivate = true;
  99.     VoiceCaptureAudioComponent->bAlwaysPlay = true;
  100.     VoiceCaptureAudioComponent->PitchMultiplier = 0.85f;
  101.     VoiceCaptureAudioComponent->VolumeMultiplier = 3.f;
  102.  
  103.     VoiceCaptureSoundWaveProcedural = NewObject<USoundWaveProcedural>();
  104.     VoiceCaptureSoundWaveProcedural->SampleRate = 22050;
  105.     //VoiceCaptureSoundWaveProcedural->SampleRate = 16000;
  106.     VoiceCaptureSoundWaveProcedural->NumChannels = 1;
  107.     VoiceCaptureSoundWaveProcedural->Duration = INDEFINITELY_LOOPING_DURATION;
  108.     VoiceCaptureSoundWaveProcedural->SoundGroup = SOUNDGROUP_Voice;
  109.     VoiceCaptureSoundWaveProcedural->bLooping = false;
  110.     VoiceCaptureSoundWaveProcedural->bProcedural = true;
  111.     VoiceCaptureSoundWaveProcedural->Pitch = 0.85f;
  112.     VoiceCaptureSoundWaveProcedural->Volume = 5.f;
  113.  
  114.  
  115. }
  116.  
  117. // Called when the game starts or when spawned
  118. void AMyCharacter::BeginPlay()
  119. {
  120.     Super::BeginPlay();
  121.  
  122.     GetWorldTimerManager().SetTimer(PlayVoiceCaptureTimer, this, &AMyCharacter::PlayVoiceCapture, 0.4f, true, 0.f);
  123.    
  124. }
  125.  
  126. // Called every frame
  127. void AMyCharacter::Tick(float DeltaTime)
  128. {
  129.     Super::Tick(DeltaTime);
  130.     VoiceCaptureTick();
  131.  
  132. }
  133.  
  134. // Called to bind functionality to input
  135. void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  136. {
  137.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  138.  
  139. }
  140.  
  141. void AMyCharacter::VoiceCaptureTick()
  142. {
  143.     if (!VoiceCapture.IsValid())
  144.         return;
  145.  
  146.     uint32 VoiceCaptureBytesAvailable = 0;
  147.     EVoiceCaptureState::Type CaptureState = VoiceCapture->GetCaptureState(VoiceCaptureBytesAvailable);
  148.  
  149.     VoiceCaptureBuffer.Reset();
  150.     PlayVoiceCaptureFlag = false;
  151.  
  152.     if (CaptureState == EVoiceCaptureState::Ok && VoiceCaptureBytesAvailable > 0)
  153.     {
  154.         int16_t VoiceCaptureSample;
  155.         uint32 VoiceCaptureReadBytes;
  156.         float VoiceCaptureTotalSquared = 0;
  157.  
  158.         VoiceCaptureBuffer.SetNumUninitialized(VoiceCaptureBytesAvailable);
  159.  
  160.         VoiceCapture->GetVoiceData(VoiceCaptureBuffer.GetData(), VoiceCaptureBytesAvailable, VoiceCaptureReadBytes);
  161.  
  162.         for (uint32 i = 0; i < (VoiceCaptureReadBytes / 2); i++)
  163.         {
  164.             VoiceCaptureSample = (VoiceCaptureBuffer[i * 2 + 1] << 8) | VoiceCaptureBuffer[i * 2];
  165.             VoiceCaptureTotalSquared += ((float)VoiceCaptureSample * (float)VoiceCaptureSample);
  166.         }
  167.  
  168.         float VoiceCaptureMeanSquare = (2 * (VoiceCaptureTotalSquared / VoiceCaptureBuffer.Num()));
  169.         float VoiceCaptureRms = FMath::Sqrt(VoiceCaptureMeanSquare);
  170.         float VoiceCaptureFinalVolume = ((VoiceCaptureRms / 32768.0) * 200.f);
  171.  
  172.         VoiceCaptureVolume = VoiceCaptureFinalVolume;
  173.  
  174.         VoiceCaptureSoundWaveProcedural->QueueAudio(VoiceCaptureBuffer.GetData(), VoiceCaptureReadBytes);
  175.         VoiceCaptureAudioComponent->SetSound(VoiceCaptureSoundWaveProcedural);
  176.  
  177.         PlayVoiceCaptureFlag = true;
  178.     }
  179.  
  180.  
  181.  
  182. }
  183.  
  184. void AMyCharacter::PlayVoiceCapture()
  185. {
  186.  
  187.     if (!PlayVoiceCaptureFlag)
  188.     {
  189.         VoiceCaptureAudioComponent->FadeOut(0.3f, 0.f);
  190.         return;
  191.     }
  192.  
  193.     if (VoiceCaptureAudioComponent->IsPlaying())
  194.         return;
  195.  
  196.     VoiceCaptureAudioComponent->Play();
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement