forked from miek/inspectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputsource.cpp
197 lines (166 loc) · 5.69 KB
/
inputsource.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (C) 2015, Mike Walters <mike@flomp.net>
* Copyright (C) 2015, Jared Boone <jared@sharebrained.com>
*
* This file is part of inspectrum.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "inputsource.h"
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <stdexcept>
#include <algorithm>
#include <QFileInfo>
class ComplexF32SampleAdapter : public SampleAdapter {
public:
size_t sampleSize() override {
return sizeof(std::complex<float>);
}
void copyRange(const void* const src, off_t start, off_t length, std::complex<float>* const dest) override {
auto s = reinterpret_cast<const std::complex<float>*>(src);
std::copy(&s[start], &s[start + length], dest);
}
};
class ComplexS16SampleAdapter : public SampleAdapter {
public:
size_t sampleSize() override {
return sizeof(std::complex<int16_t>);
}
void copyRange(const void* const src, off_t start, off_t length, std::complex<float>* const dest) override {
auto s = reinterpret_cast<const std::complex<int16_t>*>(src);
std::transform(&s[start], &s[start + length], dest,
[](const std::complex<int16_t>& v) -> std::complex<float> {
const float k = 1.0f / 32768.0f;
return { v.real() * k, v.imag() * k };
}
);
}
};
class ComplexS8SampleAdapter : public SampleAdapter {
public:
size_t sampleSize() override {
return sizeof(std::complex<int8_t>);
}
void copyRange(const void* const src, off_t start, off_t length, std::complex<float>* const dest) override {
auto s = reinterpret_cast<const std::complex<int8_t>*>(src);
std::transform(&s[start], &s[start + length], dest,
[](const std::complex<int8_t>& v) -> std::complex<float> {
const float k = 1.0f / 128.0f;
return { v.real() * k, v.imag() * k };
}
);
}
};
class ComplexU8SampleAdapter : public SampleAdapter {
public:
size_t sampleSize() override {
return sizeof(std::complex<uint8_t>);
}
void copyRange(const void* const src, off_t start, off_t length, std::complex<float>* const dest) override {
auto s = reinterpret_cast<const std::complex<uint8_t>*>(src);
std::transform(&s[start], &s[start + length], dest,
[](const std::complex<uint8_t>& v) -> std::complex<float> {
const float k = 1.0f / 128.0f;
return { v.real() * k - 1.0f, v.imag() * k - 1.0f };
}
);
}
};
InputSource::InputSource()
{
}
InputSource::~InputSource()
{
cleanup();
}
void InputSource::cleanup()
{
if (mmapData != nullptr) {
munmap(mmapData, fileSize);
mmapData = nullptr;
fileSize = 0;
}
if (inputFile != nullptr) {
fclose(inputFile);
inputFile = nullptr;
}
}
void InputSource::openFile(const char *filename)
{
QFileInfo fileInfo(filename);
const auto suffix = fileInfo.suffix().toLower();
if ((suffix == "cfile") || (suffix == "cf32") || (suffix == "fc32")) {
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexF32SampleAdapter());
}
else if ((suffix == "cs16") || (suffix == "sc16") || (suffix == "c16")) {
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexS16SampleAdapter());
}
else if ((suffix == "cs8") || (suffix == "sc8") || (suffix == "c8")) {
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexS8SampleAdapter());
}
else if ((suffix == "cu8") || (suffix == "uc8")) {
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexU8SampleAdapter());
}
else {
sampleAdapter = std::unique_ptr<SampleAdapter>(new ComplexF32SampleAdapter());
}
errno = 0;
FILE *file = fopen(filename, "rb");
if (file == nullptr) {
std::stringstream ss;
ss << "Error opening file: " << strerror(errno) << " (" << errno << ")";
throw std::runtime_error(ss.str());
}
struct stat sb;
if (fstat(fileno(file), &sb) != 0)
throw std::runtime_error("Error fstating file");
off_t size = sb.st_size;
sampleCount = size / sampleAdapter->sampleSize();
auto data = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(file), 0);
if (data == nullptr)
throw std::runtime_error("Error mmapping file");
cleanup();
inputFile = file;
fileSize = size;
mmapData = data;
invalidate();
}
void InputSource::setSampleRate(off_t rate)
{
sampleRate = rate;
invalidate();
}
off_t InputSource::rate()
{
return sampleRate;
}
std::unique_ptr<std::complex<float>[]> InputSource::getSamples(off_t start, off_t length)
{
if (inputFile == nullptr)
return nullptr;
if (mmapData == nullptr)
return nullptr;
if(start < 0 || length < 0)
return nullptr;
if (start + length > sampleCount)
return nullptr;
std::unique_ptr<std::complex<float>[]> dest(new std::complex<float>[length]);
sampleAdapter->copyRange(mmapData, start, length, dest.get());
return dest;
}