-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.cpp
107 lines (95 loc) · 2.61 KB
/
operations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef _OPERATIONS_CPP_
#define _OPERATIONS_CPP_
#include <sys/stat.h>
#include "operations.h"
off_t get_size_of_file(const std::string &file_name)
{
return get_size_of_file(file_name.c_str());
}
off_t get_size_of_file(const char *file_name)
{
struct stat s;
stat(file_name, &s);
return s.st_size;
}
void get_channels(int32_t *buffer[],
int32_t num_channels,
int32_t bps,
int32_t is_signed,
int32_t number_of_samples,
std::ifstream &fin)
{
for(int ind = 0; ind < number_of_samples; ++ind)
{
int i = 0;
for(i = 0; i < num_channels; ++i)
{
if (bps == 8 && !is_signed)
{
uint8_t value;
fin.read((char*)&value, sizeof(value));
buffer[i][ind] = (int32_t)value - 0x80;
}
else if (bps == 8 && is_signed)
{
int8_t value;
fin.read((char*)&value, sizeof(value));
buffer[i][ind] = value;
}
else if (bps == 16 && !is_signed)
{
uint16_t value;
fin.read((char*)&value, sizeof(value));
buffer[i][ind] = (int32_t)value - 0x8000;
}
else if (bps == 16 && is_signed)
{
int16_t value;
fin.read((char*)&value, sizeof(value));
buffer[i][ind] = value;
}
else if (bps == 32 && !is_signed)
{
uint32_t value;
fin.read((char*)&value, sizeof(value));
buffer[i][ind] = (int32_t)value - 0x800000;
}
else if (bps == 32 && is_signed)
fin.read((char*)&buffer[i][ind], sizeof(buffer[i][ind]));
}
if (i != num_channels)
{
std::cout << "\nError!\nWrong number of samples!\n";
exit(1);
}
}
}
uint32_t get_wasted_bits(int32_t *channel, uint32_t blocksize)
{
uint32_t i, shift, x = 0;
for(i = 0; i < blocksize && !(x&1); i++)
{
x |= channel[i];
}
if(x == 0) {
shift = 0;
}
else {
for(shift = 0; !(x&1); shift++)
x >>= 1;
}
/* for(i = 1; i < blocksize; ++i)
std::cout << channel[i] << ' ';
std::cout << "\n\n"; */
if(shift > 0) {
for(i = 0; i < blocksize; i++)
channel[i] >>= shift;
}
/* for(i = 1; i < blocksize; ++i)
std::cout << channel[i] << ' ';
std::cout << '\n'; */
/* if (channel[i] != channel[i - 1])
std::cout << "i = " << i << '\n'; */
return shift;
}
#endif