annotate audioio/AudioGenerator.cpp @ 2:df5923e33d01

* Update copyrights to 2006
author Chris Cannam
date Thu, 12 Jan 2006 13:45:27 +0000
parents db6fcbd4405c
children 5865094175ea
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@2 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "AudioGenerator.h"
Chris@0 11
Chris@0 12 #include "base/ViewManager.h"
Chris@0 13 #include "base/PlayParameters.h"
Chris@0 14
Chris@0 15 #include "model/DenseTimeValueModel.h"
Chris@0 16 #include "model/SparseOneDimensionalModel.h"
Chris@0 17
Chris@0 18 #include "plugin/RealTimePluginFactory.h"
Chris@0 19 #include "plugin/RealTimePluginInstance.h"
Chris@0 20 #include "plugin/PluginIdentifier.h"
Chris@0 21 #include "plugin/api/alsa/seq_event.h"
Chris@0 22
Chris@0 23 #include <iostream>
Chris@0 24
Chris@0 25 const size_t
Chris@0 26 AudioGenerator::m_pluginBlockSize = 2048;
Chris@0 27
Chris@0 28 // #define DEBUG_AUDIO_GENERATOR 1
Chris@0 29
Chris@0 30 AudioGenerator::AudioGenerator(ViewManager *manager) :
Chris@0 31 m_viewManager(manager),
Chris@0 32 m_sourceSampleRate(0),
Chris@0 33 m_targetChannelCount(1)
Chris@0 34 {
Chris@0 35 }
Chris@0 36
Chris@0 37 AudioGenerator::~AudioGenerator()
Chris@0 38 {
Chris@0 39 }
Chris@0 40
Chris@0 41 void
Chris@0 42 AudioGenerator::addModel(Model *model)
Chris@0 43 {
Chris@0 44 if (m_sourceSampleRate == 0) {
Chris@0 45
Chris@0 46 m_sourceSampleRate = model->getSampleRate();
Chris@0 47
Chris@0 48 } else {
Chris@0 49
Chris@0 50 DenseTimeValueModel *dtvm =
Chris@0 51 dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 52
Chris@0 53 if (dtvm) {
Chris@0 54 m_sourceSampleRate = model->getSampleRate();
Chris@0 55 }
Chris@0 56 }
Chris@0 57
Chris@0 58 SparseOneDimensionalModel *sodm =
Chris@0 59 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@0 60 if (!sodm) return; // nothing else to initialise
Chris@0 61
Chris@0 62 // QString pluginId = "dssi:/usr/lib/dssi/dssi-vst.so:FEARkILLERrev1.dll";
Chris@0 63 // QString pluginId = "dssi:/usr/lib/dssi/hexter.so:hexter";
Chris@0 64 // QString pluginId = "dssi:/usr/lib/dssi/sineshaper.so:sineshaper";
Chris@0 65 // QString pluginId = "dssi:/usr/local/lib/dssi/xsynth-dssi.so:Xsynth";
Chris@0 66 // QString pluginId = "dssi:/usr/local/lib/dssi/trivial_synth.so:TS";
Chris@0 67 QString pluginId = QString("dssi:%1:sample_player").
Chris@0 68 arg(PluginIdentifier::BUILTIN_PLUGIN_SONAME);
Chris@0 69 RealTimePluginFactory *factory =
Chris@0 70 RealTimePluginFactory::instanceFor(pluginId);
Chris@0 71
Chris@0 72 if (!factory) {
Chris@0 73 std::cerr << "Failed to get plugin factory" << std::endl;
Chris@0 74 return;
Chris@0 75 }
Chris@0 76
Chris@0 77 RealTimePluginInstance *instance =
Chris@0 78 factory->instantiatePlugin
Chris@0 79 (pluginId, 0, 0, m_sourceSampleRate, m_pluginBlockSize, m_targetChannelCount);
Chris@0 80
Chris@0 81 if (instance) {
Chris@0 82 m_synthMap[sodm] = instance;
Chris@0 83 for (unsigned int i = 0; i < instance->getParameterCount(); ++i) {
Chris@0 84 instance->setParameterValue(i, instance->getParameterDefault(i));
Chris@0 85 }
Chris@0 86 QString program = instance->getProgram(0, 0);
Chris@0 87 if (program != "") {
Chris@0 88 std::cerr << "selecting program " << program.toLocal8Bit().data() << std::endl;
Chris@0 89 instance->selectProgram(program);
Chris@0 90 }
Chris@0 91 instance->selectProgram("cowbell"); //!!!
Chris@0 92 instance->setIdealChannelCount(m_targetChannelCount); // reset!
Chris@0 93 } else {
Chris@0 94 std::cerr << "Failed to instantiate plugin" << std::endl;
Chris@0 95 }
Chris@0 96 }
Chris@0 97
Chris@0 98 void
Chris@0 99 AudioGenerator::removeModel(Model *model)
Chris@0 100 {
Chris@0 101 SparseOneDimensionalModel *sodm =
Chris@0 102 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@0 103 if (!sodm) return; // nothing to do
Chris@0 104
Chris@0 105 if (m_synthMap.find(sodm) == m_synthMap.end()) return;
Chris@0 106
Chris@0 107 RealTimePluginInstance *instance = m_synthMap[sodm];
Chris@0 108 m_synthMap.erase(sodm);
Chris@0 109 delete instance;
Chris@0 110 }
Chris@0 111
Chris@0 112 void
Chris@0 113 AudioGenerator::clearModels()
Chris@0 114 {
Chris@0 115 while (!m_synthMap.empty()) {
Chris@0 116 RealTimePluginInstance *instance = m_synthMap.begin()->second;
Chris@0 117 m_synthMap.erase(m_synthMap.begin());
Chris@0 118 delete instance;
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@0 122 void
Chris@0 123 AudioGenerator::reset()
Chris@0 124 {
Chris@0 125 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@0 126 if (i->second) {
Chris@0 127 i->second->silence();
Chris@0 128 i->second->discardEvents();
Chris@0 129 }
Chris@0 130 }
Chris@0 131
Chris@0 132 m_noteOffs.clear();
Chris@0 133 }
Chris@0 134
Chris@0 135 void
Chris@0 136 AudioGenerator::setTargetChannelCount(size_t targetChannelCount)
Chris@0 137 {
Chris@0 138 m_targetChannelCount = targetChannelCount;
Chris@0 139
Chris@0 140 for (PluginMap::iterator i = m_synthMap.begin(); i != m_synthMap.end(); ++i) {
Chris@0 141 if (i->second) i->second->setIdealChannelCount(targetChannelCount);
Chris@0 142 }
Chris@0 143 }
Chris@0 144
Chris@0 145 size_t
Chris@0 146 AudioGenerator::getBlockSize() const
Chris@0 147 {
Chris@0 148 return m_pluginBlockSize;
Chris@0 149 }
Chris@0 150
Chris@0 151 size_t
Chris@0 152 AudioGenerator::mixModel(Model *model, size_t startFrame, size_t frameCount,
Chris@0 153 float **buffer)
Chris@0 154 {
Chris@0 155 if (m_sourceSampleRate == 0) {
Chris@0 156 std::cerr << "WARNING: AudioGenerator::mixModel: No base source sample rate available" << std::endl;
Chris@0 157 return frameCount;
Chris@0 158 }
Chris@0 159
Chris@0 160 PlayParameters *parameters = m_viewManager->getPlayParameters(model);
Chris@0 161 if (!parameters) return frameCount;
Chris@0 162
Chris@0 163 bool playing = !parameters->isPlayMuted();
Chris@0 164 if (!playing) return frameCount;
Chris@0 165
Chris@0 166 float gain = parameters->getPlayGain();
Chris@0 167 float pan = parameters->getPlayPan();
Chris@0 168
Chris@0 169 DenseTimeValueModel *dtvm = dynamic_cast<DenseTimeValueModel *>(model);
Chris@0 170 if (dtvm) {
Chris@0 171 return mixDenseTimeValueModel(dtvm, startFrame, frameCount,
Chris@0 172 buffer, gain, pan);
Chris@0 173 }
Chris@0 174
Chris@0 175 SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *>
Chris@0 176 (model);
Chris@0 177 if (sodm) {
Chris@0 178 return mixSparseOneDimensionalModel(sodm, startFrame, frameCount,
Chris@0 179 buffer, gain, pan);
Chris@0 180 }
Chris@0 181
Chris@0 182 return frameCount;
Chris@0 183 }
Chris@0 184
Chris@0 185 size_t
Chris@0 186 AudioGenerator::mixDenseTimeValueModel(DenseTimeValueModel *dtvm,
Chris@0 187 size_t startFrame, size_t frames,
Chris@0 188 float **buffer, float gain, float pan)
Chris@0 189 {
Chris@0 190 static float *channelBuffer = 0;
Chris@0 191 static size_t channelBufSiz = 0;
Chris@0 192
Chris@0 193 if (channelBufSiz < frames) {
Chris@0 194 delete[] channelBuffer;
Chris@0 195 channelBuffer = new float[frames];
Chris@0 196 channelBufSiz = frames;
Chris@0 197 }
Chris@0 198
Chris@0 199 size_t got = 0;
Chris@0 200
Chris@0 201 for (size_t c = 0; c < m_targetChannelCount && c < dtvm->getChannelCount(); ++c) {
Chris@0 202 got = dtvm->getValues(c, startFrame, startFrame + frames, channelBuffer);
Chris@0 203 for (size_t i = 0; i < frames; ++i) {
Chris@0 204 buffer[c][i] += gain * channelBuffer[i];
Chris@0 205 }
Chris@0 206 }
Chris@0 207
Chris@0 208 return got;
Chris@0 209 }
Chris@0 210
Chris@0 211 size_t
Chris@0 212 AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm,
Chris@0 213 size_t startFrame, size_t frames,
Chris@0 214 float **buffer, float gain, float pan)
Chris@0 215 {
Chris@0 216 RealTimePluginInstance *plugin = m_synthMap[sodm];
Chris@0 217 if (!plugin) return 0;
Chris@0 218
Chris@0 219 size_t latency = plugin->getLatency();
Chris@0 220 size_t blocks = frames / m_pluginBlockSize;
Chris@0 221
Chris@0 222 //!!! hang on -- the fact that the audio callback play source's
Chris@0 223 //buffer is a multiple of the plugin's buffer size doesn't mean
Chris@0 224 //that we always get called for a multiple of it here (because it
Chris@0 225 //also depends on the JACK block size). how should we ensure that
Chris@0 226 //all models write the same amount in to the mix, and that we
Chris@0 227 //always have a multiple of the plugin buffer size? I guess this
Chris@0 228 //class has to be queryable for the plugin buffer size & the
Chris@0 229 //callback play source has to use that as a multiple for all the
Chris@0 230 //calls to mixModel
Chris@0 231
Chris@0 232 size_t got = blocks * m_pluginBlockSize;
Chris@0 233
Chris@0 234 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 235 std::cout << "mixModel [sparse]: frames " << frames
Chris@0 236 << ", blocks " << blocks << std::endl;
Chris@0 237 #endif
Chris@0 238
Chris@0 239 snd_seq_event_t onEv;
Chris@0 240 onEv.type = SND_SEQ_EVENT_NOTEON;
Chris@0 241 onEv.data.note.channel = 0;
Chris@0 242 onEv.data.note.note = 64;
Chris@0 243 onEv.data.note.velocity = 127;
Chris@0 244
Chris@0 245 snd_seq_event_t offEv;
Chris@0 246 offEv.type = SND_SEQ_EVENT_NOTEOFF;
Chris@0 247 offEv.data.note.channel = 0;
Chris@0 248 offEv.data.note.velocity = 0;
Chris@0 249
Chris@0 250 NoteOffSet &noteOffs = m_noteOffs[sodm];
Chris@0 251
Chris@0 252 for (size_t i = 0; i < blocks; ++i) {
Chris@0 253
Chris@0 254 size_t reqStart = startFrame + i * m_pluginBlockSize;
Chris@0 255
Chris@0 256 SparseOneDimensionalModel::PointList points =
Chris@0 257 sodm->getPoints(reqStart > 0 ? reqStart + latency : reqStart,
Chris@0 258 reqStart + latency + m_pluginBlockSize);
Chris@0 259
Chris@0 260 RealTime blockTime = RealTime::frame2RealTime
Chris@0 261 (startFrame + i * m_pluginBlockSize, m_sourceSampleRate);
Chris@0 262
Chris@0 263 for (SparseOneDimensionalModel::PointList::iterator pli =
Chris@0 264 points.begin(); pli != points.end(); ++pli) {
Chris@0 265
Chris@0 266 size_t pliFrame = pli->frame;
Chris@0 267 if (pliFrame >= latency) pliFrame -= latency;
Chris@0 268
Chris@0 269 while (noteOffs.begin() != noteOffs.end() &&
Chris@0 270 noteOffs.begin()->frame <= pliFrame) {
Chris@0 271
Chris@0 272 RealTime eventTime = RealTime::frame2RealTime
Chris@0 273 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@0 274
Chris@0 275 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@0 276 plugin->sendEvent(eventTime, &offEv);
Chris@0 277 noteOffs.erase(noteOffs.begin());
Chris@0 278 }
Chris@0 279
Chris@0 280 RealTime eventTime = RealTime::frame2RealTime
Chris@0 281 (pliFrame, m_sourceSampleRate);
Chris@0 282
Chris@0 283 plugin->sendEvent(eventTime, &onEv);
Chris@0 284
Chris@0 285 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 286 std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl;
Chris@0 287 #endif
Chris@0 288
Chris@0 289 size_t duration = 7000; // frames [for now]
Chris@0 290 NoteOff noff;
Chris@0 291 noff.pitch = onEv.data.note.note;
Chris@0 292 noff.frame = pliFrame + duration;
Chris@0 293 noteOffs.insert(noff);
Chris@0 294 }
Chris@0 295
Chris@0 296 while (noteOffs.begin() != noteOffs.end() &&
Chris@0 297 noteOffs.begin()->frame <=
Chris@0 298 startFrame + i * m_pluginBlockSize + m_pluginBlockSize) {
Chris@0 299
Chris@0 300 RealTime eventTime = RealTime::frame2RealTime
Chris@0 301 (noteOffs.begin()->frame, m_sourceSampleRate);
Chris@0 302
Chris@0 303 offEv.data.note.note = noteOffs.begin()->pitch;
Chris@0 304 plugin->sendEvent(eventTime, &offEv);
Chris@0 305 noteOffs.erase(noteOffs.begin());
Chris@0 306 }
Chris@0 307
Chris@0 308 plugin->run(blockTime);
Chris@0 309 float **outs = plugin->getAudioOutputBuffers();
Chris@0 310
Chris@0 311 for (size_t c = 0; c < m_targetChannelCount && c < plugin->getAudioOutputCount(); ++c) {
Chris@0 312 #ifdef DEBUG_AUDIO_GENERATOR
Chris@0 313 std::cout << "mixModel [sparse]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl;
Chris@0 314 #endif
Chris@0 315
Chris@0 316 for (size_t j = 0; j < m_pluginBlockSize; ++j) {
Chris@0 317 buffer[c][i * m_pluginBlockSize + j] += gain * outs[c][j];
Chris@0 318 }
Chris@0 319 }
Chris@0 320 }
Chris@0 321
Chris@0 322 return got;
Chris@0 323 }
Chris@0 324