2323#include < QXmlStreamReader>
2424#include < QXmlStreamWriter>
2525
26+ #include < set>
27+
2628namespace noteahead {
2729
2830DeviceService::DeviceService (AudioEngineS audioEngine, QObject * parent)
@@ -56,36 +58,36 @@ bool DeviceService::isInternalDevice(const QString & portName) const
5658
5759void DeviceService::processMidiNoteOn (const QString & portName, uint8_t note, uint8_t velocity)
5860{
59- if (auto dev = device (portName.toStdString ()); dev) {
61+ if (const auto dev = device (portName.toStdString ()); dev) {
6062 dev->processMidiNoteOn (note, velocity);
6163 }
6264}
6365
6466void DeviceService::processMidiNoteOff (const QString & portName, uint8_t note)
6567{
66- if (auto dev = device (portName.toStdString ()); dev) {
68+ if (const auto dev = device (portName.toStdString ()); dev) {
6769 dev->processMidiNoteOff (note);
6870 }
6971}
7072
7173void DeviceService::processMidiCc (const QString & portName, uint8_t controller, uint8_t value, uint8_t channel)
7274{
73- if (auto dev = device (portName.toStdString ()); dev) {
75+ if (const auto dev = device (portName.toStdString ()); dev) {
7476 dev->processMidiCc (controller, value, channel);
7577 }
7678}
7779
7880void DeviceService::processMidiAllNotesOff (const QString & portName)
7981{
80- if (auto dev = device (portName.toStdString ()); dev) {
82+ if (const auto dev = device (portName.toStdString ()); dev) {
8183 dev->processMidiAllNotesOff ();
8284 }
8385}
8486
8587void DeviceService::processMidiAllNotesOff ()
8688{
8789 for (const auto & name : internalDeviceNames ()) {
88- if (auto dev = device (name.toStdString ()); dev) {
90+ if (const auto dev = device (name.toStdString ()); dev) {
8991 dev->processMidiAllNotesOff ();
9092 }
9193 }
@@ -94,12 +96,41 @@ void DeviceService::processMidiAllNotesOff()
9496QStringList DeviceService::internalDeviceNames () const
9597{
9698 QStringList names;
97- if (device (Constants::samplerDeviceName ().toStdString ())) {
98- names << Constants::samplerDeviceName ();
99+ const auto samplerName = Constants::samplerDeviceName ();
100+ if (device (samplerName.toStdString ())) {
101+ names << samplerName;
99102 }
100103 return names;
101104}
102105
106+ QStringList DeviceService::categories () const
107+ {
108+ std::set<QString> categories;
109+ for (const auto & name : internalDeviceNames ()) {
110+ if (const auto dev = device (name.toStdString ()); dev) {
111+ categories.insert (QString::fromStdString (dev->category ()));
112+ }
113+ }
114+ QStringList result;
115+ for (const auto & c : categories) {
116+ result << c;
117+ }
118+ return result;
119+ }
120+
121+ QStringList DeviceService::devicesByCategory (const QString & category) const
122+ {
123+ QStringList devices;
124+ for (const auto & name : internalDeviceNames ()) {
125+ if (const auto dev = device (name.toStdString ()); dev) {
126+ if (QString::fromStdString (dev->category ()) == category) {
127+ devices << name;
128+ }
129+ }
130+ }
131+ return devices;
132+ }
133+
103134void DeviceService::setProjectPath (const std::string & projectPath)
104135{
105136 if (const auto sampler = std::dynamic_pointer_cast<SamplerDevice>(device (Constants::samplerDeviceName ().toStdString ())); sampler) {
@@ -110,21 +141,42 @@ void DeviceService::setProjectPath(const std::string & projectPath)
110141void DeviceService::serializeToXml (QXmlStreamWriter & writer) const
111142{
112143 writer.writeStartElement (Constants::NahdXml::xmlKeyDevices ());
113- if (const auto sampler = std::dynamic_pointer_cast<SamplerDevice>(device (Constants::samplerDeviceName ().toStdString ())); sampler) {
114- sampler->serializeToXml (writer);
144+ for (const auto & name : internalDeviceNames ()) {
145+ if (const auto dev = device (name.toStdString ()); dev) {
146+ dev->serializeToXml (writer);
147+ }
115148 }
116149 writer.writeEndElement (); // Devices
117150}
118151
119152void DeviceService::deserializeFromXml (QXmlStreamReader & reader)
120153{
121- while (!reader.atEnd () && !(reader.isEndElement () && reader.name () == Constants::NahdXml::xmlKeyDevices ())) {
122- if (reader.isStartElement () && reader.name () == Constants::NahdXml::xmlKeySampler ()) {
123- if (const auto sampler = std::dynamic_pointer_cast<SamplerDevice>(device (Constants::samplerDeviceName ().toStdString ())); sampler) {
154+ const auto samplerName = Constants::samplerDeviceName ().toStdString ();
155+ const auto samplersCategory = Constants::NahdXml::xmlValueSamplers ();
156+
157+ while (reader.readNextStartElement ()) {
158+ const auto name = reader.name ();
159+ if (name == Constants::NahdXml::xmlKeyDevice ()) {
160+ const auto category = reader.attributes ().value (Constants::NahdXml::xmlKeyCategory ()).toString ();
161+ const auto deviceName = reader.attributes ().value (Constants::NahdXml::xmlKeyName ()).toString ().toStdString ();
162+ if (category == samplersCategory) {
163+ if (const auto sampler = std::dynamic_pointer_cast<SamplerDevice>(device (samplerName)); sampler) {
164+ sampler->deserializeFromXml (reader);
165+ }
166+ } else if (auto dev = device (deviceName)) {
167+ dev->deserializeFromXml (reader);
168+ } else {
169+ reader.skipCurrentElement ();
170+ }
171+ } else if (name == Constants::NahdXml::xmlKeySampler ()) {
172+ if (const auto sampler = std::dynamic_pointer_cast<SamplerDevice>(device (samplerName)); sampler) {
124173 sampler->deserializeFromXml (reader);
174+ } else {
175+ reader.skipCurrentElement ();
125176 }
177+ } else {
178+ reader.skipCurrentElement ();
126179 }
127- reader.readNext ();
128180 }
129181 emit dataChanged ();
130182}
0 commit comments