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