aqnwb 0.1.0
Loading...
Searching...
No Matches
Utils.hpp
Go to the documentation of this file.
1#include <algorithm>
2#include <chrono>
3#include <cmath>
4#include <cstdint>
5#include <ctime>
6#include <iomanip>
7#include <sstream>
8
9#include <boost/date_time.hpp>
10#include <boost/endian/conversion.hpp>
11#include <boost/uuid/uuid.hpp>
12#include <boost/uuid/uuid_generators.hpp>
13#include <boost/uuid/uuid_io.hpp>
14
15#include "BaseIO.hpp"
16#include "boost/date_time/c_local_time_adjustor.hpp"
17#include "hdf5/HDF5IO.hpp"
18
19namespace AQNWB
20{
25inline std::string generateUuid()
26{
27 boost::uuids::uuid uuid = boost::uuids::random_generator()();
28 std::string uuidStr = boost::uuids::to_string(uuid);
29
30 return uuidStr;
31}
32
37inline std::string getCurrentTime()
38{
39 // Set up boost time zone adjustment and time facet
40 using local_adj =
41 boost::date_time::c_local_adjustor<boost::posix_time::ptime>;
42 boost::posix_time::time_facet* f = new boost::posix_time::time_facet();
43 f->time_duration_format("%+%H:%M");
44
45 // get local time, utc time, and offset
46 auto now = boost::posix_time::microsec_clock::universal_time();
47 auto utc_now = local_adj::utc_to_local(now);
48 boost::posix_time::time_duration td = utc_now - now;
49
50 // Format the date and time in ISO 8601 format with the UTC offset
51 std::ostringstream oss_offset;
52 oss_offset.imbue(std::locale(oss_offset.getloc(), f));
53 oss_offset << td;
54
55 std::string currentTime = to_iso_extended_string(utc_now);
56 currentTime += oss_offset.str();
57
58 return currentTime;
59}
60
65inline std::shared_ptr<BaseIO> createIO(const std::string& type,
66 const std::string& filename)
67{
68 if (type == "HDF5") {
69 return std::make_shared<HDF5::HDF5IO>(filename);
70 } else {
71 throw std::invalid_argument("Invalid IO type");
72 }
73}
74
83inline void convertFloatToInt16LE(const float* source,
84 void* dest,
85 int numSamples)
86{
87 // TODO - several steps in this function may be unnecessary for our use
88 // case. Consider simplifying the intermediate cast to char and the
89 // final cast to uint16_t.
90 auto maxVal = static_cast<double>(0x7fff);
91 auto intData = static_cast<char*>(dest);
92
93 for (int i = 0; i < numSamples; ++i) {
94 auto clampedValue = std::clamp(maxVal * source[i], -maxVal, maxVal);
95 auto intValue =
96 static_cast<uint16_t>(static_cast<int16_t>(std::round(clampedValue)));
97 intValue = boost::endian::native_to_little(intValue);
98 *reinterpret_cast<uint16_t*>(intData) = intValue;
99 intData += 2; // destBytesPerSample is always 2
100 }
101}
102
109inline std::unique_ptr<int16_t[]> transformToInt16(SizeType numSamples,
110 float conversion_factor,
111 const float* data)
112{
113 std::unique_ptr<float[]> scaledData = std::make_unique<float[]>(numSamples);
114 std::unique_ptr<int16_t[]> intData = std::make_unique<int16_t[]>(numSamples);
115
116 // copy data and multiply by scaling factor
117 double multFactor = 1 / (32767.0f * conversion_factor);
118 std::transform(data,
119 data + numSamples,
120 scaledData.get(),
121 [multFactor](float value) { return value * multFactor; });
122
123 // convert float to int16
124 convertFloatToInt16LE(scaledData.get(), intData.get(), numSamples);
125
126 return intData;
127}
128} // namespace AQNWB
AQNWB::Types::SizeType SizeType
Definition BaseIO.hpp:16
The main namespace for AqNWB.
std::unique_ptr< int16_t[]> transformToInt16(SizeType numSamples, float conversion_factor, const float *data)
Method to scale float values and convert to int16 values.
Definition Utils.hpp:109
std::shared_ptr< BaseIO > createIO(const std::string &type, const std::string &filename)
Factory method to create an IO object.
Definition Utils.hpp:65
std::string generateUuid()
Generates a UUID (Universally Unique Identifier) as a string.
Definition Utils.hpp:25
void convertFloatToInt16LE(const float *source, void *dest, int numSamples)
Method to convert float values to uint16 values. This method was adapted from JUCE AudioDataConverter...
Definition Utils.hpp:83
std::string getCurrentTime()
Get the current time in ISO 8601 format with the UTC offset.
Definition Utils.hpp:37