Mercurial > hg > gpsynth
comparison src/sc_grammar.hpp @ 0:add35537fdbb tip
Initial import
author | irh <ian.r.hobson@gmail.com> |
---|---|
date | Thu, 25 Aug 2011 11:05:55 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:add35537fdbb |
---|---|
1 // Copyright 2011, Ian Hobson. | |
2 // | |
3 // This file is part of gpsynth. | |
4 // | |
5 // gpsynth is free software: you can redistribute it and/or modify | |
6 // it under the terms of the GNU General Public License as published by | |
7 // the Free Software Foundation, either version 3 of the License, or | |
8 // (at your option) any later version. | |
9 // | |
10 // gpsynth is distributed in the hope that it will be useful, | |
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 // GNU General Public License for more details. | |
14 // | |
15 // You should have received a copy of the GNU General Public License | |
16 // along with gpsynth in the file COPYING. | |
17 // If not, see http://www.gnu.org/licenses/. | |
18 | |
19 // SuperCollider grammar, resonsible for generating and altering SuperCollider | |
20 // synth graphs | |
21 | |
22 #pragma once | |
23 | |
24 #include "grammar.hpp" | |
25 #include "range.hpp" | |
26 #include "synth_graph.hpp" | |
27 | |
28 #include <map> | |
29 #include <string> | |
30 #include <vector> | |
31 | |
32 namespace sc { | |
33 | |
34 struct Argument { | |
35 | |
36 enum ScalingMode { | |
37 kScalingLinear, | |
38 kScalingLog, | |
39 kConstant | |
40 }; | |
41 | |
42 std::string name_; | |
43 stdx::Range<double> range_; | |
44 stdx::Range<double> range_control_; | |
45 ScalingMode scaling_mode_; | |
46 double constant_value_; | |
47 bool fixed_range_; | |
48 | |
49 Argument(const std::string name = "") | |
50 : name_(name), | |
51 range_(0, 1), | |
52 range_control_(0, 1), | |
53 scaling_mode_(kScalingLinear), | |
54 constant_value_(0), | |
55 fixed_range_(false) | |
56 {} | |
57 }; | |
58 | |
59 class Command { | |
60 public: | |
61 enum CommandMode { | |
62 kSource, | |
63 kModifier, | |
64 kSpecial, | |
65 kNumberOfModes | |
66 }; | |
67 | |
68 enum SpecialCommands { | |
69 kMixer, | |
70 kMultiplier, | |
71 kNumberOfSpecialCommands | |
72 }; | |
73 | |
74 enum OutputType { | |
75 kAll, | |
76 kAudio, | |
77 kControl, | |
78 }; | |
79 | |
80 private: | |
81 std::string name_; | |
82 std::vector<Argument> arguments_; | |
83 CommandMode mode_; | |
84 OutputType output_; | |
85 | |
86 public: | |
87 Command(const std::string& name, | |
88 CommandMode mode, | |
89 OutputType output = kAll, | |
90 const std::vector<Argument>& arguments = std::vector<Argument>()) | |
91 : name_(name), | |
92 mode_(mode), | |
93 output_(output), | |
94 arguments_(arguments) | |
95 {} | |
96 | |
97 CommandMode Mode() const { return mode_; } | |
98 OutputType Output() const { return output_; } | |
99 const std::string& Name() const { return name_; } | |
100 const std::vector<Argument>& Arguments() const { return arguments_;} | |
101 const Argument& GetArgument(int argument) const { | |
102 return arguments_[argument]; | |
103 } | |
104 bool IsAudioSource() { return (mode_ == kSource) && (output_ != kControl); } | |
105 }; | |
106 | |
107 class Grammar : public GrammarInterface { | |
108 // holds the commands available to the grammar | |
109 std::vector<Command> commands_; | |
110 typedef std::vector<Command>::size_type CommandID; | |
111 int max_depth_; | |
112 | |
113 public: | |
114 // used when generating trees to indicate current tree state | |
115 enum TreeMode { | |
116 kTreeMode_Command, | |
117 kTreeMode_Mod | |
118 }; | |
119 | |
120 Grammar(const std::string& json_data); | |
121 | |
122 void ParseJSON(const std::string& json_data); | |
123 | |
124 const std::vector<Command>& Commands() const { return commands_; } | |
125 | |
126 void SetMaximumDepth(int max_depth) { max_depth_ = max_depth; } | |
127 | |
128 void RandomGraph(sg::Graph& graph) const; | |
129 void PickCrossoverNodes(const sg::Graph& parent_1, | |
130 const sg::Graph& parent_2, | |
131 sg::Vertex* crossover_node_1, | |
132 sg::Vertex* crossover_node_2) const; | |
133 bool MutationReplaceSubTree(sg::Graph& graph) const; | |
134 bool MutationAddSubTree(sg::Graph& graph) const; | |
135 bool MutationModifyInputs(sg::Graph& graph) const; | |
136 bool MutationModifyConnections(sg::Graph& graph) const; | |
137 bool MutationReplaceCommand(sg::Graph& graph) const; | |
138 bool MutationInsertCommand(sg::Graph& graph) const; | |
139 | |
140 private: | |
141 int RandomCommand(sg::CommandRate rate, | |
142 int tree_depth, | |
143 bool must_be_modifier) const; | |
144 int RandomParameter(sg::Graph& graph) const; | |
145 | |
146 sg::Vertex RandomTree(sg::Graph& graph, | |
147 sg::CommandRate rate, | |
148 TreeMode tree_mode, | |
149 int depth) const; | |
150 | |
151 sg::Vertex SpecialCommand(int command_id, | |
152 sg::Graph& graph, | |
153 sg::CommandRate output_mode, | |
154 TreeMode tree_mode, | |
155 int depth = 0, | |
156 int minimum_channels = 2) const; | |
157 void AddTreeToSpecialCommand(sg::Vertex command, | |
158 sg::Graph& graph, | |
159 int input_id, | |
160 TreeMode tree_mode, | |
161 sg::CommandRate command_rate, | |
162 bool* constant_added, | |
163 std::set<int>* parameters, | |
164 int depth) const; | |
165 sg::Vertex CreateCommand(sg::Graph &graph, | |
166 sg::CommandRate rate, | |
167 int depth, | |
168 bool must_be_modifier = false, | |
169 bool make_modifier_input = true) const; | |
170 int GetCommandInputID(int command_id) const; | |
171 const Argument& GetCommandArgument(int command_id, int argument_id) const; | |
172 const std::vector<sc::Argument>& CommandArguments(int command) const; | |
173 }; | |
174 | |
175 } // sc namespace |