online-tcp-source.cc
Go to the documentation of this file.
1 // online/online-tcp-source.cc
2 
3 // Copyright 2013 Polish-Japanese Institute of Information Technology (author: Danijel Korzinek)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #if !defined(_MSC_VER)
21 
22 #include "online-tcp-source.h"
23 #include <unistd.h>
24 
25 namespace kaldi {
26 
28 
30  : socket_desc(socket),
31  connected(true),
32  pack_size(512),
33  frame_size(512),
34  last_pack_size(0),
35  last_pack_rem(0),
36  samples_processed(0) {
37  pack = new char[pack_size];
38  frame = new char[frame_size];
39 }
40 
42  delete[] pack;
43  delete[] frame;
44 }
45 
47  return samples_processed;
48 }
51 }
52 
53 bool OnlineTcpVectorSource::ReadFull(char* buf, int32 len) {
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 }
70 
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 }
92 
93 int OnlineTcpVectorSource::FillFrame(int32 get_size) {
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 }
124 
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 }
150 
152  return connected;
153 }
154 
155 } // namespace kaldi
156 
157 #endif // !defined(_MSC_VER)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Read(Vector< BaseFloat > *data)
kaldi::int32 int32
bool ReadFull(char *buf, int32 len)
#define KALDI_ERR
Definition: kaldi-error.h:147
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
A class representing a vector.
Definition: kaldi-vector.h:406