pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_ |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "system_wrappers/include/file_wrapper.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 17 | #include "typedefs.h" // NOLINT(build/include) |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 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. |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 26 | static_assert(sizeof(Dest) == sizeof(Source), |
| 27 | "Dest and Source have different sizes"); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 118 | #endif // MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_ |