-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac_outfile_yoshimi.cpp
135 lines (120 loc) · 3.52 KB
/
ac_outfile_yoshimi.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
/* ac_outfile_yoshimi.cpp - Definition of the Yoshimi script output file 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_outfile_yoshimi.hpp"
#include <cmath>
using std::string;
using std::ofstream;
using std::round;
namespace JBS {
Ac_outfile_yoshimi::Ac_outfile_yoshimi(const string& filename):
Ac_outfile_base(filename)
{}
Ac_outfile_yoshimi::Ac_outfile_yoshimi(const char *filename):
Ac_outfile_base(filename)
{}
Ac_outfile_yoshimi::~Ac_outfile_yoshimi()
{
if (its_open == true)
{
close();
}
}
// Open the connected file for writing
bool Ac_outfile_yoshimi::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;
its_good = true;
file_state = true;
}
} // if (its_good == true) all OK to begin open
} // if (its_open == true)
return file_state;
}
void Ac_outfile_yoshimi::close()
{
if (its_open == true)
{
its_file.close();
its_open = false;
}
}
bool Ac_outfile_yoshimi::process()
{
bool process_state = false;
// Begoin writing if state is good, file is open and data is there
if (its_open == true)
{
if (its_data != nullptr)
{
if (its_good == true)
{
// Convert up to 128 double amplitudes to short ints between
// -63..+63
unsigned long int max_item = 0;
// Find the maximum number of harmonics: 128 or less
if (its_size < 128)
{
max_item = its_size;
}
else // Yoshimi can't take more than 128 harmonics
{
max_item = 128;
}
// Scale and round the amplitudes, then write to script
// Script lines take the form:
// s ha index a amplitude
// Remember: all converters rescale to a maximum of 1
int current_harmonic = 0; // harmonic amplitude to write
for (unsigned short int item = 0;item<max_item;item++)
{
current_harmonic = int(round(its_data[item] * 63));
if (current_harmonic != 0)
{ // Write a line
its_file << "s ha " << (item + 1) << " a " << (current_harmonic + 64) << "\n";
}
}
// Done processing
process_state = true;
} // if (its_good == true) // begin process if no other errors
} // if (its_data != nullptr) - being process when data is present
} // if (its_open == true) - begin process if file is open
return process_state;
}
} // End of namespace JBS