OnlineTcpVectorSource Class Reference

#include <online-tcp-source.h>

Inheritance diagram for OnlineTcpVectorSource:
Collaboration diagram for OnlineTcpVectorSource:

Public Member Functions

 OnlineTcpVectorSource (int32 socket)
 
 ~OnlineTcpVectorSource ()
 
bool Read (Vector< BaseFloat > *data)
 
bool IsConnected ()
 
size_t SamplesProcessed ()
 
void ResetSamples ()
 
- Public Member Functions inherited from OnlineAudioSourceItf
virtual ~OnlineAudioSourceItf ()
 

Private Member Functions

bool ReadFull (char *buf, int32 len)
 
int32 GetNextPack ()
 
int32 FillFrame (int32 size)
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (OnlineTcpVectorSource)
 

Private Attributes

int32 socket_desc
 
bool connected
 
char * pack
 
int32 pack_size
 
char * frame
 
int32 frame_size
 
int32 last_pack_size
 
int32 last_pack_rem
 
size_t samples_processed
 

Detailed Description

Definition at line 34 of file online-tcp-source.h.

Constructor & Destructor Documentation

◆ OnlineTcpVectorSource()

◆ ~OnlineTcpVectorSource()

Definition at line 41 of file online-tcp-source.cc.

References OnlineTcpVectorSource::frame, and OnlineTcpVectorSource::pack.

41  {
42  delete[] pack;
43  delete[] frame;
44 }

Member Function Documentation

◆ FillFrame()

int FillFrame ( int32  size)
private

Definition at line 93 of file online-tcp-source.cc.

References OnlineTcpVectorSource::frame, OnlineTcpVectorSource::GetNextPack(), OnlineTcpVectorSource::last_pack_rem, OnlineTcpVectorSource::last_pack_size, and OnlineTcpVectorSource::pack.

Referenced by OnlineTcpVectorSource::Read().

93  {
94  int32 frame_offset = 0;
95  if (last_pack_rem > 0) {
96  int pack_offset = last_pack_size - last_pack_rem;
97  int size = last_pack_rem < get_size ? last_pack_rem : get_size;
98 
99  memcpy(frame, pack + pack_offset, size);
100 
101  last_pack_rem -= size;
102  get_size -= size;
103  frame_offset += size;
104  }
105 
106  while (get_size > 0) {
107  int32 ret = GetNextPack();
108 
109  if (ret == 0)
110  return frame_offset;
111 
112  int32 size = ret < get_size ? ret : get_size;
113 
114  memcpy(frame + frame_offset, pack, size);
115 
116  last_pack_size = ret;
117  last_pack_rem = last_pack_size - size;
118  get_size -= size;
119  frame_offset += size;
120  }
121 
122  return frame_offset;
123 }
kaldi::int32 int32

◆ GetNextPack()

int GetNextPack ( )
private

Definition at line 71 of file online-tcp-source.cc.

References KALDI_ERR, OnlineTcpVectorSource::pack, OnlineTcpVectorSource::pack_size, and OnlineTcpVectorSource::ReadFull().

Referenced by OnlineTcpVectorSource::FillFrame().

71  {
72  int32 size = 0;
73  if (!ReadFull((char*) &size, 4))
74  return 0;
75 
76  if (size % 2 != 0) {
77  KALDI_ERR << "TCPVectorSource: Pack size must be even!";
78  return 0;
79  }
80 
81  if (pack_size < size) {
82  pack_size = size;
83  delete[] pack;
84  pack = new char[pack_size];
85  }
86 
87  if (!ReadFull(pack, size))
88  return 0;
89 
90  return size;
91 }
kaldi::int32 int32
bool ReadFull(char *buf, int32 len)
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsConnected()

bool IsConnected ( )

Definition at line 151 of file online-tcp-source.cc.

References OnlineTcpVectorSource::connected.

Referenced by main().

151  {
152  return connected;
153 }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( OnlineTcpVectorSource  )
private

◆ Read()

bool Read ( Vector< BaseFloat > *  data)
virtual

Implements OnlineAudioSourceItf.

Definition at line 125 of file online-tcp-source.cc.

References OnlineTcpVectorSource::connected, VectorBase< Real >::Dim(), OnlineTcpVectorSource::FillFrame(), OnlineTcpVectorSource::frame, OnlineTcpVectorSource::frame_size, rnnlm::i, and OnlineTcpVectorSource::samples_processed.

125  {
126  if (!connected)
127  return false;
128 
129  int32 n_elem = static_cast<uint32>(data->Dim());
130 
131  int32 n_bytes = n_elem * 2;
132 
133  if (frame_size < n_bytes) {
134  frame_size = n_bytes;
135  delete[] frame;
136  frame = new char[frame_size];
137  }
138 
139  int32 b_read = FillFrame(n_bytes);
140  int32 n_read = b_read / 2;
141 
142  short* s_frame = (short*) frame;
143  for (int32 i = 0; i < n_read; i++)
144  (*data)(i) = s_frame[i];
145 
146  samples_processed += n_read;
147 
148  return (n_read == n_elem);
149 }
kaldi::int32 int32

◆ ReadFull()

bool ReadFull ( char *  buf,
int32  len 
)
private

Definition at line 53 of file online-tcp-source.cc.

References OnlineTcpVectorSource::connected, and OnlineTcpVectorSource::socket_desc.

Referenced by OnlineTcpVectorSource::GetNextPack().

53  {
54  int32 to_read = len;
55  int32 has_read = 0;
56  int32 ret;
57 
58  while (to_read > 0) {
59  ret = read(socket_desc, buf + has_read, to_read);
60  if (ret <= 0) {
61  connected = false;
62  return false;
63  }
64  to_read -= ret;
65  has_read += ret;
66  }
67 
68  return true;
69 }
kaldi::int32 int32

◆ ResetSamples()

void ResetSamples ( )

Definition at line 49 of file online-tcp-source.cc.

References OnlineTcpVectorSource::samples_processed.

Referenced by main().

49  {
51 }

◆ SamplesProcessed()

size_t SamplesProcessed ( )

Definition at line 46 of file online-tcp-source.cc.

References OnlineTcpVectorSource::samples_processed.

Referenced by main().

46  {
47  return samples_processed;
48 }

Member Data Documentation

◆ connected

◆ frame

◆ frame_size

int32 frame_size
private

◆ last_pack_rem

int32 last_pack_rem
private

Definition at line 58 of file online-tcp-source.h.

Referenced by OnlineTcpVectorSource::FillFrame().

◆ last_pack_size

int32 last_pack_size
private

Definition at line 58 of file online-tcp-source.h.

Referenced by OnlineTcpVectorSource::FillFrame().

◆ pack

◆ pack_size

int32 pack_size
private

◆ samples_processed

◆ socket_desc

int32 socket_desc
private

Definition at line 51 of file online-tcp-source.h.

Referenced by OnlineTcpVectorSource::ReadFull().


The documentation for this class was generated from the following files: