blob: e69bf7935364c44dd199db16eb47c283d279cb75 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:241/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/audio_processing/transient/file_utils.h"
12
kwiberg85d8bb02016-02-17 04:39:3613#include <memory>
14
Henrik Kjellander98f53512015-10-28 17:17:4015#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org788acd12014-12-15 09:41:2416#include "webrtc/typedefs.h"
17
18namespace webrtc {
19
20int ConvertByteArrayToFloat(const uint8_t bytes[4], float* out) {
21 if (!bytes || !out) {
22 return -1;
23 }
24
25 uint32_t binary_value = 0;
26 for (int i = 3; i >= 0; --i) {
27 binary_value <<= 8;
28 binary_value += bytes[i];
29 }
30
31 *out = bit_cast<float>(binary_value);
32
33 return 0;
34}
35
36int ConvertByteArrayToDouble(const uint8_t bytes[8], double* out) {
37 if (!bytes || !out) {
38 return -1;
39 }
40
41 uint64_t binary_value = 0;
42 for (int i = 7; i >= 0; --i) {
43 binary_value <<= 8;
44 binary_value += bytes[i];
45 }
46
47 *out = bit_cast<double>(binary_value);
48
49 return 0;
50}
51
52int ConvertFloatToByteArray(float value, uint8_t out_bytes[4]) {
53 if (!out_bytes) {
54 return -1;
55 }
56
57 uint32_t binary_value = bit_cast<uint32_t>(value);
58 for (size_t i = 0; i < 4; ++i) {
59 out_bytes[i] = binary_value;
60 binary_value >>= 8;
61 }
62
63 return 0;
64}
65
66int ConvertDoubleToByteArray(double value, uint8_t out_bytes[8]) {
67 if (!out_bytes) {
68 return -1;
69 }
70
71 uint64_t binary_value = bit_cast<uint64_t>(value);
72 for (size_t i = 0; i < 8; ++i) {
73 out_bytes[i] = binary_value;
74 binary_value >>= 8;
75 }
76
77 return 0;
78}
79
80size_t ReadInt16BufferFromFile(FileWrapper* file,
81 size_t length,
82 int16_t* buffer) {
tommia6219cc2016-06-15 17:30:1483 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:2484 return 0;
85 }
86
kwiberg85d8bb02016-02-17 04:39:3687 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]);
pbos@webrtc.org788acd12014-12-15 09:41:2488
89 size_t int16s_read = 0;
90
91 while (int16s_read < length) {
92 size_t bytes_read = file->Read(byte_array.get(), 2);
93 if (bytes_read < 2) {
94 break;
95 }
96 int16_t value = byte_array[1];
97 value <<= 8;
98 value += byte_array[0];
99 buffer[int16s_read] = value;
100 ++int16s_read;
101 }
102
103 return int16s_read;
104}
105
106size_t ReadInt16FromFileToFloatBuffer(FileWrapper* file,
107 size_t length,
108 float* buffer) {
tommia6219cc2016-06-15 17:30:14109 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24110 return 0;
111 }
112
kwiberg85d8bb02016-02-17 04:39:36113 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]);
pbos@webrtc.org788acd12014-12-15 09:41:24114
115 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get());
116
117 for (size_t i = 0; i < int16s_read; ++i) {
118 buffer[i] = buffer16[i];
119 }
120
121 return int16s_read;
122}
123
124size_t ReadInt16FromFileToDoubleBuffer(FileWrapper* file,
125 size_t length,
126 double* buffer) {
tommia6219cc2016-06-15 17:30:14127 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24128 return 0;
129 }
130
kwiberg85d8bb02016-02-17 04:39:36131 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]);
pbos@webrtc.org788acd12014-12-15 09:41:24132
133 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get());
134
135 for (size_t i = 0; i < int16s_read; ++i) {
136 buffer[i] = buffer16[i];
137 }
138
139 return int16s_read;
140}
141
142size_t ReadFloatBufferFromFile(FileWrapper* file,
143 size_t length,
144 float* buffer) {
tommia6219cc2016-06-15 17:30:14145 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24146 return 0;
147 }
148
kwiberg85d8bb02016-02-17 04:39:36149 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]);
pbos@webrtc.org788acd12014-12-15 09:41:24150
151 size_t floats_read = 0;
152
153 while (floats_read < length) {
154 size_t bytes_read = file->Read(byte_array.get(), 4);
155 if (bytes_read < 4) {
156 break;
157 }
158 ConvertByteArrayToFloat(byte_array.get(), &buffer[floats_read]);
159 ++floats_read;
160 }
161
162 return floats_read;
163}
164
165size_t ReadDoubleBufferFromFile(FileWrapper* file,
166 size_t length,
167 double* buffer) {
tommia6219cc2016-06-15 17:30:14168 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24169 return 0;
170 }
171
kwiberg85d8bb02016-02-17 04:39:36172 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]);
pbos@webrtc.org788acd12014-12-15 09:41:24173
174 size_t doubles_read = 0;
175
176 while (doubles_read < length) {
177 size_t bytes_read = file->Read(byte_array.get(), 8);
178 if (bytes_read < 8) {
179 break;
180 }
181 ConvertByteArrayToDouble(byte_array.get(), &buffer[doubles_read]);
182 ++doubles_read;
183 }
184
185 return doubles_read;
186}
187
188size_t WriteInt16BufferToFile(FileWrapper* file,
189 size_t length,
190 const int16_t* buffer) {
tommia6219cc2016-06-15 17:30:14191 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24192 return 0;
193 }
194
kwiberg85d8bb02016-02-17 04:39:36195 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]);
pbos@webrtc.org788acd12014-12-15 09:41:24196
197 size_t int16s_written = 0;
198
199 for (int16s_written = 0; int16s_written < length; ++int16s_written) {
200 // Get byte representation.
201 byte_array[0] = buffer[int16s_written] & 0xFF;
202 byte_array[1] = (buffer[int16s_written] >> 8) & 0xFF;
203
204 file->Write(byte_array.get(), 2);
205 }
206
207 file->Flush();
208
209 return int16s_written;
210}
211
212size_t WriteFloatBufferToFile(FileWrapper* file,
213 size_t length,
214 const float* buffer) {
tommia6219cc2016-06-15 17:30:14215 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24216 return 0;
217 }
218
kwiberg85d8bb02016-02-17 04:39:36219 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]);
pbos@webrtc.org788acd12014-12-15 09:41:24220
221 size_t floats_written = 0;
222
223 for (floats_written = 0; floats_written < length; ++floats_written) {
224 // Get byte representation.
225 ConvertFloatToByteArray(buffer[floats_written], byte_array.get());
226
227 file->Write(byte_array.get(), 4);
228 }
229
230 file->Flush();
231
232 return floats_written;
233}
234
235size_t WriteDoubleBufferToFile(FileWrapper* file,
236 size_t length,
237 const double* buffer) {
tommia6219cc2016-06-15 17:30:14238 if (!file || !file->is_open() || !buffer || length <= 0) {
pbos@webrtc.org788acd12014-12-15 09:41:24239 return 0;
240 }
241
kwiberg85d8bb02016-02-17 04:39:36242 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]);
pbos@webrtc.org788acd12014-12-15 09:41:24243
244 size_t doubles_written = 0;
245
246 for (doubles_written = 0; doubles_written < length; ++doubles_written) {
247 // Get byte representation.
248 ConvertDoubleToByteArray(buffer[doubles_written], byte_array.get());
249
250 file->Write(byte_array.get(), 8);
251 }
252
253 file->Flush();
254
255 return doubles_written;
256}
257
258} // namespace webrtc