Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: webrtc/modules/audio_processing/transient/file_utils.cc

Issue 2054373002: FileWrapper[Impl] modifications and actually remove the "Impl" class. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix use of ASSERT instead of ASSERT_TRUE in test Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 out_bytes[i] = binary_value; 73 out_bytes[i] = binary_value;
74 binary_value >>= 8; 74 binary_value >>= 8;
75 } 75 }
76 76
77 return 0; 77 return 0;
78 } 78 }
79 79
80 size_t ReadInt16BufferFromFile(FileWrapper* file, 80 size_t ReadInt16BufferFromFile(FileWrapper* file,
81 size_t length, 81 size_t length,
82 int16_t* buffer) { 82 int16_t* buffer) {
83 if (!file || !file->Open() || !buffer || length <= 0) { 83 if (!file || !file->is_open() || !buffer || length <= 0) {
84 return 0; 84 return 0;
85 } 85 }
86 86
87 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]); 87 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]);
88 88
89 size_t int16s_read = 0; 89 size_t int16s_read = 0;
90 90
91 while (int16s_read < length) { 91 while (int16s_read < length) {
92 size_t bytes_read = file->Read(byte_array.get(), 2); 92 size_t bytes_read = file->Read(byte_array.get(), 2);
93 if (bytes_read < 2) { 93 if (bytes_read < 2) {
94 break; 94 break;
95 } 95 }
96 int16_t value = byte_array[1]; 96 int16_t value = byte_array[1];
97 value <<= 8; 97 value <<= 8;
98 value += byte_array[0]; 98 value += byte_array[0];
99 buffer[int16s_read] = value; 99 buffer[int16s_read] = value;
100 ++int16s_read; 100 ++int16s_read;
101 } 101 }
102 102
103 return int16s_read; 103 return int16s_read;
104 } 104 }
105 105
106 size_t ReadInt16FromFileToFloatBuffer(FileWrapper* file, 106 size_t ReadInt16FromFileToFloatBuffer(FileWrapper* file,
107 size_t length, 107 size_t length,
108 float* buffer) { 108 float* buffer) {
109 if (!file || !file->Open() || !buffer || length <= 0) { 109 if (!file || !file->is_open() || !buffer || length <= 0) {
110 return 0; 110 return 0;
111 } 111 }
112 112
113 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]); 113 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]);
114 114
115 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get()); 115 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get());
116 116
117 for (size_t i = 0; i < int16s_read; ++i) { 117 for (size_t i = 0; i < int16s_read; ++i) {
118 buffer[i] = buffer16[i]; 118 buffer[i] = buffer16[i];
119 } 119 }
120 120
121 return int16s_read; 121 return int16s_read;
122 } 122 }
123 123
124 size_t ReadInt16FromFileToDoubleBuffer(FileWrapper* file, 124 size_t ReadInt16FromFileToDoubleBuffer(FileWrapper* file,
125 size_t length, 125 size_t length,
126 double* buffer) { 126 double* buffer) {
127 if (!file || !file->Open() || !buffer || length <= 0) { 127 if (!file || !file->is_open() || !buffer || length <= 0) {
128 return 0; 128 return 0;
129 } 129 }
130 130
131 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]); 131 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]);
132 132
133 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get()); 133 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get());
134 134
135 for (size_t i = 0; i < int16s_read; ++i) { 135 for (size_t i = 0; i < int16s_read; ++i) {
136 buffer[i] = buffer16[i]; 136 buffer[i] = buffer16[i];
137 } 137 }
138 138
139 return int16s_read; 139 return int16s_read;
140 } 140 }
141 141
142 size_t ReadFloatBufferFromFile(FileWrapper* file, 142 size_t ReadFloatBufferFromFile(FileWrapper* file,
143 size_t length, 143 size_t length,
144 float* buffer) { 144 float* buffer) {
145 if (!file || !file->Open() || !buffer || length <= 0) { 145 if (!file || !file->is_open() || !buffer || length <= 0) {
146 return 0; 146 return 0;
147 } 147 }
148 148
149 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]); 149 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]);
150 150
151 size_t floats_read = 0; 151 size_t floats_read = 0;
152 152
153 while (floats_read < length) { 153 while (floats_read < length) {
154 size_t bytes_read = file->Read(byte_array.get(), 4); 154 size_t bytes_read = file->Read(byte_array.get(), 4);
155 if (bytes_read < 4) { 155 if (bytes_read < 4) {
156 break; 156 break;
157 } 157 }
158 ConvertByteArrayToFloat(byte_array.get(), &buffer[floats_read]); 158 ConvertByteArrayToFloat(byte_array.get(), &buffer[floats_read]);
159 ++floats_read; 159 ++floats_read;
160 } 160 }
161 161
162 return floats_read; 162 return floats_read;
163 } 163 }
164 164
165 size_t ReadDoubleBufferFromFile(FileWrapper* file, 165 size_t ReadDoubleBufferFromFile(FileWrapper* file,
166 size_t length, 166 size_t length,
167 double* buffer) { 167 double* buffer) {
168 if (!file || !file->Open() || !buffer || length <= 0) { 168 if (!file || !file->is_open() || !buffer || length <= 0) {
169 return 0; 169 return 0;
170 } 170 }
171 171
172 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]); 172 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]);
173 173
174 size_t doubles_read = 0; 174 size_t doubles_read = 0;
175 175
176 while (doubles_read < length) { 176 while (doubles_read < length) {
177 size_t bytes_read = file->Read(byte_array.get(), 8); 177 size_t bytes_read = file->Read(byte_array.get(), 8);
178 if (bytes_read < 8) { 178 if (bytes_read < 8) {
179 break; 179 break;
180 } 180 }
181 ConvertByteArrayToDouble(byte_array.get(), &buffer[doubles_read]); 181 ConvertByteArrayToDouble(byte_array.get(), &buffer[doubles_read]);
182 ++doubles_read; 182 ++doubles_read;
183 } 183 }
184 184
185 return doubles_read; 185 return doubles_read;
186 } 186 }
187 187
188 size_t WriteInt16BufferToFile(FileWrapper* file, 188 size_t WriteInt16BufferToFile(FileWrapper* file,
189 size_t length, 189 size_t length,
190 const int16_t* buffer) { 190 const int16_t* buffer) {
191 if (!file || !file->Open() || !buffer || length <= 0) { 191 if (!file || !file->is_open() || !buffer || length <= 0) {
192 return 0; 192 return 0;
193 } 193 }
194 194
195 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]); 195 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]);
196 196
197 size_t int16s_written = 0; 197 size_t int16s_written = 0;
198 198
199 for (int16s_written = 0; int16s_written < length; ++int16s_written) { 199 for (int16s_written = 0; int16s_written < length; ++int16s_written) {
200 // Get byte representation. 200 // Get byte representation.
201 byte_array[0] = buffer[int16s_written] & 0xFF; 201 byte_array[0] = buffer[int16s_written] & 0xFF;
202 byte_array[1] = (buffer[int16s_written] >> 8) & 0xFF; 202 byte_array[1] = (buffer[int16s_written] >> 8) & 0xFF;
203 203
204 file->Write(byte_array.get(), 2); 204 file->Write(byte_array.get(), 2);
205 } 205 }
206 206
207 file->Flush(); 207 file->Flush();
208 208
209 return int16s_written; 209 return int16s_written;
210 } 210 }
211 211
212 size_t WriteFloatBufferToFile(FileWrapper* file, 212 size_t WriteFloatBufferToFile(FileWrapper* file,
213 size_t length, 213 size_t length,
214 const float* buffer) { 214 const float* buffer) {
215 if (!file || !file->Open() || !buffer || length <= 0) { 215 if (!file || !file->is_open() || !buffer || length <= 0) {
216 return 0; 216 return 0;
217 } 217 }
218 218
219 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]); 219 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]);
220 220
221 size_t floats_written = 0; 221 size_t floats_written = 0;
222 222
223 for (floats_written = 0; floats_written < length; ++floats_written) { 223 for (floats_written = 0; floats_written < length; ++floats_written) {
224 // Get byte representation. 224 // Get byte representation.
225 ConvertFloatToByteArray(buffer[floats_written], byte_array.get()); 225 ConvertFloatToByteArray(buffer[floats_written], byte_array.get());
226 226
227 file->Write(byte_array.get(), 4); 227 file->Write(byte_array.get(), 4);
228 } 228 }
229 229
230 file->Flush(); 230 file->Flush();
231 231
232 return floats_written; 232 return floats_written;
233 } 233 }
234 234
235 size_t WriteDoubleBufferToFile(FileWrapper* file, 235 size_t WriteDoubleBufferToFile(FileWrapper* file,
236 size_t length, 236 size_t length,
237 const double* buffer) { 237 const double* buffer) {
238 if (!file || !file->Open() || !buffer || length <= 0) { 238 if (!file || !file->is_open() || !buffer || length <= 0) {
239 return 0; 239 return 0;
240 } 240 }
241 241
242 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]); 242 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]);
243 243
244 size_t doubles_written = 0; 244 size_t doubles_written = 0;
245 245
246 for (doubles_written = 0; doubles_written < length; ++doubles_written) { 246 for (doubles_written = 0; doubles_written < length; ++doubles_written) {
247 // Get byte representation. 247 // Get byte representation.
248 ConvertDoubleToByteArray(buffer[doubles_written], byte_array.get()); 248 ConvertDoubleToByteArray(buffer[doubles_written], byte_array.get());
249 249
250 file->Write(byte_array.get(), 8); 250 file->Write(byte_array.get(), 8);
251 } 251 }
252 252
253 file->Flush(); 253 file->Flush();
254 254
255 return doubles_written; 255 return doubles_written;
256 } 256 }
257 257
258 } // namespace webrtc 258 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698