mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Started with VAD
This commit is contained in:
parent
88a131a2e8
commit
d413fef66f
|
@ -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
|
||||
|
||||
|
|
|
@ -34,7 +34,8 @@
|
|||
#include <assert.h>
|
||||
|
||||
#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;
|
||||
}
|
|
@ -21,8 +21,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#ifndef _AVCODEC_H_
|
||||
#define _AVCODEC_H_
|
||||
#ifndef _CODEC_H_
|
||||
#define _CODEC_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
@ -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_ */
|
|
@ -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 <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -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);
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user