-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsl_codegen.cpp
More file actions
171 lines (147 loc) · 5.48 KB
/
dsl_codegen.cpp
File metadata and controls
171 lines (147 loc) · 5.48 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
//-----------------------------------------------------------------------------
// file: dsl_codegen.cpp
// desc: convert Piece proto into ChucK source code
//-----------------------------------------------------------------------------
#include "dsl_codegen.h"
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
namespace dsl {
namespace {
std::string FormatDouble(double v)
{
std::ostringstream oss;
oss << std::setprecision(12);
oss << v;
return oss.str();
}
std::string OscClass(Instrument_Osc osc)
{
switch (osc)
{
case Instrument_Osc_SINE: return "SinOsc";
case Instrument_Osc_SAW: return "SawOsc";
case Instrument_Osc_SQUARE: return "SqrOsc";
case Instrument_Osc_TRIANGLE: return "TriOsc";
case Instrument_Osc_NOISE: return "Noise";
case Instrument_Osc_BLIT: return "BlitSaw";
default: return "SinOsc";
}
}
std::string FilterClass(Filter_Type type)
{
switch (type)
{
case Filter_Type_LOWPASS: return "LPF";
case Filter_Type_HIGHPASS: return "HPF";
case Filter_Type_BANDPASS: return "BPF";
default: return "";
}
}
std::string MakeVoiceFunction(const Voice &voice, int index)
{
const Instrument &instr = voice.instrument();
const Pattern &pattern = voice.pattern();
const bool osc_has_freq = instr.osc() != Instrument_Osc_NOISE;
std::ostringstream out;
const std::string idx = std::to_string(index);
const std::string func = "voice_" + idx;
const std::string osc = "osc_" + idx;
const std::string env = "env_" + idx;
const std::string pan = "pan_" + idx;
const std::string filt = "filt_" + idx;
const std::string rev = "rev_" + idx;
out << "fun void " << func << "() {\n";
out << " " << FormatDouble(voice.offset()) << " * beat => now;\n";
out << " " << OscClass(instr.osc()) << " " << osc << ";\n";
out << " ADSR " << env << ";\n";
const bool has_filter = instr.filter().type() != Filter_Type_FILTER_NONE;
if (has_filter) out << " " << FilterClass(instr.filter().type()) << " " << filt << ";\n";
const bool use_reverb = instr.reverb();
if (use_reverb) out << " NRev " << rev << ";\n";
out << " Pan2 " << pan << ";\n";
out << " " << osc << " => ";
if (has_filter) out << filt << " => ";
out << env << " => ";
if (use_reverb) out << rev << " => ";
out << pan << " => dac;\n";
out << " " << FormatDouble(instr.gain()) << " => " << osc << ".gain;\n";
out << " " << FormatDouble(voice.pan()) << " => " << pan << ".pan;\n";
out << " " << env << ".set(" << FormatDouble(instr.env().attack()) << ", "
<< FormatDouble(instr.env().decay()) << ", "
<< FormatDouble(instr.env().sustain()) << ", "
<< FormatDouble(instr.env().release()) << ");\n";
if (has_filter)
{
if (instr.filter().cutoff() > 0.0)
out << " " << FormatDouble(instr.filter().cutoff()) << " => " << filt << ".freq;\n";
if (instr.filter().q() > 0.0)
out << " " << FormatDouble(instr.filter().q()) << " => " << filt << ".Q;\n";
}
if (use_reverb)
{
out << " 0.1 => " << rev << ".mix;\n";
}
const int repeat = pattern.repeat() > 0 ? pattern.repeat() : 1;
const double density = pattern.density() > 0.0 ? pattern.density() : 1.0;
const int scale = pattern.scale();
out << " for( 0 => int rep; rep < " << repeat << "; rep++ ) {\n";
for (int i = 0; i < pattern.notes_size(); ++i)
{
const Note ¬e = pattern.notes(i);
out << " // note " << i << "\n";
if (density < 1.0)
{
out << " if( Math.random2f(0.0, 1.0) > " << FormatDouble(density) << " ) { continue; }\n";
}
if (osc_has_freq)
{
out << " Std.mtof(applyScale(" << note.pitch() << ", " << scale << ")) + "
<< FormatDouble(instr.detune()) << " => " << osc << ".freq;\n";
}
out << " " << env << ".keyOn();\n";
out << " " << FormatDouble(note.dur()) << " * beat => now;\n";
if (!note.tie())
{
out << " " << env << ".keyOff();\n";
if (instr.env().release() > 0.0)
out << " " << FormatDouble(instr.env().release()) << " :: second => now;\n";
}
}
out << " }\n";
out << "}\n\n";
return out.str();
}
} // namespace
bool PieceToChuckSource(const Piece &piece, const std::string &source_name, std::string *out_source,
std::string *error)
{
if (!out_source)
{
if (error) *error = "out_source is null";
return false;
}
const double tempo = piece.tempo() > 0.0 ? piece.tempo() : 120.0;
std::ostringstream out;
out << "// Generated from " << source_name << "\n";
out << "60.0 / " << FormatDouble(tempo) << " => float spb;\n";
out << "spb :: second => dur beat;\n";
out << FormatDouble(piece.complexity()) << " => float g_complexity;\n\n";
out << "// Placeholder scale helper (currently pass-through)\n";
out << "fun int applyScale(int midi, int scale) { return midi; }\n\n";
for (int i = 0; i < piece.voices_size(); ++i)
{
out << MakeVoiceFunction(piece.voices(i), i);
}
out << "// spawn voices\n";
for (int i = 0; i < piece.voices_size(); ++i)
{
out << "spork ~ voice_" << i << "();\n";
}
out << "// keep alive (could be tightened with computed piece length)\n";
out << "while(true) 1::second => now;\n";
*out_source = out.str();
return true;
}
} // namespace dsl