Hii,
I’m trying to play an audio that I recorded previously here’s how I need to do it :
In my Player Blueprint, I added a press v Key event.
If the v key is pressed, MyCaptureComponent(UAudioCaptureComponent) execute its Start() Function.
Then I bind event “OnAudioEnvelopeValue” this gets me a float EnvelopeValue that I stock in a FloatSample TArray
When I release the v key, I execute the Stop() function of MyCaptureComponent
and convert the FloatSamples TArray to an IntSamples TArray by Using the FloatToInt32 function :
TArray<int32> AAudioCaptureActor::FloatToInt32(const TArray<float>& FloatData)
{
TArray<int32> Int32Data;
Int32Data.Init(0, FloatData.Num());
UE_LOG(LogTemp, Warning, TEXT("FloatData.Num() %d"), FloatData.Num());
//multiply the float data by 32767 to convert it to int16
for (int32 i = 0; i < FloatData.Num(); i++)
{
Int32Data[i] = static_cast<int32>(FloatData[i] * 32767);
}
return Int32Data;
}
Then, I convert this IntSamples array to a std::vector and create a soundwave from it :
std::vector<int16> AAudioCaptureActor::Int32ToVector(const TArray<int32>& IntData)
{
std::vector<int16> Int16Data(IntData.Num(), 0);
for (int32 Value : IntData)
{
Int16Data.push_back(static_cast<int16>(Value));
}
return Int16Data;
}
And then when I press F, I execute the ProcessAndPlayBackAudio :
void AAudioCaptureActor::ProcessAndPlaybackAudio(const TArray<int32>& CapturedAudioData)
{
// Process and/or playback the captured audio data
if (CapturedAudioData.IsEmpty())
{
UE_LOG(LogTemp, Error, TEXT("TryingToPlay an empty Buffer"));
// Handle error or empty audio data
return;
}
std::vector<int16> ProcessedAudioData = Int32ToVector(CapturedAudioData);
if (ProcessedAudioData.empty())
{
UE_LOG(LogTemp, Error, TEXT("Failed to convert Captured audio buffer to int16"));
return;
}
else
UE_LOG(LogTemp, Warning, TEXT("Converted Captured audio buffer to int16, size = %d"), ProcessedAudioData.size());
// Process the captured audio data
//std::vector<int16> ProcessedAudioData = ProcessAudioData(ProcessedAudioData);
// Play the processed audio data
USoundWave* SoundWave = NewObject<USoundWave>(GetTransientPackage());
if (SoundWave)
{
Audio::FSoundWavePCMWriter Writer;
Audio::FSampleBuffer TransformedBuffer(ProcessedAudioData.data(), ProcessedAudioData.size(), 2, 32000);
SoundWave = Writer.SynchronouslyWriteSoundWave(TransformedBuffer, nullptr, nullptr);
UGameplayStatics::PlaySound2D(GetWorld(), SoundWave);
UE_LOG(LogTemp, Warning, TEXT("Playing SoundWave"));
}
else
UE_LOG(LogTemp, Error, TEXT("Failed to create USoundWave object"));
}
But the sound emitted is just a very short noise because I think I need more samples.
I found this and this could be the reason of why the Sample Array is so short :
What I understood is that OnAudioEnvelope Value is only called 120 times per seconds (if I have a 120hz screen) instead of my sample rate desired (32000 samples per seconds) how could I change my code to get 32000 captured samples per seconds instead of the 120 I get ?