Skip to content

Commit

Permalink
Unit-test of gain applied to TT_InsertAudioBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
bear101 committed May 13, 2021
1 parent db9aa64 commit b75e2c7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
4 changes: 1 addition & 3 deletions Library/TeamTalkLib/codec/MediaUtil.cpp
Expand Up @@ -297,10 +297,8 @@ ACE_Message_Block* AudioFrameFromList(int samples_out, std::vector<ACE_Message_B
return AudioFrameToMsgBlock(muxfrm);
}

int GenerateTone(media::AudioFrame& audblock, int sample_index, int tone_freq)
int GenerateTone(media::AudioFrame& audblock, int sample_index, int tone_freq, double volume /*= 8000*/)
{
double volume = 8000;

for(int i = 0; i<audblock.input_samples; i++)
{
double t = (double)sample_index++ / audblock.inputfmt.samplerate;
Expand Down
2 changes: 1 addition & 1 deletion Library/TeamTalkLib/codec/MediaUtil.h
Expand Up @@ -250,7 +250,7 @@ StereoMask ToStereoMask(bool muteleft, bool muteright);
void SelectStereo(StereoMask stereo, short* buffer, int samples);

// returns new sample_index
int GenerateTone(media::AudioFrame& audblock, int sample_index, int tone_freq);
int GenerateTone(media::AudioFrame& audblock, int sample_index, int tone_freq, double volume = 8000);

#define GAIN_MAX 32000
#define GAIN_NORMAL 1000
Expand Down
51 changes: 51 additions & 0 deletions Library/TeamTalkLib/test/CatchDefault.cpp
Expand Up @@ -310,6 +310,8 @@ int GetAudioBlockSamplesSum(TTInstance* ttinst, int userid, StreamTypes sts)
sum_samples = sum_samples / ab->nChannels;
REQUIRE(TT_ReleaseUserAudioBlock(ttinst, ab));

// std::cout << "Sum samples: " << sum_samples << std::endl;

return sum_samples;
};

Expand Down Expand Up @@ -2421,6 +2423,55 @@ TEST_CASE("InjectAudio")
} while ((samples -= SAMPLERATE) > 0);
}

TEST_CASE("InjectAudioInputGain")
{
ttinst ttclient = InitTeamTalk();
REQUIRE(InitSound(ttclient, DEFAULT, TT_SOUNDDEVICE_ID_TEAMTALK_VIRTUAL));
REQUIRE(Connect(ttclient, ACE_TEXT("127.0.0.1"), 10333, 10333));
REQUIRE(Login(ttclient, ACE_TEXT("TxClient"), ACE_TEXT("guest"), ACE_TEXT("guest")));
REQUIRE(JoinRoot(ttclient));

Channel chan = {};
REQUIRE(TT_GetChannel(ttclient, TT_GetMyChannelID(ttclient), &chan));
REQUIRE(chan.audiocodec.nCodec == OPUS_CODEC);
auto const N_FRAMES = 5;
int samples = PCM16_DURATION_SAMPLES(chan.audiocodec.opus.nTxIntervalMSec, chan.audiocodec.opus.nSampleRate) * N_FRAMES;
std::vector<short> buffer(samples * chan.audiocodec.opus.nChannels);
media::AudioFrame frm(media::AudioFormat(chan.audiocodec.opus.nSampleRate, chan.audiocodec.opus.nChannels), &buffer[0], samples);
int sampleindex = GenerateTone(frm, 0, 500);

AudioBlock ab = {};
ab.lpRawAudio = frm.input_buffer;
ab.nChannels = frm.inputfmt.channels;
ab.nSampleRate = frm.inputfmt.samplerate;
ab.uSampleIndex = frm.sample_no;
ab.nSamples = frm.input_samples;

REQUIRE(TT_EnableAudioBlockEvent(ttclient, TT_MUXED_USERID, STREAMTYPE_VOICE, TRUE));
REQUIRE(TT_InsertAudioBlock(ttclient, &ab));

// wait for 'ab' to appear
int sumsamples_pregain;
while ((sumsamples_pregain = GetAudioBlockSamplesSum(ttclient, TT_MUXED_USERID, STREAMTYPE_VOICE)) == 0);
int recv_frames = 1;
for (;recv_frames < N_FRAMES;++recv_frames)
sumsamples_pregain += GetAudioBlockSamplesSum(ttclient, TT_MUXED_USERID, STREAMTYPE_VOICE);

// AudioMuxer underflow
REQUIRE(WaitForEvent(ttclient, CLIENTEVENT_USER_AUDIOBLOCK, chan.audiocodec.opus.nTxIntervalMSec * 2) == FALSE);

// double gain
REQUIRE(TT_SetSoundInputGainLevel(ttclient, TT_GetSoundInputGainLevel(ttclient) * 2));
ab.uSampleIndex = ab.nSamples;
REQUIRE(TT_InsertAudioBlock(ttclient, &ab));

int sumsamples_postgain = 0;
for (recv_frames=0;recv_frames < N_FRAMES;++recv_frames)
sumsamples_postgain += GetAudioBlockSamplesSum(ttclient, TT_MUXED_USERID, STREAMTYPE_VOICE);

REQUIRE(sumsamples_pregain * 1.9 < sumsamples_postgain);
}

TEST_CASE("FixedJitterBuffer")
{
auto txclient = InitTeamTalk();
Expand Down

0 comments on commit b75e2c7

Please sign in to comment.