Skip to content

Commit dbd40cd

Browse files
committed
bass: enable this again after figuring out I was a fucking idiot
1 parent becd2c8 commit dbd40cd

File tree

6 files changed

+101
-300
lines changed

6 files changed

+101
-300
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,7 +3313,10 @@ Restarts the channel.<br>
33133313
Computes the DFT of the sound channel.<br>
33143314
What even is that.<br>
33153315

3316-
#### IGModAudioChannel:EncodeToDisk(string fileName, number bassFlags, function callback, bool async = false)
3316+
#### bool(success), string(errMsg - nil) IGModAudioChannel:EncodeToDisk(string fileName, number bassFlags, function callback, bool async = false)
3317+
callback - `function(success, errMsg) end`<br>
3318+
bassFlags - bitflags, see https://www.un4seen.com/doc/#bassenc/BASS_Encode_Start.html<br>
3319+
33173320
Writes the channel into a file, the encoder internally used depends on the filename.<br>
33183321
Valid encoders are:<br>
33193322
- `.wav` (Requres `BASSENC` plugin)<br>
@@ -3327,11 +3330,6 @@ Valid encoders are:<br>
33273330
> This function requires the `BASSENC` plugin to work at all!<br>
33283331
> You can find all the plugins at https://www.un4seen.com/ drop them into the `bin/` folder besides `libbass.so`<br>
33293332
3330-
> [!WARNING]
3331-
> This function is a fucking gamble, I do not know why BASS decides this, though it behaves sometimes randomly.<br>
3332-
> the current code for it seems to be somewhat stable, though any change like shifting it, adding arguments will cause issues<br>
3333-
> until this is figured out this won't be changed in any way further (This could all point back to BASSENC itself having a bug specific to my usecase)<br>
3334-
33353333
#### IGModAudioChannel:Update(number time)
33363334
Updates the channel for the given time in seconds<br>
33373335
See: https://www.un4seen.com/doc/#bass/BASS_ChannelUpdate.html<br>

