-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsftrimmer.cpp
242 lines (199 loc) · 8.75 KB
/
sftrimmer.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
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
#include <cstring>
#include <SF.h>
#include <sndfile.h>
using namespace std;
const int LOOP_PADDING_POINTS = 16;
const int SAMPLE_ZERO_PADDING_POINTS = 46;
const int LENGTH_OF_SAMPLE_NAME = 20;
const int SAMPLE_HEADER_SIZE = 46;
string GetSampleType(uint16_t type)
{
switch (type)
{
case sf2::Sample::MONO_SAMPLE:
return "Mono Sample";
case sf2::Sample::RIGHT_SAMPLE:
return "Right Sample";
case sf2::Sample::LEFT_SAMPLE:
return "Left Sample";
case sf2::Sample::LINKED_SAMPLE:
return "Linked Sample";
case sf2::Sample::ROM_MONO_SAMPLE:
return "ROM Mono Sample";
case sf2::Sample::ROM_RIGHT_SAMPLE:
return "ROM Right Sample";
case sf2::Sample::ROM_LEFT_SAMPLE:
return "ROM Left Sample";
case sf2::Sample::ROM_LINKED_SAMPLE:
return "ROM Linked Sample";
default:
return "Unknown";
}
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
cout << "Input and output arguments required" << endl;
return EXIT_FAILURE;
}
try
{
typedef struct __attribute__ ((__packed__)) new_offsets
{
uint32_t Start;
uint32_t End;
uint32_t StartLoop;
uint32_t EndLoop;
} new_offsets_t;
std::vector<uint16_t*> modified_samples;
std::vector<size_t> size_of_individual_modified_samples;
std::vector<new_offsets_t*> newoffsets_vector;
size_t total_size_of_modified_data = 0;
RIFF::File *riff = new RIFF::File(argv[1]);
sf2::File *sf = new sf2::File(riff);
for (int i = 0; i < sf->GetSampleCount(); i++)
{
sf2::Sample *s = sf->GetSample(i);
uint depth = (s->GetFrameSize() / s->GetChannelCount()) * 8;
cout << std::dec << "Processing sample " << i << endl;
cout << "\t" << s->Name << " (Depth: " << depth;
cout << ", SampleRate: " << s->SampleRate;
cout << ", Pitch: " << ((int)s->OriginalPitch);
cout << ", Pitch Correction: " << ((int)s->PitchCorrection) << endl;
cout << "\t\tStart: " << s->Start << ", End: " << s->End;
cout << ", Start Loop: " << s->StartLoop << ", End Loop: " << s->EndLoop << endl;
cout << "\t\tSample Type: " << GetSampleType(s->SampleType) << ", Sample Link: " << s->SampleLink << ")" << endl;
// Check the validity of the loop offsets
// Check that there are least 8 sample points between
// the sample start and the loop start
if ((s->StartLoop - s->Start) < 8)
{
cout << "Insufficient sample points before loop start" << endl;
continue;
}
// Check that there are least 8 sample points between
// the sample end and the loop end.
if ((s->End - s->EndLoop) < 8)
{
cout << "Insufficient sample points after loop end" << endl;
continue;
}
// Correct the sample offsets LoadSampleData loads the sample into the start of the buffer.
size_t SampleStartLoop = s->StartLoop - s->Start;
size_t SampleEndLoop = s->EndLoop - s->Start;
size_t SampleEnd = s->End - s->Start;
size_t LoopSize = SampleEndLoop - SampleStartLoop;
// Get the sample data
sf2::Sample::buffer_t sample_data = s->LoadSampleData();
long frame_count = s->GetTotalFrameCount();
// Only handle 16 bit samples for now
if (16 != depth)
{
cout << "Sample is not 16 bit" << endl;
continue;
}
uint16_t* samples16bit = (uint16_t*)sample_data.pStart;
/*
Ideal loop layout
=================
1. 8 sample points before loop start sample point, the last 4 sample points of this section
should be identical to the 4 sample points before the loop end sample.
2. loop start sample point, this should be identical to the loop end sample point.
3. loop sample points.
5. loop end sample point.
6. 8 sample points after loop end sample point, the first 4 sample points of this section
should be identical to the 4 sample points after the loop start sample point.
7. 46 zero sample points.
This implementation looks at the 9 samples around the loop start and loop end points, It overwrites
these points with the mean of the the two corresponding samples.
*/
// Prepare to update the sample header
new_offsets_t* newoffsets = new new_offsets_t;
newoffsets_vector.push_back(newoffsets);
newoffsets->Start = total_size_of_modified_data;
// Allocate data to hold the modified sample
size_t modified_data_length = SampleEndLoop - SampleStartLoop + LOOP_PADDING_POINTS + SAMPLE_ZERO_PADDING_POINTS;
newoffsets->End = newoffsets->Start + modified_data_length - SAMPLE_ZERO_PADDING_POINTS;
newoffsets->StartLoop = newoffsets->Start + 8;
newoffsets->EndLoop = newoffsets->End - 8;
cout << "NewOffsets" << endl
<< "Start " << newoffsets->Start
<< " End " << newoffsets->End
<< " StartLoop " << newoffsets->StartLoop
<< " EndLoop " << newoffsets->EndLoop << endl;
size_of_individual_modified_samples.push_back(modified_data_length);
total_size_of_modified_data += modified_data_length;
uint16_t* modified_data = new uint16_t[modified_data_length];
if (NULL == modified_data)
{
cout << "Unable to allocate memory for modified sample" << endl;
exit(-1);
}
// Copy the sample data
std::memset(modified_data, 0, modified_data_length * 2);
std::memcpy(modified_data, samples16bit + SampleStartLoop - 8 , (modified_data_length - SAMPLE_ZERO_PADDING_POINTS) * 2);
// Recalculate the sample point
size_t newSampleStart = 0;
size_t newSampleStartLoop = 8;
size_t newSampleEndLoop = newSampleStartLoop + LoopSize;
size_t newEnd = modified_data_length;
// Fix the loop points
for (int i = newSampleStartLoop - 4; i < 9; i++)
modified_data[i] = modified_data[i + LoopSize] = (modified_data[i] + modified_data[i + LoopSize]) / 2;
modified_samples.push_back(modified_data);
s->ReleaseSampleData();
}
cout << "Total size of modified sample data " << total_size_of_modified_data << endl;
// Coalesce the modified samples
size_t coalesced_samples_index = 0;
uint16_t* coalesced_samples = new uint16_t[total_size_of_modified_data];
if (NULL == coalesced_samples)
{
cout << "Unable to allocate memory for coalesced samples" << endl;
exit(-1);
}
for (uint i = 0; i < modified_samples.size(); i++)
{
size_t size = size_of_individual_modified_samples[i];
memcpy(coalesced_samples + coalesced_samples_index, modified_samples[i], size * 2);
coalesced_samples_index += size;
}
// Replace the SMPL-CK chunk with one pointing at the new data
RIFF::Chunk* pChunk = sf->GetSample(0)->pCkSmpl;
RIFF::List* pList = pChunk->GetParent();
pChunk->Resize(total_size_of_modified_data * 2);
pChunk->GetFile()->Save(argv[2]);
// Save the sample data into soundfont object
pChunk->WriteInt16((int16_t*)coalesced_samples, total_size_of_modified_data);
// Modify the sample headers
RIFF::File* pRiff = sf->GetRiffFile();
RIFF::List* pPDTA = pRiff->GetSubList(LIST_TYPE_PDTA);
RIFF::Chunk* pSHDR = pPDTA->GetSubChunk(CHUNK_ID_SHDR);
uint8_t* pData = (uint8_t*)pSHDR->LoadChunkData();
pData += LENGTH_OF_SAMPLE_NAME;
for (uint i = 0; i < modified_samples.size(); i++)
{
// Get the new offset data
new_offsets_t* n = newoffsets_vector[i];
// Patch the chunk data
std::memcpy(pData, n, sizeof(new_offsets_t));
pData += SAMPLE_HEADER_SIZE;
}
pSHDR->GetFile()->Save(argv[2]);
pSHDR->ReleaseChunkData();
delete sf;
delete riff;
}
catch (RIFF::Exception e)
{
e.PrintMessage();
return EXIT_FAILURE;
}
catch (...)
{
cout << "Unknown exception while trying to parse file." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}