Skip to content

Commit

Permalink
mutex remove
Browse files Browse the repository at this point in the history
  • Loading branch information
emcifuntik committed Oct 2, 2023
1 parent 83a6806 commit 2a7558c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
43 changes: 33 additions & 10 deletions src/CSoundInput.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "CSoundInput.h"

#include <chrono>
#include <fstream>
#include "CSoundInput.h"
#include <bass_fx.h>
#include <iostream>
#include <algorithm>

#include <bass_fx.h>
#include "alt-voice.h"
#include <iostream>


CSoundInput::CSoundInput(int _bitRate) : encoder(new COpusEncoder(SAMPLE_RATE, AUDIO_CHANNELS, _bitRate)), bitrate(_bitRate)
Expand All @@ -15,8 +17,20 @@ CSoundInput::CSoundInput(int _bitRate) : encoder(new COpusEncoder(SAMPLE_RATE, A

CSoundInput::~CSoundInput()
{
if (recordChannel)
if (recordChannel != 0)
{
if (noiseSuppressionDsp != 0)
{
BASS_ChannelRemoveDSP(recordChannel, noiseSuppressionDsp);
noiseSuppressionDsp = 0;
}

if (normalizationDsp != 0)
{
BASS_ChannelRemoveDSP(recordChannel, normalizationDsp);
normalizationDsp = 0;
}

BASS_ChannelStop(recordChannel);
BASS_ChannelFree(recordChannel);
}
Expand Down Expand Up @@ -135,7 +149,20 @@ AltVoiceError CSoundInput::SelectDeviceByUID(const char* uid)
isDefault = !uid;

if (recordChannel != 0)
{
if (noiseSuppressionDsp != 0)
{
BASS_ChannelRemoveDSP(recordChannel, noiseSuppressionDsp);
noiseSuppressionDsp = 0;
}

if (normalizationDsp != 0)
{
BASS_ChannelRemoveDSP(recordChannel, normalizationDsp);
normalizationDsp = 0;
}
BASS_RecordFree();
}

if (!BASS_RecordInit(nextDeviceId))
return AltVoiceError::DeviceInit;
Expand All @@ -150,8 +177,8 @@ AltVoiceError CSoundInput::SelectDeviceByUID(const char* uid)
const BASS_BFX_VOLUME VolumeChangeFXParams = { BASS_BFX_CHANALL, volume };
BASS_FXSetParameters(VolumeChangeFX, &VolumeChangeFXParams);

BASS_ChannelSetDSP(recordChannel, NoiseDSP, this, 2); //higher prio called first
BASS_ChannelSetDSP(recordChannel, NormalizeDSP, this, 1);
noiseSuppressionDsp = BASS_ChannelSetDSP(recordChannel, NoiseDSP, this, 2); //higher prio called first
normalizationDsp = BASS_ChannelSetDSP(recordChannel, NormalizeDSP, this, 1);
//BASS_ChannelSetDSP(recordChannel, RMSDSP, this, 0); //higher prio called first

BASS_ChannelStart(levelChannel);
Expand Down Expand Up @@ -298,8 +325,6 @@ void CSoundInput::Normalize(void* buffer, DWORD length)
void CSoundInput::NormalizeDSP(HDSP handle, DWORD channel, void* buffer, DWORD length, void* user)
{
const auto self = static_cast<CSoundInput*>(user);
std::unique_lock lock{ self->inputMutex };

if (self->IsNormalizationEnabled())
{
for (int i = 0; i < length; i += (FRAME_SIZE_SAMPLES * sizeof(short)))
Expand All @@ -312,8 +337,6 @@ void CSoundInput::NormalizeDSP(HDSP handle, DWORD channel, void* buffer, DWORD l
void CSoundInput::NoiseDSP(HDSP handle, DWORD channel, void* buffer, DWORD length, void* user)
{
const auto self = static_cast<CSoundInput*>(user);
std::unique_lock lock{ self->inputMutex };

if(self->IsNoiseSuppressionEnabled())
{
for (int i = 0; i < length; i += (FRAME_SIZE_SAMPLES * sizeof(short)))
Expand Down
3 changes: 3 additions & 0 deletions src/CSoundInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class CSoundInput : public ISoundInput
bool normalizationEnabled = false;
float normalizeMax = 0.f;

HDSP noiseSuppressionDsp = 0;
HDSP normalizationDsp = 0;

HFX VolumeChangeFX;
DenoiseState* denoiser;

Expand Down

0 comments on commit 2a7558c

Please sign in to comment.