2#include <Media/AudioDecoder.hpp>
4#include <score/document/DocumentContext.hpp>
5#include <score/tools/File.hpp>
7#include <ossia/dataflow/exec_state_facade.hpp>
8#include <ossia/dataflow/nodes/media.hpp>
9#include <ossia/network/value/value.hpp>
15#include <avnd/binding/ossia/soundfiles.hpp>
16#include <libremidi/reader.hpp>
23[[nodiscard]]
static QString
26 if(
auto str = value.target<std::string>())
36[[nodiscard]]
static ossia::value
39 if(
auto str = value.target<std::string>(); str && !str->empty())
41 const QString resolved
43 if(!resolved.isEmpty())
44 return ossia::value{resolved.toStdString()};
50[[nodiscard]]
static auto
54 if(
auto str = filenameFromPort(value, ctx); !str.isEmpty())
56 auto dec = Media::AudioDecoder::decode_synchronous(str, rate);
60 auto hdl = std::make_shared<ossia::audio_data>();
61 hdl->data = std::move(dec->second);
62 hdl->path = str.toStdString();
67 return ossia::audio_handle{};
70using midifile_handle = std::shared_ptr<oscr::midifile_data>;
71[[nodiscard]]
inline midifile_handle
75 if(
auto str = filenameFromPort(value, ctx); !str.isEmpty())
78 if(!f.open(QIODevice::ReadOnly))
80 auto ptr = f.map(0, f.size());
82 auto hdl = std::make_shared<oscr::midifile_data>();
83 if(
auto ret = hdl->reader.parse((uint8_t*)ptr, f.size());
84 ret == libremidi::reader::invalid)
87 hdl->filename = str.toStdString();
93using raw_file_handle = std::shared_ptr<raw_file_data>;
94[[nodiscard]]
inline raw_file_handle loadRawfile(
107 const auto* raw = value.target<std::string>();
108 if(!raw || raw->empty())
111 const QString filename = filenameFromPort(value, ctx);
112 if(filename.isEmpty())
114 qWarning() <<
"file port: could not resolve"
115 << QString::fromStdString(*raw);
120 if(!QFile::exists(filename))
122 qWarning() <<
"file port: no such file:" << filename;
126 auto hdl = std::make_shared<oscr::raw_file_data>();
127 hdl->file.setFileName(filename);
128 if(!hdl->file.open(QIODevice::ReadOnly))
130 qWarning() <<
"file port: cannot open" << filename <<
":"
131 << hdl->file.errorString();
137 auto map = (
char*)hdl->file.map(0, hdl->file.size());
138 hdl->data = QByteArray::fromRawData(map, hdl->file.size());
143 hdl->file.setTextModeEnabled(
true);
145 hdl->data = hdl->file.readAll();
147 hdl->filename = filename.toStdString();
153[[nodiscard]]
inline auto loadSoundfile(
155 const std::shared_ptr<ossia::execution_state>& st)
157 const double rate = ossia::exec_state_facade{st.get()}.sampleRate();
158 return loadSoundfile(value, ctx, rate);
161template <
typename Field>
162static auto executePortPreprocess(
auto& file)
164 using field_file_type =
decltype(Field::file);
165 field_file_type ffile;
166 ffile.bytes =
decltype(ffile.bytes)(file.data.constData(), file.file.size());
167 ffile.filename = file.filename;
168 return Field::process(ffile);
Definition Controls.hpp:27
QString locateFilePath(const QString &filename) noexcept
Definition File.cpp:84
Definition DocumentContext.hpp:18