From d413fef66f01d89626d6696545c6d421eb43b6dd Mon Sep 17 00:00:00 2001 From: mannol Date: Sat, 21 Jun 2014 01:58:55 +0200 Subject: [PATCH] Started with VAD --- toxav/Makefile.inc | 4 ++-- toxav/{media.c => codec.c} | 33 +++++++++++++++++++++++++++++++-- toxav/{media.h => codec.h} | 18 +++++++++++++----- toxav/toxav.c | 8 ++++++-- toxav/toxav.h | 5 ++++- 5 files changed, 56 insertions(+), 12 deletions(-) rename toxav/{media.c => codec.c} (91%) rename toxav/{media.h => codec.h} (89%) diff --git a/toxav/Makefile.inc b/toxav/Makefile.inc index 50e2564c..fdd20a26 100644 --- a/toxav/Makefile.inc +++ b/toxav/Makefile.inc @@ -10,8 +10,8 @@ libtoxav_la_SOURCES = ../toxav/event.h \ ../toxav/rtp.c \ ../toxav/msi.h \ ../toxav/msi.c \ - ../toxav/media.h \ - ../toxav/media.c \ + ../toxav/codec.h \ + ../toxav/codec.c \ ../toxav/toxav.h \ ../toxav/toxav.c diff --git a/toxav/media.c b/toxav/codec.c similarity index 91% rename from toxav/media.c rename to toxav/codec.c index 8b50e301..61486085 100644 --- a/toxav/media.c +++ b/toxav/codec.c @@ -34,7 +34,8 @@ #include #include "rtp.h" -#include "media.h" +#include "codec.h" + int empty_queue(JitterBuffer *q) { @@ -292,7 +293,10 @@ CodecState *codec_init_session ( uint32_t audio_bitrate, free (retu); return NULL; } - + + float frame_duration_sec = audio_frame_duration / 1000; + retu->samples_per_frame = audio_sample_rate * frame_duration_sec; + return retu; } @@ -314,3 +318,28 @@ void codec_terminate_session ( CodecState *cs ) if ( cs->capabilities & v_encoding ) vpx_codec_destroy(&cs->v_encoder); } + +inline float calculate_sum_sq (int16_t* n, size_t k) +{ + float result = 0; + size_t i = 0; + + for ( ; i < k; i ++) { + result += (float) (n[i] * n[i]); + } + + return result; +} + +int calculate_VAD_from_PCM( int16_t* PCM, size_t frame_size, float energy) +{ +// int i = 0; +// for (; i < frame_size; i ++) { + LOGGER_DEBUG("Frame size: %d ref: %f", frame_size, energy); + float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size; + LOGGER_DEBUG("Frame energy calculated: %f", frame_energy); + if ( frame_energy > energy) return 1; +// } + + return 0; +} \ No newline at end of file diff --git a/toxav/media.h b/toxav/codec.h similarity index 89% rename from toxav/media.h rename to toxav/codec.h index 66798351..45116e08 100644 --- a/toxav/media.h +++ b/toxav/codec.h @@ -21,8 +21,8 @@ * */ -#ifndef _AVCODEC_H_ -#define _AVCODEC_H_ +#ifndef _CODEC_H_ +#define _CODEC_H_ #include #include @@ -46,6 +46,8 @@ typedef enum _Capabilities { v_decoding = 1 << 3 } Capabilities; + + typedef struct _CodecState { /* video encoding */ @@ -64,7 +66,9 @@ typedef struct _CodecState { OpusDecoder *audio_decoder; uint64_t capabilities; /* supports*/ - + + /* Voice activity detection */ + float samples_per_frame; } CodecState; @@ -92,8 +96,12 @@ CodecState *codec_init_session ( uint32_t audio_bitrate, uint32_t audio_channels, uint16_t video_width, uint16_t video_height, - uint32_t video_bitrate ); + uint32_t video_bitrate); void codec_terminate_session(CodecState *cs); -#endif + +/* return 1 if has voice, 0 if not */ +int calculate_VAD_from_PCM(int16_t* PCM, size_t frame_size, float energy); + +#endif /* _CODEC_H_ */ \ No newline at end of file diff --git a/toxav/toxav.c b/toxav/toxav.c index 7b9aeaef..0a6def5d 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -27,13 +27,12 @@ #define _GNU_SOURCE /* implicit declaration warning */ #include "rtp.h" -#include "media.h" +#include "codec.h" #include "msi.h" #include "toxav.h" #include "../toxcore/logger.h" - #include #include #include @@ -732,4 +731,9 @@ inline__ int toxav_set_video_queue_limit(ToxAv *av, int32_t call_index, uint64_t inline__ Tox *toxav_get_tox(ToxAv *av) { return (Tox *)av->messenger; +} + +int toxav_has_activity(int16_t* PCM, uint16_t frame_size, float ref_energy) +{ + return calculate_VAD_from_PCM(PCM, frame_size, ref_energy); } \ No newline at end of file diff --git a/toxav/toxav.h b/toxav/toxav.h index 35fe764e..180a657b 100644 --- a/toxav/toxav.h +++ b/toxav/toxav.h @@ -90,7 +90,8 @@ typedef enum { ErrorTerminatingAudioRtp = -9, /* Returned in toxav_kill_transmission() */ ErrorTerminatingVideoRtp = -10, /* Returned in toxav_kill_transmission() */ ErrorPacketTooLarge = -11, /* Buffer exceeds size while encoding */ - + ErrorInvalidCodecState = -12, /* Codec state not initialized */ + } ToxAvError; @@ -373,6 +374,8 @@ int toxav_set_video_queue_limit ( ToxAv *av, int32_t call_index, uint64_t limit Tox *toxav_get_tox(ToxAv *av); +int toxav_has_activity ( int16_t* PCM, uint16_t frame_size, float ref_energy ); + #ifdef __cplusplus } #endif