forked from Geonkick-Synthesizer/geonkick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportSoundData.cpp
177 lines (157 loc) · 5.29 KB
/
ExportSoundData.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
/**
* File name: ExportSoundData.cpp
* Project: Geonkick (A percussion synthesizer)
*
* Copyright (C) 2020 Iurie Nistor
*
* This file is part of Geonkick.
*
* GeonKick 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ExportSoundData.h"
#include <sndfile.h>
ExportSoundData::ExportSoundData(RkObject *parent,
const std::filesystem::path &file,
const std::vector<float> &data,
ExportFormat exportFormat)
: ExportAbstract(parent, file, exportFormat)
, soundData{data}
, exportSubformat{getDefaultSubformat()}
, sampleRate{Geonkick::defaultSampleRate}
, nChannels{2}
{
}
bool ExportSoundData::doExport()
{
SF_INFO sndinfo;
memset(&sndinfo, 0, sizeof(sndinfo));
sndinfo.samplerate = getSampleRate();
sndinfo.channels = numberOfChannels();
sndinfo.format = sfExportFormat();
auto tempBuffer = soundData;
sndinfo.frames = tempBuffer.size();
std::vector<gkick_real> dataBuffer;
if (sndinfo.channels == 2) {
dataBuffer.resize(2 * tempBuffer.size());
size_t k = 0;
while (k < tempBuffer.size()) {
dataBuffer[2 * k] = dataBuffer[2 * k + 1] = tempBuffer[k];
k++;
}
} else {
dataBuffer = std::move(tempBuffer);
}
if (dataBuffer.empty() || !sf_format_check(&sndinfo)) {
setError("error on exporting data");
return false;
}
auto filePath = getExportPath();
if (filePath.empty()) {
setError("wrong file name is empty");
return false;
}
SNDFILE *sndFile = sf_open(filePath.string().c_str(), SFM_WRITE, &sndinfo);
if (!sndFile) {
setError("can't open file " + filePath.string());
return false;
}
size_t n;
n = sf_write_float(sndFile, dataBuffer.data(), dataBuffer.size());
if (n != dataBuffer.size()) {
setError("error on exporting");
sf_close(sndFile);
return false;
}
sf_close(sndFile);
return true;
}
ExportSoundData::Subformat ExportSoundData::subformat() const
{
return exportSubformat;
}
void ExportSoundData::setSubformat(ExportSoundData::Subformat subFormat)
{
if (!validateSubformat(subFormat)) {
GEONKICK_LOG_ERROR("wrong subformat " << static_cast<int>(subFormat)
<< " for format "
<< static_cast<int>(format()));
} else {
exportSubformat = subFormat;
}
}
int ExportSoundData::getSampleRate() const
{
return sampleRate;
}
void ExportSoundData::setSmapleRate(int srate)
{
sampleRate = srate;
}
int ExportSoundData::numberOfChannels() const
{
return nChannels;
}
void ExportSoundData::setNumberOfChannels(int channels)
{
nChannels = channels;
}
bool ExportSoundData::validateSubformat(Subformat subFormat)
{
switch (format()) {
case ExportFormat::Flac:
return subFormat == Subformat::Flac16 || subFormat == Subformat::Flac24;
case ExportFormat::Wav:
return subFormat == Subformat::Wav16
|| subFormat == Subformat::Wav24
|| subFormat == Subformat::Wav32;
case ExportFormat::Ogg:
return subFormat == Subformat::Ogg;
default:
return false;
}
}
ExportSoundData::Subformat
ExportSoundData::getDefaultSubformat() const
{
switch (format()) {
case ExportFormat::Wav:
return Subformat::Wav16;
case ExportFormat::Ogg:
return Subformat::Ogg;
case ExportFormat::Flac:
default:
return Subformat::Flac16;
}
}
int ExportSoundData::sfExportFormat() const
{
switch (subformat())
{
case Subformat::Flac16:
return SF_FORMAT_FLAC | SF_FORMAT_PCM_16;
case Subformat::Flac24:
return SF_FORMAT_FLAC | SF_FORMAT_PCM_24;
case Subformat::Wav16:
return SF_FORMAT_WAV | SF_FORMAT_PCM_16;
case Subformat::Wav24:
return SF_FORMAT_WAV | SF_FORMAT_PCM_24;
case Subformat::Wav32:
return SF_FORMAT_WAV | SF_FORMAT_PCM_32;
case Subformat::Ogg:
return SF_FORMAT_OGG | SF_FORMAT_VORBIS;
default:
return SF_FORMAT_WAV | SF_FORMAT_PCM_24;
}
}