blob: 618401761b4ae8ed349bd6f4b5a03e746f37bdd6 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_
12#define MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
14#include <string.h>
15
Karl Wiberg6a4d4112018-03-23 10:39:34 +010016#include "rtc_base/system/file_wrapper.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017
18namespace 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
22template <class Dest, class Source>
23inline Dest bit_cast(const Source& source) {
24 // A compile error here means your Dest and Source have different sizes.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000025 static_assert(sizeof(Dest) == sizeof(Source),
26 "Dest and Source have different sizes");
pbos@webrtc.org788acd12014-12-15 09:41:24 +000027
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.
36int 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.
41int 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.
46int 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.
51int 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.
56size_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.
64size_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.
72size_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.
80size_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.
86size_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.
95size_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.
103size_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.
111size_t WriteDoubleBufferToFile(FileWrapper* file,
112 size_t length,
113 const double* buffer);
114
115} // namespace webrtc
116
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200117#endif // MODULES_AUDIO_PROCESSING_TRANSIENT_FILE_UTILS_H_