source/lua.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ inline LuaUserData* Get_##className##_Data(GarrysMod::Lua::ILuaInterface* LUA, i
713713
if (bError) \
714714
LUA->ThrowError(triedNull_##className.c_str()); \
715715
\
716-
if (!pVar || pVar->GetType() != luaType && pVar->GetType() != luaType2) \
716+
if (!pVar || (pVar->GetType() != luaType && pVar->GetType() != luaType2)) \
717717
{ \
718718
if (bError) \
719719
LUA->ThrowError(invalidType_##className.c_str()); \

source/modules/bass.cpp

Lines changed: 35 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -336,95 +336,73 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_Restart)
336336

337337

338338

339-
class BassEncoderCallback : public IGModEncoderCallback, public GarrysMod::Lua::ILuaThreadedCall
339+
class BassEncoderCallback : public IGModEncoderCallback
340340
{
341341
public:
342-
BassEncoderCallback(int nReference)
342+
BassEncoderCallback(GarrysMod::Lua::ILuaInterface* pLua)
343343
{
344-
m_nCallbackReference = nReference;
344+
m_pLua = pLua;
345+
m_nCallbackReference = Util::ReferenceCreate(pLua, "BassEncoderCallback - callback reference");
345346
}
346347

347348
virtual ~BassEncoderCallback() {
348-
if (m_nCallbackReference != -1)
349+
if (m_pLua && m_nCallbackReference != -1)
349350
{
350351
Msg("BassEncoderCallback deleted while still holding a reference!\n");
352+
Util::ReferenceFree(m_pLua, m_nCallbackReference, "BassEncoderCallback - callback leftover deletion");
351353
m_nCallbackReference = -1;
354+
m_pLua = NULL;
352355
}
353356
};
354357

355358
virtual bool ShouldForceFinish(IGModAudioChannelEncoder* pEncoder, void* nSignalData)
356359
{
357-
//if (m_pLua == nSignalData)
358-
// return true; // Force finish as this is our signal that our interface is shutting down!
360+
if (m_pLua == nSignalData)
361+
return true; // Force finish as this is our signal that our interface is shutting down!
359362

360363
return false;
361364
};
362365

363366
virtual void OnFinish(IGModAudioChannelEncoder* pEncoder, GModEncoderStatus nStatus)
364-
{
365-
m_bIsBassDone = true;
366-
367-
if (m_bForceDelete)
368-
delete this; // We serve no purpose... Lua is gone :NOOOOO:
369-
};
370-
371-
bool IsDone()
372-
{
373-
return m_bIsBassDone;
374-
}
375-
376-
void Done(GarrysMod::Lua::ILuaInterface* LUA)
377367
{
378368
if (m_nCallbackReference == -1)
379369
return;
380370

381-
Util::ReferencePush(LUA, m_nCallbackReference);
382-
if (m_nBassStatus == GModEncoderStatus::FINISHED) {
383-
LUA->PushBool(true);
384-
LUA->PushNil();
371+
Util::ReferencePush(m_pLua, m_nCallbackReference);
372+
if (nStatus == GModEncoderStatus::FINISHED) {
373+
m_pLua->PushBool(true);
374+
m_pLua->PushNil();
385375
} else {
386-
LUA->PushBool(false);
387-
LUA->PushString("Encoder was interrupted by Lua shutdown!");
376+
m_pLua->PushBool(false);
377+
m_pLua->PushString("Encoder was interrupted by Lua shutdown!");
388378
}
389-
LUA->CallFunctionProtected(2, 0, true);
379+
m_pLua->CallFunctionProtected(2, 0, true);
390380

391-
Util::ReferenceFree(LUA, m_nCallbackReference, "BassEncoderCallback - callback deletion OnFinish");
381+
Util::ReferenceFree(m_pLua, m_nCallbackReference, "BassEncoderCallback - callback deletion OnFinish");
392382
m_nCallbackReference = -1;
393-
}
394-
395-
void OnShutdown()
396-
{
397-
m_nCallbackReference = -1;
398-
if (!m_bIsBassDone)
399-
{
400-
// Bass still uses us, so we gotta delay this
401-
m_bForceDelete = true;
402-
return;
403-
}
404-
405-
delete this;
383+
m_pLua = NULL;
406384
}
407385

408386
private:
409-
bool m_bIsBassDone = false;
410-
bool m_bForceDelete = false;
387+
GarrysMod::Lua::ILuaInterface* m_pLua = nullptr;
411388
int m_nCallbackReference = -1;
412-
int m_nBassStatus = GModEncoderStatus::DIED;
413389
};
414390

415-
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT // https://github.com/RaphaelIT7/gmod-holylib/commit/48bc854f48aa26ec6539eabd01b66d87110b4598#diff-82711262b6e5109632243c62ed0ab8cba506cdf021817af4af807da268297bce
416391
LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
417392
{
418393
IGModAudioChannel* channel = Get_IGModAudioChannel(LUA, 1, true);
419394

420395
const char* pFileName = LUA->CheckString(2);
396+
// NOTE: Next time ensure I fucking use CheckNumber and not CheckString to then cast :sob: only took 8+ hours to figure out
421397
unsigned long nFlags = (unsigned long)LUA->CheckNumber(3);
422398
LUA->CheckType(4, GarrysMod::Lua::Type::Function);
399+
bool bAsync = LUA->GetBool(5);
423400

424401
LUA->Push(4);
425-
BassEncoderCallback* pCallback = new BassEncoderCallback(LUA); // We do not manage this pointer! GModAudio does for us
402+
BassEncoderCallback* pCallback = new BassEncoderCallback(LUA);
403+
// We do not manage this pointer! GModAudio does for us
426404

427-
const char* pErrorMsg;
405+
const char* pErrorMsg = NULL;
428406
IGModAudioChannelEncoder* pEncoder = channel->CreateEncoder(pFileName, nFlags, pCallback, &pErrorMsg);
429407
if (pErrorMsg)
430408
{
@@ -433,7 +411,6 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
433411
return 2;
434412
}
435413

436-
bool bAsync = LUA->GetBool(5);
437414
pEncoder->ProcessNow(bAsync);
438415
if (pEncoder->GetLastError(&pErrorMsg))
439416
{
@@ -446,7 +423,6 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
446423
LUA->PushNil();
447424
return 2;
448425
}
449-
#endif
450426

451427
LUA_FUNCTION_STATIC(IGModAudioChannel_Update)
452428
{
@@ -488,49 +464,25 @@ LUA_FUNCTION_STATIC(IGModAudioChannel_DestroyLink)
488464
return 2;
489465
}
490466

491-
LUA_FUNCTION_STATIC(IGModAudioChannel_EncodeToDisk)
467+
LUA_FUNCTION_STATIC(IGModAudioChannel_MakeServer)
492468
{
493469
IGModAudioChannel* channel = Get_IGModAudioChannel(LUA, 1, true);
494470

495-
const char* pFileName = LUA->CheckString(2);
496-
unsigned long nFlags = (unsigned long)LUA->CheckNumber(3);
497-
498-
/*BassEncoderCallback* pCallback = nullptr;
499-
if (LUA->IsType(4, GarrysMod::Lua::Type::Function))
500-
{
501-
LUA->Push(4);
502-
pCallback = new BassEncoderCallback(Util::ReferenceCreate(LUA, "BassEncoderCallback - callback reference"));
503-
}*/
504-
505-
const char* pErrorMsg = channel->EncodeToDisk(pFileName, nFlags/*, pCallback*/);
506-
if (!pErrorMsg)
507-
{ // Success
508-
LUA->PushBool(true);
471+
const char* strPort = LUA->CheckString(2);
472+
unsigned long nBuffer = (unsigned long)LUA->CheckNumber(3);
473+
unsigned long nBurst = (unsigned long)LUA->CheckNumber(4);
474+
unsigned long nFlags = (unsigned long)LUA->CheckNumber(5);
475+
476+
const char* pErrorCode = nullptr;
477+
LUA->PushBool(channel->MakeServer(strPort, nBuffer, nBurst, nFlags, &pErrorCode));
478+
if (pErrorCode) {
479+
LUA->PushString(pErrorCode);
480+
} else {
509481
LUA->PushNil();
510-
return 2;
511482
}
512-
513-
// if (pCallback)
514-
// LUA->AddThreadedCall(pCallback);
515-
516-
LUA->PushBool(false);
517-
LUA->PushString(pErrorMsg);
518483
return 2;
519484
}
520485

521-
LUA_FUNCTION_STATIC(IGModAudioChannel_MakeServer)
522-
{
523-
IGModAudioChannel* channel = Get_IGModAudioChannel(LUA, 1, true);
524-
525-
const char* strPort = LUA->CheckString(2);
526-
unsigned long nBuffer = (unsigned long)LUA->CheckString(3);
527-
unsigned long nBurst = (unsigned long)LUA->CheckString(4);
528-
unsigned long nFlags = (unsigned long)LUA->CheckString(5);
529-
530-
LUA->PushBool(channel->MakeServer(strPort, nBuffer, nBurst, nFlags));
531-
return 1;
532-
}
533-
534486
LUA_FUNCTION_STATIC(bass_PlayFile)
535487
{
536488
const char* filePath = LUA->CheckString(1);
@@ -710,9 +662,7 @@ void CBassModule::LuaShutdown(GarrysMod::Lua::ILuaInterface* pLua)
710662
{
711663
// Finish all callbacks
712664
// We pass pLua so that our BassEncoderCallback can check if its their state and force a finish
713-
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
714665
gGModAudio->FinishAllAsync(pLua);
715-
#endif
716666

717667
Util::NukeTable(pLua, "bass");
718668
}

source/sourcesdk/IGmod_Audio.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ enum GModEncoderStatus {
2323
DIED = 3,
2424
};
2525

26-
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
2726
// HolyLib specific
2827
// NOTE: Always call GetLastError after any function call to check for errors!
2928
class IGModAudioChannelEncoder
@@ -40,7 +39,6 @@ class IGModAudioChannelEncoder
4039
// Wasn't exposed since CreateEncoder already calls it so it has no real use
4140
// virtual void InitEncoder(unsigned long nEncoderFlags) = 0;
4241
};
43-
#endif
4442

4543
class IGModAudioChannelEncoder;
4644
class IGModEncoderCallback // Callback struct
@@ -95,14 +93,11 @@ class IGModAudioChannel
9593
// Uses the "DATA" path for writes! Returns NULL on success, else the error message
9694
// Does NOT require the channel to be a decoder channel!
9795
// Call IGModAudioChannelEncoder->GetLastError and check if its even valid! (Else it will be invalidated/freed on the next tick)
98-
virtual const char* EncodeToDisk( const char* pFileName, unsigned long nFlags ) = 0;
99-
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
10096
virtual IGModAudioChannelEncoder* CreateEncoder( const char* pFileName, unsigned long nFlags, IGModEncoderCallback* pCallback, const char** pErrorOut ) = 0;
101-
#endif
10297
virtual void Update( unsigned long length ) = 0; // Updates the playback buffer
10398
virtual bool CreateLink( IGModAudioChannel* pChannel, const char** pErrorOut ) = 0;
10499
virtual bool DestroyLink( IGModAudioChannel* pChannel, const char** pErrorOut ) = 0;
105-
virtual bool MakeServer(const char* port, unsigned long buffer, unsigned long burst, unsigned long flags) = 0;
100+
virtual bool MakeServer( const char* port, unsigned long buffer, unsigned long burst, unsigned long flags, const char** pErrorOut ) = 0;
106101
};
107102

108103
class IAudioStreamEvent;
@@ -144,9 +139,7 @@ abstract_class IGMod_Audio
144139
// HolyLib specific ones
145140
virtual unsigned long GetVersion() = 0; // Returns bass version
146141
virtual bool LoadPlugin(const char* pluginName, const char** pErrorOut) = 0;
147-
#if ENABLE_UTTERLY_BROKEN_ENCODER_SHIT
148142
virtual void FinishAllAsync(void* nSignalData) = 0; // Called on Lua shutdown to finish all callbacks/async tasks for that interface
149-
#endif
150143
};
151144

152145
#undef CALLBACK // Solves another error with minwindef.h

0 commit comments

Comments
 (0)