file_utils.h 4.8 KB

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