forked from miek/inspectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputsource.cpp
191 lines (159 loc) · 5.54 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
/*
* Copyright (C) 2015, Mike Walters <[email protected]>
* Copyright (C) 2015, Jared Boone <[email protected]>
*
* 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 <math.h>
#include <stdio.h>
#include <string.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, size_t start, size_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, size_t start, size_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, size_t start, size_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, size_t start, size_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() - 127.4f) * k, (v.imag() - 127.4f) * k };
}
);
}
};
InputSource::InputSource()
{
}
InputSource::~InputSource()
{
cleanup();
}
void InputSource::cleanup()
{
if (mmapData != nullptr) {
inputFile->unmap(mmapData);
mmapData = nullptr;
}
if (inputFile != nullptr) {
delete inputFile;
inputFile = nullptr;
}
}
void InputSource::openFile(const char *filename)
{
QFileInfo fileInfo(filename);
std::string suffix = std::string(fileInfo.suffix().toLower().toUtf8().constData());
if(_fmt!=""){ suffix = _fmt; } // allow fmt override
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());
}
std::unique_ptr<QFile> file(new QFile(filename));
if (!file->open(QFile::ReadOnly)) {
throw std::runtime_error(file->errorString().toStdString());
}
auto size = file->size();
sampleCount = size / sampleAdapter->sampleSize();
auto data = file->map(0, size);
if (data == nullptr)
throw std::runtime_error("Error mmapping file");
cleanup();
inputFile = file.release();
mmapData = data;
invalidate();
}
void InputSource::setSampleRate(size_t rate)
{
sampleRate = rate;
invalidate();
}
size_t InputSource::rate()
{
return sampleRate;
}
std::unique_ptr<std::complex<float>[]> InputSource::getSamples(size_t start, size_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;
}
void InputSource::setFormat(std::string fmt){
_fmt = fmt;
}