comparison audioio/AudioGenerator.cpp @ 108:e25e8f5d785b

* Fix #1628781 changes to layer visibility and mute should use a command * Also use a command for changes to layer playback pan, gain, plugin settings * Refactor PlayParameterRepository to remove dependency on audioio from base * Fix failure to save play parameters for main model in session file
author Chris Cannam
date Thu, 13 Mar 2008 14:06:03 +0000
parents 448ff6e34b99
children f8110d1a3a37
comparison
equal deleted inserted replaced
107:52af71802ffd 108:e25e8f5d785b
48 AudioGenerator::AudioGenerator() : 48 AudioGenerator::AudioGenerator() :
49 m_sourceSampleRate(0), 49 m_sourceSampleRate(0),
50 m_targetChannelCount(1), 50 m_targetChannelCount(1),
51 m_soloing(false) 51 m_soloing(false)
52 { 52 {
53 initialiseSampleDir();
54
53 connect(PlayParameterRepository::getInstance(), 55 connect(PlayParameterRepository::getInstance(),
54 SIGNAL(playPluginIdChanged(const Model *, QString)), 56 SIGNAL(playPluginIdChanged(const Playable *, QString)),
55 this, 57 this,
56 SLOT(playPluginIdChanged(const Model *, QString))); 58 SLOT(playPluginIdChanged(const Playable *, QString)));
57 59
58 connect(PlayParameterRepository::getInstance(), 60 connect(PlayParameterRepository::getInstance(),
59 SIGNAL(playPluginConfigurationChanged(const Model *, QString)), 61 SIGNAL(playPluginConfigurationChanged(const Playable *, QString)),
60 this, 62 this,
61 SLOT(playPluginConfigurationChanged(const Model *, QString))); 63 SLOT(playPluginConfigurationChanged(const Playable *, QString)));
62 } 64 }
63 65
64 AudioGenerator::~AudioGenerator() 66 AudioGenerator::~AudioGenerator()
65 { 67 {
66 } 68 }
67 69
68 bool 70 void
69 AudioGenerator::canPlay(const Model *model) 71 AudioGenerator::initialiseSampleDir()
70 { 72 {
71 if (dynamic_cast<const DenseTimeValueModel *>(model) || 73 if (m_sampleDir != "") return;
72 dynamic_cast<const SparseOneDimensionalModel *>(model) || 74
73 dynamic_cast<const NoteModel *>(model)) { 75 try {
74 return true; 76 m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples");
75 } else { 77 } catch (DirectoryCreationFailed f) {
76 return false; 78 std::cerr << "WARNING: AudioGenerator::initialiseSampleDir:"
79 << " Failed to create temporary sample directory"
80 << std::endl;
81 m_sampleDir = "";
82 return;
83 }
84
85 QDir sampleResourceDir(":/samples", "*.wav");
86
87 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
88
89 QString fileName(sampleResourceDir[i]);
90 QFile file(sampleResourceDir.filePath(fileName));
91
92 if (!file.copy(QDir(m_sampleDir).filePath(fileName))) {
93 std::cerr << "WARNING: AudioGenerator::getSampleDir: "
94 << "Unable to copy " << fileName.toStdString()
95 << " into temporary directory \""
96 << m_sampleDir.toStdString() << "\"" << std::endl;
97 }
77 } 98 }
78 } 99 }
79 100
80 bool 101 bool
81 AudioGenerator::addModel(Model *model) 102 AudioGenerator::addModel(Model *model)
104 125
105 return false; 126 return false;
106 } 127 }
107 128
108 void 129 void
109 AudioGenerator::playPluginIdChanged(const Model *model, QString) 130 AudioGenerator::playPluginIdChanged(const Playable *playable, QString)
110 { 131 {
132 const Model *model = dynamic_cast<const Model *>(playable);
133 if (!model) {
134 std::cerr << "WARNING: AudioGenerator::playPluginIdChanged: playable "
135 << playable << " is not a supported model type"
136 << std::endl;
137 return;
138 }
139
111 if (m_synthMap.find(model) == m_synthMap.end()) return; 140 if (m_synthMap.find(model) == m_synthMap.end()) return;
112 141
113 RealTimePluginInstance *plugin = loadPluginFor(model); 142 RealTimePluginInstance *plugin = loadPluginFor(model);
114 if (plugin) { 143 if (plugin) {
115 QMutexLocker locker(&m_mutex); 144 QMutexLocker locker(&m_mutex);
117 m_synthMap[model] = plugin; 146 m_synthMap[model] = plugin;
118 } 147 }
119 } 148 }
120 149
121 void 150 void
122 AudioGenerator::playPluginConfigurationChanged(const Model *model, 151 AudioGenerator::playPluginConfigurationChanged(const Playable *playable,
123 QString configurationXml) 152 QString configurationXml)
124 { 153 {
125 // std::cerr << "AudioGenerator::playPluginConfigurationChanged" << std::endl; 154 // std::cerr << "AudioGenerator::playPluginConfigurationChanged" << std::endl;
155
156 const Model *model = dynamic_cast<const Model *>(playable);
157 if (!model) {
158 std::cerr << "WARNING: AudioGenerator::playPluginIdChanged: playable "
159 << playable << " is not a supported model type"
160 << std::endl;
161 return;
162 }
126 163
127 if (m_synthMap.find(model) == m_synthMap.end()) { 164 if (m_synthMap.find(model) == m_synthMap.end()) {
128 std::cerr << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << std::endl; 165 std::cerr << "AudioGenerator::playPluginConfigurationChanged: We don't know about this plugin" << std::endl;
129 return; 166 return;
130 } 167 }
133 if (plugin) { 170 if (plugin) {
134 PluginXml(plugin).setParametersFromXml(configurationXml); 171 PluginXml(plugin).setParametersFromXml(configurationXml);
135 } 172 }
136 } 173 }
137 174
138 QString
139 AudioGenerator::getDefaultPlayPluginId(const Model *model)
140 {
141 const SparseOneDimensionalModel *sodm =
142 dynamic_cast<const SparseOneDimensionalModel *>(model);
143 if (sodm) {
144 return QString("dssi:%1:sample_player").
145 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
146 }
147
148 const NoteModel *nm = dynamic_cast<const NoteModel *>(model);
149 if (nm) {
150 return QString("dssi:%1:sample_player").
151 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
152 }
153
154 return "";
155 }
156
157 QString
158 AudioGenerator::getDefaultPlayPluginConfiguration(const Model *model)
159 {
160 QString program = "";
161
162 const SparseOneDimensionalModel *sodm =
163 dynamic_cast<const SparseOneDimensionalModel *>(model);
164 if (sodm) {
165 program = "tap";
166 }
167
168 const NoteModel *nm = dynamic_cast<const NoteModel *>(model);
169 if (nm) {
170 program = "piano";
171 }
172
173 if (program == "") return "";
174
175 return
176 QString("<plugin configuration=\"%1\" program=\"%2\"/>")
177 .arg(XmlExportable::encodeEntities
178 (QString("sampledir=%1")
179 .arg(PluginXml::encodeConfigurationChars(getSampleDir()))))
180 .arg(XmlExportable::encodeEntities(program));
181 }
182
183 QString
184 AudioGenerator::getSampleDir()
185 {
186 if (m_sampleDir != "") return m_sampleDir;
187
188 try {
189 m_sampleDir = TempDirectory::getInstance()->getSubDirectoryPath("samples");
190 } catch (DirectoryCreationFailed f) {
191 std::cerr << "WARNING: AudioGenerator::getSampleDir: Failed to create "
192 << "temporary sample directory" << std::endl;
193 m_sampleDir = "";
194 return "";
195 }
196
197 QDir sampleResourceDir(":/samples", "*.wav");
198
199 for (unsigned int i = 0; i < sampleResourceDir.count(); ++i) {
200
201 QString fileName(sampleResourceDir[i]);
202 QFile file(sampleResourceDir.filePath(fileName));
203
204 if (!file.copy(QDir(m_sampleDir).filePath(fileName))) {
205 std::cerr << "WARNING: AudioGenerator::getSampleDir: "
206 << "Unable to copy " << fileName.toStdString()
207 << " into temporary directory \""
208 << m_sampleDir.toStdString() << "\"" << std::endl;
209 }
210 }
211
212 return m_sampleDir;
213 }
214
215 void 175 void
216 AudioGenerator::setSampleDir(RealTimePluginInstance *plugin) 176 AudioGenerator::setSampleDir(RealTimePluginInstance *plugin)
217 { 177 {
218 plugin->configure("sampledir", getSampleDir().toStdString()); 178 if (m_sampleDir != "") {
179 plugin->configure("sampledir", m_sampleDir.toStdString());
180 }
219 } 181 }
220 182
221 RealTimePluginInstance * 183 RealTimePluginInstance *
222 AudioGenerator::loadPluginFor(const Model *model) 184 AudioGenerator::loadPluginFor(const Model *model)
223 { 185 {
224 QString pluginId, configurationXml; 186 QString pluginId, configurationXml;
225 187
188 const Playable *playable = model;
189 if (!playable || !playable->canPlay()) return 0;
190
226 PlayParameters *parameters = 191 PlayParameters *parameters =
227 PlayParameterRepository::getInstance()->getPlayParameters(model); 192 PlayParameterRepository::getInstance()->getPlayParameters(playable);
228 if (parameters) { 193 if (parameters) {
229 pluginId = parameters->getPlayPluginId(); 194 pluginId = parameters->getPlayPluginId();
230 configurationXml = parameters->getPlayPluginConfiguration(); 195 configurationXml = parameters->getPlayPluginConfiguration();
231 } 196 }
232 197
233 if (pluginId == "") {
234 pluginId = getDefaultPlayPluginId(model);
235 configurationXml = getDefaultPlayPluginConfiguration(model);
236 }
237
238 if (pluginId == "") return 0; 198 if (pluginId == "") return 0;
239 199
240 RealTimePluginInstance *plugin = loadPlugin(pluginId, ""); 200 RealTimePluginInstance *plugin = loadPlugin(pluginId, "");
241 if (!plugin) return 0; 201 if (!plugin) return 0;
242 202
243 if (configurationXml != "") { 203 if (configurationXml != "") {
244 PluginXml(plugin).setParametersFromXml(configurationXml); 204 PluginXml(plugin).setParametersFromXml(configurationXml);
245 } 205 }
206
207 configurationXml = PluginXml(plugin).toXmlString();
246 208
247 if (parameters) { 209 if (parameters) {
248 parameters->setPlayPluginId(pluginId); 210 parameters->setPlayPluginId(pluginId);
249 parameters->setPlayPluginConfiguration(configurationXml); 211 parameters->setPlayPluginConfiguration(configurationXml);
250 } 212 }
380 return frameCount; 342 return frameCount;
381 } 343 }
382 344
383 QMutexLocker locker(&m_mutex); 345 QMutexLocker locker(&m_mutex);
384 346
347 Playable *playable = model;
348 if (!playable || !playable->canPlay()) return frameCount;
349
385 PlayParameters *parameters = 350 PlayParameters *parameters =
386 PlayParameterRepository::getInstance()->getPlayParameters(model); 351 PlayParameterRepository::getInstance()->getPlayParameters(playable);
387 if (!parameters) return frameCount; 352 if (!parameters) return frameCount;
388 353
389 bool playing = !parameters->isPlayMuted(); 354 bool playing = !parameters->isPlayMuted();
390 if (!playing) { 355 if (!playing) {
391 #ifdef DEBUG_AUDIO_GENERATOR 356 #ifdef DEBUG_AUDIO_GENERATOR