-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_pool.cpp
More file actions
262 lines (228 loc) · 7.25 KB
/
Copy pathsample_pool.cpp
File metadata and controls
262 lines (228 loc) · 7.25 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "sample_pool.h"
#include <wx/ffile.h>
#include <wx/filename.h>
#include <wx/datetime.h>
#include <algorithm>
#include <cstring>
#include <vector>
namespace
{
unsigned short ReadU16(const unsigned char* p)
{
return static_cast<unsigned short>(p[0] | (p[1] << 8));
}
unsigned int ReadU32(const unsigned char* p)
{
return static_cast<unsigned int>(p[0]) |
(static_cast<unsigned int>(p[1]) << 8) |
(static_cast<unsigned int>(p[2]) << 16) |
(static_cast<unsigned int>(p[3]) << 24);
}
}
bool SamplePool::AddWav(const wxString& filePath,
SamplePoolItem* addedItem,
wxString* errorMessage)
{
WavMetadata metadata;
if (!ReadWavMetadata(filePath, &metadata, errorMessage))
return false;
wxFileName normalized(filePath);
normalized.Normalize(wxPATH_NORM_ABSOLUTE | wxPATH_NORM_DOTS);
const wxString fullPath = normalized.GetFullPath();
for (size_t i = 0; i < m_items.size(); ++i)
{
if (m_items[i].filePath.CmpNoCase(fullPath) == 0)
{
if (errorMessage)
*errorMessage = wxT("This WAV is already in the Sample Pool.");
return false;
}
}
SamplePoolItem item;
item.id = MakeStableId(fullPath);
item.filePath = fullPath;
item.displayName = normalized.GetFullName();
item.wav = metadata;
m_items.push_back(item);
if (addedItem)
*addedItem = item;
return true;
}
bool SamplePool::AddFestivalWav(const wxString& filePath,
int sourceEventIndex,
const SingingEvent& sourceEvent,
const wxString& sourceVoice,
double sourceBpm,
SamplePoolItem* addedItem,
wxString* errorMessage)
{
SamplePoolItem item;
if (!AddWav(filePath, &item, errorMessage))
return false;
SamplePoolItem& stored = m_items.back();
stored.generatedByFestival = true;
stored.sourceEventIndex = sourceEventIndex;
stored.sourcePitch = sourceEvent.pitch;
stored.sourcePhoneme = sourceEvent.phoneme;
stored.sourceBeats = sourceEvent.beats;
stored.sourceVoice = sourceVoice;
stored.sourceBpm = sourceBpm;
if (addedItem)
*addedItem = stored;
return true;
}
bool SamplePool::RemoveAt(size_t index)
{
if (index >= m_items.size())
return false;
m_items.erase(m_items.begin() + index);
return true;
}
void SamplePool::Clear()
{
m_items.clear();
}
size_t SamplePool::GetCount() const
{
return m_items.size();
}
const SamplePoolItem* SamplePool::GetAt(size_t index) const
{
if (index >= m_items.size())
return NULL;
return &m_items[index];
}
SamplePoolItem* SamplePool::GetAtMutable(size_t index)
{
if (index >= m_items.size())
return NULL;
return &m_items[index];
}
bool SamplePool::ReadWavMetadata(const wxString& filePath,
WavMetadata* metadata,
wxString* errorMessage) const
{
if (!metadata)
return false;
wxFFile file(filePath, wxT("rb"));
if (!file.IsOpened())
{
if (errorMessage)
*errorMessage = wxT("Unable to open the selected file.");
return false;
}
unsigned char header[12];
if (file.Read(header, sizeof(header)) != sizeof(header) ||
memcmp(header, "RIFF", 4) != 0 ||
memcmp(header + 8, "WAVE", 4) != 0)
{
if (errorMessage)
*errorMessage = wxT("The selected file is not a valid RIFF/WAVE file.");
return false;
}
bool foundFormat = false;
bool foundData = false;
WavMetadata result;
while (!file.Eof())
{
unsigned char chunkHeader[8];
if (file.Read(chunkHeader, sizeof(chunkHeader)) != sizeof(chunkHeader))
break;
const unsigned int chunkSize = ReadU32(chunkHeader + 4);
const wxString chunkId(reinterpret_cast<const char*>(chunkHeader),
wxConvISO8859_1, 4);
if (chunkId == wxT("fmt "))
{
if (chunkSize < 16)
{
if (errorMessage)
*errorMessage = wxT("Invalid WAV format chunk.");
return false;
}
std::vector<unsigned char> format(chunkSize);
if (file.Read(&format[0], chunkSize) != chunkSize)
{
if (errorMessage)
*errorMessage = wxT("The WAV format chunk is truncated.");
return false;
}
result.audioFormat = ReadU16(&format[0]);
result.channelCount = ReadU16(&format[2]);
result.sampleRate = ReadU32(&format[4]);
result.bitsPerSample = ReadU16(&format[14]);
foundFormat = true;
}
else if (chunkId == wxT("data"))
{
result.dataBytes = chunkSize;
result.dataOffset = static_cast<unsigned long long>(file.Tell());
foundData = true;
if (!file.Seek(chunkSize, wxFromCurrent))
break;
}
else
{
if (!file.Seek(chunkSize, wxFromCurrent))
break;
}
if ((chunkSize & 1U) != 0)
file.Seek(1, wxFromCurrent);
}
if (!foundFormat || !foundData || result.channelCount == 0 ||
result.sampleRate == 0 || result.bitsPerSample == 0)
{
if (errorMessage)
*errorMessage = wxT("The WAV does not contain usable format and audio data chunks.");
return false;
}
if (result.audioFormat != 1 && result.audioFormat != 3)
{
if (errorMessage)
*errorMessage = wxString::Format(
wxT("Unsupported WAV encoding (%u). PCM and IEEE float are accepted."),
static_cast<unsigned int>(result.audioFormat));
return false;
}
const unsigned int bytesPerFrame =
static_cast<unsigned int>(result.channelCount) *
static_cast<unsigned int>(result.bitsPerSample / 8);
if (bytesPerFrame == 0)
{
if (errorMessage)
*errorMessage = wxT("Invalid WAV sample size.");
return false;
}
result.frameCount = result.dataBytes / bytesPerFrame;
result.durationSeconds =
static_cast<double>(result.frameCount) /
static_cast<double>(result.sampleRate);
*metadata = result;
return true;
}
wxString SamplePool::MakeStableId(const wxString& filePath) const
{
unsigned long hash = 2166136261UL;
for (size_t i = 0; i < filePath.length(); ++i)
{
hash ^= static_cast<unsigned long>(filePath[i].GetValue());
hash *= 16777619UL;
}
wxString candidate = wxString::Format(wxT("sample-%08lx"), hash);
wxString unique = candidate;
unsigned int suffix = 2;
bool collision = true;
while (collision)
{
collision = false;
for (size_t i = 0; i < m_items.size(); ++i)
{
if (m_items[i].id == unique)
{
collision = true;
unique = wxString::Format(wxT("%s-%u"), candidate.c_str(), suffix++);
break;
}
}
}
return unique;
}