Skip to content

Commit

Permalink
expand capture options
Browse files Browse the repository at this point in the history
-added channels
-added sample rate
-fixed up windows links to use the options properly
  • Loading branch information
getnamo committed Apr 28, 2017
1 parent e0fa095 commit e4349aa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Source/AudioCapture/Private/AudioCaptureData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
FAudioCaptureOptions::FAudioCaptureOptions()
{
SampleRate = 22050;
BitsPerSample = 16;
Channels = 1;
}
19 changes: 9 additions & 10 deletions Source/AudioCapture/Private/FWindowsAudioCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ using namespace std;
FWindowsAudioCapture::FWindowsAudioCapture()
{
bRunLoopActive = false;
SampleRate = 22050;
//SampleRate = 44100;
}

void FWindowsAudioCapture::StartCapture(TFunction<void(const TArray<uint8>&)> OnAudioData /*= nullptr*/, TFunction<void(const TArray<uint8>&)> OnCaptureFinished /*= nullptr*/)
Expand All @@ -35,18 +33,19 @@ void FWindowsAudioCapture::StartCapture(TFunction<void(const TArray<uint8>&)> On
WAVEFORMATEX pFormat;

pFormat.wFormatTag = WAVE_FORMAT_PCM;
pFormat.nChannels = 1; //mono
pFormat.nSamplesPerSec = SampleRate;
pFormat.nAvgBytesPerSec = SampleRate * 2; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.nBlockAlign = 2; // = n.Channels * wBitsPerSample/8
pFormat.wBitsPerSample = 16; // 16 for high quality, 8 for telephone-grade
pFormat.nChannels = Options.Channels; //typically 1 or 2
pFormat.nSamplesPerSec = Options.SampleRate;
pFormat.wBitsPerSample = Options.BitsPerSample; // 16 for high quality, 8 for telephone-grade
pFormat.nBlockAlign = pFormat.nChannels * pFormat.wBitsPerSample / 8; // = n.Channels * wBitsPerSample/8
pFormat.nAvgBytesPerSec = Options.SampleRate * pFormat.nBlockAlign; // = nSamplesPerSec * n.Channels * wBitsPerSample/8

pFormat.cbSize = 0;

result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);

WAVEHDR hWaveInHdr;
TArray<uint8> AudioBuffer;
AudioBuffer.SetNum(SampleRate / 2); //half a second of mono
AudioBuffer.SetNum(pFormat.nAvgBytesPerSec / 2); //half a second

hWaveInHdr.lpData = (LPSTR)AudioBuffer.GetData();
hWaveInHdr.dwBufferLength = AudioBuffer.Num();
Expand Down Expand Up @@ -103,9 +102,9 @@ void FWindowsAudioCapture::StopCapture()
bRunLoopActive = false;
}

void FWindowsAudioCapture::SetOptions(const FAudioCaptureOptions& Options)
void FWindowsAudioCapture::SetOptions(const FAudioCaptureOptions& InOptions)
{
SampleRate = Options.SampleRate;
Options = InOptions;
}

#include "HideWindowsPlatformTypes.h"
4 changes: 2 additions & 2 deletions Source/AudioCapture/Private/FWindowsAudioCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class FWindowsAudioCapture : public IAudioCaptureInterface

virtual void StartCapture(TFunction<void(const TArray<uint8>&)> OnAudioData = nullptr, TFunction<void(const TArray<uint8>&)> OnCaptureFinished = nullptr) override;
virtual void StopCapture() override;
virtual void SetOptions(const FAudioCaptureOptions& SampleRate) override;
virtual void SetOptions(const FAudioCaptureOptions& InOptions) override;

int32 SampleRate;
FAudioCaptureOptions Options;
private:
FThreadSafeBool bRunLoopActive;
};
2 changes: 1 addition & 1 deletion Source/AudioCapture/Private/IAudioCaptureInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ class IAudioCaptureInterface
/** Stop the audio capture and cleanup */
virtual void StopCapture() {};

virtual void SetOptions(const FAudioCaptureOptions& Options) {};
virtual void SetOptions(const FAudioCaptureOptions& InOptions) {};
};
6 changes: 6 additions & 0 deletions Source/AudioCapture/Public/AudioCaptureData.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ struct AUDIOCAPTURE_API FAudioCaptureOptions

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Audio Capture Options")
int32 SampleRate;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Audio Capture Options")
int32 BitsPerSample;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Audio Capture Options")
int32 Channels;
};

0 comments on commit e4349aa

Please sign in to comment.