Class: WebRTC::Vad

Inherits:
Object
  • Object
show all
Defined in:
lib/webrtcvad.rb,
ext/webrtcvad/webrtcvad.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rb_rage) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/webrtcvad/webrtcvad.c', line 30

static VALUE rb_vad_init(VALUE self, VALUE rb_rage) {
  struct vad_instance* vad;
  Data_Get_Struct(self, struct vad_instance, vad);
  
  vad->inst = WebRtcVad_Create();
  vad->rage = get_aggressiveness(rb_rage);
  
  if (WebRtcVad_Init(vad->inst)) {
    rb_raise(rb_eRuntimeError, "Cannot init VAD");
  }
  if (WebRtcVad_set_mode(vad->inst, vad->rage)) {
    rb_raise(rb_eRuntimeError, "Cannot set VAD aggressiveness");
  }
  return self;
}

Class Method Details

.new(rb_rage) ⇒ Object



13
14
15
16
17
18
19
20
# File 'ext/webrtcvad/webrtcvad.c', line 13

static VALUE rb_vad_create(VALUE klass, VALUE rb_rage) {
  struct vad_instance* vad;
  VALUE obj = Data_Make_Struct(klass, struct vad_instance, NULL, vad_free, vad);
  VALUE argv[1];
  argv[0] = rb_rage;
  rb_obj_call_init(obj, 1, argv);
  return obj;
}

.valid_rate_and_frame_length?(rb_rate, rb_frame_len) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
# File 'ext/webrtcvad/webrtcvad.c', line 65

static VALUE rb_valid_rate_and_frame_length(VALUE self, VALUE rb_rate, VALUE rb_frame_len) {
  int rate, frame_length;
  rate = NUM2INT(rb_rate);
  frame_length = NUM2INT(rb_frame_len);

  if (WebRtcVad_ValidRateAndFrameLength(rate, frame_length)) {
    return Qfalse;
  } else {
    return Qtrue;
  }
}

Instance Method Details

#aggressivenessObject



58
59
60
61
62
63
# File 'ext/webrtcvad/webrtcvad.c', line 58

static VALUE rb_vad_get_aggressiveness(VALUE self) {
  struct vad_instance* vad;
  Data_Get_Struct(self, struct vad_instance, vad);
  
  return INT2NUM(vad->rage);
}

#aggressiveness=(rb_rage) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'ext/webrtcvad/webrtcvad.c', line 46

static VALUE rb_vad_set_aggressiveness(VALUE self, VALUE rb_rage) {
  struct vad_instance* vad;
  Data_Get_Struct(self, struct vad_instance, vad);
  
  int rage = get_aggressiveness(rb_rage);
  if (WebRtcVad_set_mode(vad->inst, rage)) {
    rb_raise(rb_eRuntimeError, "Cannot set VAD aggressiveness");
  }
  vad->rage = rage;
  return rb_rage;
}

#classify(buf:, sample_rate:, offset: 0, samples_count: (buf.size - offset).div(2)) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/webrtcvad.rb', line 7

def classify(buf:, sample_rate:, offset: 0, samples_count: (buf.size - offset).div(2))
  raise ArgumentError, 'Length exceeds buffer length' if samples_count > (buf.size - offset).div(2)

  process(sample_rate, buf, offset, samples_count)
end

#process(rb_sample_rate, rb_audio_frame, rb_offset, rb_frame_len) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/webrtcvad/webrtcvad.c', line 77

static VALUE rb_vad_process(VALUE self, VALUE rb_sample_rate, VALUE rb_audio_frame, VALUE rb_offset, VALUE rb_frame_len) {
  int sr = NUM2INT(rb_sample_rate);
  int offset = NUM2INT(rb_offset);
  int frame_length = NUM2INT(rb_frame_len);
  int result;
  int16_t *buf = (int16_t*)(StringValuePtr(rb_audio_frame) + offset);
  
  struct vad_instance* ptr;
  Data_Get_Struct(self, struct vad_instance, ptr);
  
  result = WebRtcVad_Process(ptr->inst, sr, buf, frame_length);
  switch (result) {
  case 1:
    return INT2NUM(1);
  case 0:
    return INT2NUM(0);
  case -1:
    break;
  default:
    rb_raise(rb_eRuntimeError, "Error while processing frame");
  }
  return Qnil;
}

#valid_frame?(rb_rate, rb_frame_len) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
# File 'ext/webrtcvad/webrtcvad.c', line 65

static VALUE rb_valid_rate_and_frame_length(VALUE self, VALUE rb_rate, VALUE rb_frame_len) {
  int rate, frame_length;
  rate = NUM2INT(rb_rate);
  frame_length = NUM2INT(rb_frame_len);

  if (WebRtcVad_ValidRateAndFrameLength(rate, frame_length)) {
    return Qfalse;
  } else {
    return Qtrue;
  }
}