ysfx_api_file.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // Copyright 2021 Jean Pierre Cimalando
  2. // Copyright 2024 Joep Vanlier
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // Modifications by Joep Vanlier, 2024
  17. //
  18. // SPDX-License-Identifier: Apache-2.0
  19. //
  20. #include "ysfx.hpp"
  21. #include "ysfx_config.hpp"
  22. #include "ysfx_api_file.hpp"
  23. #include "ysfx_eel_utils.hpp"
  24. #include <cstring>
  25. #include <cstdio>
  26. #include <cassert>
  27. ysfx_raw_file_t::ysfx_raw_file_t(NSEEL_VMCTX vm, const char *filename)
  28. : m_vm(vm),
  29. m_stream(ysfx::fopen_utf8(filename, "rb"))
  30. {
  31. }
  32. int32_t ysfx_raw_file_t::avail()
  33. {
  34. if (!m_stream)
  35. return 0;
  36. int64_t cur_off = ysfx::ftell_lfs(m_stream.get());
  37. if (cur_off == -1)
  38. return 0;
  39. if (ysfx::fseek_lfs(m_stream.get(), 0, SEEK_END) == -1)
  40. return 0;
  41. int64_t end_off = ysfx::ftell_lfs(m_stream.get());
  42. if (end_off == -1)
  43. return 0;
  44. if (ysfx::fseek_lfs(m_stream.get(), cur_off, SEEK_SET) == -1)
  45. return 0;
  46. if ((uint64_t)end_off < (uint64_t)cur_off)
  47. return 0;
  48. uint64_t byte_count = (uint64_t)end_off - (uint64_t)cur_off;
  49. uint64_t f32_count = byte_count / 4;
  50. return (f32_count > 0x7fffffff) ? 0x7fffffff : (uint32_t)f32_count;
  51. }
  52. void ysfx_raw_file_t::rewind()
  53. {
  54. if (!m_stream)
  55. return;
  56. ::rewind(m_stream.get());
  57. }
  58. bool ysfx_raw_file_t::var(ysfx_real *var)
  59. {
  60. if (!m_stream)
  61. return false;
  62. uint8_t data[4];
  63. if (fread(data, 1, 4, m_stream.get()) != 4)
  64. return false;
  65. *var = (EEL_F)ysfx::unpack_f32le(data);
  66. return true;
  67. }
  68. uint32_t ysfx_raw_file_t::mem(uint32_t offset, uint32_t length)
  69. {
  70. if (!m_stream)
  71. return 0;
  72. ysfx_eel_ram_writer writer{m_vm, offset};
  73. uint32_t read;
  74. for (read = 0; read < length; ++read) {
  75. ysfx_real value;
  76. if (!var(&value))
  77. break;
  78. writer.write_next(value);
  79. }
  80. return read;
  81. }
  82. uint32_t ysfx_raw_file_t::string(std::string &str)
  83. {
  84. if (!m_stream)
  85. return 0;
  86. uint8_t data[4];
  87. if (fread(data, 1, 4, m_stream.get()) != 4)
  88. return 0;
  89. str.clear();
  90. uint32_t srclen = ysfx::unpack_u32le(data);
  91. str.reserve((srclen < ysfx_string_max_length) ? srclen : ysfx_string_max_length);
  92. uint32_t count = 0;
  93. for (int byte; count < srclen && (byte = fgetc(m_stream.get())) != EOF; ) {
  94. if (str.size() < ysfx_string_max_length)
  95. str.push_back((unsigned char)byte);
  96. ++count;
  97. }
  98. return count;
  99. }
  100. //------------------------------------------------------------------------------
  101. ysfx_text_file_t::ysfx_text_file_t(NSEEL_VMCTX vm, const char *filename)
  102. : m_vm(vm),
  103. m_stream(ysfx::fopen_utf8(filename, "rb"))
  104. {
  105. m_buf.reserve(256);
  106. }
  107. int32_t ysfx_text_file_t::avail()
  108. {
  109. if (!m_stream || ferror(m_stream.get()))
  110. return -1;
  111. return (feof(m_stream.get()) == 0) ? 1 : 0;
  112. }
  113. void ysfx_text_file_t::rewind()
  114. {
  115. if (!m_stream)
  116. return;
  117. ::rewind(m_stream.get());
  118. }
  119. bool ysfx_text_file_t::var(ysfx_real *var)
  120. {
  121. if (!m_stream)
  122. return false;
  123. //TODO support the expression language for arithmetic
  124. int ch;
  125. do {
  126. // get the next number separated by newline or comma
  127. // but skip invalid lines
  128. m_buf.clear();
  129. while ((ch = fgetc(m_stream.get())) != EOF && ch != '\n' && ch != ',')
  130. m_buf.push_back((unsigned char)ch);
  131. const char *startp = m_buf.c_str();
  132. const char *endp = (char *)startp;
  133. double value = ysfx::dot_strtod(startp, (char **)&endp);
  134. if (endp != startp) {
  135. *var = (EEL_F)value;
  136. return true;
  137. }
  138. } while (ch != EOF);
  139. return false;
  140. }
  141. uint32_t ysfx_text_file_t::mem(uint32_t offset, uint32_t length)
  142. {
  143. if (!m_stream)
  144. return 0;
  145. ysfx_eel_ram_writer writer{m_vm, offset};
  146. uint32_t read;
  147. for (read = 0; read < length; ++read) {
  148. ysfx_real value;
  149. if (!var(&value))
  150. break;
  151. writer.write_next(value);
  152. }
  153. return read;
  154. }
  155. uint32_t ysfx_text_file_t::string(std::string &str)
  156. {
  157. if (!m_stream)
  158. return 0;
  159. str.clear();
  160. str.reserve(256);
  161. int ch;
  162. do {
  163. ch = fgetc(m_stream.get());
  164. if (ch != EOF && str.size() < ysfx_string_max_length)
  165. str.push_back((unsigned char)ch);
  166. } while (ch != EOF && ch != '\n');
  167. return (uint32_t)str.size();
  168. }
  169. //------------------------------------------------------------------------------
  170. ysfx_audio_file_t::ysfx_audio_file_t(NSEEL_VMCTX vm, const ysfx_audio_format_t &fmt, const char *filename)
  171. : m_vm(vm),
  172. m_fmt(fmt),
  173. m_reader(fmt.open(filename), fmt.close)
  174. {
  175. }
  176. int32_t ysfx_audio_file_t::avail()
  177. {
  178. if (!m_reader)
  179. return -1;
  180. uint64_t avail = m_fmt.avail(m_reader.get());
  181. return (avail > 0x7fffffff) ? 0x7fffffff : (int32_t)avail;
  182. }
  183. void ysfx_audio_file_t::rewind()
  184. {
  185. if (!m_reader)
  186. return;
  187. m_fmt.rewind(m_reader.get());
  188. }
  189. bool ysfx_audio_file_t::var(ysfx_real *var)
  190. {
  191. if (!m_reader)
  192. return false;
  193. return m_fmt.read(m_reader.get(), var, 1) == 1;
  194. }
  195. uint32_t ysfx_audio_file_t::mem(uint32_t offset, uint32_t length)
  196. {
  197. if (!m_reader)
  198. return 0;
  199. uint32_t numread = 0;
  200. ysfx_real *buf = m_buf.get();
  201. ysfx_eel_ram_writer writer(m_vm, offset);
  202. while (numread < length) {
  203. uint32_t n = length - numread;
  204. if (n > buffer_size)
  205. n = buffer_size;
  206. uint32_t m = (uint32_t)m_fmt.read(m_reader.get(), buf, n);
  207. for (uint32_t i = 0; i < m; ++i)
  208. writer.write_next(buf[i]);
  209. numread += m;
  210. if (m < n)
  211. break;
  212. }
  213. return numread;
  214. }
  215. uint32_t ysfx_audio_file_t::string(std::string &str)
  216. {
  217. (void)str;
  218. return 0;
  219. }
  220. bool ysfx_audio_file_t::riff(uint32_t &nch, ysfx_real &samplerate)
  221. {
  222. if (!m_reader)
  223. return false;
  224. ysfx_audio_file_info_t info = m_fmt.info(m_reader.get());
  225. nch = info.channels;
  226. samplerate = info.sample_rate;
  227. return true;
  228. }
  229. //------------------------------------------------------------------------------
  230. ysfx_serializer_t::ysfx_serializer_t(NSEEL_VMCTX vm)
  231. : m_vm(vm)
  232. {
  233. }
  234. void ysfx_serializer_t::begin(bool write, std::string &buffer)
  235. {
  236. m_write = (int)write;
  237. m_buffer = &buffer;
  238. m_pos = 0;
  239. }
  240. void ysfx_serializer_t::end()
  241. {
  242. m_write = -1;
  243. m_buffer = nullptr;
  244. }
  245. int32_t ysfx_serializer_t::avail()
  246. {
  247. if (m_write)
  248. return -1;
  249. else
  250. return (m_buffer->size() > m_pos) ? 1 : 0;
  251. }
  252. void ysfx_serializer_t::rewind()
  253. {
  254. }
  255. bool ysfx_serializer_t::var(ysfx_real *var)
  256. {
  257. if (m_write == 1) {
  258. uint8_t buf[4];
  259. ysfx::pack_f32le((float)*var, buf);
  260. m_buffer->append((char *)buf, 4);
  261. return true;
  262. }
  263. else if (m_write == 0) {
  264. if (m_pos + 4 > m_buffer->size()) {
  265. m_pos = m_buffer->size();
  266. *var = 0;
  267. return false;
  268. }
  269. *var = (EEL_F)ysfx::unpack_f32le((uint8_t *)&(*m_buffer)[m_pos]);
  270. m_pos += 4;
  271. return true;
  272. }
  273. return false;
  274. }
  275. uint32_t ysfx_serializer_t::mem(uint32_t offset, uint32_t length)
  276. {
  277. if (m_write == 1) {
  278. ysfx_eel_ram_reader reader{m_vm, offset};
  279. for (uint32_t i = 0; i < length; ++i) {
  280. ysfx_real value = reader.read_next();
  281. if (!var(&value))
  282. return i;
  283. }
  284. return length;
  285. }
  286. else if (m_write == 0) {
  287. ysfx_eel_ram_writer writer{m_vm, offset};
  288. for (uint32_t i = 0; i < length; ++i) {
  289. ysfx_real value{};
  290. if (!var(&value))
  291. return i;
  292. writer.write_next(value);
  293. }
  294. return length;
  295. }
  296. return 0;
  297. }
  298. uint32_t ysfx_serializer_t::string(std::string &str)
  299. {
  300. // TODO implement me; docs claim support in Reaper 4.59+ but it seems
  301. // non-working (as of Reaper 6.40)
  302. return 0;
  303. }
  304. //------------------------------------------------------------------------------
  305. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_open(void *opaque, EEL_F *file_)
  306. {
  307. ysfx_t *fx = (ysfx_t *)opaque;
  308. std::string filepath;
  309. if (!ysfx_find_data_file(fx, file_, filepath))
  310. return -1;
  311. void *fmtobj = nullptr;
  312. ysfx_file_type_t ftype = ysfx_detect_file_type(fx, filepath.c_str(), &fmtobj);
  313. ysfx_file_u file;
  314. switch (ftype) {
  315. case ysfx_file_type_txt:
  316. file.reset(new ysfx_text_file_t(fx->vm.get(), filepath.c_str()));
  317. break;
  318. case ysfx_file_type_raw:
  319. file.reset(new ysfx_raw_file_t(fx->vm.get(), filepath.c_str()));
  320. break;
  321. case ysfx_file_type_audio:
  322. file.reset(new ysfx_audio_file_t(fx->vm.get(), *(ysfx_audio_format_t *)fmtobj, filepath.c_str()));
  323. break;
  324. case ysfx_file_type_none:
  325. file.reset(new ysfx_raw_file_t(fx->vm.get(), filepath.c_str()));
  326. break;
  327. default:
  328. assert(false);
  329. }
  330. if (file) {
  331. int32_t handle = ysfx_insert_file(fx, file.get());
  332. if (handle == -1)
  333. return -1;
  334. (void)file.release();
  335. return (EEL_F)(uint32_t)handle;
  336. }
  337. return -1;
  338. }
  339. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_close(void *opaque, EEL_F *handle_)
  340. {
  341. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  342. if (handle <= 0) //NOTE: cannot close the serializer handle (0)
  343. return -1;
  344. ysfx_t *fx = (ysfx_t *)opaque;
  345. std::unique_ptr<ysfx::mutex> file_mutex;
  346. std::unique_lock<ysfx::mutex> lock;
  347. std::unique_lock<ysfx::mutex> list_lock;
  348. // hold both locks to protect file and list access during removal
  349. if (!ysfx_get_file(fx, (uint32_t)handle, lock, &list_lock))
  350. return -1;
  351. // preserve the locked mutex of the object being removed
  352. file_mutex = std::move(fx->file.list[(uint32_t)handle]->m_mutex);
  353. fx->file.list[(uint32_t)handle].reset();
  354. return 0;
  355. }
  356. static EEL_F *NSEEL_CGEN_CALL ysfx_api_file_rewind(void *opaque, EEL_F *handle_)
  357. {
  358. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  359. if (handle < 0)
  360. return handle_;
  361. ysfx_t *fx = (ysfx_t *)opaque;
  362. std::unique_lock<ysfx::mutex> lock;
  363. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  364. if (!file)
  365. return 0;
  366. file->rewind();
  367. return handle_;
  368. }
  369. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_var(void *opaque, EEL_F *handle_, EEL_F *var)
  370. {
  371. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  372. if (handle < 0)
  373. return 0;
  374. ysfx_t *fx = (ysfx_t *)opaque;
  375. std::unique_lock<ysfx::mutex> lock;
  376. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  377. if (!file)
  378. return 0;
  379. if (!file->var(var))
  380. return 0;
  381. return 1;
  382. }
  383. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_mem(void *opaque, EEL_F *handle_, EEL_F *offset_, EEL_F *length_)
  384. {
  385. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  386. int32_t offset = ysfx_eel_round<int32_t>(*offset_);
  387. int32_t length = ysfx_eel_round<int32_t>(*length_);
  388. if (handle < 0 || offset < 0 || length <= 0)
  389. return 0;
  390. ysfx_t *fx = (ysfx_t *)opaque;
  391. std::unique_lock<ysfx::mutex> lock;
  392. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  393. if (!file)
  394. return 0;
  395. return (EEL_F)file->mem((uint32_t)offset, (uint32_t)length);
  396. }
  397. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_avail(void *opaque, EEL_F *handle_)
  398. {
  399. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  400. if (handle < 0)
  401. return 0;
  402. ysfx_t *fx = (ysfx_t *)opaque;
  403. std::unique_lock<ysfx::mutex> lock;
  404. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  405. if (!file)
  406. return 0;
  407. return file->avail();
  408. }
  409. static EEL_F *NSEEL_CGEN_CALL ysfx_api_file_riff(void *opaque, EEL_F *handle_, EEL_F *nch_, EEL_F *samplerate_)
  410. {
  411. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  412. if (handle < 0)
  413. return 0;
  414. ysfx_t *fx = (ysfx_t *)opaque;
  415. std::unique_lock<ysfx::mutex> lock;
  416. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  417. if (!file) {
  418. *nch_ = 0;
  419. *samplerate_ = 0;
  420. return nch_;
  421. }
  422. uint32_t nch = 0;
  423. ysfx_real samplerate = 0;
  424. if (!file->riff(nch, samplerate)) {
  425. *nch_ = 0;
  426. *samplerate_ = 0;
  427. return nch_;
  428. }
  429. *nch_ = (EEL_F)nch;
  430. *samplerate_ = samplerate;
  431. return nch_;
  432. }
  433. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_text(void *opaque, EEL_F *handle_)
  434. {
  435. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  436. if (handle < 0)
  437. return 0;
  438. ysfx_t *fx = (ysfx_t *)opaque;
  439. std::unique_lock<ysfx::mutex> lock;
  440. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  441. if (!file)
  442. return 0;
  443. return (EEL_F)file->is_text();
  444. }
  445. static EEL_F NSEEL_CGEN_CALL ysfx_api_file_string(void *opaque, EEL_F *handle_, EEL_F *string_)
  446. {
  447. int32_t handle = ysfx_eel_round<int32_t>(*handle_);
  448. if (handle < 0)
  449. return 0;
  450. ysfx_t *fx = (ysfx_t *)opaque;
  451. std::unique_lock<ysfx::mutex> lock;
  452. ysfx_file_t *file = ysfx_get_file(fx, (uint32_t)handle, lock);
  453. if (!file)
  454. return 0;
  455. std::string txt;
  456. uint32_t count;
  457. if (!file->is_in_write_mode()) {
  458. count = file->string(txt);
  459. ysfx_string_set(fx, *string_, txt);
  460. }
  461. else {
  462. ysfx_string_get(fx, *string_, txt);
  463. count = file->string(txt);
  464. }
  465. return (EEL_F)count;
  466. }
  467. void ysfx_api_init_file()
  468. {
  469. NSEEL_addfunc_retval("file_open", 1, NSEEL_PProc_THIS, &ysfx_api_file_open);
  470. NSEEL_addfunc_retval("file_close", 1, NSEEL_PProc_THIS, &ysfx_api_file_close);
  471. NSEEL_addfunc_retptr("file_rewind", 1, NSEEL_PProc_THIS, &ysfx_api_file_rewind);
  472. NSEEL_addfunc_retval("file_var", 2, NSEEL_PProc_THIS, &ysfx_api_file_var);
  473. NSEEL_addfunc_retval("file_mem", 3, NSEEL_PProc_THIS, &ysfx_api_file_mem);
  474. NSEEL_addfunc_retval("file_avail", 1, NSEEL_PProc_THIS, &ysfx_api_file_avail);
  475. NSEEL_addfunc_retptr("file_riff", 3, NSEEL_PProc_THIS, &ysfx_api_file_riff);
  476. NSEEL_addfunc_retval("file_text", 1, NSEEL_PProc_THIS, &ysfx_api_file_text);
  477. NSEEL_addfunc_retval("file_string", 2, NSEEL_PProc_THIS, &ysfx_api_file_string);
  478. }