mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
added int to float and float to int helpers
This commit is contained in:
parent
8c2e721b32
commit
8246c5b5dd
1 changed files with 22 additions and 7 deletions
29
wav.hh
29
wav.hh
|
|
@ -35,6 +35,15 @@ class ReadWAV : public ReadPCM<TYPE>
|
|||
return true;
|
||||
return false;
|
||||
}
|
||||
static float int2float(int value)
|
||||
{
|
||||
union {
|
||||
int i;
|
||||
float f;
|
||||
} u;
|
||||
u.i = value;
|
||||
return u.f;
|
||||
}
|
||||
public:
|
||||
ReadWAV(const char *name) : is(name, std::ios::binary)
|
||||
{
|
||||
|
|
@ -123,15 +132,13 @@ public:
|
|||
stride = channels_;
|
||||
for (int n = 0; n < num; ++n) {
|
||||
for (int c = 0; c < channels_; ++c) {
|
||||
if (bytes == 4) {
|
||||
int v = readLE(4);
|
||||
buf[stride * n + c] = *reinterpret_cast<float *>(&v);
|
||||
} else {
|
||||
if (bytes == 4)
|
||||
buf[stride * n + c] = int2float(readLE(4));
|
||||
else
|
||||
buf[stride * n + c] = TYPE(readLE(bytes) - offset) / TYPE(factor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bool good()
|
||||
{
|
||||
return is.good();
|
||||
|
|
@ -169,6 +176,15 @@ class WriteWAV : public WritePCM<TYPE>
|
|||
for (int i = 0; i < b; ++i)
|
||||
os.put(255 & (v >> (8 * i)));
|
||||
}
|
||||
static int float2int(float value)
|
||||
{
|
||||
union {
|
||||
float f;
|
||||
int i;
|
||||
} u;
|
||||
u.f = value;
|
||||
return u.i;
|
||||
}
|
||||
public:
|
||||
WriteWAV(const char *name, int rate, int bits, int channels) :
|
||||
os(name, std::ios::binary | std::ios::trunc),
|
||||
|
|
@ -242,8 +258,7 @@ public:
|
|||
for (int n = 0; n < num; ++n) {
|
||||
for (int c = 0; c < channels_; ++c) {
|
||||
if (bytes == 4) {
|
||||
float v = buf[stride * n + c];
|
||||
writeLE(*reinterpret_cast<int *>(&v), 4);
|
||||
writeLE(float2int(buf[stride * n + c]), 4);
|
||||
} else {
|
||||
TYPE v = TYPE(offset) + TYPE(factor) * buf[stride * n + c];
|
||||
writeLE(std::nearbyint(std::min(std::max(v, TYPE(min)), TYPE(max))), bytes);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue