1 /* 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 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_ 12 #define WEBRTC_MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_ 13 14 #include <string.h> 15 16 #include "webrtc/system_wrappers/include/file_wrapper.h" 17 #include "webrtc/typedefs.h" 18 19 namespace webrtc { 20 21 // This is a copy of the cast included in the Chromium codebase here: 22 // http://cs.chromium.org/src/third_party/cld/base/casts.h 23 template <class Dest, class Source> 24 inline Dest bit_cast(const Source& source) { 25 // A compile error here means your Dest and Source have different sizes. 26 static_assert(sizeof(Dest) == sizeof(Source), 27 "Dest and Source have different sizes"); 28 29 Dest dest; 30 memcpy(&dest, &source, sizeof(dest)); 31 return dest; 32 } 33 34 // Converts the byte array with binary float representation to float. 35 // Bytes must be in little-endian order. 36 // Returns 0 if correct, -1 on error. 37 int ConvertByteArrayToFloat(const uint8_t bytes[4], float* out); 38 39 // Converts the byte array with binary double representation to double. 40 // Bytes must be in little-endian order. 41 // Returns 0 if correct, -1 on error. 42 int ConvertByteArrayToDouble(const uint8_t bytes[8], double* out); 43 44 // Converts a float to a byte array with binary float representation. 45 // Bytes will be in little-endian order. 46 // Returns 0 if correct, -1 on error. 47 int ConvertFloatToByteArray(float value, uint8_t out_bytes[4]); 48 49 // Converts a double to a byte array with binary double representation. 50 // Bytes will be in little-endian order. 51 // Returns 0 if correct, -1 on error. 52 int ConvertDoubleToByteArray(double value, uint8_t out_bytes[8]); 53 54 // Reads |length| 16-bit integers from |file| to |buffer|. 55 // |file| must be previously opened. 56 // Returns the number of 16-bit integers read or -1 on error. 57 size_t ReadInt16BufferFromFile(FileWrapper* file, 58 size_t length, 59 int16_t* buffer); 60 61 // Reads |length| 16-bit integers from |file| and stores those values 62 // (converting them) in |buffer|. 63 // |file| must be previously opened. 64 // Returns the number of 16-bit integers read or -1 on error. 65 size_t ReadInt16FromFileToFloatBuffer(FileWrapper* file, 66 size_t length, 67 float* buffer); 68 69 // Reads |length| 16-bit integers from |file| and stores those values 70 // (converting them) in |buffer|. 71 // |file| must be previously opened. 72 // Returns the number of 16-bit integers read or -1 on error. 73 size_t ReadInt16FromFileToDoubleBuffer(FileWrapper* file, 74 size_t length, 75 double* buffer); 76 77 // Reads |length| floats in binary representation (4 bytes) from |file| to 78 // |buffer|. 79 // |file| must be previously opened. 80 // Returns the number of floats read or -1 on error. 81 size_t ReadFloatBufferFromFile(FileWrapper* file, size_t length, float* buffer); 82 83 // Reads |length| doubles in binary representation (8 bytes) from |file| to 84 // |buffer|. 85 // |file| must be previously opened. 86 // Returns the number of doubles read or -1 on error. 87 size_t ReadDoubleBufferFromFile(FileWrapper* file, 88 size_t length, 89 double* buffer); 90 91 // Writes |length| 16-bit integers from |buffer| in binary representation (2 92 // bytes) to |file|. It flushes |file|, so after this call there are no 93 // writings pending. 94 // |file| must be previously opened. 95 // Returns the number of doubles written or -1 on error. 96 size_t WriteInt16BufferToFile(FileWrapper* file, 97 size_t length, 98 const int16_t* buffer); 99 100 // Writes |length| floats from |buffer| in binary representation (4 bytes) to 101 // |file|. It flushes |file|, so after this call there are no writtings pending. 102 // |file| must be previously opened. 103 // Returns the number of doubles written or -1 on error. 104 size_t WriteFloatBufferToFile(FileWrapper* file, 105 size_t length, 106 const float* buffer); 107 108 // Writes |length| doubles from |buffer| in binary representation (8 bytes) to 109 // |file|. It flushes |file|, so after this call there are no writings pending. 110 // |file| must be previously opened. 111 // Returns the number of doubles written or -1 on error. 112 size_t WriteDoubleBufferToFile(FileWrapper* file, 113 size_t length, 114 const double* buffer); 115 116 } // namespace webrtc 117 118 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_ 119