Skip to content

Commit d413fef

Browse files
author
mannol
committed
Started with VAD
1 parent 88a131a commit d413fef

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

toxav/Makefile.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ libtoxav_la_SOURCES = ../toxav/event.h \
1010
../toxav/rtp.c \
1111
../toxav/msi.h \
1212
../toxav/msi.c \
13-
../toxav/media.h \
14-
../toxav/media.c \
13+
../toxav/codec.h \
14+
../toxav/codec.c \
1515
../toxav/toxav.h \
1616
../toxav/toxav.c
1717

toxav/media.c renamed to toxav/codec.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
#include <assert.h>
3535

3636
#include "rtp.h"
37-
#include "media.h"
37+
#include "codec.h"
38+
3839

3940
int empty_queue(JitterBuffer *q)
4041
{
@@ -292,7 +293,10 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
292293
free (retu);
293294
return NULL;
294295
}
295-
296+
297+
float frame_duration_sec = audio_frame_duration / 1000;
298+
retu->samples_per_frame = audio_sample_rate * frame_duration_sec;
299+
296300
return retu;
297301
}
298302

@@ -314,3 +318,28 @@ void codec_terminate_session ( CodecState *cs )
314318
if ( cs->capabilities & v_encoding )
315319
vpx_codec_destroy(&cs->v_encoder);
316320
}
321+
322+
inline float calculate_sum_sq (int16_t* n, size_t k)
323+
{
324+
float result = 0;
325+
size_t i = 0;
326+
327+
for ( ; i < k; i ++) {
328+
result += (float) (n[i] * n[i]);
329+
}
330+
331+
return result;
332+
}
333+
334+
int calculate_VAD_from_PCM( int16_t* PCM, size_t frame_size, float energy)
335+
{
336+
// int i = 0;
337+
// for (; i < frame_size; i ++) {
338+
LOGGER_DEBUG("Frame size: %d ref: %f", frame_size, energy);
339+
float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size;
340+
LOGGER_DEBUG("Frame energy calculated: %f", frame_energy);
341+
if ( frame_energy > energy) return 1;
342+
// }
343+
344+
return 0;
345+
}

toxav/media.h renamed to toxav/codec.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*
2222
*/
2323

24-
#ifndef _AVCODEC_H_
25-
#define _AVCODEC_H_
24+
#ifndef _CODEC_H_
25+
#define _CODEC_H_
2626

2727
#include <stdio.h>
2828
#include <math.h>
@@ -46,6 +46,8 @@ typedef enum _Capabilities {
4646
v_decoding = 1 << 3
4747
} Capabilities;
4848

49+
50+
4951
typedef struct _CodecState {
5052

5153
/* video encoding */
@@ -64,7 +66,9 @@ typedef struct _CodecState {
6466
OpusDecoder *audio_decoder;
6567

6668
uint64_t capabilities; /* supports*/
67-
69+
70+
/* Voice activity detection */
71+
float samples_per_frame;
6872
} CodecState;
6973

7074

@@ -92,8 +96,12 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
9296
uint32_t audio_channels,
9397
uint16_t video_width,
9498
uint16_t video_height,
95-
uint32_t video_bitrate );
99+
uint32_t video_bitrate);
96100

97101
void codec_terminate_session(CodecState *cs);
98102

99-
#endif
103+
104+
/* return 1 if has voice, 0 if not */
105+
int calculate_VAD_from_PCM(int16_t* PCM, size_t frame_size, float energy);
106+
107+
#endif /* _CODEC_H_ */

toxav/toxav.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
#define _GNU_SOURCE /* implicit declaration warning */
2828

2929
#include "rtp.h"
30-
#include "media.h"
30+
#include "codec.h"
3131
#include "msi.h"
3232
#include "toxav.h"
3333

3434
#include "../toxcore/logger.h"
3535

36-
3736
#include <assert.h>
3837
#include <stdlib.h>
3938
#include <string.h>
@@ -732,4 +731,9 @@ inline__ int toxav_set_video_queue_limit(ToxAv *av, int32_t call_index, uint64_t
732731
inline__ Tox *toxav_get_tox(ToxAv *av)
733732
{
734733
return (Tox *)av->messenger;
734+
}
735+
736+
int toxav_has_activity(int16_t* PCM, uint16_t frame_size, float ref_energy)
737+
{
738+
return calculate_VAD_from_PCM(PCM, frame_size, ref_energy);
735739
}

toxav/toxav.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ typedef enum {
9090
ErrorTerminatingAudioRtp = -9, /* Returned in toxav_kill_transmission() */
9191
ErrorTerminatingVideoRtp = -10, /* Returned in toxav_kill_transmission() */
9292
ErrorPacketTooLarge = -11, /* Buffer exceeds size while encoding */
93-
93+
ErrorInvalidCodecState = -12, /* Codec state not initialized */
94+
9495
} ToxAvError;
9596

9697

@@ -373,6 +374,8 @@ int toxav_set_video_queue_limit ( ToxAv *av, int32_t call_index, uint64_t limit
373374

374375
Tox *toxav_get_tox(ToxAv *av);
375376

377+
int toxav_has_activity ( int16_t* PCM, uint16_t frame_size, float ref_energy );
378+
376379
#ifdef __cplusplus
377380
}
378381
#endif

0 commit comments

Comments
 (0)