Mercurial > hg > svapp
diff audioio/AudioGenerator.cpp @ 234:a98f1638c5ec sonification
Refactor mixNoteModel and mixSparseOneDimensionalModel into a single mixSparseModel -- attempting to clear the decks a bit for asynchronous example-note playing
author | Chris Cannam |
---|---|
date | Fri, 24 Jun 2011 15:39:00 +0100 |
parents | 8aace2d9f1c2 |
children | 1fcee2a1c03e |
line wrap: on
line diff
--- a/audioio/AudioGenerator.cpp Tue Jun 14 15:27:12 2011 +0100 +++ b/audioio/AudioGenerator.cpp Fri Jun 24 15:39:00 2011 +0100 @@ -390,14 +390,14 @@ SparseOneDimensionalModel *sodm = dynamic_cast<SparseOneDimensionalModel *> (model); if (sodm) { - return mixSparseOneDimensionalModel(sodm, startFrame, frameCount, - buffer, gain, pan, fadeIn, fadeOut); + return mixSparseModel(sodm, startFrame, frameCount, + buffer, gain, pan, fadeIn, fadeOut); } NoteModel *nm = dynamic_cast<NoteModel *>(model); if (nm) { - return mixNoteModel(nm, startFrame, frameCount, - buffer, gain, pan, fadeIn, fadeOut); + return mixSparseModel(nm, startFrame, frameCount, + buffer, gain, pan, fadeIn, fadeOut); } return frameCount; @@ -498,14 +498,73 @@ return got; } +AudioGenerator::Notes +AudioGenerator::getNotesFromModel(Model *model, + size_t startFrame, + size_t frameCount) +{ + Notes notes; + + Note n; + n.pitch = 64; + n.duration = 0; + n.velocity = 100; + + SparseOneDimensionalModel *sodm = + qobject_cast<SparseOneDimensionalModel *>(model); + + if (sodm) { + + SparseOneDimensionalModel::PointList points = + sodm->getPoints(startFrame, startFrame + frameCount); + + for (SparseOneDimensionalModel::PointList::iterator pli = + points.begin(); pli != points.end(); ++pli) { + n.frame = pli->frame; + notes.push_back(n); + } + } + + NoteModel *nm = + qobject_cast<NoteModel *>(model); + + if (nm) { + + NoteModel::PointList points = + nm->getPoints(startFrame, startFrame + frameCount); + + for (NoteModel::PointList::iterator pli = + points.begin(); pli != points.end(); ++pli) { + + n.frame = pli->frame; + n.duration = pli->duration; + if (n.duration == 1) n.duration = m_sourceSampleRate / 20; + + if (nm->getScaleUnits() == "Hz") { + n.pitch = Pitch::getPitchForFrequency(pli->value); + } else { + n.pitch = lrintf(pli->value); + } + + if (pli->level > 0.f && pli->level <= 1.f) { + n.velocity = lrintf(pli->level * 127); + } + + notes.push_back(n); + } + } + + return notes; +} + size_t -AudioGenerator::mixSparseOneDimensionalModel(SparseOneDimensionalModel *sodm, - size_t startFrame, size_t frames, - float **buffer, float gain, float pan, - size_t /* fadeIn */, - size_t /* fadeOut */) +AudioGenerator::mixSparseModel(Model *model, + size_t startFrame, size_t frames, + float **buffer, float gain, float pan, + size_t /* fadeIn */, + size_t /* fadeOut */) { - RealTimePluginInstance *plugin = m_synthMap[sodm]; + RealTimePluginInstance *plugin = m_synthMap[model]; if (!plugin) return 0; size_t latency = plugin->getLatency(); @@ -531,39 +590,38 @@ snd_seq_event_t onEv; onEv.type = SND_SEQ_EVENT_NOTEON; onEv.data.note.channel = 0; - onEv.data.note.note = 64; - onEv.data.note.velocity = 100; snd_seq_event_t offEv; offEv.type = SND_SEQ_EVENT_NOTEOFF; offEv.data.note.channel = 0; offEv.data.note.velocity = 0; - NoteOffSet ¬eOffs = m_noteOffs[sodm]; + NoteOffSet ¬eOffs = m_noteOffs[model]; + + int halfSecond = 0.5 * m_sourceSampleRate; for (size_t i = 0; i < blocks; ++i) { size_t reqStart = startFrame + i * m_pluginBlockSize; - SparseOneDimensionalModel::PointList points = - sodm->getPoints(reqStart + latency, - reqStart + latency + m_pluginBlockSize); - Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime (startFrame + i * m_pluginBlockSize, m_sourceSampleRate); - for (SparseOneDimensionalModel::PointList::iterator pli = - points.begin(); pli != points.end(); ++pli) { + Notes notes = getNotesFromModel + (model, reqStart + latency, m_pluginBlockSize); + + for (Notes::const_iterator ni = notes.begin(); ni != notes.end(); ++ni) { - size_t pliFrame = pli->frame; + size_t frame = ni->frame; + if (frame > latency) frame -= latency; - if (pliFrame >= latency) pliFrame -= latency; - - if (pliFrame < reqStart || - pliFrame >= reqStart + m_pluginBlockSize) continue; + if (frame < reqStart || + frame >= reqStart + m_pluginBlockSize) { + continue; + } while (noteOffs.begin() != noteOffs.end() && - noteOffs.begin()->frame <= pliFrame) { + noteOffs.begin()->frame <= frame) { Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime (noteOffs.begin()->frame, m_sourceSampleRate); @@ -571,26 +629,31 @@ offEv.data.note.note = noteOffs.begin()->pitch; #ifdef DEBUG_AUDIO_GENERATOR - std::cerr << "mixModel [sparse]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << std::endl; + std::cerr << "mixModel: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << std::endl; #endif plugin->sendEvent(eventTime, &offEv); noteOffs.erase(noteOffs.begin()); - } + } + + Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime + (frame, m_sourceSampleRate); - Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime - (pliFrame, m_sourceSampleRate); - + onEv.data.note.note = ni->pitch; + onEv.data.note.velocity = ni->velocity; + plugin->sendEvent(eventTime, &onEv); #ifdef DEBUG_AUDIO_GENERATOR - std::cout << "mixModel [sparse]: point at frame " << pliFrame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; + std::cerr << "mixModel: point at frame " << frame << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; #endif - - size_t duration = 7000; // frames [for now] + + size_t duration = ni->duration; + if (duration == 0) duration = halfSecond; + NoteOff noff; noff.pitch = onEv.data.note.note; - noff.frame = pliFrame + duration; + noff.frame = frame + duration; noteOffs.insert(noff); } @@ -640,169 +703,3 @@ return got; } - -//!!! mucho duplication with above -- refactor -size_t -AudioGenerator::mixNoteModel(NoteModel *nm, - size_t startFrame, size_t frames, - float **buffer, float gain, float pan, - size_t /* fadeIn */, - size_t /* fadeOut */) -{ - RealTimePluginInstance *plugin = m_synthMap[nm]; - if (!plugin) return 0; - - size_t latency = plugin->getLatency(); - size_t blocks = frames / m_pluginBlockSize; - - //!!! hang on -- the fact that the audio callback play source's - //buffer is a multiple of the plugin's buffer size doesn't mean - //that we always get called for a multiple of it here (because it - //also depends on the JACK block size). how should we ensure that - //all models write the same amount in to the mix, and that we - //always have a multiple of the plugin buffer size? I guess this - //class has to be queryable for the plugin buffer size & the - //callback play source has to use that as a multiple for all the - //calls to mixModel - - size_t got = blocks * m_pluginBlockSize; - -#ifdef DEBUG_AUDIO_GENERATOR - Vamp::RealTime startTime = Vamp::RealTime::frame2RealTime - (startFrame, m_sourceSampleRate); - - std::cout << "mixModel [note]: frames " << frames << " from " << startFrame - << " (time " << startTime << "), blocks " << blocks << std::endl; -#endif - - snd_seq_event_t onEv; - onEv.type = SND_SEQ_EVENT_NOTEON; - onEv.data.note.channel = 0; - onEv.data.note.note = 64; - onEv.data.note.velocity = 100; - - snd_seq_event_t offEv; - offEv.type = SND_SEQ_EVENT_NOTEOFF; - offEv.data.note.channel = 0; - offEv.data.note.velocity = 0; - - NoteOffSet ¬eOffs = m_noteOffs[nm]; - - for (size_t i = 0; i < blocks; ++i) { - - size_t reqStart = startFrame + i * m_pluginBlockSize; - - NoteModel::PointList points = - nm->getPoints(reqStart + latency, - reqStart + latency + m_pluginBlockSize); - - Vamp::RealTime blockTime = Vamp::RealTime::frame2RealTime - (startFrame + i * m_pluginBlockSize, m_sourceSampleRate); - - for (NoteModel::PointList::iterator pli = - points.begin(); pli != points.end(); ++pli) { - - size_t pliFrame = pli->frame; - - if (pliFrame >= latency) pliFrame -= latency; - - if (pliFrame < reqStart || - pliFrame >= reqStart + m_pluginBlockSize) continue; - - while (noteOffs.begin() != noteOffs.end() && - noteOffs.begin()->frame <= pliFrame) { - - Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime - (noteOffs.begin()->frame, m_sourceSampleRate); - - offEv.data.note.note = noteOffs.begin()->pitch; - -#ifdef DEBUG_AUDIO_GENERATOR - std::cerr << "mixModel [note]: sending note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << std::endl; -#endif - - plugin->sendEvent(eventTime, &offEv); - noteOffs.erase(noteOffs.begin()); - } - - Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime - (pliFrame, m_sourceSampleRate); - - if (nm->getScaleUnits() == "Hz") { - onEv.data.note.note = Pitch::getPitchForFrequency(pli->value); - } else { - onEv.data.note.note = lrintf(pli->value); - } - - if (pli->level > 0.f && pli->level <= 1.f) { - onEv.data.note.velocity = lrintf(pli->level * 127); - } else { - onEv.data.note.velocity = 100; - } - - plugin->sendEvent(eventTime, &onEv); - -#ifdef DEBUG_AUDIO_GENERATOR - std::cout << "mixModel [note]: point at frame " << pliFrame << ", pitch " << (int)onEv.data.note.note << ", block start " << (startFrame + i * m_pluginBlockSize) << ", resulting time " << eventTime << std::endl; -#endif - - size_t duration = pli->duration; - if (duration == 0 || duration == 1) { - duration = m_sourceSampleRate / 20; - } - NoteOff noff; - noff.pitch = onEv.data.note.note; - noff.frame = pliFrame + duration; - noteOffs.insert(noff); - -#ifdef DEBUG_AUDIO_GENERATOR - std::cout << "mixModel [note]: recording note off at " << noff.frame << std::endl; -#endif - } - - while (noteOffs.begin() != noteOffs.end() && - noteOffs.begin()->frame <= - startFrame + i * m_pluginBlockSize + m_pluginBlockSize) { - - Vamp::RealTime eventTime = Vamp::RealTime::frame2RealTime - (noteOffs.begin()->frame, m_sourceSampleRate); - - offEv.data.note.note = noteOffs.begin()->pitch; - -#ifdef DEBUG_AUDIO_GENERATOR - std::cerr << "mixModel [note]: sending leftover note-off event at time " << eventTime << " frame " << noteOffs.begin()->frame << " pitch " << noteOffs.begin()->pitch << std::endl; -#endif - - plugin->sendEvent(eventTime, &offEv); - noteOffs.erase(noteOffs.begin()); - } - - plugin->run(blockTime); - float **outs = plugin->getAudioOutputBuffers(); - - for (size_t c = 0; c < m_targetChannelCount; ++c) { -#ifdef DEBUG_AUDIO_GENERATOR - std::cout << "mixModel [note]: adding " << m_pluginBlockSize << " samples from plugin output " << c << std::endl; -#endif - - size_t sourceChannel = (c % plugin->getAudioOutputCount()); - - float channelGain = gain; - if (pan != 0.0) { - if (c == 0) { - if (pan > 0.0) channelGain *= 1.0 - pan; - } else { - if (pan < 0.0) channelGain *= pan + 1.0; - } - } - - for (size_t j = 0; j < m_pluginBlockSize; ++j) { - buffer[c][i * m_pluginBlockSize + j] += - channelGain * outs[sourceChannel][j]; - } - } - } - - return got; -} -