OLD | NEW |
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 |
11 #include "webrtc/modules/audio_processing/transient/file_utils.h" | 11 #include "webrtc/modules/audio_processing/transient/file_utils.h" |
12 | 12 |
13 #include "webrtc/base/scoped_ptr.h" | 13 #include <memory> |
| 14 |
14 #include "webrtc/system_wrappers/include/file_wrapper.h" | 15 #include "webrtc/system_wrappers/include/file_wrapper.h" |
15 #include "webrtc/typedefs.h" | 16 #include "webrtc/typedefs.h" |
16 | 17 |
17 namespace webrtc { | 18 namespace webrtc { |
18 | 19 |
19 int ConvertByteArrayToFloat(const uint8_t bytes[4], float* out) { | 20 int ConvertByteArrayToFloat(const uint8_t bytes[4], float* out) { |
20 if (!bytes || !out) { | 21 if (!bytes || !out) { |
21 return -1; | 22 return -1; |
22 } | 23 } |
23 | 24 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 return 0; | 77 return 0; |
77 } | 78 } |
78 | 79 |
79 size_t ReadInt16BufferFromFile(FileWrapper* file, | 80 size_t ReadInt16BufferFromFile(FileWrapper* file, |
80 size_t length, | 81 size_t length, |
81 int16_t* buffer) { | 82 int16_t* buffer) { |
82 if (!file || !file->Open() || !buffer || length <= 0) { | 83 if (!file || !file->Open() || !buffer || length <= 0) { |
83 return 0; | 84 return 0; |
84 } | 85 } |
85 | 86 |
86 rtc::scoped_ptr<uint8_t[]> byte_array(new uint8_t[2]); | 87 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]); |
87 | 88 |
88 size_t int16s_read = 0; | 89 size_t int16s_read = 0; |
89 | 90 |
90 while (int16s_read < length) { | 91 while (int16s_read < length) { |
91 size_t bytes_read = file->Read(byte_array.get(), 2); | 92 size_t bytes_read = file->Read(byte_array.get(), 2); |
92 if (bytes_read < 2) { | 93 if (bytes_read < 2) { |
93 break; | 94 break; |
94 } | 95 } |
95 int16_t value = byte_array[1]; | 96 int16_t value = byte_array[1]; |
96 value <<= 8; | 97 value <<= 8; |
97 value += byte_array[0]; | 98 value += byte_array[0]; |
98 buffer[int16s_read] = value; | 99 buffer[int16s_read] = value; |
99 ++int16s_read; | 100 ++int16s_read; |
100 } | 101 } |
101 | 102 |
102 return int16s_read; | 103 return int16s_read; |
103 } | 104 } |
104 | 105 |
105 size_t ReadInt16FromFileToFloatBuffer(FileWrapper* file, | 106 size_t ReadInt16FromFileToFloatBuffer(FileWrapper* file, |
106 size_t length, | 107 size_t length, |
107 float* buffer) { | 108 float* buffer) { |
108 if (!file || !file->Open() || !buffer || length <= 0) { | 109 if (!file || !file->Open() || !buffer || length <= 0) { |
109 return 0; | 110 return 0; |
110 } | 111 } |
111 | 112 |
112 rtc::scoped_ptr<int16_t[]> buffer16(new int16_t[length]); | 113 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]); |
113 | 114 |
114 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get()); | 115 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get()); |
115 | 116 |
116 for (size_t i = 0; i < int16s_read; ++i) { | 117 for (size_t i = 0; i < int16s_read; ++i) { |
117 buffer[i] = buffer16[i]; | 118 buffer[i] = buffer16[i]; |
118 } | 119 } |
119 | 120 |
120 return int16s_read; | 121 return int16s_read; |
121 } | 122 } |
122 | 123 |
123 size_t ReadInt16FromFileToDoubleBuffer(FileWrapper* file, | 124 size_t ReadInt16FromFileToDoubleBuffer(FileWrapper* file, |
124 size_t length, | 125 size_t length, |
125 double* buffer) { | 126 double* buffer) { |
126 if (!file || !file->Open() || !buffer || length <= 0) { | 127 if (!file || !file->Open() || !buffer || length <= 0) { |
127 return 0; | 128 return 0; |
128 } | 129 } |
129 | 130 |
130 rtc::scoped_ptr<int16_t[]> buffer16(new int16_t[length]); | 131 std::unique_ptr<int16_t[]> buffer16(new int16_t[length]); |
131 | 132 |
132 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get()); | 133 size_t int16s_read = ReadInt16BufferFromFile(file, length, buffer16.get()); |
133 | 134 |
134 for (size_t i = 0; i < int16s_read; ++i) { | 135 for (size_t i = 0; i < int16s_read; ++i) { |
135 buffer[i] = buffer16[i]; | 136 buffer[i] = buffer16[i]; |
136 } | 137 } |
137 | 138 |
138 return int16s_read; | 139 return int16s_read; |
139 } | 140 } |
140 | 141 |
141 size_t ReadFloatBufferFromFile(FileWrapper* file, | 142 size_t ReadFloatBufferFromFile(FileWrapper* file, |
142 size_t length, | 143 size_t length, |
143 float* buffer) { | 144 float* buffer) { |
144 if (!file || !file->Open() || !buffer || length <= 0) { | 145 if (!file || !file->Open() || !buffer || length <= 0) { |
145 return 0; | 146 return 0; |
146 } | 147 } |
147 | 148 |
148 rtc::scoped_ptr<uint8_t[]> byte_array(new uint8_t[4]); | 149 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]); |
149 | 150 |
150 size_t floats_read = 0; | 151 size_t floats_read = 0; |
151 | 152 |
152 while (floats_read < length) { | 153 while (floats_read < length) { |
153 size_t bytes_read = file->Read(byte_array.get(), 4); | 154 size_t bytes_read = file->Read(byte_array.get(), 4); |
154 if (bytes_read < 4) { | 155 if (bytes_read < 4) { |
155 break; | 156 break; |
156 } | 157 } |
157 ConvertByteArrayToFloat(byte_array.get(), &buffer[floats_read]); | 158 ConvertByteArrayToFloat(byte_array.get(), &buffer[floats_read]); |
158 ++floats_read; | 159 ++floats_read; |
159 } | 160 } |
160 | 161 |
161 return floats_read; | 162 return floats_read; |
162 } | 163 } |
163 | 164 |
164 size_t ReadDoubleBufferFromFile(FileWrapper* file, | 165 size_t ReadDoubleBufferFromFile(FileWrapper* file, |
165 size_t length, | 166 size_t length, |
166 double* buffer) { | 167 double* buffer) { |
167 if (!file || !file->Open() || !buffer || length <= 0) { | 168 if (!file || !file->Open() || !buffer || length <= 0) { |
168 return 0; | 169 return 0; |
169 } | 170 } |
170 | 171 |
171 rtc::scoped_ptr<uint8_t[]> byte_array(new uint8_t[8]); | 172 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]); |
172 | 173 |
173 size_t doubles_read = 0; | 174 size_t doubles_read = 0; |
174 | 175 |
175 while (doubles_read < length) { | 176 while (doubles_read < length) { |
176 size_t bytes_read = file->Read(byte_array.get(), 8); | 177 size_t bytes_read = file->Read(byte_array.get(), 8); |
177 if (bytes_read < 8) { | 178 if (bytes_read < 8) { |
178 break; | 179 break; |
179 } | 180 } |
180 ConvertByteArrayToDouble(byte_array.get(), &buffer[doubles_read]); | 181 ConvertByteArrayToDouble(byte_array.get(), &buffer[doubles_read]); |
181 ++doubles_read; | 182 ++doubles_read; |
182 } | 183 } |
183 | 184 |
184 return doubles_read; | 185 return doubles_read; |
185 } | 186 } |
186 | 187 |
187 size_t WriteInt16BufferToFile(FileWrapper* file, | 188 size_t WriteInt16BufferToFile(FileWrapper* file, |
188 size_t length, | 189 size_t length, |
189 const int16_t* buffer) { | 190 const int16_t* buffer) { |
190 if (!file || !file->Open() || !buffer || length <= 0) { | 191 if (!file || !file->Open() || !buffer || length <= 0) { |
191 return 0; | 192 return 0; |
192 } | 193 } |
193 | 194 |
194 rtc::scoped_ptr<uint8_t[]> byte_array(new uint8_t[2]); | 195 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[2]); |
195 | 196 |
196 size_t int16s_written = 0; | 197 size_t int16s_written = 0; |
197 | 198 |
198 for (int16s_written = 0; int16s_written < length; ++int16s_written) { | 199 for (int16s_written = 0; int16s_written < length; ++int16s_written) { |
199 // Get byte representation. | 200 // Get byte representation. |
200 byte_array[0] = buffer[int16s_written] & 0xFF; | 201 byte_array[0] = buffer[int16s_written] & 0xFF; |
201 byte_array[1] = (buffer[int16s_written] >> 8) & 0xFF; | 202 byte_array[1] = (buffer[int16s_written] >> 8) & 0xFF; |
202 | 203 |
203 file->Write(byte_array.get(), 2); | 204 file->Write(byte_array.get(), 2); |
204 } | 205 } |
205 | 206 |
206 file->Flush(); | 207 file->Flush(); |
207 | 208 |
208 return int16s_written; | 209 return int16s_written; |
209 } | 210 } |
210 | 211 |
211 size_t WriteFloatBufferToFile(FileWrapper* file, | 212 size_t WriteFloatBufferToFile(FileWrapper* file, |
212 size_t length, | 213 size_t length, |
213 const float* buffer) { | 214 const float* buffer) { |
214 if (!file || !file->Open() || !buffer || length <= 0) { | 215 if (!file || !file->Open() || !buffer || length <= 0) { |
215 return 0; | 216 return 0; |
216 } | 217 } |
217 | 218 |
218 rtc::scoped_ptr<uint8_t[]> byte_array(new uint8_t[4]); | 219 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[4]); |
219 | 220 |
220 size_t floats_written = 0; | 221 size_t floats_written = 0; |
221 | 222 |
222 for (floats_written = 0; floats_written < length; ++floats_written) { | 223 for (floats_written = 0; floats_written < length; ++floats_written) { |
223 // Get byte representation. | 224 // Get byte representation. |
224 ConvertFloatToByteArray(buffer[floats_written], byte_array.get()); | 225 ConvertFloatToByteArray(buffer[floats_written], byte_array.get()); |
225 | 226 |
226 file->Write(byte_array.get(), 4); | 227 file->Write(byte_array.get(), 4); |
227 } | 228 } |
228 | 229 |
229 file->Flush(); | 230 file->Flush(); |
230 | 231 |
231 return floats_written; | 232 return floats_written; |
232 } | 233 } |
233 | 234 |
234 size_t WriteDoubleBufferToFile(FileWrapper* file, | 235 size_t WriteDoubleBufferToFile(FileWrapper* file, |
235 size_t length, | 236 size_t length, |
236 const double* buffer) { | 237 const double* buffer) { |
237 if (!file || !file->Open() || !buffer || length <= 0) { | 238 if (!file || !file->Open() || !buffer || length <= 0) { |
238 return 0; | 239 return 0; |
239 } | 240 } |
240 | 241 |
241 rtc::scoped_ptr<uint8_t[]> byte_array(new uint8_t[8]); | 242 std::unique_ptr<uint8_t[]> byte_array(new uint8_t[8]); |
242 | 243 |
243 size_t doubles_written = 0; | 244 size_t doubles_written = 0; |
244 | 245 |
245 for (doubles_written = 0; doubles_written < length; ++doubles_written) { | 246 for (doubles_written = 0; doubles_written < length; ++doubles_written) { |
246 // Get byte representation. | 247 // Get byte representation. |
247 ConvertDoubleToByteArray(buffer[doubles_written], byte_array.get()); | 248 ConvertDoubleToByteArray(buffer[doubles_written], byte_array.get()); |
248 | 249 |
249 file->Write(byte_array.get(), 8); | 250 file->Write(byte_array.get(), 8); |
250 } | 251 } |
251 | 252 |
252 file->Flush(); | 253 file->Flush(); |
253 | 254 |
254 return doubles_written; | 255 return doubles_written; |
255 } | 256 } |
256 | 257 |
257 } // namespace webrtc | 258 } // namespace webrtc |
OLD | NEW |