Skip to content

Commit c62e98f

Browse files
committed
Rawr, capture is working
1 parent 668cacb commit c62e98f

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

AudioCapture/Src/AudioCapture.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace AudioCapture
1717
HANDLE hStartedEvent;
1818
LoopbackCaptureThreadFunctionArguments threadArgs;
1919
WAVEFORMATEX* pwfx;
20+
bool errorCondition = false;
2021

2122
const int bufferSize = 1 * 1024 * 1024; // Must be power of 2
2223
int currentWritePos = 0;
@@ -73,6 +74,7 @@ namespace AudioCapture
7374

7475
int Init(BSTR deviceName)
7576
{
77+
errorCondition = false;
7678
HRESULT hr = S_OK;
7779
hr = CoInitialize(NULL);
7880
if (FAILED(hr)) {
@@ -180,6 +182,11 @@ namespace AudioCapture
180182
return pwfx == nullptr ? 0 : pwfx->nChannels;
181183
}
182184

185+
bool HasError()
186+
{
187+
return errorCondition;
188+
}
189+
183190

184191
DWORD WINAPI LoopbackCaptureThreadFunction(LPVOID pContext) {
185192
LoopbackCaptureThreadFunctionArguments* pArgs =
@@ -389,6 +396,7 @@ namespace AudioCapture
389396
);
390397
if (FAILED(hr)) {
391398
ERR(L"IAudioCaptureClient::GetBuffer failed on pass %u after %u frames: hr = 0x%08x", nPasses, *pnFrames, hr);
399+
errorCondition = true;
392400
return hr;
393401
}
394402

@@ -397,11 +405,13 @@ namespace AudioCapture
397405
}
398406
else if (0 != dwFlags) {
399407
LOG(L"IAudioCaptureClient::GetBuffer set flags to 0x%08x on pass %u after %u frames", dwFlags, nPasses, *pnFrames);
408+
errorCondition = true;
400409
return E_UNEXPECTED;
401410
}
402411

403412
if (0 == nNumFramesToRead) {
404413
ERR(L"IAudioCaptureClient::GetBuffer said to read 0 frames on pass %u after %u frames", nPasses, *pnFrames);
414+
errorCondition = true;
405415
return E_UNEXPECTED;
406416
}
407417

@@ -418,6 +428,7 @@ namespace AudioCapture
418428
hr = pAudioCaptureClient->ReleaseBuffer(nNumFramesToRead);
419429
if (FAILED(hr)) {
420430
ERR(L"IAudioCaptureClient::ReleaseBuffer failed on pass %u after %u frames: hr = 0x%08x", nPasses, *pnFrames, hr);
431+
errorCondition = true;
421432
return hr;
422433
}
423434

AudioCapture/Src/AudioCapture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace AudioCapture
99
__declspec(dllexport) int Init(BSTR deviceName);
1010
__declspec(dllexport) int Shutdown();
1111
__declspec(dllexport) int GetNumChannels();
12+
__declspec(dllexport) bool HasError();
1213
__declspec(dllexport) void ReadData(byte* data, int numBytes);
1314
__declspec(dllexport) BSTR GetSelectedDeviceName();
1415
__declspec(dllexport) BSTR GetDeviceName(int index);

TestProject/Src/AudioLink/AssetBundleManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ internal static class AssetBundleManager
1313

