-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac_infile_mw2.cpp
278 lines (249 loc) · 7.76 KB
/
ac_infile_mw2.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* ac_infile_mw2.cpp - Definition of the Waldorf Microwave II/XT wave dump
* input class
* Copyright (C) 2021 Jeanette C. <jeanette@juliencoder.de>
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "ac_infile_mw2.hpp"
#include "ac_utility.hpp"
#include <cstring>
using std::string;
using std::ifstream;
using std::list;
using std::vector;
using std::strstr;
using std::to_string;
namespace JBS {
Ac_infile_mw2::Ac_infile_mw2(const string& filename):
Ac_infile_base(filename), its_sysex_string(nullptr), its_item_count(0), \
its_ready(false), its_file_size(0)
{}
Ac_infile_mw2::Ac_infile_mw2(const char *filename):
Ac_infile_base(filename), its_sysex_string(nullptr), its_item_count(0), \
its_ready(false), its_file_size(0)
{}
Ac_infile_mw2::~Ac_infile_mw2()
{
if (its_open == true)
{
close();
}
if (its_sysex_string != nullptr)
{
delete [] its_sysex_string;
}
}
// Open the file and initialise the item count
bool Ac_infile_mw2::open()
{
bool file_state = false;
if (its_open == true) // file is open, nothing to do
{
file_state = true;
}
else // File is not open
{
if (its_good == true) // file can be opened for writing
{
its_file.open(its_filename);
if (its_file.fail() == true)
{
its_good = false;
its_error_msg = string("Can't open file ") + its_filename + string(": unknown error.");
// Make sure that the file is closed
if (its_file.is_open() == true)
{
its_file.close();
}
}
else // File is OK
{
its_open = true;
read_data(); // read the SysEx string
count_items(); // if item count is 0 it's not a valid file
if (its_item_count == 0)
{
its_file.close();
its_open = false;
its_good = false;
its_error_msg = its_filename + string(" does not contain valid Microwave II/XT waveforms.");
}
else // The file is acceptable
{
its_open = true;
its_good = true;
file_state = true;
}
}
} // if (its_good == true) all OK to begin open
} // if (its_open == true)
return file_state;
}
// Close the connected file
void Ac_infile_mw2::close()
{
if (its_open == true)
{
its_file.close();
its_open = false;
its_ready = false;
}
}
// Read the SysEx string into internal data and verify the file format
void Ac_infile_mw2::read_data()
{
// Only begin if the file is open
if (its_open == true)
{
// Get the file size
its_file.seekg(0,std::ios_base::end); // go to end of file
std::streampos length = its_file.tellg(); // get the end position
its_file.seekg(0,std::ios_base::beg); // Go back to beginning
// Allocate storage for the complete data
its_sysex_string = new char[length];
// Read the whole file into the SysEx string
its_file.read(its_sysex_string, length);
// Check that the string contains the Microwave II/XT header
if (ac_check_header_mw2(its_sysex_string, length) != true)
{
its_good = false;
its_error_msg = its_filename + string(" is not a Microwave II/XT SysEx file.");
delete [] its_sysex_string;
its_sysex_string = nullptr;
}
else // it's a microwave II/XT file
{
its_file_size = length;
}
} // if (its_open == true)
}
// Count items in the file
void Ac_infile_mw2::count_items()
{
// Begin if object state is good and sysEx data is present
if (its_good == true)
{
if (its_sysex_string != nullptr)
{
const char mw_header[] = { char(0xf0), char(0x3e), char(0x0e) };
char *tmp_pos, *found_pos;
tmp_pos = its_sysex_string; // incremented in search
unsigned int found_index = 0;
unsigned int found_wave_number = 0;
// Search for waves to the end of the string
while ((found_pos = strstr(tmp_pos, mw_header)) != nullptr)
{
found_index = int(found_pos - its_sysex_string);
if ((found_index + 6) < its_file_size) // enough for wavd and location
{
// check for 0x12 WAVD (waveform dump) command
if (its_sysex_string[found_index + 4] == 0x12) // it's a wave
{
found_wave_number = int(its_sysex_string[found_index + 5] * 128 + its_sysex_string[found_index + 6]);
found_index += 7;
its_item_numbers.push_back(found_wave_number);
its_item_indices.push_back(found_index);
its_item_count++;
} // if found pos + 2 is a WAVD command
} // if ((found_index + 2) < its_file_size)
tmp_pos = ++found_pos;
} // end of while mw_header is found loop
} // if (its_sysex_string != nullptr)
} // if (its_good == true)
if (its_item_count >0) // waves were found, it's good
{
its_ready = true;
}
else // this file does not contain waves
{
its_good = false;
its_error_msg = its_filename + string(" does not contain any waveforms.");
}
}
// Return a list of wave names (either ROM#index or USER#index
list<string> Ac_infile_mw2::get_item_list()
{
list<string> wave_list;
string current_name;
// Only begin processing if its_ready is true and its_item_count >0
if (its_ready == true)
{
if (its_item_count >0) // if there are any waves
{
unsigned int list_size = its_item_count;
for (unsigned int item = 0;item<list_size;item++)
{
if (its_item_numbers[item] <300) // ROM wave
{
current_name = to_string(item) + string(" ROM#") + to_string(its_item_numbers[item] + 1);
}
else if (its_item_numbers[item] >= 1000)
{
current_name = to_string(item) + string(" USER#") + to_string(its_item_numbers[item] - 999);
}
else // must be an illegal wave
{
current_name = to_string(item) + string(" illegal#") + to_string(its_item_numbers[item]);
}
wave_list.push_back(current_name);
} // End of for-loop going through found waves
} // if (its_count >0)
} // if (its_ready == true)
return wave_list;
}
// Set number of item to read
bool Ac_infile_mw2::set_item(unsigned int number)
{
bool set_state = false;
if (number <= its_item_count)
{
its_item = number;
set_state = true;
}
return set_state;
}
// Read the requested wave into the data array
bool Ac_infile_mw2::process()
{
bool process_state = false;
// Begin if its_ready true and item number within range
if (its_ready == true) // there is data
{
if (its_item < its_item_count)
{
unsigned int wave_start = its_item_indices[its_item];
unsigned int wave_end = wave_start + 128;
// Allocate data array
its_data = new double[128];
its_size = 128;
// Read the data and convert to double, this will only read
// a half cycle (see Microwave II/XT wavd format)
for (unsigned int index = wave_start;index<wave_end;index += 2)
{
its_data[index / 2] = ac_convert_mw2_sample(its_sysex_string, index);
}
// Generate the other half of the wave by wave[64+n] = -wave[63-n]
for (unsigned int index = 0;index<64;index++)
{
its_data[64 + index] = (-its_data[63 - index]);
}
// Process completed successfully
its_good = true;
process_state = true;
} // if (its_item < its_item_count) - begin if item within range
} // if (its_ready == true) - begin if there is data
return process_state;
}
} // End of namespace JBS