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