1414
internal static void LoadFromMemoryAsync()
1515
{
16+
if (Material != null)
17+
return;
18+
1619
byte[] bytes;
1720

1821
//var names = typeof(AssetBundleManager).Assembly.GetManifestResourceNames();

TestProject/Src/AudioLink/AudioLink.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ internal class AudioLink
9696
private int _rightChannelTestCounter;
9797
private bool _ignoreRightChannel;
9898

99-
MyAudioRecorder_PlayerwMixer.clsRecord _record;
100-
MyAudioRecorder_PlayerwMixer.clsWinMMBase.WAVEFORMATEX _wavFmt;
99+
private string _device = "";
101100
public AudioLink(GameObject gameObject)
102101
{
103102

@@ -125,14 +124,14 @@ public AudioLink(GameObject gameObject)
125124
parser.Tokenize(readText);
126125

127126
_volume = CameraPluginSettings.ParseFloat(root.GetChildSafe("Volume"));
128-
string device = root.GetChildSafe("Device").mValue;
127+
_device = root.GetChildSafe("Device").mValue;
129128

130129
MyCameraPlugin.Log("AudioLink Startup");
131130
MyCameraPlugin.Log("Loading Asset Bundle from Memory");
132131
AssetBundleManager.LoadFromMemoryAsync();
133132

134133
MyCameraPlugin.Log("Starting Audio Capture");
135-
var result = FriesBSCameraPlugin.AudioCapture.Init(device);
134+
var result = FriesBSCameraPlugin.AudioCapture.Init(_device);
136135
if (result == 0)
137136
MyCameraPlugin.Log("Done");
138137
else
@@ -142,7 +141,7 @@ public AudioLink(GameObject gameObject)
142141
MyCameraPlugin.Log("Device List: " + numDevices);
143142
for (int i = 0; i < numDevices; i++)
144143
{
145-
device = FriesBSCameraPlugin.AudioCapture.GetDeviceName(i);
144+
var device = FriesBSCameraPlugin.AudioCapture.GetDeviceName(i);
146145
MyCameraPlugin.Log(" " + device);
147146
}
148147

@@ -155,21 +154,6 @@ public AudioLink(GameObject gameObject)
155154

156155
MyCameraPlugin.Log("Done");
157156

158-
_wavFmt = new MyAudioRecorder_PlayerwMixer.clsWinMMBase.WAVEFORMATEX();
159-
_wavFmt.wFormatTag = 1;//pcm
160-
_wavFmt.nChannels = 2;//stereo
161-
_wavFmt.nSamplesPerSec = 44100;//44100 samples per sec
162-
_wavFmt.nAvgBytesPerSec = 176400;//2 channels*2 bytes * 44100 samples
163-
_wavFmt.wBitsPerSample = 16;//16 bits per sample
164-
_wavFmt.nBlockAlign = (ushort)(_wavFmt.nChannels * _wavFmt.wBitsPerSample / 8);
165-
_wavFmt.cbSize = (ushort)System.Runtime.InteropServices.Marshal.SizeOf(_wavFmt);
166-
167-
_record = new MyAudioRecorder_PlayerwMixer.clsRecord(_wavFmt);
168-
if (_record.StartRecording(2) == true)
169-
MyCameraPlugin.Log("StartRecording!");
170-
else
171-
MyCameraPlugin.Log("StartRecording failed!");
172-
173157
_customThemeColor0 = Color.red;
174158
_customThemeColor1 = Color.green;
175159
_customThemeColor2 = Color.blue;
@@ -244,7 +228,7 @@ public void Tick()
244228
// Casting and encoding as UInt32 as 2 floats, to prevent aliasing, twice: 5.1ms / 255
245229
// Casting and encoding as UInt32 as 2 floats, to prevent aliasing, once: 3.2ms / 255
246230
// ReSharper disable once InvertIf
247-
if (_record != null)
231+
//if (_record != null)
248232
{
249233
SendAudioOutputData();
250234

@@ -324,6 +308,11 @@ private void FPSUpdate()
324308

325309
private bool GetSamples()
326310
{
311+
if (FriesBSCameraPlugin.AudioCapture.HasError())
312+
{
313+
FriesBSCameraPlugin.AudioCapture.Shutdown();
314+
FriesBSCameraPlugin.AudioCapture.Init(_device);
315+
}
327316
FriesBSCameraPlugin.AudioCapture.ReadData(_rawData, _rawData.Length);
328317

329318
bool rv = false;

TestProject/Src/Camera/CameraData.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public override string ToString()
9090
public static class CameraPluginSettings
9191
{
9292
public static float GlobalBias;
93+
public static float FOV;
9394
public static bool SongSpecific;
9495
public static bool Debug = false;
9596
// public static float BlendSpeed;
@@ -112,6 +113,7 @@ public static void LoadSettings()
112113
// Create a default settings file.
113114
using (var ws = new StreamWriter(@settingLoc))
114115
{
116+
ws.WriteLine("FOV='60.0'");
115117
ws.WriteLine("GlobalBias='0.0'");
116118
ws.WriteLine("MenuCamera={");
117119
ws.WriteLine(" Name='MenuCamera'");
@@ -258,6 +260,7 @@ public static void ParseSettingsFile(string fileName)
258260
parser.Tokenize(readText);
259261

260262
GlobalBias = ParseFloat(root.GetChildSafe("GlobalBias"));
263+
FOV = ParseFloat(root.GetChildSafe("FOV"), 60.0f);
261264
Debug = ParseBool(root.GetChildSafe("Debug"));
262265
// BlendSpeed = ParseFloat(root.GetChildSafe("BlendSpeed"));
263266

@@ -324,15 +327,15 @@ public static bool ParseBool(ReflectionToken token)
324327
return false;
325328
}
326329

327-
public static float ParseFloat(ReflectionToken token)
330+
public static float ParseFloat(ReflectionToken token, float def = 0.0f)
328331
{
329332
float outFloat = 0.0f;
330333
if (float.TryParse(token.mValue, out outFloat))
331334
{
332335
return outFloat;
333336
}
334337

335-
return 0.0f;
338+
return def;
336339
}
337340

338341
public static OrbitalDirection GetOrbitalDirectionFromToken(ReflectionToken token)

TestProject/Src/MyCameraPlugin.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ public void OnActivate(PluginCameraHelper helper)
114114

115115
Log("Startup");
116116

117-
Log("Starting AudioLink");
118-
_audioLink = new AudioLink.Scripts.AudioLink(helper.behaviour.manager.camera.gameObject);
119-
Log("AudioLink Started");
117+
if (_audioLink == null)
118+
{
119+
Log("Starting AudioLink");
120+
_audioLink = new AudioLink.Scripts.AudioLink(helper.behaviour.manager.camera.gameObject);
121+
Log("AudioLink Started");
122+
}
120123

121124
Log("Loading Settings");
122125
CameraPluginSettings.LoadSettings();
@@ -127,7 +130,8 @@ public void OnActivate(PluginCameraHelper helper)
127130

128131
beatSaberStatus = new BeatSaberStatus();
129132

130-
_helper.UpdateFov(60.0f);
133+
Log("FOV: " + CameraPluginSettings.FOV);
134+
_helper.UpdateFov(CameraPluginSettings.FOV);
131135
UpdateCameraChange();
132136

133137
}

0 commit comments

Comments
 (0)